use simple array based hash, so assembly of it will be easy
This commit is contained in:
parent
0bc0506c5a
commit
d3c4087871
48
lib/parfait/hash.rb
Normal file
48
lib/parfait/hash.rb
Normal file
@ -0,0 +1,48 @@
|
||||
# almost simplest hash imaginable. make good use of arrays
|
||||
class Hash
|
||||
def initialize
|
||||
@keys = Array.new()
|
||||
@values = Array.new()
|
||||
end
|
||||
def empty?
|
||||
@keys.nil?
|
||||
end
|
||||
|
||||
def length()
|
||||
return @keys.length()
|
||||
end
|
||||
|
||||
def get(key)
|
||||
index = key_index(key)
|
||||
if( index )
|
||||
@keys[index]
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def key_index(key)
|
||||
len = @keys.length()
|
||||
index = 0
|
||||
found = nil
|
||||
while(index < len)
|
||||
if( @keys[index] == key)
|
||||
found = index
|
||||
break
|
||||
end
|
||||
index += 1
|
||||
end
|
||||
found
|
||||
end
|
||||
|
||||
def set(key , value)
|
||||
index = key_index(key)
|
||||
if( index )
|
||||
@keys[index] = value
|
||||
else
|
||||
@keys.push(key)
|
||||
@value.push(value)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user