Class: OrderedSet
Overview
Set Classes and Additions
- Kyanite definitions
- Kyanite tests and examples
-
TestKyaniteSet
- Usage
-
require ‘kyanite/set’
Differences between the set classes
Set
-
is unordered. Examples: test_set
- OrderedSet
-
is ordered, it retains the original order, but it will not continually re-sorted – as long you don’t use Dictionary#order_by. Examples: test_ordered_set
SortedSet
-
is ordered and sorted. It is always in order. Examples: test_sorted_set
Instance Method Summary collapse
- #==(other) ⇒ Boolean
-
#[](index) ⇒ Object
N-th Element.
- #each ⇒ Object
-
#first ⇒ Object
First element.
-
#index(object) ⇒ Object
like Array#index.
-
#initialize(enum = nil, &block) ⇒ OrderedSet
constructor
:yields: o.
-
#last ⇒ Object
Last element.
- #order_by(&block) ⇒ Object
-
#to_a ⇒ Array
This method is fast.
- #zugrundeliegendes_dictionary ⇒ Object
Methods inherited from Set
Constructor Details
#initialize(enum = nil, &block) ⇒ OrderedSet
:yields: o
73 74 75 76 77 78 79 80 81 |
# File 'lib/kyanite/set.rb', line 73 def initialize(enum = nil, &block) # :yields: o @hash ||= Dictionary.new enum.nil? and return if block enum.each { |o| add(block[o]) } else merge(enum) end end |
Instance Method Details
#==(other) ⇒ Boolean
103 104 105 106 107 |
# File 'lib/kyanite/set.rb', line 103 def ==(other) silence_warnings do self.to_a.to_set == other.to_a.to_set end end |
#[](index) ⇒ Object
Returns n-th Element.
85 86 87 |
# File 'lib/kyanite/set.rb', line 85 def [](index) @hash.order[index] end |
#each ⇒ Object
114 115 116 117 |
# File 'lib/kyanite/set.rb', line 114 def each to_a.each { |e| yield( e ) } self end |
#first ⇒ Object
Returns first element.
124 125 126 |
# File 'lib/kyanite/set.rb', line 124 def first self[0] end |
#index(object) ⇒ Object
like Array#index
110 111 112 |
# File 'lib/kyanite/set.rb', line 110 def index(object) self.to_a.index(object) end |
#last ⇒ Object
Returns last element.
129 130 131 |
# File 'lib/kyanite/set.rb', line 129 def last self[-1] end |
#order_by(&block) ⇒ Object
119 120 121 |
# File 'lib/kyanite/set.rb', line 119 def order_by( &block ) @hash.order_by( &block ) end |
#to_a ⇒ Array
This method is fast. Nothing needs to be converted.
97 98 99 |
# File 'lib/kyanite/set.rb', line 97 def to_a @hash.order end |
#zugrundeliegendes_dictionary ⇒ Object
90 91 92 |
# File 'lib/kyanite/set.rb', line 90 def zugrundeliegendes_dictionary @hash end |