make all instances attr read writers
unfortunately the writers have to have self.var = otherwise it is just a local var Also need to make the type explicit for all Protocol included memory_length on the class for now
This commit is contained in:
@ -3,35 +3,41 @@
|
||||
module Parfait
|
||||
class Dictionary < Object
|
||||
|
||||
attr :type, :i_keys , :i_values
|
||||
|
||||
def self.type_length
|
||||
3
|
||||
end
|
||||
|
||||
# only empty initialization for now
|
||||
#
|
||||
# internally we store keys and values in lists, which means this does **not** scale well
|
||||
def initialize
|
||||
super()
|
||||
@keys = List.new()
|
||||
@values = List.new()
|
||||
self.i_keys = List.new()
|
||||
self.i_values = List.new()
|
||||
end
|
||||
|
||||
def keys
|
||||
@keys.dup
|
||||
i_keys.dup
|
||||
end
|
||||
|
||||
def values
|
||||
@values.dup
|
||||
i_values.dup
|
||||
end
|
||||
|
||||
# are there any key/value items in the list
|
||||
def empty?
|
||||
@keys.empty?
|
||||
i_keys.empty?
|
||||
end
|
||||
|
||||
# How many key/value pairs there are
|
||||
def length()
|
||||
return @keys.get_length()
|
||||
return i_keys.get_length()
|
||||
end
|
||||
|
||||
def next_value(val)
|
||||
return @values.next_value(val)
|
||||
return i_values.next_value(val)
|
||||
end
|
||||
|
||||
# get a value fot the given key
|
||||
@ -40,7 +46,7 @@ module Parfait
|
||||
def get(key)
|
||||
index = key_index(key)
|
||||
if( index )
|
||||
@values.get(index)
|
||||
i_values.get(index)
|
||||
else
|
||||
nil
|
||||
end
|
||||
@ -53,17 +59,17 @@ module Parfait
|
||||
|
||||
# private method
|
||||
def key_index(key)
|
||||
@keys.index_of(key)
|
||||
i_keys.index_of(key)
|
||||
end
|
||||
|
||||
# set key with value, returns value
|
||||
def set(key , value)
|
||||
index = key_index(key)
|
||||
if( index )
|
||||
@values.set(index , value)
|
||||
i_values.set(index , value)
|
||||
else
|
||||
@keys.push(key)
|
||||
@values.push(value)
|
||||
i_keys.push(key)
|
||||
i_values.push(value)
|
||||
end
|
||||
value
|
||||
end
|
||||
@ -76,9 +82,9 @@ module Parfait
|
||||
# yield to each key value pair
|
||||
def each
|
||||
index = 0
|
||||
while index < @keys.get_length
|
||||
key = @keys.get(index)
|
||||
value = @values.get(index)
|
||||
while index < i_keys.get_length
|
||||
key = i_keys.get(index)
|
||||
value = i_values.get(index)
|
||||
yield key , value
|
||||
index = index + 1
|
||||
end
|
||||
|
Reference in New Issue
Block a user