Class: RedisClient::Cluster::Node::CharArray

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_client/cluster/node.rb

Constant Summary collapse

BASE =
''
PADDING =
'0'

Instance Method Summary collapse

Constructor Details

#initialize(size, elements) ⇒ CharArray

Returns a new instance of CharArray.



46
47
48
49
50
# File 'lib/redis_client/cluster/node.rb', line 46

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

Raises:

  • (IndexError)


52
53
54
55
56
57
# File 'lib/redis_client/cluster/node.rb', line 52

def [](index)
  raise IndexError if index < 0
  return if index >= @string.bytesize

  @elements[@string.getbyte(index)]
end

#[]=(index, element) ⇒ Object

Raises:

  • (IndexError)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/redis_client/cluster/node.rb', line 59

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