Dear Uniconers:
For a particular program that I am writing, I created a class (OList,
see code at end of message) to manage a list. If anyone knows of a
better or more general implementation, I'd like to hear about it.
Anyway, I am looking at the l_reorder() method, which moves the list
item at position specified by the first argument to the position
specified by the second argument. To clarify what I mean,
object := OList( [1,2,3,4,5] )
# object.data is now [1,2,3,4,5]
# next, move data[-2:+1] to the position data[2]
object.l_reorder( -2, 2 )
# object.data is now [1,4,2,3,5]
My question is, "Is there a way to do this that is more straightforward
than using delete and insert?" For instance, exchanging items could
straightforwardly be coded in a method body as
return (data[imove] :=: data[ib4]) & 1
but I cannot think of an equally terse way of moving a data item.
Thanks for your insight.
-Art
class OList ( # class to handle both categories and subcategories
data # holds the list itself
)
method l_delete( # remove an object from the list
ib4 # index of object to delete
)
local length
length := *data
(0 > ib4) & ib4 +:= length + 1 # make sure ib4 >0
0 <= ib4 <= length | fail # reject ib4 out of range
return delete( data, ib4 ) & 1
end
method l_get_copy( # get a copy of the data member
)
return copy(data)
end
method l_insert( # insert an object into the list
ib4, # the position in the list before which the object should be
inserted
o # an object to append to the list
)
local length
length := *data
(0 > ib4) & ib4 +:= length + 1 # make sure ib4 > 0
0 <= ib4 <= length | fail # reject ib4 out of range
return insert( data, ib4, o ) & 1
end
method l_reorder( # change the order of items in the list
imove, # index of the item to move
ib4 # index before which item should be moved
)
local item, length
length := *data
(0 > ib4) & ib4 +:= length + 1 # make sure ib4 > 0
0 <= ib4 <= length | fail # reject ib4 out of range
(0 > imove) & imove +:= length + 1 # make sure imove > 0
0 < imove <= length | fail # reject imove out of range
item := data[imove]
delete( data, imove )
insert( data, imove >= ib4 | (0 < ib4) - 1 , item) | put( data, item )
return
end
method l_set( # set the data member to the list supplied
d_list # data to store in data member
)
return ( type(d_list)=="list", data := d_list )
stop( "OList.lset() failed - was argument a list?" )
end
initially( l )
l_set( l )
end
-------------------------------------------------------
This SF.net email is sponsored by: ValueWeb:
Dedicated Hosting for just $79/mo with 500 GB of bandwidth!
No other company gives more support or power for your dedicated server
http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/
_______________________________________________
Unicon-group mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/unicon-group
- [Unicon-group] Object to manage a list, with capability to... Art Eschenlauer
- Re: [Unicon-group] Object to manage a list, with capa... Steve Wampler
