This project is closed and read-only.
Bug #118 » set.patch
| lib/set.rb (Arbeitskopie) | ||
|---|---|---|
|
# == Overview
|
||
|
#
|
||
|
# This library provides the Set class, which deals with a collection
|
||
|
# of unordered values with no duplicates. It is a hybrid of Array's
|
||
|
# of unordered values with no duplicates. It is a hybrid of Array's
|
||
|
# intuitive inter-operation facilities and Hash's fast lookup. If you
|
||
|
# need to keep values ordered, use the SortedSet class.
|
||
|
#
|
||
|
# The method +to_set+ is added to Enumerable for convenience.
|
||
|
#
|
||
|
# See the Set class for an example of usage.
|
||
|
# See the Set and SortedSet documentation for examples of usage.
|
||
|
#
|
||
| ... | ... | |
|
end
|
||
|
end
|
||
|
# SortedSet implements a set which elements are sorted in order. See Set.
|
||
|
#
|
||
|
# SortedSet implements a Set that guarantees that it's element are
|
||
|
# yielded in sorted order (according to the return values of their #<=> methods)
|
||
|
# when iterating over them.
|
||
|
#
|
||
|
# All elements that are added to a SortedSet must include the Comparable module.
|
||
|
#
|
||
|
# Also, all elements must be <em>mutually comparable</em>:
|
||
|
# <tt>el1 <=> el2</tt> must not return <tt>nil</tt> for any elements
|
||
|
# <tt>el1</tt> and <tt>el2</tt>, else an ArgumentError will be raised
|
||
|
# when iterating over the SortedSet.
|
||
|
#
|
||
|
# == Example
|
||
|
#
|
||
|
# require "set"
|
||
|
#
|
||
|
# set = SortedSet.new(2, 1, 5, 6, 4, 5, 3, 3, 3)
|
||
|
# ary = []
|
||
|
#
|
||
|
# set.each do |obj|
|
||
|
# ary << obj
|
||
|
# end
|
||
|
#
|
||
|
# p ary # => [1, 2, 3, 4, 5, 6]
|
||
|
#
|
||
|
# set2 = SortedSet.new(1, 2, "3")
|
||
|
# set2.each { |obj| } # => raises ArgumentError: comparison of Fixnum with String failed
|
||
|
#
|
||
|
class SortedSet < Set
|
||
|
@@setup = false
|
||
| ... | ... | |
|
@hash = RBTree.new
|
||
|
super
|
||
|
end
|
||
|
|
||
|
def add(obj)
|
||
|
obj.is_a?(Comparable) or raise ArgumentError, "value must be comparable"
|
||
|
super
|
||
|
end
|
||
|
alias << add
|
||
|
}
|
||
|
rescue LoadError
|
||
|
module_eval %{
|
||
| ... | ... | |
|
super
|
||
|
end
|
||
|
def add(o)
|
||
|
def add(obj)
|
||
|
obj.is_a?(Comparable) or raise ArgumentError, "value must be comparable"
|
||
|
@keys = nil
|
||
|
@hash[o] = true
|
||
|
self
|
||
|
super
|
||
|
end
|
||
|
alias << add
|
||