Class: Yardstick::OrderedSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/yardstick/ordered_set.rb

Overview

A base class for an ordered set

Direct Known Subclasses

MeasurementSet, RuleSet

Instance Method Summary collapse

Constructor Details

#initialize(entries = nil) ⇒ Yardstick::OrderedSet

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the OrderedSet instance

Parameters:

  • entries (Array) (defaults to: nil)

    optional entries



18
19
20
21
22
# File 'lib/yardstick/ordered_set.rb', line 18

def initialize(entries = nil)
  @entries = []
  @index   = {}
  merge(entries || [])
end

Instance Method Details

#<<(entry) ⇒ Yardstick::OrderedSet

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Append to the OrderedSet

Parameters:

  • entry (Object)

    the object to append

Returns:



33
34
35
36
37
38
39
# File 'lib/yardstick/ordered_set.rb', line 33

def <<(entry)
  unless include?(entry)
    @index[entry] = @entries.length
    @entries << entry
  end
  self
end

#each {|entry| ... } ⇒ Yardstick::OrderedSet

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Iterate over each entry

Yields:

  • (entry)

    yield to the entry

Yield Parameters:

  • entry (Object)

    an entry in the ordered set

Returns:



67
68
69
70
# File 'lib/yardstick/ordered_set.rb', line 67

def each(&block)
  @entries.each(&block)
  self
end

#empty?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if there are any entries

Returns:

  • (Boolean)

    true if there are no entries, false if there are



78
79
80
# File 'lib/yardstick/ordered_set.rb', line 78

def empty?
  @entries.empty?
end

#include?(entry) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if the entry exists in the set

Parameters:

  • entry (Object)

    the entry to test for

Returns:

  • (Boolean)

    true if the entry exists in the set, false if not



101
102
103
# File 'lib/yardstick/ordered_set.rb', line 101

def include?(entry)
  @index.key?(entry)
end

#index(entry) ⇒ Integer?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the index for the entry in the set

Parameters:

  • entry (Object)

    the entry to check the set for

Returns:

  • (Integer, nil)

    the index for the entry, or nil if it does not exist



114
115
116
# File 'lib/yardstick/ordered_set.rb', line 114

def index(entry)
  @index[entry]
end

#lengthInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The number of entries

Returns:

  • (Integer)

    number of entries



88
89
90
# File 'lib/yardstick/ordered_set.rb', line 88

def length
  @entries.length
end

#merge(other) ⇒ Yardstick::OrderedSet

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Merge in another OrderedSet

Parameters:

  • other (#each)

    the other ordered set

Returns:



50
51
52
53
# File 'lib/yardstick/ordered_set.rb', line 50

def merge(other)
  other.each { |entry| self << entry }
  self
end