Class: Farce::Abstract::SortedSet Abstract
- Inherits:
-
Set
- Object
- Collection
- Set
- Farce::Abstract::SortedSet
- Defined in:
- lib/farce/abstract/sorted_set.rb
Overview
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.
Direct Known Subclasses
Local::SortedSet, SortedSet, Strict::SortedSet, Transaction::SortedSet, Unshared::SortedSet
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Return whether this set has comparator-equivalent members in the same order.
-
#eql?(other) ⇒ Boolean
Compare canonical ordered members using eql?.
-
#hash ⇒ Integer
Return a hash derived from the canonical ordered members.
-
#initialize(enumerable = nil, normalize: nil, compare_by_identity: false, mode: :copy, **options) {|element| ... } ⇒ Farce::Abstract::SortedSet
constructor
Construct a set in ascending comparator order.
-
#intersect?(other) ⇒ Boolean
Return whether another set has a comparator-equivalent member.
-
#intersection(*enumerables) ⇒ Farce::Abstract::SortedSet
(also: #&)
Return a same-kind set containing members present by comparator in every input.
-
#proper_subset?(other) ⇒ Boolean
(also: #<)
Return whether this is a proper comparator subset of other.
-
#proper_superset?(other) ⇒ Boolean
(also: #>)
Return whether this is a proper comparator superset of other.
-
#subset?(other) ⇒ Boolean
(also: #<=)
Return whether every member has a comparator-equivalent member in other.
-
#subtract(enumerable) ⇒ self
Remove every comparator-equivalent member yielded by enumerable.
-
#superset?(other) ⇒ Boolean
(also: #>=)
Return whether every member of other has a comparator-equivalent member here.
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
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.
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.
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?.
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.
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.
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.
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.
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.
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.
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.
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.
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 |