summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-hash-ext/mrblib/hash.rb
blob: 73d1fbe6df303dab761e714cd749b6a497583137 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
class Hash

  #  ISO does not define Hash#each_pair, so each_pair is defined in gem.
  alias each_pair each

  ##
  # call-seq:
  #     Hash[ key, value, ... ] -> new_hash
  #     Hash[ [ [key, value], ... ] ] -> new_hash
  #     Hash[ object ] -> new_hash
  #
  # Creates a new hash populated with the given objects.
  #
  # Similar to the literal `{ _key_ => _value_, ... }`. In the first
  # form, keys and values occur in pairs, so there must be an even number of
  # arguments.
  #
  # The second and third form take a single argument which is either an array
  # of key-value pairs or an object convertible to a hash.
  #
  #     Hash["a", 100, "b", 200] #=> {"a"=>100, "b"=>200}
  #     Hash[ [ ["a", 100], ["b", 200] ] ] #=> {"a"=>100, "b"=>200}
  #     Hash["a" => 100, "b" => 200] #=> {"a"=>100, "b"=>200}
  #

  def self.[](*object)
    length = object.length
    if length == 1
      o = object[0]
      if o.respond_to?(:to_hash)
        h = self.new
        object[0].to_hash.each { |k, v| h[k] = v }
        return h
      elsif o.respond_to?(:to_a)
        h = self.new
        o.to_a.each do |i|
          raise ArgumentError, "wrong element type #{i.class} (expected array)" unless i.respond_to?(:to_a)
          k, v = nil
          case i.size
          when 2
            k = i[0]
            v = i[1]
          when 1
            k = i[0]
          else
            raise ArgumentError, "invalid number of elements (#{i.size} for 1..2)"
          end
          h[k] = v
        end
        return h
      end
    end
    unless length % 2 == 0
      raise ArgumentError, 'odd number of arguments for Hash'
    end
    h = self.new
    0.step(length - 2, 2) do |i|
      h[object[i]] = object[i + 1]
    end
    h
  end

  ##
  # call-seq:
  #     Hash.try_convert(obj) -> hash or nil
  #
  # Try to convert <i>obj</i> into a hash, using to_hash method.
  # Returns converted hash or nil if <i>obj</i> cannot be converted
  # for any reason.
  #
  #     Hash.try_convert({1=>2})   # => {1=>2}
  #     Hash.try_convert("1=>2")   # => nil
  #
  def self.try_convert(obj)
    if obj.respond_to?(:to_hash)
      obj.to_hash
    else
      nil
    end
  end

  ##
  # call-seq:
  #     hsh.merge!(other_hash)                                 -> hsh
  #     hsh.merge!(other_hash){|key, oldval, newval| block}    -> hsh
  #
  #  Adds the contents of _other_hash_ to _hsh_.  If no block is specified,
  #  entries with duplicate keys are overwritten with the values from
  #  _other_hash_, otherwise the value of each duplicate key is determined by
  #  calling the block with the key, its value in _hsh_ and its value in
  #  _other_hash_.
  #
  #     h1 = { "a" => 100, "b" => 200 }
  #     h2 = { "b" => 254, "c" => 300 }
  #     h1.merge!(h2)   #=> {"a"=>100, "b"=>254, "c"=>300}
  #
  #     h1 = { "a" => 100, "b" => 200 }
  #     h2 = { "b" => 254, "c" => 300 }
  #     h1.merge!(h2) { |key, v1, v2| v1 }
  #                     #=> {"a"=>100, "b"=>200, "c"=>300}
  #

  def merge!(other, &block)
    raise TypeError, "can't convert argument into Hash" unless other.respond_to?(:to_hash)
    if block
      other.each_key{|k|
        self[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k]
      }
    else
      other.each_key{|k| self[k] = other[k]}
    end
    self
  end

  alias update merge!

  ##
  # call-seq:
  #    hsh.compact     -> new_hsh
  #
  # Returns a new hash with the nil values/key pairs removed
  #
  #    h = { a: 1, b: false, c: nil }
  #    h.compact     #=> { a: 1, b: false }
  #    h             #=> { a: 1, b: false, c: nil }
  #
  def compact
    result = self.dup
    result.compact!
    result
  end

  ##
  #  call-seq:
  #     hsh.fetch(key [, default] )       -> obj
  #     hsh.fetch(key) {| key | block }   -> obj
  #
  #  Returns a value from the hash for the given key. If the key can't be
  #  found, there are several options: With no other arguments, it will
  #  raise an <code>KeyError</code> exception; if <i>default</i> is
  #  given, then that will be returned; if the optional code block is
  #  specified, then that will be run and its result returned.
  #
  #     h = { "a" => 100, "b" => 200 }
  #     h.fetch("a")                            #=> 100
  #     h.fetch("z", "go fish")                 #=> "go fish"
  #     h.fetch("z") { |el| "go fish, #{el}"}   #=> "go fish, z"
  #
  #  The following example shows that an exception is raised if the key
  #  is not found and a default value is not supplied.
  #
  #     h = { "a" => 100, "b" => 200 }
  #     h.fetch("z")
  #
  #  <em>produces:</em>
  #
  #     prog.rb:2:in 'fetch': key not found (KeyError)
  #      from prog.rb:2
  #

  def fetch(key, none=NONE, &block)
    unless self.key?(key)
      if block
        block.call(key)
      elsif none != NONE
        none
      else
        raise KeyError, "Key not found: #{key}"
      end
    else
      self[key]
    end
  end

  ##
  #  call-seq:
  #     hsh.delete_if {| key, value | block }  -> hsh
  #     hsh.delete_if                          -> an_enumerator
  #
  #  Deletes every key-value pair from <i>hsh</i> for which <i>block</i>
  #  evaluates to <code>true</code>.
  #
  #  If no block is given, an enumerator is returned instead.
  #
  #     h = { "a" => 100, "b" => 200, "c" => 300 }
  #     h.delete_if {|key, value| key >= "b" }   #=> {"a"=>100}
  #

  def delete_if(&block)
    return to_enum :delete_if unless block

    self.each do |k, v|
      self.delete(k) if block.call(k, v)
    end
    self
  end

  ##
  #  call-seq:
  #     hash.flatten -> an_array
  #     hash.flatten(level) -> an_array
  #
  #  Returns a new array that is a one-dimensional flattening of this
  #  hash. That is, for every key or value that is an array, extract
  #  its elements into the new array.  Unlike Array#flatten, this
  #  method does not flatten recursively by default.  The optional
  #  <i>level</i> argument determines the level of recursion to flatten.
  #
  #     a =  {1=> "one", 2 => [2,"two"], 3 => "three"}
  #     a.flatten    # => [1, "one", 2, [2, "two"], 3, "three"]
  #     a.flatten(2) # => [1, "one", 2, 2, "two", 3, "three"]
  #

  def flatten(level=1)
    self.to_a.flatten(level)
  end

  ##
  #  call-seq:
  #     hsh.invert -> new_hash
  #
  #  Returns a new hash created by using <i>hsh</i>'s values as keys, and
  #  the keys as values.
  #
  #     h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 }
  #     h.invert   #=> {0=>"a", 100=>"m", 200=>"d", 300=>"y"}
  #

  def invert
    h = self.class.new
    self.each {|k, v| h[v] = k }
    h
  end

  ##
  #  call-seq:
  #     hsh.keep_if {| key, value | block }  -> hsh
  #     hsh.keep_if                          -> an_enumerator
  #
  #  Deletes every key-value pair from <i>hsh</i> for which <i>block</i>
  #  evaluates to false.
  #
  #  If no block is given, an enumerator is returned instead.
  #

  def keep_if(&block)
    return to_enum :keep_if unless block

    keys = []
    self.each do |k, v|
      unless block.call([k, v])
        self.delete(k)
      end
    end
    self
  end

  ##
  #  call-seq:
  #     hsh.key(value)    -> key
  #
  #  Returns the key of an occurrence of a given value. If the value is
  #  not found, returns <code>nil</code>.
  #
  #     h = { "a" => 100, "b" => 200, "c" => 300, "d" => 300 }
  #     h.key(200)   #=> "b"
  #     h.key(300)   #=> "c"
  #     h.key(999)   #=> nil
  #

  def key(val)
    self.each do |k, v|
      return k if v == val
    end
    nil
  end

  ##
  #  call-seq:
  #     hsh.to_h     -> hsh or new_hash
  #
  #  Returns +self+. If called on a subclass of Hash, converts
  #  the receiver to a Hash object.
  #
  def to_h
    self
  end

  ##
  #  call-seq:
  #    hash < other -> true or false
  #
  #  Returns <code>true</code> if <i>hash</i> is subset of
  #  <i>other</i>.
  #
  #     h1 = {a:1, b:2}
  #     h2 = {a:1, b:2, c:3}
  #     h1 < h2    #=> true
  #     h2 < h1    #=> false
  #     h1 < h1    #=> false
  #
  def <(hash)
    begin
      hash = hash.to_hash
    rescue NoMethodError
      raise TypeError, "can't convert #{hash.class} to Hash"
    end
    size < hash.size and all? {|key, val|
      hash.key?(key) and hash[key] == val
    }
  end

  ##
  #  call-seq:
  #    hash <= other -> true or false
  #
  #  Returns <code>true</code> if <i>hash</i> is subset of
  #  <i>other</i> or equals to <i>other</i>.
  #
  #     h1 = {a:1, b:2}
  #     h2 = {a:1, b:2, c:3}
  #     h1 <= h2   #=> true
  #     h2 <= h1   #=> false
  #     h1 <= h1   #=> true
  #
  def <=(hash)
    begin
      hash = hash.to_hash
    rescue NoMethodError
      raise TypeError, "can't convert #{hash.class} to Hash"
    end
    size <= hash.size and all? {|key, val|
      hash.key?(key) and hash[key] == val
    }
  end

  ##
  #  call-seq:
  #    hash > other -> true or false
  #
  #  Returns <code>true</code> if <i>other</i> is subset of
  #  <i>hash</i>.
  #
  #     h1 = {a:1, b:2}
  #     h2 = {a:1, b:2, c:3}
  #     h1 > h2    #=> false
  #     h2 > h1    #=> true
  #     h1 > h1    #=> false
  #
  def >(hash)
    begin
      hash = hash.to_hash
    rescue NoMethodError
      raise TypeError, "can't convert #{hash.class} to Hash"
    end
    size > hash.size and hash.all? {|key, val|
      key?(key) and self[key] == val
    }
  end

  ##
  #  call-seq:
  #    hash >= other -> true or false
  #
  #  Returns <code>true</code> if <i>other</i> is subset of
  #  <i>hash</i> or equals to <i>hash</i>.
  #
  #     h1 = {a:1, b:2}
  #     h2 = {a:1, b:2, c:3}
  #     h1 >= h2   #=> false
  #     h2 >= h1   #=> true
  #     h1 >= h1   #=> true
  #
  def >=(hash)
    begin
      hash = hash.to_hash
    rescue NoMethodError
      raise TypeError, "can't convert #{hash.class} to Hash"
    end
    size >= hash.size and hash.all? {|key, val|
      key?(key) and self[key] == val
    }
  end

  ##
  # call-seq:
  #   hsh.dig(key,...)                 -> object
  #
  # Extracts the nested value specified by the sequence of <i>key</i>
  # objects by calling +dig+ at each step, returning +nil+ if any
  # intermediate step is +nil+.
  #
  def dig(idx,*args)
    n = self[idx]
    if args.size > 0
      n&.dig(*args)
    else
      n
    end
  end

  ##
  # call-seq:
  #    hsh.transform_keys {|key| block } -> new_hash
  #    hsh.transform_keys                -> an_enumerator
  #
  # Returns a new hash, with the keys computed from running the block
  # once for each key in the hash, and the values unchanged.
  #
  # If no block is given, an enumerator is returned instead.
  #
  def transform_keys(&block)
    return to_enum :transform_keys unless block
    hash = {}
    self.keys.each do |k|
      new_key = block.call(k)
      hash[new_key] = self[k]
    end
    hash
  end
  ##
  # call-seq:
  #    hsh.transform_keys! {|key| block } -> hsh
  #    hsh.transform_keys!                -> an_enumerator
  #
  # Invokes the given block once for each key in <i>hsh</i>, replacing it
  # with the new key returned by the block, and then returns <i>hsh</i>.
  #
  # If no block is given, an enumerator is returned instead.
  #
  def transform_keys!(&block)
    return to_enum :transform_keys! unless block
    self.keys.each do |k|
      value = self[k]
      new_key = block.call(k)
      self.__delete(k)
      self[new_key] = value
    end
    self
  end
  ##
  # call-seq:
  #    hsh.transform_values {|value| block } -> new_hash
  #    hsh.transform_values                  -> an_enumerator
  #
  # Returns a new hash with the results of running the block once for
  # every value.
  # This method does not change the keys.
  #
  # If no block is given, an enumerator is returned instead.
  #
  def transform_values(&b)
    return to_enum :transform_values unless block_given?
    hash = {}
    self.keys.each do |k|
      hash[k] = yield(self[k])
    end
    hash
  end
  ##
  # call-seq:
  #    hsh.transform_values! {|key| block } -> hsh
  #    hsh.transform_values!                -> an_enumerator
  #
  # Invokes the given block once for each value in the hash, replacing
  # with the new value returned by the block, and then returns <i>hsh</i>.
  #
  # If no block is given, an enumerator is returned instead.
  #
  def transform_values!(&b)
    return to_enum :transform_values! unless block_given?
    self.keys.each do |k|
      self[k] = yield(self[k])
    end
    self
  end
end