Class: RbLovely::SortedHash

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

Overview

Note:

A SortedHash should not be modified during iteration.

This class is only provided if boost is available on the system when the gem is installed as it is built using the Boost Multi-index Containers Library.

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.

A SortedHash provides hashed unique keys and ordered non-unique values. Values are sorted using “<=>” and keys are tested for equality using “eql?”.

Examples:

empty_hash = RbLovely::SortedHash.new

# constructor is like: hash[:y] = 5 ; hash[:i] = 1
hash = RbLovely::SortedHash [:y, 5, :i, 1]
hash[:b] = 16
hash[:y] = 4 # updates previous value
expect(hash.first).to equal [:i, 1]
expect(hash[:y]).to equal 4
expect(hash.length).to equal 3
expect(hash.to_a).to eql [[:i, 1], [:y, 4], [:b, 16]]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content = [], compare: nil) ⇒ SortedHash

Returns a new instance of SortedHash.

Examples:

hash = RbLovely::SortedHash.new [:a, 10, :c, 5, :b, 1 ]
expect(hash.to_a).to eql [[:b, 1], [:c, 5], [:a, 10]]

# compare function reverses default sort order
hash = RbLovely::SortedHash.new(compare: proc { |a, b| b <=> a })
hash[:a] = 1
hash[:c] = 5
hash[:b] = 10
expect(hash.to_a).to eql [[:b, 10], [:c, 5], [:a, 1 ]]

Complexity:

  • O(n).



181
# File 'yard.rb', line 181

def initialize content = [], compare: nil ; end

Class Method Details

.[](*content) ⇒ SortedHash

Factory method for creating sorted hash from array.

Examples:

hash = RbLovely::SortedHash [:a, 3, :b, 1]
expect(hash.to_a).to eql [[:b, 1], [:a, 3]]

Complexity:

  • O(n).



190
# File 'yard.rb', line 190

def self.[](*content) ; end

Instance Method Details

#[](key) ⇒ Object

Retrieve value associated with the corresponding key or nil if the key doesn’t exist.

Examples:

hash = RbLovely::SortedHash [:a, 2]
expect(hash[:a]).to equal 2
expect(hash[:b]).to equal nil

Complexity:



239
# File 'yard.rb', line 239

def [](key) ; end

#[]=(key, value) ⇒ Object

Set the value associated with a key. If the key already then it and its value are removed.

Examples:

hash = RbLovely::SortedHash.new
hash[:a] = 'yo'

Complexity:

  • O©.



198
# File 'yard.rb', line 198

def []=(key, value) ; end

#clearObject

Remove all values from the hash.

Examples:

hash = RbLovely::SortedHash [:a, 10]
hash.clear
expect(hash.empty?).to equal true

Complexity:

  • O(n)



231
# File 'yard.rb', line 231

def clear ; end

#delete(key) ⇒ Object

Delete the value associated with a key.

Examples:

hash = RbLovely::SortedHash [:a, 5 ]
expect(hash.delete(:a)).to equal 5
expect(hash.delete(:b)).to equal nil

Complexity:



215
# File 'yard.rb', line 215

def delete key ; end

#each(&block) ⇒ Object

Calls block once for each key, passing the key-value pair as parameters.

Examples:

hash = RbLovely::SortedHash [:a, 10, :b, 1]
# This would call the block in value order: with (:b, 1) followed by (:a, 10).
hash.each { |key, value| puts "#{key} => #{value}" }

Complexity:

  • O(n)



223
# File 'yard.rb', line 223

def each(&block) ; end

#empty?Boolean

Return true if the hash is empty.

Complexity:

  • O©.



372
# File 'yard.rb', line 372

def empty? ; end

#firstObject

Retrieve first key-value pair as determined by value sort order or nil if the hash is empty.

Examples:

@set = RbLovely::SortedHash [:a, 2, :b, 1]
expect(@set.first).to eql [:b, 1]

Complexity:



252
# File 'yard.rb', line 252

def first ; end

#first_keyObject

Retrieve first key as determined by value sort order or nil if the hash is empty.

