add delete to list
This commit is contained in:
parent
e651b57d08
commit
96a20ff554
@ -44,6 +44,22 @@ module Parfait
|
||||
self.set( self.get_length + 1 , value)
|
||||
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 unless empty?
|
||||
get(1)
|
||||
@ -86,8 +102,13 @@ module Parfait
|
||||
end
|
||||
|
||||
def set_length len
|
||||
# TODO check if not shrinking
|
||||
was = self.get_length
|
||||
return if was == len
|
||||
if(was < len)
|
||||
grow_to len
|
||||
else
|
||||
shrink_to len
|
||||
end
|
||||
end
|
||||
|
||||
def grow_to(len)
|
||||
@ -97,6 +118,13 @@ module Parfait
|
||||
internal_object_grow(len + 1)
|
||||
end
|
||||
|
||||
def shrink_to(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
|
||||
|
||||
def ==(other)
|
||||
# this should call parfait get_class, alas that is not implemented yet
|
||||
return false if other.class != self.class
|
||||
|
@ -90,6 +90,13 @@ module Parfait
|
||||
old_length = old_length + 1
|
||||
end
|
||||
end
|
||||
def internal_object_shrink(length)
|
||||
old_length = internal_object_length()
|
||||
while( length < old_length )
|
||||
@memory.delete_at(old_length)
|
||||
old_length = old_length - 1
|
||||
end
|
||||
end
|
||||
|
||||
def to_s
|
||||
Sof::Writer.write(self)
|
||||
|
@ -49,6 +49,19 @@ class TestList < MiniTest::Test
|
||||
assert_equal v , @list.get(k)
|
||||
end
|
||||
end
|
||||
def test_delete_at
|
||||
test_many_get
|
||||
assert @list.delete_at 2
|
||||
assert_equal 2 , @list.get_length
|
||||
assert_equal 2 , @list.index_of( :three )
|
||||
end
|
||||
|
||||
def test_delete
|
||||
test_many_get
|
||||
assert @list.delete :two
|
||||
assert_equal 2 , @list.get_length
|
||||
assert_equal 2 , @list.index_of( :three )
|
||||
end
|
||||
def test_index_of
|
||||
test_many_get
|
||||
assert_equal 2 , @list.index_of( :two )
|
||||
|
Loading…
Reference in New Issue
Block a user