rubyx/lib/register/parfait/list.rb

53 lines
1.5 KiB
Ruby
Raw Normal View History

require_relative "indexed"
# 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
# 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
#
# 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
# so all incoming/outgoing indexes have to be shifted one up/down
module Parfait
class List < Object
include Indexed
self.offset(1)
2015-05-31 10:07:49 +02:00
def initialize( )
super()
end
alias :[] :get
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
# 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
end
end