Class: Farce::Abstract::SortedSet Abstract

Inherits:
Set show all
Defined in:
lib/farce/abstract/sorted_set.rb

Overview

This class is abstract.

A set maintained in ascending comparator order.

Membership, insertion, and deletion use <=>. Two elements occupy the same slot when their comparison returns zero. Elements must be mutually comparable and must not change their ordering while stored. Incompatible elements raise during insertion or lookup.

Instance Method Summary collapse

Methods inherited from Set

#<=>, #^, #add, #add?, #as_extended_json, #as_json, #bson_type, #classify, #clear, #compact_blank, #compact_blank!, #compare_by_identity?, #deep_dup, #delete, #delete?, #delete_if, #difference, #disjoint?, #divide, #duplicable?, #each, #excluding, #flatten, #include?, #including, #keep_if, #merge, #reject, #reject!, #select, #select!, #size, #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 Collection

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

Methods included from Internal::Copyable

#duplicable?

Constructor Details

#initialize(enumerable = nil, normalize: nil, compare_by_identity: false, mode: :copy, **options) {|element| ... } ⇒ Farce::Abstract::SortedSet

Construct a set in ascending comparator order.

Returns The new sorted set.

Parameters:

  • enumerable (#each, nil) (defaults to: nil) —

    The initial elements, or nil for an empty set.

  • normalize (Symbol, Proc, Hash, Farce::Abstract::Map, nil) (defaults to: nil) —

    The element normalizer.

  • compare_by_identity (Boolean) (defaults to: false) —

    Must be false. Membership uses <=>.

  • mode (Symbol) (defaults to: :copy) —

    The default transfer mode. Only Farce::SortedSet accepts this option, which defaults to :copy.

  • options (Hash) —

    Additional options for the selected variant.

Options Hash (**options):

  • scope (Symbol) — default: :ractor —

    The scope used by Farce::Local::SortedSet.

Yields:

  • (element) —

    Optionally transform each initial element before normalization.

Yield Parameters:

  • element (BasicObject) —

    An element from enumerable.

Yield Returns:

  • (BasicObject) —

    The comparable element to normalize and store.

Raises:

  • (ArgumentError) —

    If compare_by_identity is true.

See Also:

Raises:

  • (ArgumentError)


28
29
30
31
32
33
# File 'lib/farce/abstract/sorted_set.rb', line 28

def initialize(
  enumerable = nil, normalize: nil, compare_by_identity: false, mode: UNDEFINED, **, &transform
)
  raise ArgumentError, "sorted sets do not support identity comparison" if compare_by_identity
  super(enumerable, normalize:, compare_by_identity: false, mode:, **, &transform)
end

Instance Method Details

#==(other) ⇒ Boolean

Return whether this set has comparator-equivalent members in the same order.

Parameters:

  • other (BasicObject) —

    The object to compare against.

Returns:

  • (Boolean) —

    Whether other is a Farce sorted set with comparator-equivalent members.



41
42
43
44
45
# File 'lib/farce/abstract/sorted_set.rb', line 41

def ==(other)
  return true if equal?(other)
  return false unless other.is_a?(Abstract::SortedSet) && size == other.size
  ordered_keys.zip(other.ordered_keys).all? { comparator_equal?(_1, _2) }
end

#eql?(other) ⇒ Boolean

Compare canonical ordered members using eql?.

Parameters:

  • other (BasicObject) —

    The object to compare against.

Returns:

  • (Boolean) —

    Whether other is a Farce sorted set with eql? ordered members.



50
51
52
53
# File 'lib/farce/abstract/sorted_set.rb', line 50

def eql?(other)
  return true if equal?(other)
  other.is_a?(Abstract::SortedSet) && ordered_keys.eql?(other.ordered_keys)
end

#hash ⇒ Integer

Return a hash derived from the canonical ordered members.

Returns:

  • (Integer) —

    The hash code.



57
# File 'lib/farce/abstract/sorted_set.rb', line 57

def hash = ordered_keys.hash

#intersect?(other) ⇒ Boolean

Return whether another set has a comparator-equivalent member.

Parameters:

Returns:

  • (Boolean) —

    Whether the comparator-based relation holds.

Raises:

  • (ArgumentError) —

    If other is not a Farce or Ruby set.



132
133
134
135
136
137
# File 'lib/farce/abstract/sorted_set.rb', line 132

def intersect?(other)
  validate_set_like(other)
  index = ordered_index(other)
  left, right = size <= index.size ? [ordered_keys, index] : [index.keys, @map]
  left.any? { right.key?(it) }
end

#intersection(*enumerables) ⇒ Farce::Abstract::SortedSet Also known as: &

Return a same-kind set containing members present by comparator in every input.

Parameters:

  • enumerables (Array<#each>) —

    The collections whose comparator-equivalent members must be present.

Returns:



78
79
80
81
# File 'lib/farce/abstract/sorted_set.rb', line 78

def intersection(*enumerables)
  indexes = enumerables.map { ordered_index(it) }
  dup.filter_stored! { |key, _| indexes.all? { it.key?(key) } }
end

#proper_subset?(other) ⇒ Boolean Also known as: <

Return whether this is a proper comparator subset of other.

Parameters:

Returns:

  • (Boolean) —

    Whether the comparator-based relation holds.

Raises:

  • (ArgumentError) —

    If other is not a Farce or Ruby set.



99
100
101
102
103
# File 'lib/farce/abstract/sorted_set.rb', line 99

def proper_subset?(other)
  validate_set_like(other)
  index = ordered_index(other)
  size < index.size && ordered_keys.all? { index.key?(it) }
end

#proper_superset?(other) ⇒ Boolean Also known as: >

Return whether this is a proper comparator superset of other.

Parameters:

Returns:

  • (Boolean) —

    Whether the comparator-based relation holds.

Raises:

  • (ArgumentError) —

    If other is not a Farce or Ruby set.



121
122
123
124
125
# File 'lib/farce/abstract/sorted_set.rb', line 121

def proper_superset?(other)
  validate_set_like(other)
  index = ordered_index(other)
  size > index.size && index.keys.all? { @map.key?(it) }
end

#subset?(other) ⇒ Boolean Also known as: <=

Return whether every member has a comparator-equivalent member in other.

Parameters:

Returns:

  • (Boolean) —

    Whether the comparator-based relation holds.

Raises:

  • (ArgumentError) —

    If other is not a Farce or Ruby set.



88
89
90
91
92
# File 'lib/farce/abstract/sorted_set.rb', line 88

def subset?(other)
  validate_set_like(other)
  index = ordered_index(other)
  size <= index.size && ordered_keys.all? { index.key?(it) }
end

#subtract(enumerable) ⇒ self

Remove every comparator-equivalent member yielded by enumerable.

Parameters:

  • enumerable (#each) —

    The elements to normalize and remove by comparison.

Returns:

  • (self) —

    The set.



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/farce/abstract/sorted_set.rb', line 62

def subtract(enumerable)
  check_frozen!
  if ordered_compatible?(enumerable)
    enumerable.each_stored { |key, _| @map.delete(key) }
  else
    each_input(enumerable) do |element|
      key = lookup_key(normalize_element(element))
      @map.delete(key) unless MISSING_KEY.equal?(key)
    end
  end
  self
end

#superset?(other) ⇒ Boolean Also known as: >=

Return whether every member of other has a comparator-equivalent member here.

Parameters:

Returns:

  • (Boolean) —

    Whether the comparator-based relation holds.

Raises:

  • (ArgumentError) —

    If other is not a Farce or Ruby set.



110
111
112
113
114
# File 'lib/farce/abstract/sorted_set.rb', line 110

def superset?(other)
  validate_set_like(other)
  index = ordered_index(other)
  size >= index.size && index.keys.all? { @map.key?(it) }
end