Class: OklahomaMixer::HashMap

Inherits:
Object
  • Object
show all
Defined in:
lib/oklahoma_mixer/hash_map.rb,
lib/oklahoma_mixer/hash_map/c.rb

Overview

:nodoc:

Defined Under Namespace

Modules: C

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pointer = C.new) ⇒ HashMap



5
6
7
# File 'lib/oklahoma_mixer/hash_map.rb', line 5

def initialize(pointer = C.new)
  @pointer = pointer
end

Instance Attribute Details

#pointerObject (readonly)

Returns the value of attribute pointer.



9
10
11
# File 'lib/oklahoma_mixer/hash_map.rb', line 9

def pointer
  @pointer
end

Instance Method Details

#eachObject



17
18
19
20
21
22
23
# File 'lib/oklahoma_mixer/hash_map.rb', line 17

def each
  C.iterinit(@pointer)
  loop do
    return self unless key = C.read_from_func(:iternext, :no_free, @pointer)
    yield [key, C.read_from_func(:get, :no_free, @pointer, key, key.size)]
  end
end

#freeObject



38
39
40
# File 'lib/oklahoma_mixer/hash_map.rb', line 38

def free
  C.del(@pointer)
end

#replace(pairs, &cast) ⇒ Object



33
34
35
36
# File 'lib/oklahoma_mixer/hash_map.rb', line 33

def replace(pairs, &cast)
  C.clear(@pointer)
  update(pairs, &cast)
end

#to_hashObject



25
26
27
28
29
30
31
# File 'lib/oklahoma_mixer/hash_map.rb', line 25

def to_hash
  hash = { }
  each do |key, value|
    hash[yield(key)] = yield(value)
  end
  hash
end

#update(pairs) ⇒ Object



11
12
13
14
15
# File 'lib/oklahoma_mixer/hash_map.rb', line 11

def update(pairs)
  pairs.each do |key, value|
    C.put(@pointer, *[yield(key), yield(value)].flatten)
  end
end