Class: ThreadSafe::Util::PowerOfTwoTuple

Inherits:
VolatileTuple show all
Defined in:
lib/thread_safe/util/power_of_two_tuple.rb

Direct Known Subclasses

AtomicReferenceCacheBackend::Table

Constant Summary

Constants inherited from VolatileTuple

VolatileTuple::Tuple

Instance Method Summary collapse

Methods inherited from VolatileTuple

#compare_and_set, #each, #size, #volatile_get, #volatile_set

Constructor Details

#initialize(size) ⇒ PowerOfTwoTuple

Returns a new instance of PowerOfTwoTuple.

Raises:

  • (ArgumentError)


4
5
6
7
# File 'lib/thread_safe/util/power_of_two_tuple.rb', line 4

def initialize(size)
  raise ArgumentError, "size must be a power of 2 (#{size.inspect} provided)" unless size > 0 && size & (size - 1) == 0
  super(size)
end

Instance Method Details

#hash_to_index(hash) ⇒ Object



9
10
11
# File 'lib/thread_safe/util/power_of_two_tuple.rb', line 9

def hash_to_index(hash)
  (size - 1) & hash
end

#next_in_size_tableObject



21
22
23
# File 'lib/thread_safe/util/power_of_two_tuple.rb', line 21

def next_in_size_table
  self.class.new(size << 1)
end

#volatile_get_by_hash(hash) ⇒ Object



13
14
15
# File 'lib/thread_safe/util/power_of_two_tuple.rb', line 13

def volatile_get_by_hash(hash)
  volatile_get(hash_to_index(hash))
end

#volatile_set_by_hash(hash, value) ⇒ Object



17
18
19
# File 'lib/thread_safe/util/power_of_two_tuple.rb', line 17

def volatile_set_by_hash(hash, value)
  volatile_set(hash_to_index(hash), value)
end