2014-08-28 15:20:09 +02:00
|
|
|
# almost simplest hash imaginable. make good use of arrays
|
2015-03-25 16:16:20 +01:00
|
|
|
|
|
|
|
class Hash
|
2014-08-28 15:20:09 +02:00
|
|
|
def initialize
|
|
|
|
@keys = Array.new()
|
|
|
|
@values = Array.new()
|
|
|
|
end
|
2014-09-16 15:05:38 +02:00
|
|
|
def values()
|
|
|
|
@values
|
|
|
|
end
|
|
|
|
def keys()
|
|
|
|
@keys
|
|
|
|
end
|
2014-08-28 15:20:09 +02:00
|
|
|
def empty?
|
2014-08-28 15:28:25 +02:00
|
|
|
@keys.empty?
|
2014-08-28 15:20:09 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def length()
|
|
|
|
return @keys.length()
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(key)
|
|
|
|
index = key_index(key)
|
|
|
|
if( index )
|
2014-08-28 15:28:25 +02:00
|
|
|
@values[index]
|
2014-08-28 15:20:09 +02:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2014-09-16 15:05:38 +02:00
|
|
|
def [](key)
|
|
|
|
get(key)
|
|
|
|
end
|
2014-08-28 15:20:09 +02:00
|
|
|
|
|
|
|
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)
|
2014-08-28 15:28:25 +02:00
|
|
|
@values.push(value)
|
2014-08-28 15:20:09 +02:00
|
|
|
end
|
2014-08-28 15:28:25 +02:00
|
|
|
value
|
2014-08-28 15:20:09 +02:00
|
|
|
end
|
2014-09-16 15:05:38 +02:00
|
|
|
def []=(key,val)
|
|
|
|
set(key,val)
|
|
|
|
end
|
2014-08-28 15:20:09 +02:00
|
|
|
end
|
|
|
|
|