switch to 0 based indexing

the world rocked for a moment (and more fixes to follow)
also the crumbling of idealism was heard
This commit is contained in:
Torsten Ruger
2018-05-14 11:55:01 +03:00
parent 4856b9891d
commit ab01fa3862
19 changed files with 205 additions and 235 deletions

View File

@ -14,58 +14,60 @@
module Parfait
class List < Object
def self.get_length_index
2
return 2
end
def self.get_indexed(index)
index + 2
return index + 2
end
def length_index
1
end
def get_offset
2
return 2
end
def get_length
r = get_internal_word( 2 ) #one for type
r = get_internal_word( length_index ) #one for type
r.nil? ? 0 : r
end
# set the value at index.
# Lists start from index 1
# Lists start from index 0
def set( index , value)
raise "Only positive indexes #{index}" if index <= 0
if index > get_length
grow_to(index)
raise "Only positive indexes #{index}" if index < 0
if index >= get_length
grow_to(index + 1)
end
# start one higher than offset, which is where the length is
set_internal_word( index + 2, value)
set_internal_word( index + get_offset, value)
end
# set the value at index.
# Lists start from index 1
# Lists start from index 0
def get( index )
raise "Only positive indexes, #{index}" if index <= 0
raise "Only positive indexes, #{index}" if index < 0
ret = nil
if(index <= get_length)
if(index < get_length)
# start one higher than offset, which is where the length is
ret = get_internal_word(index + 2 )
ret = get_internal_word(index + get_offset )
end
ret
end
def grow_to( len)
def grow_to( len )
raise "Only positive lenths, #{len}" if len < 0
old_length = get_length
return if old_length >= len
# raise "bounds error at #{len}" if( len + offset > 16 )
# be nice to use the indexed_length , but that relies on booted space
set_internal_word( 2 , len) #one for type
set_internal_word( length_index , len) #one for type
end
def shrink_to( len )
raise "Only positive lenths, #{len}" if len < 0
old_length = get_length
return if old_length <= len
set_internal_word( 2 , len)
set_internal_word( length_index , len)
end
def indexed_length
@ -82,13 +84,13 @@ module Parfait
return index_of(item) != nil
end
# index of item, remeber first item has index 1
# index of item
# return nil if no such item
def index_of( item )
max = self.get_length
#puts "length #{max} #{max.class}"
counter = 1
while( counter <= max )
counter = 0
while( counter < max )
if( get(counter) == item)
return counter
end
@ -101,14 +103,14 @@ module Parfait
def next_value(val)
index = index_of(val)
return nil unless index
return nil if index == get_length
return nil if index == (get_length - 1)
return get(index + 1)
end
# push means add to the end
# this automatically grows the List
def push( value )
to = self.get_length + 1
to = self.get_length
set( to , value)
to
end
@ -131,12 +133,12 @@ module Parfait
def first
return nil if empty?
get(1)
get(0)
end
def last
return nil if empty?
get(get_length())
get(get_length() - 1)
end
def empty?
@ -148,7 +150,7 @@ module Parfait
return false if other.class != self.class
return false if other.get_length != self.get_length
index = self.get_length
while(index > 0)
while(index >= 0)
return false if other.get(index) != self.get(index)
index = index - 1
end
@ -158,7 +160,7 @@ module Parfait
# 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
def == other
def ==( other )
self.object_id == other.object_id
end
@ -172,8 +174,8 @@ module Parfait
end
def each
index = 1
while index <= self.get_length
index = 0
while index < self.get_length
item = get(index)
yield item
index = index + 1
@ -182,8 +184,8 @@ module Parfait
end
def each_with_index
index = 1
while index <= self.get_length
index = 0
while index < self.get_length
item = get(index)
yield item , index
index = index + 1
@ -192,8 +194,8 @@ module Parfait
end
def each_pair
index = 1
while index <= self.get_length
index = 0
while index < self.get_length
key = get( index )
value = get(index + 1)
yield key , value
@ -203,8 +205,8 @@ module Parfait
end
def find
index = 1
while index <= self.get_length
index = 0
while index < self.get_length
item = get(index)
return item if yield item
index = index + 1
@ -212,34 +214,34 @@ module Parfait
return nil
end
def set_length len
def set_length( len )
was = self.get_length
return if was == len
if(was < len)
grow_to len
grow_to( len )
else
shrink_to len
shrink_to( len )
end
end
def inspect
index = 1
index = 0
ret = ""
while index <= self.get_length
while index < self.get_length
item = get(index)
ret += item.inspect
ret += "," unless index == self.get_length
ret += "," unless index == (self.get_length - 1)
index = index + 1
end
ret
end
# 1 -based index
# 0 -based index
def get_internal_word(index)
@memory[index]
end
# 1 -based index
# 0 -based index
def set_internal_word(index , value)
raise "Word[#{index}] = " if((self.class == Parfait::Word) and value.nil? )
@memory[index] = value
@ -248,8 +250,8 @@ module Parfait
alias :[] :get
def to_sof_node(writer , level , ref )
Sof.array_to_sof_node(self , writer , level , ref )
def to_rxf_node(writer , level , ref )
Sof.array_to_rxf_node(self , writer , level , ref )
end
def dup
@ -262,8 +264,8 @@ module Parfait
def to_a
array = []
index = 1
while( index <= self.get_length)
index = 0
while( index < self.get_length)
array[index - 1] = get(index)
index = index + 1
end
@ -275,9 +277,9 @@ module Parfait
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 = 0
while index < array.length do
list.set(index , array[index])
index = index + 1
end
list