class Concurrent::Map

Experimental RBS support (using type sampling data from the type_fusion project).

# sig/concurrent-ruby/concurrent/map.rbs

class Concurrent::Map < Concurrent::Collection::MapImplementation
  def fetch_or_store: (String key, ?Object default_value) -> Array[String]?
end

‘Concurrent::Map` instead of `Concurrent::Hash` for your concurrency-safe hash needs.
does. For most uses it should do fine though, and we recommend you consider
– for instance, it does not necessarily retain ordering by insertion time as `Hash`
However, `Concurrent::Map `is not strictly semantically equivalent to a ruby `Hash`
characteristics, especially under high concurrency, than `Concurrent::Hash`.
`Concurrent::Map` is a hash-like object and should have much better performance

def [](key)

Returns:
  • (Object) - the value

Parameters:
  • key (Object) --
def [](key)
  if value = super # non-falsy value is an existing mapping, return it right away
    value
    # re-check is done with get_or_default(key, NULL) instead of a simple !key?(key) in order to avoid a race condition, whereby by the time the current thread gets to the key?(key) call
    # a key => value mapping might have already been created by a different thread (key?(key) would then return true, this elsif branch wouldn't be taken and an incorrent +nil+ value
    # would be returned)
    # note: nil == value check is not technically necessary
  elsif @default_proc && nil == value && NULL == (value = get_or_default(key, NULL))
    @default_proc.call(self, key)
  else
    value
  end
end

def each_key

Returns:
  • (self) -

Other tags:
    Yieldparam: key -

Other tags:
    Yield: - for each key in the map
def each_key
  each_pair { |k, v| yield k }
end unless method_defined?(:each_key)

def each_pair

Returns:
  • (self) -

Other tags:
    Yieldparam: value -
    Yieldparam: key -

Other tags:
    Yield: - for each key value pair in the map
def each_pair
  return enum_for :each_pair unless block_given?
  super
end

def each_value

Returns:
  • (self) -

Other tags:
    Yieldparam: value -

Other tags:
    Yield: - for each value in the map
def each_value
  each_pair { |k, v| yield v }
end unless method_defined?(:each_value)

def empty?

Returns:
  • (true, false) -
def empty?
  each_pair { |k, v| return false }
  true
end unless method_defined?(:empty?)

def fetch(key, default_value = NULL)

Other tags:
    Note: - The "fetch-then-act" methods of `Map` are not atomic. `Map` is intended

Raises:
  • (KeyError) - when key is missing and no default_value is provided

Returns:
  • (Object) - the value or default value

Other tags:
    Yieldreturn: - default value

Other tags:
    Yieldparam: key -

Other tags:
    Yield: - default value for a key

Parameters:
  • default_value (Object) --
  • key (Object) --
def fetch(key, default_value = NULL)
  if NULL != (value = get_or_default(key, NULL))
    value
  elsif block_given?
    yield key
  elsif NULL != default_value
    default_value
  else
    raise_fetch_no_key
  end
end

def fetch_or_store(key, default_value = NULL)

Experimental RBS support (using type sampling data from the type_fusion project).

def fetch_or_store: (String key, ?Object default_value) -> ["String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String"]?

This signature was generated using 2 samples from 1 application.

Returns:
  • (Object) - the value or default value

Other tags:
    Yieldreturn: - default value

Other tags:
    Yieldparam: key -

Other tags:
    Yield: - default value for a key

Parameters:
  • default_value (Object) --
  • key (Object) --
def fetch_or_store(key, default_value = NULL)
  fetch(key) do
    put(key, block_given? ? yield(key) : (NULL == default_value ? raise_fetch_no_key : default_value))
  end
end

def initialize(options = nil, &default_proc)

Parameters:
  • default_proc (Proc) -- Optional block to compute the default value if the key is not set, like `Hash#default_proc`
  • options (Hash, nil) -- options to set the :initial_capacity or :load_factor. Ignored on some Rubies.
def initialize(options = nil, &default_proc)
  if options.kind_of?(::Hash)
    validate_options_hash!(options)
  else
    options = nil
  end
  super(options)
  @default_proc = default_proc
end

def initialize_copy(other)

def initialize_copy(other)
  super
  populate_from(other)
end

def inspect

@!visibility private
def inspect
  format '%s entries=%d default_proc=%s>', to_s[0..-2], size.to_s, @default_proc.inspect
end

def key(value)

Returns:
  • (Object, nil) - key or nil when not found

Parameters:
  • value (Object) --
def key(value)
  each_pair { |k, v| return k if v == value }
  nil
end unless method_defined?(:key)

def keys

Returns:
  • (::Array) - keys
    def keys
      arr = []
      each_pair { |k, v| arr << k }
      arr
    end unless method_defined?(:keys)

    def marshal_dump

    @!visibility private
    def marshal_dump
      raise TypeError, "can't dump hash with default proc" if @default_proc
      h = {}
      each_pair { |k, v| h[k] = v }
      h
    end

    def marshal_load(hash)

    @!visibility private
    def marshal_load(hash)
      initialize
      populate_from(hash)
    end

    def populate_from(hash)

    def populate_from(hash)
      hash.each_pair { |k, v| self[k] = v }
      self
    end

    def put_if_absent(key, value)

    Returns:
    • (Object, nil) - the previous value when key was present or nil when there was no key

    Parameters:
    • value (Object) --
    • key (Object) --
    def put_if_absent(key, value)
      computed = false
      result   = compute_if_absent(key) do
        computed = true
        value
      end
      computed ? nil : result
    end unless method_defined?(:put_if_absent)

    def raise_fetch_no_key

    def raise_fetch_no_key
      raise KeyError, 'key not found'
    end

    def size

    Returns:
    • (Integer) - size
    def size
      count = 0
      each_pair { |k, v| count += 1 }
      count
    end unless method_defined?(:size)

    def validate_options_hash!(options)

    def validate_options_hash!(options)
      if (initial_capacity = options[:initial_capacity]) && (!initial_capacity.kind_of?(Integer) || initial_capacity < 0)
        raise ArgumentError, ":initial_capacity must be a positive Integer"
      end
      if (load_factor = options[:load_factor]) && (!load_factor.kind_of?(Numeric) || load_factor <= 0 || load_factor > 1)
        raise ArgumentError, ":load_factor must be a number between 0 and 1"
      end
    end

    def value?(value)

    Returns:
    • (true, false) -

    Parameters:
    • value (Object) --
    def value?(value)
      each_value do |v|
        return true if value.equal?(v)
      end
      false
    end

    def values

    Returns:
    • (::Array) - values
      def values
        arr = []
        each_pair { |k, v| arr << v }
        arr
      end unless method_defined?(:values)