Class: Farce::WeakSet

Inherits:
Abstract::WeakSet show all
Includes:
Shareable::Delegated
Defined in:
lib/farce/weak_set.rb

Overview

A Ractor-shareable concurrent set that retains prepared elements weakly. Elements support :raise (the default), :make_shareable, and :dedup. Membership refers to the prepared element itself. Lookup and deletion do not freeze or deduplicate their arguments. Already-shareable elements pass through unchanged, as with other mode-backed containers.

With :dedup, the stored canonical element can differ from the input. Keeping only the input alive may not keep that element alive. Callers are responsible for retaining elements they need. Both add and add? retain their usual set return values rather than returning the stored element.

Examples:

Tracking an object without keeping it alive

resource = []
set = Farce::WeakSet.new(mode: :make_shareable)
set.add(resource)
set.first.equal?(resource) # => true
resource = nil
# After collection, the set can become empty.

Instance Method Summary collapse

Methods included from Shareable

#initialize, #ractor_shareable?

Methods inherited from Abstract::Set

#<=>, #==, #^, #add, #add?, #as_extended_json, #as_json, #bson_type, #classify, #clear, #compact_blank, #compact_blank!, #compare_by_identity?, #deep_dup, #delete_if, #difference, #disjoint?, #divide, #duplicable?, #each, #eql?, #excluding, #flatten, #hash, #including, #initialize, #intersect?, #intersection, #keep_if, #merge, #proper_subset?, #proper_superset?, #reject, #reject!, #select, #select!, #size, #subset?, #subtract, #superset?, #to_a, #to_bson, #to_bson_normalized_value, #to_cbor, #to_json, #to_msgpack, #to_param, #to_query, #to_set, #union, #weak?

Methods inherited from Abstract::Collection

#<<, [], #clear, #compare_by_identity?, #count, #each, #empty?, #filter, #join, #length, #reject, #select, #size, #to_a, #to_s

Methods included from Internal::Copyable

#duplicable?

Instance Method Details

#delete(element) ⇒ self

Remove an element and return self.

Parameters:

  • element (BasicObject) —

    The element to normalize and look up.

Returns:

  • (self) —

    The set.



40
41
42
43
44
45
# File 'lib/farce/weak_set.rb', line 40

def delete(element)
  check_frozen!
  key = lookup_key(normalize_element(element))
  @map.delete(key) unless MISSING_KEY.equal?(key)
  self
end

#delete?(element) ⇒ self?

Remove an element and return self, or nil if it was absent.

Parameters:

  • element (BasicObject) —

    The element to normalize and look up.

Returns:

  • (self, nil) —

    The set if removed, otherwise nil.



48
49
50
51
52
# File 'lib/farce/weak_set.rb', line 48

def delete?(element) # rubocop:disable Naming/PredicateMethod
  check_frozen!
  key = lookup_key(normalize_element(element))
  self unless MISSING_KEY.equal?(key) || !@map.delete(key)
end

#include?(element) ⇒ Boolean Also known as: member?, ===

Return whether an element is present.

Parameters:

  • element (BasicObject) —

    The element to normalize and look up.

Returns:

  • (Boolean) —

    Whether the normalized element is present.



32
33
34
35
# File 'lib/farce/weak_set.rb', line 32

def include?(element)
  key = lookup_key(normalize_element(element))
  !MISSING_KEY.equal?(key) && @map.key?(key)
end

#mode ⇒ Symbol

The default mode used to prepare elements for insertion.

Returns:

  • (Symbol) —

    :raise, :make_shareable, or :dedup



29
# File 'lib/farce/weak_set.rb', line 29

def mode = @manager.mode