2015-05-17 13:56:06 +02:00
|
|
|
# A List, or rather an ordered list, is just that, a list of items.
|
|
|
|
|
|
|
|
#
|
|
|
|
# For the implementation we use Objects memory which is index addressable
|
2018-06-29 19:58:59 +02:00
|
|
|
# But, objects are also lists where indexes start with 0, except 0 is taken for the Type
|
|
|
|
# so all incoming/outgoing indexes have to be shifted two up/down (one more for length)
|
2015-04-15 10:39:12 +02:00
|
|
|
|
2015-05-11 17:55:49 +02:00
|
|
|
module Parfait
|
2018-06-29 19:58:59 +02:00
|
|
|
class List < Data16
|
2019-09-09 23:18:20 +02:00
|
|
|
attr_reader :indexed_length , :next_list
|
2018-05-28 14:45:29 +02:00
|
|
|
|
|
|
|
def self.type_length
|
2019-09-22 23:07:30 +02:00
|
|
|
3 # 0 type , 1 indexed_length , 2 - next_list
|
2016-12-30 20:00:18 +01:00
|
|
|
end
|
2018-06-29 19:58:59 +02:00
|
|
|
def self.data_length
|
2018-08-11 18:15:34 +02:00
|
|
|
self.memory_size - self.type_length - 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
super
|
2019-09-09 19:26:54 +02:00
|
|
|
@indexed_length = 0
|
2019-09-22 23:07:30 +02:00
|
|
|
@next_list = nil
|
2018-06-29 19:58:59 +02:00
|
|
|
end
|
2019-02-17 17:16:19 +01:00
|
|
|
|
2018-06-29 19:58:59 +02:00
|
|
|
def data_length
|
|
|
|
self.class.data_length
|
2016-12-30 20:00:18 +01:00
|
|
|
end
|
|
|
|
def get_length
|
2019-09-09 19:26:54 +02:00
|
|
|
r = @indexed_length
|
2016-12-30 20:00:18 +01:00
|
|
|
r.nil? ? 0 : r
|
|
|
|
end
|
|
|
|
|
2018-06-29 19:58:59 +02:00
|
|
|
def ensure_next
|
2019-09-09 19:26:54 +02:00
|
|
|
@next_list = List.new unless @next_list
|
|
|
|
@next_list
|
2018-06-29 19:58:59 +02:00
|
|
|
end
|
|
|
|
|
2016-12-30 20:00:18 +01:00
|
|
|
# set the value at index.
|
2018-05-14 10:55:01 +02:00
|
|
|
# Lists start from index 0
|
2016-12-30 20:00:18 +01:00
|
|
|
def set( index , value)
|
2018-05-14 10:55:01 +02:00
|
|
|
raise "Only positive indexes #{index}" if index < 0
|
|
|
|
if index >= get_length
|
|
|
|
grow_to(index + 1)
|
2016-12-30 20:00:18 +01:00
|
|
|
end
|
2018-06-29 19:58:59 +02:00
|
|
|
if index >= data_length
|
|
|
|
ensure_next
|
2019-09-09 19:26:54 +02:00
|
|
|
@next_list.set( index - data_length , value)
|
2018-06-29 19:58:59 +02:00
|
|
|
else
|
|
|
|
set_internal_word( index + self.class.type_length, value)
|
|
|
|
end
|
2016-12-30 20:00:18 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# set the value at index.
|
2018-05-14 10:55:01 +02:00
|
|
|
# Lists start from index 0
|
2016-12-30 20:00:18 +01:00
|
|
|
def get( index )
|
2018-05-14 10:55:01 +02:00
|
|
|
raise "Only positive indexes, #{index}" if index < 0
|
2018-06-29 19:58:59 +02:00
|
|
|
if index >= data_length
|
2019-09-09 19:26:54 +02:00
|
|
|
return nil unless @next_list
|
|
|
|
return @next_list.get( index - data_length)
|
2018-06-29 19:58:59 +02:00
|
|
|
else
|
|
|
|
ret = nil
|
|
|
|
if(index < get_length)
|
|
|
|
ret = get_internal_word(index + self.class.type_length )
|
|
|
|
end
|
|
|
|
return ret
|
2016-12-30 20:00:18 +01:00
|
|
|
end
|
|
|
|
end
|
2018-05-28 14:45:29 +02:00
|
|
|
alias :[] :get
|
2018-06-29 13:26:25 +02:00
|
|
|
|
2018-05-14 10:55:01 +02:00
|
|
|
def grow_to( len )
|
2016-12-30 20:00:18 +01:00
|
|
|
raise "Only positive lenths, #{len}" if len < 0
|
|
|
|
old_length = get_length
|
|
|
|
return if old_length >= len
|
2018-05-28 14:45:29 +02:00
|
|
|
internal_set_length( len)
|
2016-12-30 20:00:18 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def shrink_to( len )
|
|
|
|
raise "Only positive lenths, #{len}" if len < 0
|
|
|
|
old_length = get_length
|
|
|
|
return if old_length <= len
|
2018-05-28 14:45:29 +02:00
|
|
|
internal_set_length( len)
|
2016-12-30 20:00:18 +01:00
|
|
|
end
|
2015-05-12 17:52:01 +02:00
|
|
|
|
2018-08-11 18:15:34 +02:00
|
|
|
# def indexed_length
|
|
|
|
# get_length()
|
|
|
|
# end
|
2016-12-29 17:49:03 +01:00
|
|
|
|
2016-12-30 20:00:18 +01:00
|
|
|
# include? means non nil index
|
2018-05-13 12:03:04 +02:00
|
|
|
def include?( item )
|
2016-12-30 20:00:18 +01:00
|
|
|
return index_of(item) != nil
|
|
|
|
end
|
|
|
|
|
2018-05-14 10:55:01 +02:00
|
|
|
# index of item
|
2016-12-30 20:00:18 +01:00
|
|
|
# return nil if no such item
|
|
|
|
def index_of( item )
|
|
|
|
max = self.get_length
|
|
|
|
#puts "length #{max} #{max.class}"
|
2018-05-14 10:55:01 +02:00
|
|
|
counter = 0
|
|
|
|
while( counter < max )
|
2016-12-30 20:00:18 +01:00
|
|
|
if( get(counter) == item)
|
|
|
|
return counter
|
|
|
|
end
|
|
|
|
counter = counter + 1
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2018-05-13 12:03:04 +02:00
|
|
|
# return the next of given. Nil if item not in list or there is not next
|
|
|
|
def next_value(val)
|
|
|
|
index = index_of(val)
|
|
|
|
return nil unless index
|
2018-05-14 10:55:01 +02:00
|
|
|
return nil if index == (get_length - 1)
|
2018-05-13 12:03:04 +02:00
|
|
|
return get(index + 1)
|
|
|
|
end
|
2018-05-14 10:55:01 +02:00
|
|
|
|
2016-12-30 20:00:18 +01:00
|
|
|
# push means add to the end
|
|
|
|
# this automatically grows the List
|
|
|
|
def push( value )
|
2018-05-14 10:55:01 +02:00
|
|
|
to = self.get_length
|
2016-12-30 20:00:18 +01:00
|
|
|
set( to , value)
|
|
|
|
to
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete( value )
|
|
|
|
index = index_of value
|
|
|
|
return false unless index
|
|
|
|
delete_at index
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_at( index )
|
|
|
|
# TODO bounds check
|
|
|
|
while(index < self.get_length)
|
|
|
|
set( index , get(index + 1))
|
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
set_length( self.get_length - 1)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def first
|
|
|
|
return nil if empty?
|
2018-05-14 10:55:01 +02:00
|
|
|
get(0)
|
2016-12-30 20:00:18 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def last
|
|
|
|
return nil if empty?
|
2018-05-14 10:55:01 +02:00
|
|
|
get(get_length() - 1)
|
2016-12-30 20:00:18 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def empty?
|
|
|
|
self.get_length == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def equal? other
|
|
|
|
# this should call parfait get_class, alas that is not implemented yet
|
|
|
|
return false if other.class != self.class
|
|
|
|
return false if other.get_length != self.get_length
|
|
|
|
index = self.get_length
|
2018-05-14 10:55:01 +02:00
|
|
|
while(index >= 0)
|
2016-12-30 20:00:18 +01:00
|
|
|
return false if other.get(index) != self.get(index)
|
|
|
|
index = index - 1
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
# above, correct, implementation causes problems in the machine object space
|
|
|
|
# because when a second empty (newly created) list is added, it is not actually
|
|
|
|
# added as it exists already. TODO, but hack with below identity function
|
2018-05-14 10:55:01 +02:00
|
|
|
def ==( other )
|
2016-12-30 20:00:18 +01:00
|
|
|
self.object_id == other.object_id
|
|
|
|
end
|
|
|
|
|
|
|
|
# word length (padded) is the amount of space taken by the object
|
|
|
|
# For your basic object this means the number of instance variables as determined by type
|
|
|
|
# This is off course 0 for a list, unless someone squeezed an instance variable in
|
|
|
|
# but additionally, the amount of data comes on top.
|
|
|
|
# unfortuntely we can't just use super because of the Padding
|
|
|
|
def padded_length
|
2019-08-17 20:07:07 +02:00
|
|
|
Object.padded_words( get_type().instance_length + get_length() )
|
2016-12-30 20:00:18 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def each
|
2018-05-14 10:55:01 +02:00
|
|
|
index = 0
|
|
|
|
while index < self.get_length
|
2016-12-30 20:00:18 +01:00
|
|
|
item = get(index)
|
|
|
|
yield item
|
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_with_index
|
2018-05-14 10:55:01 +02:00
|
|
|
index = 0
|
|
|
|
while index < self.get_length
|
2016-12-30 20:00:18 +01:00
|
|
|
item = get(index)
|
|
|
|
yield item , index
|
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_pair
|
2018-05-14 10:55:01 +02:00
|
|
|
index = 0
|
|
|
|
while index < self.get_length
|
2016-12-30 20:00:18 +01:00
|
|
|
key = get( index )
|
|
|
|
value = get(index + 1)
|
|
|
|
yield key , value
|
|
|
|
index = index + 2
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def find
|
2018-05-14 10:55:01 +02:00
|
|
|
index = 0
|
|
|
|
while index < self.get_length
|
2016-12-30 20:00:18 +01:00
|
|
|
item = get(index)
|
|
|
|
return item if yield item
|
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2018-05-14 10:55:01 +02:00
|
|
|
def set_length( len )
|
2016-12-30 20:00:18 +01:00
|
|
|
was = self.get_length
|
|
|
|
return if was == len
|
|
|
|
if(was < len)
|
2018-05-14 10:55:01 +02:00
|
|
|
grow_to( len )
|
2016-12-30 20:00:18 +01:00
|
|
|
else
|
2018-05-14 10:55:01 +02:00
|
|
|
shrink_to( len )
|
2016-12-30 20:00:18 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
2018-05-14 10:55:01 +02:00
|
|
|
index = 0
|
2016-12-30 20:00:18 +01:00
|
|
|
ret = ""
|
2018-05-14 10:55:01 +02:00
|
|
|
while index < self.get_length
|
2016-12-30 20:00:18 +01:00
|
|
|
item = get(index)
|
|
|
|
ret += item.inspect
|
2018-05-14 10:55:01 +02:00
|
|
|
ret += "," unless index == (self.get_length - 1)
|
2016-12-30 20:00:18 +01:00
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
2018-05-14 10:55:01 +02:00
|
|
|
def to_rxf_node(writer , level , ref )
|
|
|
|
Sof.array_to_rxf_node(self , writer , level , ref )
|
2015-11-18 10:55:29 +01:00
|
|
|
end
|
2016-12-30 19:46:18 +01:00
|
|
|
|
|
|
|
def dup
|
|
|
|
list = List.new
|
|
|
|
each do |item|
|
|
|
|
list.push(item)
|
|
|
|
end
|
|
|
|
list
|
|
|
|
end
|
|
|
|
|
2019-02-17 17:16:19 +01:00
|
|
|
def to_s
|
|
|
|
res = "["
|
|
|
|
each do |item|
|
|
|
|
res = res + item.to_s
|
|
|
|
res = res + " ,"
|
|
|
|
end
|
|
|
|
res = res + " ]"
|
|
|
|
return res
|
|
|
|
end
|
2015-11-18 10:55:29 +01:00
|
|
|
def to_a
|
|
|
|
array = []
|
2018-05-14 10:55:01 +02:00
|
|
|
index = 0
|
|
|
|
while( index < self.get_length)
|
2018-05-14 11:38:44 +02:00
|
|
|
array[index] = get(index)
|
2015-11-18 10:55:29 +01:00
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
array
|
|
|
|
end
|
2018-05-28 14:45:29 +02:00
|
|
|
private
|
|
|
|
def internal_set_length( i )
|
2019-09-09 19:26:54 +02:00
|
|
|
@indexed_length = i
|
2018-05-28 14:45:29 +02:00
|
|
|
end
|
2015-11-18 10:55:29 +01:00
|
|
|
end
|
2015-05-11 17:55:49 +02:00
|
|
|
|
2014-05-31 11:52:29 +02:00
|
|
|
end
|