2015-10-25 17:40:17 +01:00
|
|
|
require_relative "indexed"
|
2015-05-17 13:56:06 +02:00
|
|
|
# A List, or rather an ordered list, is just that, a list of items.
|
|
|
|
|
|
|
|
# For a programmer this may be a little strange as this new start goes with trying to break old
|
|
|
|
# bad habits. A List would be an array in some languages, but list is a better name, closer to
|
|
|
|
# common language.
|
2016-02-25 21:03:11 +01:00
|
|
|
# Another bad habit is to start a list from 0. This is "just" programmers lazyness, as it goes
|
2015-05-17 13:56:06 +02:00
|
|
|
# with the standard c implementation. But it bends the mind, and in oo we aim not to.
|
2016-02-25 21:03:11 +01:00
|
|
|
# If you have a list of three items, they will be first, second and third, ie 1,2,3
|
2015-05-17 13:56:06 +02:00
|
|
|
#
|
|
|
|
# For the implementation we use Objects memory which is index addressable
|
2016-02-25 21:03:11 +01:00
|
|
|
# But, objects are also lists where indexes start with 1, except 1 is taken for the Type
|
2015-05-17 13:56:06 +02:00
|
|
|
# so all incoming/outgoing indexes have to be shifted one up/down
|
2015-04-15 10:39:12 +02:00
|
|
|
|
2015-05-11 17:55:49 +02:00
|
|
|
module Parfait
|
2015-05-12 18:10:45 +02:00
|
|
|
class List < Object
|
2015-10-25 18:16:12 +01:00
|
|
|
include Indexed
|
2015-10-26 13:33:36 +01:00
|
|
|
self.offset(1)
|
2015-05-12 17:52:01 +02:00
|
|
|
|
2015-05-31 10:07:49 +02:00
|
|
|
def initialize( )
|
|
|
|
super()
|
|
|
|
end
|
|
|
|
|
2015-10-05 23:27:13 +02:00
|
|
|
alias :[] :get
|
2015-05-12 17:52:01 +02:00
|
|
|
|
2015-11-18 10:55:29 +01:00
|
|
|
def to_sof_node(writer , level , ref )
|
|
|
|
Sof.array_to_sof_node(self , writer , level , ref )
|
|
|
|
end
|
|
|
|
def to_a
|
|
|
|
array = []
|
|
|
|
index = 1
|
|
|
|
while( index <= self.get_length)
|
|
|
|
array[index - 1] = get(index)
|
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
array
|
|
|
|
end
|
|
|
|
end
|
2015-05-11 17:55:49 +02:00
|
|
|
|
2015-11-18 10:55:29 +01:00
|
|
|
# new list from ruby array to be precise
|
|
|
|
def self.new_list array
|
|
|
|
list = Parfait::List.new
|
|
|
|
list.set_length array.length
|
|
|
|
index = 1
|
|
|
|
while index <= array.length do
|
|
|
|
list.set(index , array[index - 1])
|
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
list
|
2015-05-11 17:55:49 +02:00
|
|
|
end
|
2015-11-18 10:55:29 +01:00
|
|
|
|
2014-05-31 11:52:29 +02:00
|
|
|
end
|