Class: RbLovely::SortedSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
yard.rb

Overview

Note:

A SortedSet should not be modified during iteration.

Some methods come from the Enumerable module. If you do not find the method you are looking for in this documentation then please look there.

Implement a sorted set with no duplicates where values are compared and sorted using the <=> operator on each member.

Examples:

class Person < Struct.new(:name, :age)
  def <=> other
    return age - other.age
  end
end

empty_set = RbLovely::SortedSet.new

set = RbLovely::SortedSet [Person.new('Nyamuk', 2), Person.new('Cold Rain', 9999)]
set.add Person.new('Beards', 15)
set << Person.new('Anna', 12)
set.add Person.new('Moust', 18)
expect(set.first.name).to equal 'Nyamuk'
expect(set.last.name).to equal 'Cold Rain'
expect(set.length).to equal 5

# Gond isn't added because <=> returns 0 for him and Anna
set.add Person.new('Gond', 12)
expect(set.length).to equal 5

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content = []) ⇒ SortedSet

Returns a new instance of SortedSet.

Examples:

set = RbLovely::SortedSet.new [3,1,2]
expect(set.to_a).to eql [1,2,3]

Complexity:

  • O(n).



44
# File 'yard.rb', line 44

def initialize content = [] ; end

Class Method Details

.[](*content) ⇒ SortedSet

Factory method for creating sorted set from array.

Examples:

set = RbLovely::SortedSet [3,1,2]
expect(set.to_a).to eql [1,2,3]

Complexity:

  • O(n).



53
# File 'yard.rb', line 53

def self.[](*content) ; end

Instance Method Details

#delete(value) ⇒ Object

Deletes first member equivalent to value.

Examples:

set = RbLovely::SortedSet [1, 5, 3]
set.delete 3
expect(set.to_a).to eql [1, 5]

Complexity:

  • O(log(n)).



63
# File 'yard.rb', line 63

def delete value ; end

#each(&block) ⇒ Object

Calls block once with each value in the set.

Examples:

set = RbLovely::SortedSet [0, 1, 2, 3]
set.each { |x| puts x }

Complexity:

  • O(n)



131
# File 'yard.rb', line 131

def each(&block) ; end

#empty?Boolean

Return true if the set is empty.

Complexity:

  • O©.



141
# File 'yard.rb', line 141

def empty? ; end

#firstObject

Access the first element in the set.

Examples:

set = RbLovely::SortedSet [4, 0, 2]
expect(set.first).to equal 0

Complexity:

  • O©.



71
# File 'yard.rb', line 71

def first ; end

#lastObject

Access the last element in the set.

Examples:

set = RbLovely::SortedSet [4, 0, 2]
expect(set.last).to equal 4

Complexity:

  • O©.



79
# File 'yard.rb', line 79

def last ; end

#lengthNumber

Gets the number of elements in the set.

Complexity:

  • O©.



136
# File 'yard.rb', line 136

def length ; end

#popObject

Remove the last element from the set.

Examples:

set = RbLovely::SortedSet [4, 0, 2]
expect(set.pop).to equal 4

See Also:

Complexity:

  • O©.



97
# File 'yard.rb', line 97

def pop ; end

#reject!(&predicate) ⇒ Object

Remove elements from the set match a given predicate.

Examples:

set = RbLovely::SortedSet [0, 1, 2, 3]
set.reject!(&:odd?)
expect(set.to_a).to eql([0, 2])

Complexity:

  • O(n)



106
# File 'yard.rb', line 106

def reject!(&predicate) ; end

#reject_first!(&predicate) ⇒ Object

Remove the first element from the set that matches the given predicate.

Examples:

set = RbLovely::SortedSet [0, 1, 2, 3]
set.reject!(&:odd?)
expect(set.to_a).to eql([0, 2, 3])

Complexity:

  • O(log n)



115
# File 'yard.rb', line 115

def reject_first!(&predicate) ; end

#select!(&predicate) ⇒ Object

Remove elements from the set that do not match a given predicate.

Examples:

set = RbLovely::SortedSet [0, 1, 2, 3]
set.select!(&:odd?)
expect(set.to_a).to eql([1, 3])

Complexity:

  • O(n)



124
# File 'yard.rb', line 124

def select!(&predicate) ; end

#shiftObject

Remove the first element from the set.

Examples:

set = RbLovely::SortedSet [4, 0, 2]
expect(set.shift).to equal 0

See Also:

Complexity:

  • O©.



88
# File 'yard.rb', line 88

def shift ; end