Class: SortedContainers::SortedHash
- Inherits:
-
Object
- Object
- SortedContainers::SortedHash
- Includes:
- Enumerable
- Defined in:
- lib/sorted_containers/sorted_hash.rb
Overview
The SortedHash class represents a sorted hash.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Retrieves the value associated with the specified key.
-
#[]=(key, value) ⇒ Object
Associates the specified value with the specified key.
-
#bisect_left(key) ⇒ Integer
Returns the number of key-value pairs in the SortedHash.
-
#bisect_right(key) ⇒ Integer
Returns the number of key-value pairs in the SortedHash.
-
#delete(key) ⇒ void
Deletes the key-value pair associated with the specified key.
-
#delete_at(index) ⇒ Array
Deletes the key-value pair at the specified index and returns it as a two-element array.
-
#each {|key, value| ... } ⇒ void
Iterates over each key-value pair in the SortedHash.
-
#first ⇒ Array
Retrieves the first key-value pair from the SortedHash as a two-element array.
-
#initialize(hash = {}, load_factor: SortedArray::DEFAULT_LOAD_FACTOR) ⇒ SortedHash
constructor
Initializes a new instance of the SortedHash class.
-
#keys ⇒ Array
Returns an array of all the keys in the SortedHash.
-
#last ⇒ Array
Removes the first key-value pair from the SortedHash and returns it as a two-element array.
-
#pop ⇒ Array
Removes the last key-value pair from the SortedHash and returns it as a two-element array.
-
#shift ⇒ Array
Removes the first key-value pair from the SortedHash and returns it as a two-element array.
-
#to_s ⇒ String
Returns a string representation of the SortedHash.
-
#values ⇒ Array
Returns an array of all the values in the SortedHash.
Constructor Details
#initialize(hash = {}, load_factor: SortedArray::DEFAULT_LOAD_FACTOR) ⇒ SortedHash
Initializes a new instance of the SortedHash class.
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.
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.
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.
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.
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.
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.
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.
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 |
#first ⇒ Array
Retrieves the first key-value pair from the SortedHash as a two-element array.
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 |
#keys ⇒ Array
Returns an array of all the keys in the SortedHash.
128 129 130 |
# File 'lib/sorted_containers/sorted_hash.rb', line 128 def keys @sorted_array.to_a end |
#last ⇒ Array
Removes the first key-value pair from the SortedHash and returns it as a two-element array.
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 |
#pop ⇒ Array
Removes the last key-value pair from the SortedHash and returns it as a two-element array.
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 |
#shift ⇒ Array
Removes the first key-value pair from the SortedHash and returns it as a two-element array.
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_s ⇒ String
Returns 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 |
#values ⇒ Array
Returns an array of all the values in the SortedHash.
135 136 137 |
# File 'lib/sorted_containers/sorted_hash.rb', line 135 def values @sorted_array.to_a.map { |key| @hash[key] } end |