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]]

# reverse order by overriding key comparison function.
rhash = RbLovely::SortedHash(compare: proc { |a, b| b <=> a })

Author:

  • nuisanceofcats

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 ]]

Parameters:

  • content (Array) (defaults to: [])

    An array containing key, value, key, value …

  • compare (Proc) (defaults to: nil)

    Comparison function used to order values (rather than default of using <=> method).

See Also:

Complexity:

  • O(n).



185
# File 'yard.rb', line 185

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]]

Parameters:

  • content (Array)

    An array of values to insert into the created hash.

Returns:

Complexity:

  • O(n).



194
# File 'yard.rb', line 194

def self.[](*content) ; end

Instance Method Details

#[](key) ⇒ Object

Retrieve value from hash using key.

Examples:

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

Returns:

  • Value associated with the corresponding key or nil if the key doesn’t exist.

Complexity:



247
# File 'yard.rb', line 247

def [](key) ; end

#[]=(key, value) ⇒ Object

Set the value associated with a key, replacing the existing key’s value.

Examples:

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

Returns:

  • The value that was passed.

See Also:

Complexity:

  • O©.



203
# File 'yard.rb', line 203

def []=(key, value) ; end

#clearObject

Remove all key-value pairs from the hash.

Examples:

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

Complexity:

  • O(n)



238
# File 'yard.rb', line 238

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

Returns:

  • The value associated with the deleted key or nil if the key was not in the hash.

Complexity:



222
# File 'yard.rb', line 222

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)



230
# File 'yard.rb', line 230

def each(&block) ; end

#empty?Boolean

Return true if the hash is empty.

Returns:

  • (Boolean)

    True only if the hash is empty else false.

Complexity:

  • O©.



362
# File 'yard.rb', line 362

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:



260
# File 'yard.rb', line 260

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_key).to equal :a

Returns:

  • The first key in the hash

Complexity:



276
# File 'yard.rb', line 276

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

Returns:

  • (Array)

    The first key-value pair in the hash.

Complexity:



268
# File 'yard.rb', line 268

def first_value ; end

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

Return true if the key is contained in the hash.

Returns:

  • (Boolean)

Complexity:



251
# File 'yard.rb', line 251

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]

Returns:

  • (Array)

    The last key-value pair in the hash.

Complexity:



284
# File 'yard.rb', line 284

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:



298
# File 'yard.rb', line 298

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:



291
# File 'yard.rb', line 291

def last_value ; end

#lengthNumber

Gets the number of elements in the hash.

Returns:

  • (Number)

    Number of items in hash.

Complexity:

  • O©.



357
# File 'yard.rb', line 357

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]

Returns:

  • (Array)

    The last key-value pair according to the <=> method defined on each member or nil if the hash is empty.

See Also:

Complexity:

  • O©.



334
# File 'yard.rb', line 334

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

Returns:

  • The last key according to the <=> method defined on each member or nil if the hash is empty.

See Also:

Complexity:

  • O©.



352
# File 'yard.rb', line 352

def pop_key ; end

#pop_valueObject

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

Examples:

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

Returns:

  • The last key according to the <=> method defined on each member or nil if the hash is empty.

See Also:

Complexity:

  • O©.



343
# File 'yard.rb', line 343

def pop_value ; end

#replace(key, value) ⇒ Object

Set the value associated with a key, differs to #[]= in return value.

Examples:

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

Returns:

  • The value that was previously associated with the key or nil if the key was not present in the hash.

See Also:

Complexity:

  • O©.



213
# File 'yard.rb', line 213

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]

Returns:

  • (Array)

    The first key-value pair according to the <=> method defined on each member or nil if the hash is empty.

See Also:

Complexity:

  • O©.



307
# File 'yard.rb', line 307

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

Returns:

  • The first key pair according to the <=> method defined on each member or nil if the hash is empty.

See Also:

Complexity:

  • O©.



325
# File 'yard.rb', line 325

def shift_key ; end

#shift_valueObject

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

Examples:

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

Returns:

  • The first key pair according to the <=> method defined on each member or nil if the hash is empty.

See Also:

Complexity:

  • O©.



316
# File 'yard.rb', line 316

def shift_value ; end