GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Rubinius, the Ruby VM
Homepage: http://rubini.us
Clone URL: git://github.com/evanphx/rubinius.git
commit  a6b85351d18f26b0ba7d14201bfe3fff008e3d84
tree    b5b36164f2a060bed1392a6f88b443db43720a02
parent  c7147df3c30b2f049f5f9dfc75c0f013e9ff113b
rubinius / kernel / core / hash.rb
100644 452 lines (363 sloc) 8.762 kb
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
# depends on: enumerable.rb misc.rb class.rb
 
class Hash
 
  #--
  # The result of #hash is not allowed to be larger than this.
  #++
 
  HASH_MAX = 0x1fffffff
  
  include Enumerable
 
  def self.[](*args)
    if args.first.kind_of? Hash and args.length == 1
      return new.replace(args.first)
    end
 
    raise ArgumentError, "Expected an even number, got #{args.length}" if args.length % 2 == 1
 
    hsh = new()
    while args.length >= 2
      k = args.shift
      v = args.shift
      hsh[k] = v
    end
    hsh
  end
 
  def initialize(default = Undefined, &block)
    if !default.equal?(Undefined) and block
      raise ArgumentError, "Specify a default or a block, not both"
    end
 
    if block
      @default = block
      @default_proc = true
    elsif !default.equal?(Undefined)
      @default = default
      @default_proc = false
    end
  end
  private :initialize
 
  def initialize_copy(other)
    replace(other)
  end
  private :initialize_copy
 
  def ==(other)
    return true if self.equal? other
    return false unless other.kind_of? Hash or other.respond_to? :to_hash
    other = Type.coerce_to(other, Hash, :to_hash)
    return false unless other.size == size
    # Pickaxe claims that defaults are compared, but MRI 1.8.[46] doesn't actually do that
    # return false unless other.default == default
    each_pair do |k, v|
      return false unless other[k] == self[k]
    end
    true
  end
 
  ##
  # looks for a key in a bin found by hash_entry
 
  def search_bin(entry,hash,key)
    while entry
      cur_hash, cur_key, cur_val, nxt = *entry
 
      if cur_hash == hash and key.eql?(cur_key)
        return entry
      end
 
      entry = nxt
    end
    return Undefined
  end
  private :search_bin
  
  def fetch(key, default = Undefined)
    entry, hash, = hash_entry key
    entry = search_bin(entry,hash,key)
    return entry[2] if(entry != Undefined)
 
    return yield(key) if block_given?
    return default if !default.equal?(Undefined)
    raise IndexError, 'Key not found'
  end
  
  def get_key_cv(key)
    entry, hash, = hash_entry key
    entry = search_bin(entry,hash,key)
    return entry[2] if(entry != Undefined)
    
    return default(key)
  end
  
  def set_key_cv(key, val)
    key = key.dup if key.kind_of?(String)
 
    redistribute
    
    entry, hash, bin = hash_entry key
    lst = nil
 
    while entry
      cur_hash, cur_key, cur_val, nxt = *entry
 
      # Check if this entry is for the key in question
      if cur_hash == hash and key.eql?(cur_key)
        entry.put 2, val
        return val
      end
 
      lst = entry
      entry = nxt
    end
 
    entry = Tuple.new(4)
    entry.put 0, hash
    entry.put 1, key
    entry.put 2, val
    entry.put 3, nil
 
    if lst
      lst.put 3, entry
    else
      @values.put bin, entry
    end
 
    @entries += 1
    return val
  end
  alias_method :store, :set_key_cv
 
  def self.after_loaded()
    alias_method :[], :get_key_cv
    alias_method :[]=, :set_key_cv
  end
 
  def clear()
    delete_if { true }
  end
 
  def clone
    new_hash = dup
    new_hash.freeze if self.frozen?
    new_hash
  end
 
  def default(key = Undefined)
    # current MRI documentation comment is wrong. Actual behavior is:
    # Hash.new { 1 }.default #=> nil
    if @default_proc
      return key.equal?(Undefined) ? nil : @default.call(self, key)
    else
      return @default
    end
  end
 
  def default=(val)
    @default_proc = false
    @default = val
  end
 
  def default_proc()
    return @default if @default_proc
    nil
  end
 
  def delete(key)
    entry, hash, bin = hash_entry key
    lst = nil
 
    while entry
      cur_hash, cur_key, val, nxt = *entry
 
      # Check if this entry is for the key in question
      if cur_hash == hash and key.eql?(cur_key)
 
        # Ok, relink the other entries, leaving this one out.
        if lst
          lst.put 3, nxt
        else
          @values.put bin, nxt
        end
 
        @entries = @entries - 1
 
        return val
      end
 
      lst = entry
      entry = nxt
    end
 
    return yield(key) if block_given?
    nil
  end
 
  def delete_if()
    raise LocalJumpError, "no block given" unless block_given? or empty?
 
    # Do this in 2 steps, so we're not altering the structure while we walk it.
    # TODO: I'd like to write it like this:
    # select(&block).each { |k, v| delete k }
    to_del = []
    each_pair { |k, v| to_del << k if yield(k, v) }
    to_del.each { |k| delete k }
    self
  end
 
  def dup
    new_hash = self.class.new
    new_hash.send :initialize_copy, self
    new_hash.taint if self.tainted?
    new_hash
  end
 
  def each_key(&block)
    keys.each(&block)
    self
  end
 
  def each
    raise LocalJumpError, "no block given" unless block_given? or empty?
 
    @values.each do |tup|
      while tup
        yield([tup.at(1), tup.at(2)])
        tup = tup.at(3)
      end
    end
    self
  end
 
  def each_pair
    raise LocalJumpError, "no block given" unless block_given? or empty?
 
    @values.each do |tup|
      while tup
        yield(tup.at(1), tup.at(2))
        tup = tup.at(3)
      end
    end
    self
  end
 
  def each_value(&block)
    values.each(&block)
    self
  end
 
  def empty?()
    @entries == 0
  end
 
  def index(val)
    each_pair { |k, v| return k if v == val }
    nil
  end
 
  def inspect()
    # recursively_inspect
    return '{...}' if RecursionGuard.inspecting?(self)
 
    out = []
    RecursionGuard.inspect(self) do
      each_pair do |key, val|
        str = key.inspect
        str << '=>'
        str << val.inspect
        out << str
      end
    end
 
    "{#{out.join ', '}}"
  end
 
  def invert()
    out = {}
    each_pair { |k, v| out[v] = k }
    out
  end
 
  def key?(key)
    entry, hash, = hash_entry key
 
    while entry do # REFACTOR this loop is duplicated many times
      cur_hash, cur_key, cur_val, nxt = *entry
 
      return true if cur_hash == hash and key.eql?(cur_key)
 
      entry = nxt
    end
 
    false
  end
 
  alias_method :has_key?, :key?
  alias_method :include?, :key?
  alias_method :member?, :key?
 
  def keys()
    out = []
    @values.each do |tup|
      while tup
        out << tup.at(1)
        tup = tup.at(3)
      end
    end
    out
  end
 
  def merge(other, &block)
    dup.merge!(other, &block)
  end
 
  def merge!(other)
    other = Type.coerce_to(other, Hash, :to_hash)
    other.each_pair do |k, v|
      if block_given? and self.key? k
        self[k] = yield(k, self[k], v)
      else
        self[k] = v
      end
    end
    self
  end
  alias_method :update, :merge!
 
  # TODO: Improve the performance of this. NOTE, however
  # that #rehash is fundamentally impossible to do via a
  # primitive because classes (e.g. Array) can and do
  # redefine the #hash method. That method is not available
  # to primitive C code because we do not call back to Ruby
  def rehash
    out = {}
    each_pair { |k, v| out[k] = v }
    replace out
  end
 
  def reject(&block)
    dup.delete_if(&block)
  end
 
  def reject!(&block)
    old_size = size
    delete_if(&block)
    return nil if old_size == size
    self
  end
 
  def replace(other)
    other = Type.coerce_to(other, Hash, :to_hash)
    return self if self.equal? other
    clear
    other.each_pair { |k, v| self[k] = v }
    if other.default_proc
      @default = other.default_proc
      @default_proc = true
    else
      @default = other.default
      @default_proc = false
    end
    self
  end
 
  def select()
    raise LocalJumpError, "no block given" unless block_given? or empty?
 
    out = []
    each_pair { |k, v| out << [k, v] if yield(k, v) }
    out
  end
 
  def shift()
    return default(nil) if empty?
 
    # TODO: keep around for efficiency? It's not much faster though.
    # i = 0
    # tup = nil
    # while !tup
    # tup = @values.at(i)
    # i += 1
    # end
    # key = tup.at(1)
    # out = [key, self[key]]
    # delete(key)
    # out
 
    key = keys.first
    out = [key, self[key]]
    delete key
    out
  end
 
  def size()
    @entries
  end
  alias_method :length, :size
 
  def sort(&block)
    to_a.sort(&block)
  end
 
  def to_a()
    select { true }
  end
 
  def to_hash()
    self
  end
 
  def to_s()
    to_a.join
  end
 
  def value?(val)
    values.include?(val)
  end
  alias_method :has_value?, :value?
 
  def values()
    out = []
    @values.each do |tup|
      while tup
        out << tup.at(2)
        tup = tup.at(3)
      end
    end
    out
  end
 
  def values_at(*args)
    args.collect { |key| self[key] }
  end
  alias_method :indexes, :values_at
  alias_method :indices, :values_at
 
  def hash_entry(obj)
    hash = obj.hash
    hash = hash % HASH_MAX unless hash.kind_of? Fixnum
 
    bin = hash & (@bins - 1)
 
    entry = @values.at bin
 
    return entry, hash, bin
  end
 
end