Class: Hamster::SortedSet
- Inherits:
-
Object
- Object
- Hamster::SortedSet
- Includes:
- Enumerable, Immutable
- Defined in:
- lib/hamster/sorted_set.rb
Overview
A SortedSet is a collection of ordered values with no duplicates. Unlike a Vector, in which items can appear in any arbitrary order, a SortedSet always keeps items either in their natural order, or in an order defined by a comparator block which is provided at initialization time.
SortedSet uses ‘#<=>` (or its comparator block) to determine which items are equivalent. If the comparator indicates that an existing item and a new item are equal, any attempt to insert the new item will have no effect.
This means that all the items inserted into any one SortedSet must all be comparable. For example, you cannot put ‘String`s and `Integer`s in the same SortedSet. This is unlike Set, which can store items of any type, as long as they all support #hash and #eql?.
A SortedSet can be created in either of the following ways:
Hamster::SortedSet.new([1, 2, 3]) # any Enumerable can be used to initialize
Hamster::SortedSet['A', 'B', 'C', 'D']
Or if you want to use a custom ordering:
Hamster::SortedSet.new([1,2,3]) { |a, b| -a <=> -b }
Hamster::SortedSet.new([1, 2, 3]) { |num| -num }
SortedSet can use a 2-parameter block which returns 0, 1, or -1 as a comparator (like ‘Array#sort`), or use a 1-parameter block to derive sort keys (like `Array#sort_by`) which will be compared using `#<=>`.
Like all Hamster collections, ‘SortedSet`s are immutable. Any operation which you might expect to “modify” a SortedSet will actually return a new collection and leave the existing one unchanged.
SortedSet supports the same basic set-theoretic operations as Set, including #union, #intersection, #difference, and #exclusion, as well as #subset?, #superset?, and so on. Unlike Set, it does not define comparison operators like ‘#>` or `#<` as aliases for the superset/subset predicates. Instead, these comparison operators do a item-by-item comparison between the SortedSet and another sequential collection. (See `Array#<=>` for details.)
Additionally, since ‘SortedSet`s are ordered, they also support indexed retrieval of items using #at or #[]. Like Vector, negative indices count back from the end of the SortedSet.
Getting the #max or #min item from a SortedSet, as defined by its comparator, is a constant time operation.
Defined Under Namespace
Classes: AVLNode, PlainAVLNode
Class Method Summary collapse
-
.[](*items) ⇒ SortedSet
Create a new
SortedSetpopulated with the given items. -
.alloc(node) ⇒ Set
“Raw” allocation of a new
SortedSet. -
.empty ⇒ SortedSet
Return an empty
SortedSet.
Instance Method Summary collapse
-
#above(item, &block) ⇒ Object
Select elements greater than a value.
-
#add(item) ⇒ SortedSet
(also: #<<)
Return a new
SortedSetwithitemadded. -
#add?(item) ⇒ SortedSet, false
If
itemis not a member of thisSortedSet, return a newSortedSetwithitemadded. -
#at(index) ⇒ Object
Retrieve the item at
index. -
#below(item, &block) ⇒ Object
Select elements less than a value.
-
#between(from, to, &block) ⇒ Object
Select elements between two values.
-
#clear ⇒ SortedSet
Return an empty
SortedSetinstance, of the same class as this one. -
#delete(item) ⇒ SortedSet
Return a new
SortedSetwithitemremoved. -
#delete?(item) ⇒ SortedSet, false
If
itemis a member of thisSortedSet, return a newSortedSetwithitemremoved. -
#delete_at(index) ⇒ SortedSet
Return a new
SortedSetwith the item atindexremoved. -
#difference(other) ⇒ SortedSet
(also: #subtract, #-)
Return a new
SortedSetwith all the items inotherremoved. -
#disjoint?(other) ⇒ Boolean
Return
trueif this set andotherdo not share any items. -
#drop(n) ⇒ SortedSet
Drop the first
nelements and return the rest in a newSortedSet. -
#drop_while {|item| ... } ⇒ SortedSet, Enumerator
Drop elements up to, but not including, the first element for which the block returns
nilorfalse. -
#each {|item| ... } ⇒ self, Enumerator
Call the given block once for each item in the set, passing each item from first to last successively to the block.
-
#empty? ⇒ Boolean
Return
trueif thisSortedSetcontains no items. -
#eql?(other) ⇒ Boolean
Return true if
otherhas the same type and contents as thisSortedSet. -
#exclusion(other) ⇒ SortedSet
(also: #^)
Return a new
SortedSetwith all the items which are members of this set or ofother, but not both. -
#fetch(index, default = (missing_default = true)) ⇒ Object
Retrieve the value at
indexwith optional default. -
#find_index(obj = (missing_obj = true), &block) ⇒ Integer
(also: #index)
Find the index of a given object or an element that satisfies the given block.
-
#first ⇒ Object
Return the “lowest” element in this set, as determined by its sort order.
-
#from(item, &block) ⇒ Object
Select elements greater than or equal to a value.
-
#hash ⇒ Integer
See ‘Object#hash`.
-
#include?(item) ⇒ Boolean
(also: #member?)
Return
trueif the given item is present in thisSortedSet. -
#initialize(items = [], &block) ⇒ SortedSet
constructor
A new instance of SortedSet.
-
#intersect?(other) ⇒ Boolean
Return
trueif this set andotherhave at least one item in common. -
#intersection(other) ⇒ SortedSet
(also: #&)
Return a new
SortedSetwhich contains all the items which are members of both this set andother. -
#last ⇒ Object
Return the “highest” element in this set, as determined by its sort order.
-
#map {|item| ... } ⇒ SortedSet, Enumerator
(also: #collect)
Invoke the given block once for each item in the set, and return a new
SortedSetcontaining the values returned by the block. - #marshal_dump ⇒ ::Array
- #marshal_load(array) ⇒ Object
-
#max {|a, b| ... } ⇒ Object
Return the “highest” element in this set, as determined by its sort order.
-
#min {|a, b| ... } ⇒ Object
Return the “lowest” element in this set, as determined by its sort order.
-
#proper_subset?(other) ⇒ Boolean
Returns
trueifothercontains all the items in this set, plus at least one item which is not in this set. -
#proper_superset?(other) ⇒ Boolean
Returns
trueif this set contains all the items inother, plus at least one item which is not inother. -
#reverse_each(&block) ⇒ self
Call the given block once for each item in the set, passing each item starting from the last, and counting back to the first, successively to the block.
-
#sample ⇒ Object
Return a randomly chosen item from this set.
-
#select {|item| ... } ⇒ SortedSet
(also: #find_all, #keep_if)
Return a new
SortedSetcontaining all elements for which the given block returns true. -
#size ⇒ Integer
(also: #length)
Return the number of items in this
SortedSet. -
#slice(arg, length = (missing_length = true)) ⇒ Object
(also: #[])
Return specific objects from the
Vector. -
#sort(&block) ⇒ SortedSet
(also: #sort_by)
Return a new
SortedSetwith the same items, but a sort order determined by the given block. -
#subset?(other) ⇒ Boolean
Return
trueif all items in this set are also inother. -
#superset?(other) ⇒ Boolean
Return
trueif all items inotherare also in this set. -
#take(n) ⇒ SortedSet
Return only the first
nelements in a newSortedSet. -
#take_while {|item| ... } ⇒ SortedSet, Enumerator
Gather elements up to, but not including, the first element for which the block returns
nilorfalse, and return them in a newSortedSet. -
#union(other) ⇒ SortedSet
(also: #|, #+, #merge)
Return a new
SortedSetwhich contains all the members of both this set andother. -
#up_to(item, &block) ⇒ Object
Select elements less than or equal to a value.
-
#values_at(*indices) ⇒ SortedSet
Return a new
SortedSetwith only the elements at the givenindices.
Methods included from Enumerable
#<=>, #==, #compact, #each_index, #grep, #grep_v, #group_by, #inspect, #join, #partition, #pretty_print, #product, #reject, #sum, #to_set
Methods included from Enumerable
Methods included from Immutable
Constructor Details
#initialize(items = [], &block) ⇒ SortedSet
Returns a new instance of SortedSet.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/hamster/sorted_set.rb', line 85 def initialize(items=[], &block) items = items.to_a if block if block.arity == 1 || block.arity == -1 comparator = lambda { |a,b| block.call(a) <=> block.call(b) } items = items.sort_by(&block) else comparator = block items = items.sort(&block) end @node = AVLNode.from_items(items, comparator) else @node = PlainAVLNode.from_items(items.sort) end end |
Class Method Details
.[](*items) ⇒ SortedSet
Create a new SortedSet populated with the given items. This method does not accept a comparator block.
61 62 63 |
# File 'lib/hamster/sorted_set.rb', line 61 def [](*items) new(items) end |
.alloc(node) ⇒ Set
“Raw” allocation of a new SortedSet. Used internally to create a new instance quickly after obtaining a modified binary tree.
78 79 80 81 82 |
# File 'lib/hamster/sorted_set.rb', line 78 def alloc(node) result = allocate result.instance_variable_set(:@node, node) result end |
.empty ⇒ SortedSet
Return an empty SortedSet. If used on a subclass, returns an empty instance of that class.
69 70 71 |
# File 'lib/hamster/sorted_set.rb', line 69 def empty @empty ||= self.alloc(PlainAVLNode::EmptyNode) end |
Instance Method Details
#above(item) ⇒ SortedSet #above(item) {|item| ... } ⇒ nil
Select elements greater than a value.
760 761 762 763 764 765 766 |
# File 'lib/hamster/sorted_set.rb', line 760 def above(item, &block) if block_given? @node.each_greater(item, false, &block) else self.class.alloc(@node.suffix(item, false)) end end |
#add(item) ⇒ SortedSet Also known as: <<
Return a new SortedSet with item added. If item is already in the set, return self.
128 129 130 131 132 133 134 |
# File 'lib/hamster/sorted_set.rb', line 128 def add(item) catch :present do node = @node.insert(item) return self.class.alloc(node) end self end |
#add?(item) ⇒ SortedSet, false
If item is not a member of this SortedSet, return a new SortedSet with item added. Otherwise, return false.
148 149 150 |
# File 'lib/hamster/sorted_set.rb', line 148 def add?(item) !include?(item) && add(item) end |
#at(index) ⇒ Object
Retrieve the item at index. If there is none (either the provided index is too high or too low), return nil.
212 213 214 215 216 |
# File 'lib/hamster/sorted_set.rb', line 212 def at(index) index += @node.size if index < 0 return nil if index >= @node.size || index < 0 @node.at(index) end |
#below(item) ⇒ SortedSet #below(item) {|item| ... } ⇒ nil
Select elements less than a value.
791 792 793 794 795 796 797 |
# File 'lib/hamster/sorted_set.rb', line 791 def below(item, &block) if block_given? @node.each_less(item, false, &block) else self.class.alloc(@node.prefix(item, false)) end end |
#between(from, to) ⇒ SortedSet #between(item) {|item| ... } ⇒ nil
Select elements between two values.
891 892 893 894 895 896 897 |
# File 'lib/hamster/sorted_set.rb', line 891 def between(from, to, &block) if block_given? @node.each_between(from, to, &block) else self.class.alloc(@node.between(from, to)) end end |
#clear ⇒ SortedSet
Return an empty SortedSet instance, of the same class as this one. Useful if you have multiple subclasses of SortedSet and want to treat them polymorphically.
913 914 915 916 917 918 919 |
# File 'lib/hamster/sorted_set.rb', line 913 def clear if @node.natural_order? self.class.empty else self.class.alloc(@node.clear) end end |
#delete(item) ⇒ SortedSet
Return a new SortedSet with item removed. If item is not a member of the set, return self.
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/hamster/sorted_set.rb', line 161 def delete(item) catch :not_present do node = @node.delete(item) if node.empty? && node.natural_order? return self.class.empty else return self.class.alloc(node) end end self end |
#delete?(item) ⇒ SortedSet, false
If item is a member of this SortedSet, return a new SortedSet with item removed. Otherwise, return false.
184 185 186 |
# File 'lib/hamster/sorted_set.rb', line 184 def delete?(item) include?(item) && delete(item) end |
#delete_at(index) ⇒ SortedSet
Return a new SortedSet with the item at index removed. If the given index does not exist (if it is too high or too low), return self.
197 198 199 |
# File 'lib/hamster/sorted_set.rb', line 197 def delete_at(index) (item = at(index)) ? delete(item) : self end |
#difference(other) ⇒ SortedSet Also known as: subtract, -
Return a new SortedSet with all the items in other removed. other can be any Enumerable object.
637 638 639 |
# File 'lib/hamster/sorted_set.rb', line 637 def difference(other) self.class.alloc(@node.bulk_delete(other)) end |
#disjoint?(other) ⇒ Boolean
Return true if this set and other do not share any items.
714 715 716 717 718 719 720 721 |
# File 'lib/hamster/sorted_set.rb', line 714 def disjoint?(other) if size < other.size each { |item| return false if other.include?(item) } else other.each { |item| return false if include?(item) } end true end |
#drop(n) ⇒ SortedSet
Drop the first n elements and return the rest in a new SortedSet.
542 543 544 |
# File 'lib/hamster/sorted_set.rb', line 542 def drop(n) derive_new_sorted_set(@node.drop(n)) end |
#drop_while {|item| ... } ⇒ SortedSet, Enumerator
Drop elements up to, but not including, the first element for which the block returns nil or false. Gather the remaining elements into a new SortedSet. If no block is given, an Enumerator is returned instead.
568 569 570 571 572 573 574 575 576 |
# File 'lib/hamster/sorted_set.rb', line 568 def drop_while return enum_for(:drop_while) if not block_given? n = 0 each do |item| break unless yield item n += 1 end drop(n) end |
#each {|item| ... } ⇒ self, Enumerator
Call the given block once for each item in the set, passing each item from first to last successively to the block. If no block is provided, returns an Enumerator.
357 358 359 360 361 |
# File 'lib/hamster/sorted_set.rb', line 357 def each(&block) return @node.to_enum if not block_given? @node.each(&block) self end |
#empty? ⇒ Boolean
Return true if this SortedSet contains no items.
104 105 106 |
# File 'lib/hamster/sorted_set.rb', line 104 def empty? @node.empty? end |
#eql?(other) ⇒ Boolean
Return true if other has the same type and contents as this SortedSet.
925 926 927 928 929 930 931 932 933 934 935 |
# File 'lib/hamster/sorted_set.rb', line 925 def eql?(other) return true if other.equal?(self) return false if not instance_of?(other.class) return false if size != other.size a, b = self.to_enum, other.to_enum while true return false if !a.next.eql?(b.next) end rescue StopIteration true end |
#exclusion(other) ⇒ SortedSet Also known as: ^
Return a new SortedSet with all the items which are members of this set or of other, but not both. other can be any Enumerable object.
652 653 654 |
# File 'lib/hamster/sorted_set.rb', line 652 def exclusion(other) ((self | other) - (self & other)) end |
#fetch(index) ⇒ Object #fetch(index) {|index| ... } ⇒ Object #fetch(index, default) ⇒ Object
Retrieve the value at index with optional default.
257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/hamster/sorted_set.rb', line 257 def fetch(index, default = (missing_default = true)) if index >= -@node.size && index < @node.size at(index) elsif block_given? yield(index) elsif !missing_default default else raise IndexError, "index #{index} outside of sorted set bounds" end end |
#find_index(obj) ⇒ Integer #find_index {|element| ... } ⇒ Integer Also known as: index
Find the index of a given object or an element that satisfies the given block.
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/hamster/sorted_set.rb', line 510 def find_index(obj = (missing_obj = true), &block) if !missing_obj # Enumerable provides a default implementation, but this is more efficient node = @node index = node.left.size while !node.empty? direction = node.direction(obj) if direction > 0 node = node.right index += (node.left.size + 1) elsif direction < 0 node = node.left index -= (node.right.size + 1) else return index end end nil else super(&block) end end |
#first ⇒ Object
Return the “lowest” element in this set, as determined by its sort order.
397 398 399 |
# File 'lib/hamster/sorted_set.rb', line 397 def first @node.min end |
#from(item) ⇒ SortedSet #from(item) {|item| ... } ⇒ nil
Select elements greater than or equal to a value.
823 824 825 826 827 828 829 |
# File 'lib/hamster/sorted_set.rb', line 823 def from(item, &block) if block_given? @node.each_greater(item, true, &block) else self.class.alloc(@node.suffix(item, true)) end end |
#hash ⇒ Integer
See ‘Object#hash`.
939 940 941 |
# File 'lib/hamster/sorted_set.rb', line 939 def hash reduce(0) { |hash, item| (hash << 5) - hash + item.hash } end |
#include?(item) ⇒ Boolean Also known as: member?
Return true if the given item is present in this SortedSet. More precisely, return true if an object which compares as “equal” using this set’s comparator is present.
464 465 466 |
# File 'lib/hamster/sorted_set.rb', line 464 def include?(item) @node.include?(item) end |
#intersect?(other) ⇒ Boolean
Return true if this set and other have at least one item in common.
730 731 732 |
# File 'lib/hamster/sorted_set.rb', line 730 def intersect?(other) !disjoint?(other) end |
#intersection(other) ⇒ SortedSet Also known as: &
Return a new SortedSet which contains all the items which are members of both this set and other. other can be any Enumerable object.
623 624 625 |
# File 'lib/hamster/sorted_set.rb', line 623 def intersection(other) self.class.alloc(@node.keep_only(other)) end |
#last ⇒ Object
Return the “highest” element in this set, as determined by its sort order.
416 417 418 |
# File 'lib/hamster/sorted_set.rb', line 416 def last @node.max end |
#map {|item| ... } ⇒ SortedSet, Enumerator Also known as: collect
Invoke the given block once for each item in the set, and return a new SortedSet containing the values returned by the block. If no block is given, returns an Enumerator.
448 449 450 451 452 |
# File 'lib/hamster/sorted_set.rb', line 448 def map return enum_for(:map) if not block_given? return self if empty? self.class.alloc(@node.from_items(super)) end |
#marshal_dump ⇒ ::Array
945 946 947 948 949 950 951 |
# File 'lib/hamster/sorted_set.rb', line 945 def marshal_dump if @node.natural_order? to_a else raise TypeError, "can't dump SortedSet with custom sort order" end end |
#marshal_load(array) ⇒ Object
954 955 956 |
# File 'lib/hamster/sorted_set.rb', line 954 def marshal_load(array) initialize(array) end |
#max {|a, b| ... } ⇒ Object
Return the “highest” element in this set, as determined by its sort order. Or, if a block is provided, use the block as a comparator to find the “highest” element. (See ‘Enumerable#max`.)
410 411 412 |
# File 'lib/hamster/sorted_set.rb', line 410 def max block_given? ? super : @node.max end |
#min {|a, b| ... } ⇒ Object
Return the “lowest” element in this set, as determined by its sort order. Or, if a block is provided, use the block as a comparator to find the “lowest” element. (See ‘Enumerable#min`.)
391 392 393 |
# File 'lib/hamster/sorted_set.rb', line 391 def min block_given? ? super : @node.min end |
#proper_subset?(other) ⇒ Boolean
Returns true if other contains all the items in this set, plus at least one item which is not in this set.
689 690 691 692 |
# File 'lib/hamster/sorted_set.rb', line 689 def proper_subset?(other) return false if other.size <= size all? { |item| other.include?(item) } end |
#proper_superset?(other) ⇒ Boolean
Returns true if this set contains all the items in other, plus at least one item which is not in other.
703 704 705 |
# File 'lib/hamster/sorted_set.rb', line 703 def proper_superset?(other) other.proper_subset?(self) end |
#reverse_each(&block) ⇒ self
Call the given block once for each item in the set, passing each item starting from the last, and counting back to the first, successively to the block.
376 377 378 379 380 |
# File 'lib/hamster/sorted_set.rb', line 376 def reverse_each(&block) return @node.enum_for(:reverse_each) if not block_given? @node.reverse_each(&block) self end |
#sample ⇒ Object
Return a randomly chosen item from this set. If the set is empty, return nil.
905 906 907 |
# File 'lib/hamster/sorted_set.rb', line 905 def sample @node.at(rand(@node.size)) end |
#select {|item| ... } ⇒ SortedSet Also known as: find_all, keep_if
Return a new SortedSet containing all elements for which the given block returns true.
429 430 431 432 433 434 |
# File 'lib/hamster/sorted_set.rb', line 429 def select return enum_for(:select) unless block_given? items_to_delete = [] each { |item| items_to_delete << item unless yield(item) } derive_new_sorted_set(@node.bulk_delete(items_to_delete)) end |
#size ⇒ Integer Also known as: length
Return the number of items in this SortedSet.
114 115 116 |
# File 'lib/hamster/sorted_set.rb', line 114 def size @node.size end |
#set.slice(index) ⇒ Object #set.slice(index, length) ⇒ SortedSet #set.slice(index..end) ⇒ SortedSet Also known as: []
Return specific objects from the Vector. All overloads return nil if the starting index is out of range.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/hamster/sorted_set.rb', line 309 def slice(arg, length = (missing_length = true)) if missing_length if arg.is_a?(Range) from, to = arg.begin, arg.end from += @node.size if from < 0 to += @node.size if to < 0 to += 1 if !arg.exclude_end? length = to - from length = 0 if length < 0 subsequence(from, length) else at(arg) end else arg += @node.size if arg < 0 subsequence(arg, length) end end |
#sort(&block) ⇒ SortedSet Also known as: sort_by
Return a new SortedSet with the same items, but a sort order determined by the given block.
479 480 481 482 483 484 485 |
# File 'lib/hamster/sorted_set.rb', line 479 def sort(&block) if block self.class.new(self.to_a, &block) else self.class.new(self.to_a.sort) end end |
#subset?(other) ⇒ Boolean
Return true if all items in this set are also in other.
664 665 666 667 |
# File 'lib/hamster/sorted_set.rb', line 664 def subset?(other) return false if other.size < size all? { |item| other.include?(item) } end |
#superset?(other) ⇒ Boolean
Return true if all items in other are also in this set.
676 677 678 |
# File 'lib/hamster/sorted_set.rb', line 676 def superset?(other) other.subset?(self) end |
#take(n) ⇒ SortedSet
Return only the first n elements in a new SortedSet.
554 555 556 |
# File 'lib/hamster/sorted_set.rb', line 554 def take(n) derive_new_sorted_set(@node.take(n)) end |
#take_while {|item| ... } ⇒ SortedSet, Enumerator
Gather elements up to, but not including, the first element for which the block returns nil or false, and return them in a new SortedSet. If no block is given, an Enumerator is returned instead.
588 589 590 591 592 593 594 595 596 |
# File 'lib/hamster/sorted_set.rb', line 588 def take_while return enum_for(:take_while) if not block_given? n = 0 each do |item| break unless yield item n += 1 end take(n) end |
#union(other) ⇒ SortedSet Also known as: |, +, merge
Return a new SortedSet which contains all the members of both this set and other. other can be any Enumerable object.
607 608 609 |
# File 'lib/hamster/sorted_set.rb', line 607 def union(other) self.class.alloc(@node.bulk_insert(other)) end |
#up_to(item) ⇒ SortedSet #up_to(item) {|item| ... } ⇒ nil
Select elements less than or equal to a value.
857 858 859 860 861 862 863 |
# File 'lib/hamster/sorted_set.rb', line 857 def up_to(item, &block) if block_given? @node.each_less(item, true, &block) else self.class.alloc(@node.prefix(item, true)) end end |
#values_at(*indices) ⇒ SortedSet
Return a new SortedSet with only the elements at the given indices. If any of the indices do not exist, they will be skipped.
338 339 340 341 |
# File 'lib/hamster/sorted_set.rb', line 338 def values_at(*indices) indices.select! { |i| i >= -@node.size && i < @node.size } self.class.new(indices.map! { |i| at(i) }) end |