Class: SortedContainers::SortedHash

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sorted_containers/sorted_hash.rb

Overview

The SortedHash class represents a sorted hash.

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}, load_factor: SortedArray::DEFAULT_LOAD_FACTOR) ⇒ SortedHash

Initializes a new instance of the SortedHash class.

Parameters:

  • load_factor (Integer) (defaults to: SortedArray::DEFAULT_LOAD_FACTOR)

    The load factor for the SortedHash.



14
15
16
17
# File 'lib/sorted_containers/sorted_hash.rb', line 14

def initialize(hash = {}, load_factor: SortedArray::DEFAULT_LOAD_FACTOR)
  @hash = hash
  @sorted_array = SortedArray.new(hash.keys, load_factor: load_factor)
end

Instance Method Details

#[](key) ⇒ Object

Retrieves the value associated with the specified key.

Parameters:

  • key (Object)

    The key to retrieve the value for.

Returns:

  • (Object)

    The value associated with the key, or nil if the key is not found.



23
24
25
# File 'lib/sorted_containers/sorted_hash.rb', line 23

def [](key)
  @hash[key]
end

#[]=(key, value) ⇒ Object

Associates the specified value with the specified key. If the key already exists, the previous value will be replaced.

Parameters:

  • key (Object)

    The key to associate the value with.

  • value (Object)

    The value to be associated with the key.

Returns:

  • (Object)

    The value that was associated with the key.



33
34
35
36
37
# File 'lib/sorted_containers/sorted_hash.rb', line 33

def []=(key, value)
  @sorted_array.delete(key) if @hash.key?(key)
  @sorted_array.add(key)
  @hash[key] = value
end

#bisect_left(key) ⇒ Integer

Returns the number of key-value pairs in the SortedHash.

Returns:

  • (Integer)

    The number of key-value pairs.



114
115
116
# File 'lib/sorted_containers/sorted_hash.rb', line 114

def bisect_left(key)
  @sorted_array.bisect_left(key)
end

#bisect_right(key) ⇒ Integer

Returns the number of key-value pairs in the SortedHash.

Returns:

  • (Integer)

    The number of key-value pairs.



121
122
123
# File 'lib/sorted_containers/sorted_hash.rb', line 121

def bisect_right(key)
  @sorted_array.bisect_right(key)
end

#delete(key) ⇒ void

This method returns an undefined value.

Deletes the key-value pair associated with the specified key.

Parameters:

  • key (Object)

    The key to delete.



50
51
52
53
54
55
# File 'lib/sorted_containers/sorted_hash.rb', line 50

def delete(key)
  return unless @hash.key?(key)

  @hash.delete(key)
  @sorted_array.delete(key)
end

#delete_at(index) ⇒ Array

Deletes the key-value pair at the specified index and returns it as a two-element array.

Parameters:

  • index (Integer)

    The index of the key-value pair to delete.

Returns:

  • (Array)

    A two-element array containing the key and value of the deleted key-value pair.



61
62
63
64
65
66
67
# File 'lib/sorted_containers/sorted_hash.rb', line 61

def delete_at(index)
  return nil if index.abs >= @sorted_array.size

  key = @sorted_array.delete_at(index)
  value = @hash.delete(key)
  [key, value]
end

#each {|key, value| ... } ⇒ void

This method returns an undefined value.

Iterates over each key-value pair in the SortedHash.

Yields:

  • (key, value)

    The block to be executed for each key-value pair.

Yield Parameters:

  • key (Object)

    The key of the current key-value pair.

  • value (Object)

    The value of the current key-value pair.



145
146
147
148
149
150
# File 'lib/sorted_containers/sorted_hash.rb', line 145

def each(&block)
  @sorted_array.each do |key|
    value = @hash[key]
    block.call(key, value)
  end
end

#firstArray

Retrieves the first key-value pair from the SortedHash as a two-element array.

Returns:

  • (Array)

    A two-element array containing the key and value of the first key-value pair.



72
73
74
75
76
77
# File 'lib/sorted_containers/sorted_hash.rb', line 72

def first
  return nil if @sorted_array.empty?

  key = @sorted_array.first
  [key, @hash[key]]
end

#keysArray

Returns an array of all the keys in the SortedHash.

Returns:

  • (Array)

    An array of all the keys.



128
129
130
# File 'lib/sorted_containers/sorted_hash.rb', line 128

def keys
  @sorted_array.to_a
end

#lastArray

Removes the first key-value pair from the SortedHash and returns it as a two-element array.

Returns:

  • (Array)

    A two-element array containing the key and value of the first key-value pair.



82
83
84
85
86
87
# File 'lib/sorted_containers/sorted_hash.rb', line 82

def last
  return nil if @sorted_array.empty?

  key = @sorted_array.last
  [key, @hash[key]]
end

#popArray

Removes the last key-value pair from the SortedHash and returns it as a two-element array.

Returns:

  • (Array)

    A two-element array containing the key and value of the last key-value pair.



92
93
94
95
96
97
98
# File 'lib/sorted_containers/sorted_hash.rb', line 92

def pop
  return nil if @sorted_array.empty?

  key = @sorted_array.pop
  value = @hash.delete(key)
  [key, value]
end

#shiftArray

Removes the first key-value pair from the SortedHash and returns it as a two-element array.

Returns:

  • (Array)

    A two-element array containing the key and value of the first key-value pair.



103
104
105
106
107
108
109
# File 'lib/sorted_containers/sorted_hash.rb', line 103

def shift
  return nil if @sorted_array.empty?

  key = @sorted_array.shift
  value = @hash.delete(key)
  [key, value]
end

#to_sString

Returns a string representation of the SortedHash.

Returns:

  • (String)

    A string representation of the SortedHash.



42
43
44
# File 'lib/sorted_containers/sorted_hash.rb', line 42

def to_s
  "SortedHash({#{keys.map { |key| "#{key}: #{self[key]}" }.join(", ")}})"
end

#valuesArray

Returns an array of all the values in the SortedHash.

Returns:

  • (Array)

    An array of all the values.



135
136
137
# File 'lib/sorted_containers/sorted_hash.rb', line 135

def values
  @sorted_array.to_a.map { |key| @hash[key] }
end