Class: SortedContainers::SortedHash

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(default_value = nil, load_factor: SortedArray::DEFAULT_LOAD_FACTOR) {|Proc| ... } ⇒ SortedHash

Initializes a new instance of the SortedHash class.

Parameters:

  • default_value (Object) (defaults to: nil)

    The default value for the SortedHash.

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

    The load factor for the SortedHash.

Yields:

  • (Proc)

    The block to call to calculate the default value.

Raises:

  • (ArgumentError)


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.

Parameters:

  • args (Array)

    The initial key-value pairs for the SortedHash.

Returns:

  • (SortedHash)

    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

See Also:

  • Hash#<


179
# File 'lib/sorted_containers/sorted_hash.rb', line 179

def <(other) = @internal_hash < other.internal_hash

#<=(other) ⇒ Object

See Also:

  • Hash#<=


182
# File 'lib/sorted_containers/sorted_hash.rb', line 182

def <=(other) = @internal_hash <= other.internal_hash

#==(other) ⇒ Object

See Also:

  • Hash#==


185
# File 'lib/sorted_containers/sorted_hash.rb', line 185

def ==(other) = @internal_hash == other.internal_hash

#>(other) ⇒ Object

See Also:

  • Hash#>


188
# File 'lib/sorted_containers/sorted_hash.rb', line 188

def >(other) = @internal_hash > other.internal_hash

#>=(other) ⇒ Object

See Also:

  • Hash#>=


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.

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.



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.

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.



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

See Also:

  • Hash#any?


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

#assocObject

See Also:

  • Hash#assoc


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_leftObject



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_rightObject



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

def_delegators :@sorted_array,
:first,
:last,
:bisect_left,
:bisect_right

#clearObject

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

#compactObject

See Also:

  • Hash#compact


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

See Also:

  • Hash#compact!


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_keysObject

See Also:

  • Hash#deconstruct_keys


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

#defaultObject

See Also:

  • Hash#default


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

See Also:

  • Hash#default=


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_procObject

See Also:

  • Hash#default_proc


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.

Parameters:

  • key (Object)

    The key to delete.



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.

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.



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

See Also:

  • Hash#delete_if


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

#digObject

See Also:

  • Hash#dig


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.

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.



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

See Also:

  • Hash#each_key


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

See Also:

  • Hash#each_value


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

See Also:

  • Hash#empty?


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

See Also:

  • Hash#eql?


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

See Also:

  • Hash#except


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

#fetchObject

See Also:

  • Hash#fetch


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_valuesObject

See Also:

  • Hash#fetch_values


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

See Also:

  • Hash#filter


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!

See Also:

  • Hash#filter!


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

#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.



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

def_delegators :@sorted_array,
:first,
:last,
:bisect_left,
:bisect_right

#flattenObject

See Also:

  • Hash#flatten


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

See Also:

  • Hash#has_key?


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

See Also:

  • Hash#has_value?


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

#hashObject

See Also:

  • Hash#hash


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

See Also:

  • Hash#include?


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.

Parameters:

  • value (Object)

    The value to find the index of.

Returns:

  • (Integer, nil)

    The index of the value, or nil if the value is not found.



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

#invertObject

Values must be comparable for this method to work.

See Also:

  • Hash#invert


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

See Also:

  • Hash#keep_if


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

See Also:

  • Hash#key


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

See Also:

  • Hash#key?


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

#keysObject

See Also:

  • Hash#keys


81
# File 'lib/sorted_containers/sorted_hash.rb', line 81

def_delegator :@sorted_array, :to_a, :keys

#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.



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

def_delegators :@sorted_array,
:first,
:last,
:bisect_left,
:bisect_right

#lengthObject

See Also:

  • Hash#length


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

See Also:

  • Hash#member?


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

See Also:

  • Hash#merge


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

See Also:

  • Hash#merge!


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

#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.



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

#rassocObject

See Also:

  • Hash#rassoc


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

#rehashObject

Also resorts the SortedHash.

See Also:

  • Hash#rehash


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

See Also:

  • Hash#reject


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

See Also:

  • Hash#reject!


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

See Also:

  • Hash#replace


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

#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.



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

#sizeObject

See Also:

  • Hash#size


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

See Also:

  • Hash#slice


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

See Also:

  • Hash#store


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_aObject

Returns in sorted order by key.

See Also:

  • Hash#to_a


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_hObject

See Also:

  • Hash#to_h


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_hashObject

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_procObject

See Also:

  • Hash#to_proc


447
448
449
# File 'lib/sorted_containers/sorted_hash.rb', line 447

def to_proc
  ->(key) { @internal_hash[key] }
end

#to_sString Also known as: inspect

Returns a string representation of the SortedHash.

Returns:

  • (String)

    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

See Also:

  • Hash#transform_keys


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

See Also:

  • Hash#transform_keys!


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

See Also:

  • Hash#transform_values


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

See Also:

  • Hash#transform_values!


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

See Also:

  • Hash#value?


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

#valuesArray

Returns an array of all the values in the SortedHash.

Returns:

  • (Array)

    An array of all the values.



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_atObject

See Also:

  • Hash#values_at


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