Class: OrderedSet

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

Overview

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

Methods inherited from Set

#<=>, #push

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

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

#[](index) ⇒ Object

Returns n-th Element.

Returns:

  • n-th Element



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

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

#eachObject



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

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

#firstObject

Returns first element.

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

#lastObject

Returns last element.

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_aArray

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

#zugrundeliegendes_dictionaryObject



90
91
92
# File 'lib/kyanite/set.rb', line 90

def zugrundeliegendes_dictionary 
  @hash
end