123456789_123456789_123456789_123456789_123456789_

Class: Concurrent::ThreadSafe::Util::PowerOfTwoTuple

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Tuple
Instance Chain:
self, Tuple, Enumerable
Inherits: Concurrent::Tuple
Defined in: lib/concurrent-ruby/concurrent/thread_safe/util/power_of_two_tuple.rb

Class Method Summary

Tuple - Inherited

.new

Create a new tuple of the given size.

Instance Attribute Summary

Tuple - Inherited

#size

The (fixed) size of the tuple.

Instance Method Summary

Tuple - Inherited

#cas
#compare_and_set

Set the value at the given index to the new value if and only if the current value matches the given old value.

#each

Calls the given block once for each element in self, passing that element as a parameter.

#get

Get the value of the element at the given index.

#set

Set the element at the given index to the given value.

#volatile_get
#volatile_set

Constructor Details

.new(size) ⇒ PowerOfTwoTuple

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/thread_safe/util/power_of_two_tuple.rb', line 15

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)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/thread_safe/util/power_of_two_tuple.rb', line 20

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

#next_in_size_table

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/thread_safe/util/power_of_two_tuple.rb', line 32

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

#volatile_get_by_hash(hash)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/thread_safe/util/power_of_two_tuple.rb', line 24

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

#volatile_set_by_hash(hash, value)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/thread_safe/util/power_of_two_tuple.rb', line 28

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