File: power_of_two_tuple.rb

package info (click to toggle)
ruby-concurrent 1.1.6%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 30,284 kB
  • sloc: ruby: 30,875; java: 6,117; javascript: 1,114; ansic: 288; makefile: 10; sh: 6
file content (38 lines) | stat: -rw-r--r-- 831 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require 'concurrent/thread_safe/util'
require 'concurrent/tuple'

module Concurrent

  # @!visibility private
  module ThreadSafe

    # @!visibility private
    module Util

      # @!visibility private
      class PowerOfTwoTuple < Concurrent::Tuple

        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

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

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

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

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