Class: SortedContainers::SortedHash
- Inherits:
-
Object
- Object
- SortedContainers::SortedHash
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/sorted_containers/sorted_hash.rb
Overview
SortedHash is a hash that maintains the order of its keys.
SortedHash has most of the same methods as a regular Hash, but also has the additional following methods:
-
#bisect_left
-
#bisect_right
-
#delete_at
-
#index
-
#last
-
#pop
Addtionally, there are methods that work differently than their Hash counterparts. Generally, methods that use Entry Order will use the sort order of the keys instead. For example:
h = Hash.new
h[:b] = 1
h[:a] = 2
h.first # => [:b, 1]
sh = SortedHash.new
sh[:b] = 1
sh[:a] = 2
sh.first # => [:a, 2]
Class Method Summary collapse
-
.[](*args) ⇒ SortedHash
Creates a new instance of the SortedHash class.
Instance Method Summary collapse
- #<(other) ⇒ Object
- #<=(other) ⇒ Object
- #==(other) ⇒ Object
- #>(other) ⇒ Object
- #>=(other) ⇒ Object
-
#[](key) ⇒ Object
Retrieves the value associated with the specified key.
-
#[]=(key, value) ⇒ Object
Associates the specified value with the specified key.
- #any? ⇒ Object
- #assoc ⇒ Object
- #bisect_left ⇒ Object
- #bisect_right ⇒ Object
-
#clear ⇒ Object
Clears the SortedHash.
- #compact ⇒ Object
- #compact! ⇒ Object
- #deconstruct_keys ⇒ Object
- #default ⇒ Object
- #default= ⇒ Object
- #default_proc ⇒ Object
-
#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.
- #delete_if(&block) ⇒ Object
- #dig ⇒ Object
-
#each {|key, value| ... } ⇒ void
(also: #each_pair)
Iterates over each key-value pair in the SortedHash.
- #each_key(&block) ⇒ Object
- #each_value(&block) ⇒ Object
- #empty? ⇒ Object
- #eql? ⇒ Object
- #except(*keys) ⇒ Object
- #fetch ⇒ Object
- #fetch_values ⇒ Object
- #filter(&block) ⇒ Object (also: #select)
- #filter! ⇒ Object (also: #select!)
-
#first ⇒ Array
Retrieves the first key-value pair from the SortedHash as a two-element array.
- #flatten ⇒ Object
- #has_key? ⇒ Object
- #has_value? ⇒ Object
- #hash ⇒ Object
- #include? ⇒ Object
-
#index(value) ⇒ Integer?
Returns the index of the given value.
-
#initialize(default_value = nil, load_factor: SortedArray::DEFAULT_LOAD_FACTOR) {|Proc| ... } ⇒ SortedHash
constructor
Initializes a new instance of the SortedHash class.
-
#invert ⇒ Object
Values must be comparable for this method to work.
- #keep_if(&block) ⇒ Object
- #key(value) ⇒ Object
- #key? ⇒ Object
- #keys ⇒ Object
-
#last ⇒ Array
Removes the first key-value pair from the SortedHash and returns it as a two-element array.
- #length ⇒ Object
- #member? ⇒ Object
- #merge(*other, &block) ⇒ Object
- #merge!(*other, &block) ⇒ Object (also: #update)
-
#pop ⇒ Array
Removes the last key-value pair from the SortedHash and returns it as a two-element array.
- #rassoc ⇒ Object
-
#rehash ⇒ Object
Also resorts the SortedHash.
- #reject(&block) ⇒ Object
- #reject!(&block) ⇒ Object
- #replace(other) ⇒ Object
-
#shift ⇒ Array
Removes the first key-value pair from the SortedHash and returns it as a two-element array.
- #size ⇒ Object
- #slice(*keys) ⇒ Object
- #store(key, value) ⇒ Object
-
#to_a ⇒ Object
Returns in sorted order by key.
- #to_h ⇒ Object
-
#to_hash ⇒ Object
Converts SortedHash to a hash.
- #to_proc ⇒ Object
-
#to_s ⇒ String
(also: #inspect)
Returns a string representation of the SortedHash.
- #transform_keys(&block) ⇒ Object
- #transform_keys!(&block) ⇒ Object
- #transform_values(&block) ⇒ Object
- #transform_values!(&block) ⇒ Object
- #value? ⇒ Object
-
#values ⇒ Array
Returns an array of all the values in the SortedHash.
- #values_at ⇒ Object
Constructor Details
#initialize(default_value = nil, load_factor: SortedArray::DEFAULT_LOAD_FACTOR) {|Proc| ... } ⇒ SortedHash
Initializes a new instance of the SortedHash class.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/sorted_containers/sorted_hash.rb', line 45 def initialize(default_value = nil, load_factor: SortedArray::DEFAULT_LOAD_FACTOR) raise ArgumentError, "cannot specify both default value and block" if !default_value.nil? && block_given? @internal_hash = if block_given? Hash.new { |hash, key| hash[key] = yield(key) } else Hash.new(default_value) end @sorted_array = SortedArray.new(@internal_hash.keys, load_factor: load_factor) end |
Class Method Details
.[](*args) ⇒ SortedHash
Creates a new instance of the SortedHash class.
60 61 62 63 |
# File 'lib/sorted_containers/sorted_hash.rb', line 60 def self.[](*args) hash = new(nil) hash.merge!(Hash[*args]) end |
Instance Method Details
#<(other) ⇒ Object
179 |
# File 'lib/sorted_containers/sorted_hash.rb', line 179 def <(other) = @internal_hash < other.internal_hash |
#<=(other) ⇒ Object
182 |
# File 'lib/sorted_containers/sorted_hash.rb', line 182 def <=(other) = @internal_hash <= other.internal_hash |
#==(other) ⇒ Object
185 |
# File 'lib/sorted_containers/sorted_hash.rb', line 185 def ==(other) = @internal_hash == other.internal_hash |
#>(other) ⇒ Object
188 |
# File 'lib/sorted_containers/sorted_hash.rb', line 188 def >(other) = @internal_hash > other.internal_hash |
#>=(other) ⇒ Object
191 |
# File 'lib/sorted_containers/sorted_hash.rb', line 191 def >=(other) = @internal_hash >= other.internal_hash |
#[](key) ⇒ Object
Retrieves the value associated with the specified key.
162 163 164 |
# File 'lib/sorted_containers/sorted_hash.rb', line 162 def [](key) @internal_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.
172 173 174 175 176 |
# File 'lib/sorted_containers/sorted_hash.rb', line 172 def []=(key, value) @sorted_array.delete(key) if @internal_hash.key?(key) @sorted_array.add(key) @internal_hash[key] = value end |
#any? ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#assoc ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#bisect_left ⇒ Object
73 74 75 76 77 |
# File 'lib/sorted_containers/sorted_hash.rb', line 73 def_delegators :@sorted_array, :first, :last, :bisect_left, :bisect_right |
#bisect_right ⇒ Object
73 74 75 76 77 |
# File 'lib/sorted_containers/sorted_hash.rb', line 73 def_delegators :@sorted_array, :first, :last, :bisect_left, :bisect_right |
#clear ⇒ Object
Clears the SortedHash. After this operation, the SortedHash will be empty.
194 195 196 197 198 |
# File 'lib/sorted_containers/sorted_hash.rb', line 194 def clear @internal_hash.clear @sorted_array.clear self end |
#compact ⇒ Object
201 202 203 204 205 |
# File 'lib/sorted_containers/sorted_hash.rb', line 201 def compact new_hash = dup new_hash.compact! new_hash end |
#compact! ⇒ Object
208 209 210 211 212 213 214 |
# File 'lib/sorted_containers/sorted_hash.rb', line 208 def compact! return nil if @internal_hash.compact!.nil? @sorted_array.clear @sorted_array.update(@internal_hash.keys) self end |
#deconstruct_keys ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#default ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#default= ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#default_proc ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#delete(key) ⇒ void
This method returns an undefined value.
Deletes the key-value pair associated with the specified key.
220 221 222 223 224 225 |
# File 'lib/sorted_containers/sorted_hash.rb', line 220 def delete(key) return unless @internal_hash.key?(key) @internal_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.
246 247 248 249 250 251 252 |
# File 'lib/sorted_containers/sorted_hash.rb', line 246 def delete_at(index) return nil if index.abs >= @sorted_array.size key = @sorted_array.delete_at(index) value = @internal_hash.delete(key) [key, value] end |
#delete_if(&block) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/sorted_containers/sorted_hash.rb', line 228 def delete_if(&block) return enum_for(:delete_if) unless block_given? @sorted_array.delete_if do |key| if block.call(key, @internal_hash[key]) @internal_hash.delete(key) true else false end end self end |
#dig ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#each {|key, value| ... } ⇒ void Also known as: each_pair
This method returns an undefined value.
Iterates over each key-value pair in the SortedHash.
260 261 262 263 264 265 266 267 268 |
# File 'lib/sorted_containers/sorted_hash.rb', line 260 def each(&block) return enum_for(:each) unless block_given? @sorted_array.each do |key| value = @internal_hash[key] block.call(key, value) end self end |
#each_key(&block) ⇒ Object
272 273 274 275 276 277 |
# File 'lib/sorted_containers/sorted_hash.rb', line 272 def each_key(&block) return enum_for(:each_key) unless block_given? @sorted_array.each(&block) self end |
#each_value(&block) ⇒ Object
280 281 282 283 284 285 |
# File 'lib/sorted_containers/sorted_hash.rb', line 280 def each_value(&block) return enum_for(:each_value) unless block_given? @sorted_array.each { |key| block.call(@internal_hash[key]) } self end |
#empty? ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#eql? ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#except(*keys) ⇒ Object
288 289 290 291 292 |
# File 'lib/sorted_containers/sorted_hash.rb', line 288 def except(*keys) new_hash = dup keys.each { |key| new_hash.delete(key) } new_hash end |
#fetch ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#fetch_values ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#filter(&block) ⇒ Object Also known as: select
295 296 297 298 299 300 301 |
# File 'lib/sorted_containers/sorted_hash.rb', line 295 def filter(&block) return enum_for(:filter) unless block_given? new_hash = dup new_hash.filter!(&block) new_hash end |
#filter! ⇒ Object Also known as: select!
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/sorted_containers/sorted_hash.rb', line 307 def filter! return enum_for(:filter!) unless block_given? changed = false @sorted_array.filter! do |key| if yield(key, @internal_hash[key]) true else @internal_hash.delete(key) changed = true false end end changed ? self : nil end |
#first ⇒ Array
Retrieves the first key-value pair from the SortedHash as a two-element array.
73 74 75 76 77 |
# File 'lib/sorted_containers/sorted_hash.rb', line 73 def_delegators :@sorted_array, :first, :last, :bisect_left, :bisect_right |
#flatten ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#has_key? ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#has_value? ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#hash ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#include? ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#index(value) ⇒ Integer?
Returns the index of the given value. If the value is not found, returns nil.
500 501 502 503 504 |
# File 'lib/sorted_containers/sorted_hash.rb', line 500 def index(value) return nil unless @internal_hash.key?(value) @sorted_array.bisect_left(value) end |
#invert ⇒ Object
Values must be comparable for this method to work.
328 329 330 331 332 333 |
# File 'lib/sorted_containers/sorted_hash.rb', line 328 def invert @internal_hash = @internal_hash.invert @sorted_array.clear @sorted_array.update(@internal_hash.keys) self end |
#keep_if(&block) ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/sorted_containers/sorted_hash.rb', line 336 def keep_if(&block) return enum_for(:keep_if) unless block_given? @sorted_array.keep_if do |key| if block.call(key, @internal_hash[key]) true else @internal_hash.delete(key) false end end self end |
#key(value) ⇒ Object
351 352 353 354 355 356 |
# File 'lib/sorted_containers/sorted_hash.rb', line 351 def key(value) @sorted_array.each do |key| return key if @internal_hash[key] == value end nil end |
#key? ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#keys ⇒ Object
81 |
# File 'lib/sorted_containers/sorted_hash.rb', line 81 def_delegator :@sorted_array, :to_a, :keys |
#last ⇒ Array
Removes the first key-value pair from the SortedHash and returns it as a two-element array.
73 74 75 76 77 |
# File 'lib/sorted_containers/sorted_hash.rb', line 73 def_delegators :@sorted_array, :first, :last, :bisect_left, :bisect_right |
#length ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#member? ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#merge(*other, &block) ⇒ Object
359 360 361 362 363 |
# File 'lib/sorted_containers/sorted_hash.rb', line 359 def merge(*other, &block) new_hash = dup new_hash.merge!(*other, &block) new_hash end |
#merge!(*other, &block) ⇒ Object Also known as: update
366 367 368 369 370 371 372 373 374 375 |
# File 'lib/sorted_containers/sorted_hash.rb', line 366 def merge!(*other, &block) other.each do |other_hash| other_hash.each do |key, value| value = block.call(key, @internal_hash[key], value) if block_given? && @internal_hash.key?(key) @sorted_array.add(key) unless @internal_hash.key?(key) @internal_hash[key] = value end end self end |
#pop ⇒ Array
Removes the last key-value pair from the SortedHash and returns it as a two-element array.
529 530 531 532 533 534 535 |
# File 'lib/sorted_containers/sorted_hash.rb', line 529 def pop return nil if @sorted_array.empty? key = @sorted_array.pop value = @internal_hash.delete(key) [key, value] end |
#rassoc ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#rehash ⇒ Object
Also resorts the SortedHash.
380 381 382 383 384 |
# File 'lib/sorted_containers/sorted_hash.rb', line 380 def rehash @internal_hash.rehash @sorted_array.sort! self end |
#reject(&block) ⇒ Object
387 388 389 390 391 |
# File 'lib/sorted_containers/sorted_hash.rb', line 387 def reject(&block) return enum_for(:reject) unless block_given? filter { |key, value| !block.call(key, value) } end |
#reject!(&block) ⇒ Object
394 395 396 397 398 |
# File 'lib/sorted_containers/sorted_hash.rb', line 394 def reject!(&block) return enum_for(:reject!) unless block_given? filter! { |key, value| !block.call(key, value) } end |
#replace(other) ⇒ Object
401 402 403 404 405 406 |
# File 'lib/sorted_containers/sorted_hash.rb', line 401 def replace(other) @internal_hash.replace(other.internal_hash) @sorted_array.clear @sorted_array.update(@internal_hash.keys) self end |
#shift ⇒ Array
Removes the first key-value pair from the SortedHash and returns it as a two-element array.
540 541 542 543 544 545 546 |
# File 'lib/sorted_containers/sorted_hash.rb', line 540 def shift return nil if @sorted_array.empty? key = @sorted_array.shift value = @internal_hash.delete(key) [key, value] end |
#size ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#slice(*keys) ⇒ Object
409 410 411 412 413 414 415 |
# File 'lib/sorted_containers/sorted_hash.rb', line 409 def slice(*keys) new_hash = self.class.new(@internal_hash.default, load_factor: @sorted_array.load_factor) keys.each do |key| new_hash[key] = @internal_hash[key] if @internal_hash.key?(key) end new_hash end |
#store(key, value) ⇒ Object
418 419 420 421 |
# File 'lib/sorted_containers/sorted_hash.rb', line 418 def store(key, value) @sorted_array.add(key) unless @internal_hash.key?(key) @internal_hash[key] = value end |
#to_a ⇒ Object
Returns in sorted order by key.
425 426 427 |
# File 'lib/sorted_containers/sorted_hash.rb', line 425 def to_a @sorted_array.map { |key| [key, @internal_hash[key]] } end |
#to_h ⇒ Object
430 431 432 433 434 435 436 437 438 439 |
# File 'lib/sorted_containers/sorted_hash.rb', line 430 def to_h if block_given? @sorted_array.each_with_object({}) do |key, hash| key_value = yield(key, @internal_hash[key]) hash[key_value.first] = key_value.last end else @internal_hash.dup end end |
#to_hash ⇒ Object
Converts SortedHash to a hash.
442 443 444 |
# File 'lib/sorted_containers/sorted_hash.rb', line 442 def to_hash @internal_hash.dup end |
#to_proc ⇒ Object
447 448 449 |
# File 'lib/sorted_containers/sorted_hash.rb', line 447 def to_proc ->(key) { @internal_hash[key] } end |
#to_s ⇒ String Also known as: inspect
Returns a string representation of the SortedHash.
454 455 456 |
# File 'lib/sorted_containers/sorted_hash.rb', line 454 def to_s "SortedHash({#{keys.map { |key| "#{key}: #{self[key]}" }.join(", ")}})" end |
#transform_keys(&block) ⇒ Object
460 461 462 463 464 465 466 |
# File 'lib/sorted_containers/sorted_hash.rb', line 460 def transform_keys(&block) return enum_for(:transform_keys) unless block_given? new_hash = dup new_hash.transform_keys!(&block) new_hash end |
#transform_keys!(&block) ⇒ Object
469 470 471 472 473 474 475 476 |
# File 'lib/sorted_containers/sorted_hash.rb', line 469 def transform_keys!(&block) return enum_for(:transform_keys!) unless block_given? @internal_hash.transform_keys!(&block) @sorted_array.clear @sorted_array.update(@internal_hash.keys) self end |
#transform_values(&block) ⇒ Object
479 480 481 482 483 484 485 |
# File 'lib/sorted_containers/sorted_hash.rb', line 479 def transform_values(&block) return enum_for(:transform_values) unless block_given? new_hash = dup new_hash.transform_values!(&block) new_hash end |
#transform_values!(&block) ⇒ Object
488 489 490 491 492 493 |
# File 'lib/sorted_containers/sorted_hash.rb', line 488 def transform_values!(&block) return enum_for(:transform_values!) unless block_given? @internal_hash.transform_values!(&block) self end |
#value? ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#values ⇒ Array
Returns an array of all the values in the SortedHash.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |
#values_at ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sorted_containers/sorted_hash.rb', line 131 def_delegators :@internal_hash, :any?, :assoc, :deconstruct_keys, :default, :default=, :default_proc, :default_proc=, :dig, :empty?, :eql?, :fetch, :fetch_values, :flatten, :has_key?, :has_value?, :hash, :include?, :key?, :length, :member?, :rassoc, :size, :value?, :values, :values_at |