2015-05-12 18:10:45 +02:00
|
|
|
# almost simplest hash imaginable. make good use of Lists
|
2015-03-25 16:16:20 +01:00
|
|
|
|
2015-05-11 17:55:49 +02:00
|
|
|
module Parfait
|
2015-05-12 18:10:45 +02:00
|
|
|
class Dictionary < Object
|
2016-12-29 17:47:45 +01:00
|
|
|
|
2019-09-09 19:26:54 +02:00
|
|
|
attr_reader :type, :i_keys , :i_values
|
2018-08-11 18:15:34 +02:00
|
|
|
|
|
|
|
def self.type_length
|
|
|
|
3
|
|
|
|
end
|
|
|
|
|
2015-05-20 12:50:56 +02:00
|
|
|
# only empty initialization for now
|
|
|
|
#
|
|
|
|
# internally we store keys and values in lists, which means this does **not** scale well
|
2015-05-11 17:55:49 +02:00
|
|
|
def initialize
|
2015-05-21 20:50:17 +02:00
|
|
|
super()
|
2019-09-09 19:26:54 +02:00
|
|
|
@i_keys = List.new()
|
|
|
|
@i_values = List.new()
|
2015-05-11 17:55:49 +02:00
|
|
|
end
|
2015-05-20 12:50:56 +02:00
|
|
|
|
2016-12-30 19:46:18 +01:00
|
|
|
def keys
|
2019-09-09 19:26:54 +02:00
|
|
|
@i_keys.dup
|
2016-12-30 19:46:18 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def values
|
2019-09-09 19:26:54 +02:00
|
|
|
@i_values.dup
|
2016-12-30 19:46:18 +01:00
|
|
|
end
|
2016-12-29 17:47:45 +01:00
|
|
|
|
2015-05-20 12:50:56 +02:00
|
|
|
# are there any key/value items in the list
|
2015-05-11 17:55:49 +02:00
|
|
|
def empty?
|
2019-09-09 19:26:54 +02:00
|
|
|
@i_keys.empty?
|
2015-05-11 17:55:49 +02:00
|
|
|
end
|
2014-08-28 15:20:09 +02:00
|
|
|
|
2015-05-20 12:50:56 +02:00
|
|
|
# How many key/value pairs there are
|
2015-05-11 17:55:49 +02:00
|
|
|
def length()
|
2019-09-09 19:26:54 +02:00
|
|
|
return @i_keys.get_length()
|
2015-05-11 17:55:49 +02:00
|
|
|
end
|
2014-08-28 15:20:09 +02:00
|
|
|
|
2018-05-13 12:03:04 +02:00
|
|
|
def next_value(val)
|
2019-09-09 19:26:54 +02:00
|
|
|
return @i_values.next_value(val)
|
2018-05-13 12:03:04 +02:00
|
|
|
end
|
|
|
|
|
2015-05-20 12:50:56 +02:00
|
|
|
# get a value fot the given key
|
2015-05-25 17:48:35 +02:00
|
|
|
# key identity is checked with == not === (ie equals not identity)
|
2015-05-20 12:50:56 +02:00
|
|
|
# return nil if no such key
|
2015-05-11 17:55:49 +02:00
|
|
|
def get(key)
|
|
|
|
index = key_index(key)
|
|
|
|
if( index )
|
2019-09-09 19:26:54 +02:00
|
|
|
@i_values.get(index)
|
2015-05-11 17:55:49 +02:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2015-05-20 12:50:56 +02:00
|
|
|
|
|
|
|
# same as get(key)
|
2015-05-11 17:55:49 +02:00
|
|
|
def [](key)
|
|
|
|
get(key)
|
2014-08-28 15:20:09 +02:00
|
|
|
end
|
|
|
|
|
2015-05-20 12:50:56 +02:00
|
|
|
# private method
|
2015-05-11 17:55:49 +02:00
|
|
|
def key_index(key)
|
2019-09-09 19:26:54 +02:00
|
|
|
@i_keys.index_of(key)
|
2014-08-28 15:20:09 +02:00
|
|
|
end
|
|
|
|
|
2015-05-20 12:50:56 +02:00
|
|
|
# set key with value, returns value
|
2015-05-11 17:55:49 +02:00
|
|
|
def set(key , value)
|
|
|
|
index = key_index(key)
|
|
|
|
if( index )
|
2019-09-09 19:26:54 +02:00
|
|
|
@i_values.set(index , value)
|
2015-05-11 17:55:49 +02:00
|
|
|
else
|
2019-09-09 19:26:54 +02:00
|
|
|
@i_keys.push(key)
|
|
|
|
@i_values.push(value)
|
2015-05-11 17:55:49 +02:00
|
|
|
end
|
|
|
|
value
|
2014-08-28 15:20:09 +02:00
|
|
|
end
|
2015-05-20 12:50:56 +02:00
|
|
|
|
|
|
|
#same as set(k,v)
|
2015-05-11 17:55:49 +02:00
|
|
|
def []=(key,val)
|
|
|
|
set(key,val)
|
|
|
|
end
|
|
|
|
|
2015-05-20 12:50:56 +02:00
|
|
|
# yield to each key value pair
|
2015-05-20 09:57:20 +02:00
|
|
|
def each
|
2018-05-14 10:55:01 +02:00
|
|
|
index = 0
|
2019-09-09 19:26:54 +02:00
|
|
|
while index < @i_keys.get_length
|
|
|
|
key = @i_keys.get(index)
|
|
|
|
value = @i_values.get(index)
|
2015-05-20 09:57:20 +02:00
|
|
|
yield key , value
|
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2016-12-08 11:48:08 +01:00
|
|
|
def inspect
|
|
|
|
string = "Dictionary{"
|
|
|
|
each do |key , value|
|
|
|
|
string += key.to_s + " => " + value.to_s + " ,"
|
|
|
|
end
|
|
|
|
string + "}"
|
|
|
|
end
|
|
|
|
|
2018-05-14 10:55:01 +02:00
|
|
|
def to_rxf_node(writer , level , ref)
|
|
|
|
Sof.hash_to_rxf_node( self , writer , level , ref)
|
2015-11-18 10:55:29 +01:00
|
|
|
end
|
2014-09-16 15:05:38 +02:00
|
|
|
end
|
2014-08-28 15:20:09 +02:00
|
|
|
end
|