move methods back to the module
easier to read and debug
This commit is contained in:
parent
60098257e9
commit
92fe12a0d1
@ -16,16 +16,14 @@ module Parfait
|
|||||||
base.extend(Methods)
|
base.extend(Methods)
|
||||||
end
|
end
|
||||||
|
|
||||||
module Methods
|
# include? means non nil index
|
||||||
def offset( offset )
|
def include? item
|
||||||
|
return index_of(item) != nil
|
||||||
define_method :get_length do
|
|
||||||
internal_object_length - 1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# index of item, remeber first item has index 1
|
# index of item, remeber first item has index 1
|
||||||
# return nil if no such item
|
# return nil if no such item
|
||||||
define_method :index_of do |item|
|
def index_of item
|
||||||
max = self.get_length
|
max = self.get_length
|
||||||
counter = 1
|
counter = 1
|
||||||
while( counter <= max )
|
while( counter <= max )
|
||||||
@ -37,24 +35,19 @@ module Parfait
|
|||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# include? means non nil index
|
|
||||||
define_method :include? do |item|
|
|
||||||
return index_of(item) != nil
|
|
||||||
end
|
|
||||||
|
|
||||||
# push means add to the end
|
# push means add to the end
|
||||||
# this automatically grows the List
|
# this automatically grows the List
|
||||||
define_method :push do |value|
|
def push value
|
||||||
set( self.get_length + 1 , value)
|
set( self.get_length + 1 , value)
|
||||||
end
|
end
|
||||||
|
|
||||||
define_method :delete do |value|
|
def delete value
|
||||||
index = index_of value
|
index = index_of value
|
||||||
return false unless index
|
return false unless index
|
||||||
delete_at index
|
delete_at index
|
||||||
end
|
end
|
||||||
|
|
||||||
define_method :delete_at do |index|
|
def delete_at index
|
||||||
# TODO bounds check
|
# TODO bounds check
|
||||||
while(index < self.get_length)
|
while(index < self.get_length)
|
||||||
set( index , get(index + 1))
|
set( index , get(index + 1))
|
||||||
@ -64,68 +57,21 @@ module Parfait
|
|||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def first
|
||||||
define_method :first do
|
|
||||||
return nil if empty?
|
return nil if empty?
|
||||||
get(1)
|
get(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
define_method :last do
|
def last
|
||||||
return nil if empty?
|
return nil if empty?
|
||||||
get(get_length())
|
get(get_length())
|
||||||
end
|
end
|
||||||
|
|
||||||
# set the value at index.
|
def empty?
|
||||||
# Lists start from index 1
|
|
||||||
define_method :set do | index , value|
|
|
||||||
raise "Only positive indexes #{index}" if index <= 0
|
|
||||||
if index > self.get_length
|
|
||||||
grow_to(index)
|
|
||||||
end
|
|
||||||
# internally 1 is reserved for the layout
|
|
||||||
internal_object_set( index + 1, value)
|
|
||||||
end
|
|
||||||
|
|
||||||
# set the value at index.
|
|
||||||
# Lists start from index 1
|
|
||||||
define_method :get do | index|
|
|
||||||
raise "Only positive indexes, #{index}" if index <= 0
|
|
||||||
if index > self.get_length
|
|
||||||
return nil
|
|
||||||
else
|
|
||||||
return internal_object_get(index + 1)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
define_method :empty? do
|
|
||||||
self.get_length == 0
|
self.get_length == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
define_method :set_length do | len|
|
def equal? other
|
||||||
was = self.get_length
|
|
||||||
return if was == len
|
|
||||||
if(was < len)
|
|
||||||
grow_to len
|
|
||||||
else
|
|
||||||
shrink_to len
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
define_method :grow_to do | len|
|
|
||||||
raise "Only positive lenths, #{len}" if len < 0
|
|
||||||
old_length = self.get_length
|
|
||||||
return if old_length >= len
|
|
||||||
internal_object_grow(len + 1)
|
|
||||||
end
|
|
||||||
|
|
||||||
define_method :shrink_to do | len|
|
|
||||||
raise "Only positive lenths, #{len}" if len < 0
|
|
||||||
old_length = self.get_length
|
|
||||||
return if old_length <= len
|
|
||||||
internal_object_shrink(len + 1)
|
|
||||||
end
|
|
||||||
|
|
||||||
define_method :equal? do | other|
|
|
||||||
# this should call parfait get_class, alas that is not implemented yet
|
# this should call parfait get_class, alas that is not implemented yet
|
||||||
return false if other.class != self.class
|
return false if other.class != self.class
|
||||||
return false if other.get_length != self.get_length
|
return false if other.get_length != self.get_length
|
||||||
@ -140,7 +86,7 @@ module Parfait
|
|||||||
# above, correct, implementation causes problems in the machine object space
|
# above, correct, implementation causes problems in the machine object space
|
||||||
# because when a second empty (newly created) list is added, it is not actually
|
# 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
|
# added as it exists already. TODO, but hack with below identity function
|
||||||
define_method :== do | other|
|
def == other
|
||||||
self.object_id == other.object_id
|
self.object_id == other.object_id
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -149,15 +95,37 @@ module Parfait
|
|||||||
# This is off course 0 for a list, unless someone squeezed an instance variable in
|
# This is off course 0 for a list, unless someone squeezed an instance variable in
|
||||||
# but additionally, the amount of data comes on top.
|
# but additionally, the amount of data comes on top.
|
||||||
# unfortuntely we can't just use super because of the Padding
|
# unfortuntely we can't just use super because of the Padding
|
||||||
define_method :word_length do
|
def word_length
|
||||||
padded_words( get_layout().get_length() + get_length() )
|
padded_words( get_layout().get_length() + get_length() )
|
||||||
end
|
end
|
||||||
|
|
||||||
define_method :inspect do
|
def each
|
||||||
|
# not sure how to do this with define_method, because of the double block issue.
|
||||||
|
# probably some clever way around that, but not important
|
||||||
|
index = 1 + get_offset
|
||||||
|
while index <= self.get_length
|
||||||
|
item = get(index)
|
||||||
|
yield item
|
||||||
|
index = index + 1
|
||||||
|
end
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_length len
|
||||||
|
was = self.get_length
|
||||||
|
return if was == len
|
||||||
|
if(was < len)
|
||||||
|
grow_to len
|
||||||
|
else
|
||||||
|
shrink_to len
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def inspect
|
||||||
inspect_from 1
|
inspect_from 1
|
||||||
end
|
end
|
||||||
|
|
||||||
define_method :inspect_from do |index|
|
def inspect_from index
|
||||||
ret = ""
|
ret = ""
|
||||||
while index <= self.get_length
|
while index <= self.get_length
|
||||||
item = get(index)
|
item = get(index)
|
||||||
@ -168,6 +136,54 @@ module Parfait
|
|||||||
ret
|
ret
|
||||||
end
|
end
|
||||||
|
|
||||||
|
module Methods
|
||||||
|
def offset( offset )
|
||||||
|
|
||||||
|
define_method :get_offset do
|
||||||
|
offset
|
||||||
|
end
|
||||||
|
|
||||||
|
define_method :get_length do
|
||||||
|
internal_object_length - 1 - offset
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
# set the value at index.
|
||||||
|
# Lists start from index 1
|
||||||
|
define_method :set do | index , value|
|
||||||
|
raise "Only positive indexes #{index}" if index <= 0
|
||||||
|
if index > self.get_length
|
||||||
|
grow_to(index)
|
||||||
|
end
|
||||||
|
# internally 1 is reserved for the layout
|
||||||
|
internal_object_set( index + 1 + offset, value)
|
||||||
|
end
|
||||||
|
|
||||||
|
# set the value at index.
|
||||||
|
# Lists start from index 1
|
||||||
|
define_method :get do | index|
|
||||||
|
raise "Only positive indexes, #{index}" if index <= 0
|
||||||
|
if index > self.get_length
|
||||||
|
return nil
|
||||||
|
else
|
||||||
|
return internal_object_get(index + offset + 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
define_method :grow_to do | len|
|
||||||
|
raise "Only positive lenths, #{len}" if len < 0
|
||||||
|
old_length = self.get_length
|
||||||
|
return if old_length >= len
|
||||||
|
internal_object_grow(len + 1 + offset)
|
||||||
|
end
|
||||||
|
|
||||||
|
define_method :shrink_to do | len|
|
||||||
|
raise "Only positive lenths, #{len}" if len < 0
|
||||||
|
old_length = self.get_length
|
||||||
|
return if old_length <= len
|
||||||
|
internal_object_shrink(len + 1 + offset)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -25,18 +25,6 @@ module Parfait
|
|||||||
include Indexed
|
include Indexed
|
||||||
self.offset(0)
|
self.offset(0)
|
||||||
|
|
||||||
def each
|
|
||||||
# not sure how to do this with define_method, because of the double block issue.
|
|
||||||
# probably some clever way around that, but not important
|
|
||||||
index = 1
|
|
||||||
while index <= self.get_length
|
|
||||||
item = get(index)
|
|
||||||
yield item
|
|
||||||
index = index + 1
|
|
||||||
end
|
|
||||||
self
|
|
||||||
end
|
|
||||||
|
|
||||||
attribute :object_class
|
attribute :object_class
|
||||||
|
|
||||||
def initialize( object_class )
|
def initialize( object_class )
|
||||||
|
@ -23,19 +23,6 @@ module Parfait
|
|||||||
|
|
||||||
alias :[] :get
|
alias :[] :get
|
||||||
|
|
||||||
|
|
||||||
def each
|
|
||||||
# not sure how to do this with define_method, because of the double block issue.
|
|
||||||
# probably some clever way around that, but not important
|
|
||||||
index = 1
|
|
||||||
while index <= self.get_length
|
|
||||||
item = get(index)
|
|
||||||
yield item
|
|
||||||
index = index + 1
|
|
||||||
end
|
|
||||||
self
|
|
||||||
end
|
|
||||||
|
|
||||||
#ruby 2.1 list (just for reference, keep at bottom)
|
#ruby 2.1 list (just for reference, keep at bottom)
|
||||||
# :at, :fetch, :first, :last, :concat, :<<, :push, :pop, :shift, :unshift, :insert, :each, :each_index, :reverse_each,
|
# :at, :fetch, :first, :last, :concat, :<<, :push, :pop, :shift, :unshift, :insert, :each, :each_index, :reverse_each,
|
||||||
# :length, :size, :empty?, :find_index, :index, :rindex, :join, :reverse, :reverse!, :rotate, :rotate!,
|
# :length, :size, :empty?, :find_index, :index, :rindex, :join, :reverse, :reverse!, :rotate, :rotate!,
|
||||||
|
Loading…
Reference in New Issue
Block a user