Examples:

@set = RbLovely::SortedHash [:a, 1, :b, 2]
expect(@set.first_value).to equal :a

Complexity:



268
# File 'yard.rb', line 268

def first_key ; end

#first_valueArray

Retrieve first value as determined by value sort order or nil if the hash is empty.

Examples:

@set = RbLovely::SortedHash [:a, 2, :b, 1]
expect(@set.first_value).to equal 1

Complexity:



260
# File 'yard.rb', line 260

def first_value ; end

#include?(key) ⇒ Boolean Also known as: has_key?, key?

Return true if the key is contained in the hash.

Complexity:



243
# File 'yard.rb', line 243

def include?(key) ; end

#lastArray

Retrieve last key-value pair as determined by value sort order or nil if the hash is empty.

Examples:

@set = RbLovely::SortedHash [:a, 2, :b, 1]
expect(@set.last).to equal [:a, 2]

Complexity:



276
# File 'yard.rb', line 276

def last ; end

#last_keyObject

Retrieve last key as determined by value sort order or nil if the hash is empty.

Examples:

@set = RbLovely::SortedHash [:a, 1, :b, 2]
expect(@set.last_key).to equal :b

Complexity:



290
# File 'yard.rb', line 290

def last_key ; end

#last_valueObject

Retrieve last value as determined by value sort order or nil if the hash is empty.

Examples:

@set = RbLovely::SortedHash [:a, 2, :b, 1]
expect(@set.last_value).to equal 2

Complexity:



283
# File 'yard.rb', line 283

def last_value ; end

#lengthNumber

Gets the number of elements in the hash.

Complexity:

  • O©.



367
# File 'yard.rb', line 367

def length ; end

#popArray

Remove the last key-value pair in the hash and return it or return nil if the hash is empty.

Examples:

set = RbLovely::SortedHash [:a, 2, :b, 10]
expect(hash.pop).to equal [:b, 10]

See Also:

Complexity:

  • O©.



326
# File 'yard.rb', line 326

def pop ; end

#pop_keyObject

Remove the last key-value pair in the hash and return the key or return nil if the hash is empty.

Examples:

set = RbLovely::SortedHash [:a, 2, :b, 10]
expect(hash.pop_key).to equal :b

See Also:

Complexity:

  • O©.



335
# File 'yard.rb', line 335

def pop_key ; end

#pop_valueObject

Remove the last key-value pair in the hash and return it or return nil if the hash is empty.

Examples:

set = RbLovely::SortedHash [:a, 2, :b, 10]
expect(hash.pop_value).to equal 10

See Also:

Complexity:

  • O©.



344
# File 'yard.rb', line 344

def pop_value ; end

#replace(key, value) ⇒ Object

Set the value associated with a key. If the key already then it and its value are removed.

Examples:

hash = RbLovely::SortedHash[:a, 'yi']
expect(hash.replace(:a, 'yo')).to eql('yi')
expect(hash.replace(:b, 'hm')).to eql(nil)


206
# File 'yard.rb', line 206

def replace(key, value) ; end

#shiftArray

Remove the first key-value pair in the hash and return it or return nil if the hash is empty.

Examples:

set = RbLovely::SortedHash [:a, 2, :b, 10]
expect(hash.shift).to equal [:a, 2]

See Also:

Complexity:

  • O©.



299
# File 'yard.rb', line 299

def shift ; end

#shift_keyObject

Remove the first key-value pair in the hash and return the key or return nil if the hash is empty.

Examples:

set = RbLovely::SortedHash [:a, 2, :b, 10]
expect(hash.shift_key).to equal :a

See Also:

Complexity:

  • O©.



308
# File 'yard.rb', line 308

def shift_key ; end

#shift_valueObject

Remove the first key-value pair in the hash and return it or return nil if the hash is empty.

Examples:

set = RbLovely::SortedHash [:a, 2, :b, 10]
expect(hash.shift_value).to equal 2

See Also:

Complexity:

  • O©.



317
# File 'yard.rb', line 317

def shift_value ; end