Class: OrderedSet

Inherits
Set show all
Defined in
lib/kyanite/set.rb

Set Classes and Additions

Kyanite definitions

Set, SortedSet, OrderedSet

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)

Constructor Details

- (OrderedSet) initialize(enum = nil, &block)

: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

- (Boolean) ==(other)

Returns:

  • (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

- (Object) [](index)

N-th Element

Returns:

  • n-th Element



85
86
87
# File 'lib/kyanite/set.rb', line 85

def [](index)
  @hash.order[index]
end

- (Object) each



114
115
116
117
# File 'lib/kyanite/set.rb', line 114

def each
  to_a.each { |e| yield( e ) }
  self
end

- (Object) first

First element

Returns:

  • first element



124
125
126
# File 'lib/kyanite/set.rb', line 124

def first
  self[0]
end

- (Object) index(object)

like Array#index



110
111
112
# File 'lib/kyanite/set.rb', line 110

def index(object)
  self.to_a.index(object)
end

- (Object) last

Last element

Returns:

  • last element



129
130
131
# File 'lib/kyanite/set.rb', line 129

def last
  self[-1]
end

- (Object) order_by(&block)



119
120
121
# File 'lib/kyanite/set.rb', line 119

def order_by( &block )
  @hash.order_by( &block )
end

- (Array) to_a

This method is fast. Nothing needs to be converted.

Returns:



97
98
99
# File 'lib/kyanite/set.rb', line 97

def to_a 
  @hash.order
end