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.



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

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)


58
59
60
61
62
63
# File 'lib/redis_client/cluster/node.rb', line 58

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

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

#[]=(index, element) ⇒ Object

Raises:

  • (IndexError)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/redis_client/cluster/node.rb', line 65

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