Sha256: 1ceb0a19cd1663e711f8887ba439b1a9c0b236653bc12a688e48ceaca58905da

Contents?: true

Size: 1.1 KB

Versions: 12

Compression:

Stored size: 1.1 KB

Contents

# typed: true

require_relative 'sequence'

module Datadog
  module Core
    module Utils
      # Acts as a unique dictionary of objects
      class ObjectSet
        # You can provide a block that defines how the key
        # for this message type is resolved.
        def initialize(seed = 0, &block)
          @sequence = Sequence.new(seed)
          @items = {}
          @key_block = block
        end

        # Submit an array of arguments that define the message.
        # If they match an existing message, it will return the
        # matching object. If it doesn't match, it will yield to
        # the block with the next ID & args given.
        def fetch(*args)
          # TODO: Array hashing is **really** expensive, we probably want to get rid of it in the future
          key = @key_block ? @key_block.call(*args) : args.hash
          @items[key] ||= yield(@sequence.next, *args)
        end

        def length
          @items.length
        end

        def objects
          @items.values
        end

        def freeze
          super
          @items.freeze
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
ddtrace-1.9.0 lib/datadog/core/utils/object_set.rb
ddtrace-1.8.0 lib/datadog/core/utils/object_set.rb
ddtrace-1.7.0 lib/datadog/core/utils/object_set.rb
ddtrace-1.6.1 lib/datadog/core/utils/object_set.rb
ddtrace-1.6.0 lib/datadog/core/utils/object_set.rb
ddtrace-1.5.2 lib/datadog/core/utils/object_set.rb
ddtrace-1.5.1 lib/datadog/core/utils/object_set.rb
ddtrace-1.5.0 lib/datadog/core/utils/object_set.rb
ddtrace-1.4.2 lib/datadog/core/utils/object_set.rb
ddtrace-1.4.1 lib/datadog/core/utils/object_set.rb
ddtrace-1.4.0 lib/datadog/core/utils/object_set.rb
ddtrace-1.3.0 lib/datadog/core/utils/object_set.rb