Class: RedisClient::Cluster::Node::CharArray
- Inherits:
-
Object
- Object
- RedisClient::Cluster::Node::CharArray
- Defined in:
- lib/redis_client/cluster/node.rb
Constant Summary collapse
- BASE =
''
- PADDING =
'0'
Instance Method Summary collapse
- #[](index) ⇒ Object
- #[]=(index, element) ⇒ Object
-
#initialize(size, elements) ⇒ CharArray
constructor
A new instance of CharArray.
Constructor Details
#initialize(size, elements) ⇒ CharArray
Returns a new instance of CharArray.
48 49 50 51 52 |
# File 'lib/redis_client/cluster/node.rb', line 48 def initialize(size, elements) @elements = elements @string = String.new(BASE, encoding: Encoding::BINARY, capacity: size) size.times { @string << PADDING } end |
Instance Method Details
#[](index) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/redis_client/cluster/node.rb', line 54 def [](index) raise IndexError if index < 0 return if index >= @string.bytesize @elements[@string.getbyte(index)] end |
#[]=(index, element) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/redis_client/cluster/node.rb', line 61 def []=(index, element) raise IndexError if index < 0 return if index >= @string.bytesize pos = @elements.find_index(element) # O(N) if pos.nil? raise(RangeError, 'full of elements') if @elements.size >= 256 pos = @elements.size @elements << element end @string.setbyte(index, pos) end |