File: sorted_set.rb

package info (click to toggle)
ruby-sorted-set 1.0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 136 kB
  • sloc: ruby: 114; makefile: 4; sh: 4
file content (57 lines) | stat: -rw-r--r-- 1,430 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# :markup: markdown

require 'set'

return if defined?(JRUBY_VERSION)

require 'rbtree'

Object.instance_exec do
  # Undefine SortedSet for two reasons.
  #
  # 1. We should wipe out an existing implementation if Ruby 2.x loads
  #    this library after loading the standard set library which
  #    defines SortedSet.
  #
  # 2. Ruby >=3.0 (set >=1.0.0) sets autoload on SortedSet, and we
  #    shouldn't let the following reference to SortedSet fire it
  #    because it would end up requiring this library, leading to
  #    circular require.
  #
  remove_const :SortedSet if const_defined?(:SortedSet)
end

##
# SortedSet implements a Set whose elements are sorted in ascending
# order (according to the return values of their `<=>` methods) when
# iterating over them.
#
# Every element in SortedSet must be *mutually comparable* to every
# other: comparison with `<=>` must not return nil for any pair of
# elements.  Otherwise ArgumentError will be raised.
#
# ## Example
#
# ```ruby
# require "sorted_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
  # Creates a SortedSet.  See Set.new for details.
  def initialize(*args)
    @hash = RBTree.new
    super
  end
end