About Monkey 2 › Forums › Monkey 2 Development › list Node.InsertBefore and Node.InsertAfter broken
This topic contains 5 replies, has 3 voices, and was last updated by
skn3 2 years, 6 months ago.
-
AuthorPosts
-
October 18, 2016 at 4:28 pm #4513
Looks like insert before/after are broken? Unless I am doing it wrong?
Monkey123456789101112131415161718192021222324252627282930313233343536373839#Import "<std>"Using std..Function Main:Void()Local items := New List<Item>()Local item1 := New Item("one")item1.node = items.AddLast(item1)Local item2 := New Item("two")item2.node = items.AddLast(item2)Local item3 := New Item("three")item3.node = items.AddLast(item3)Print "before"For Local item := Eachin itemsPrint item.nameNextPrint ""item2.node.InsertBefore(item2.node.Pred.Value.node)Print "after"For Local item := Eachin itemsPrint item.nameNextPrint ""EndClass ItemField node:List<Item>.NodeField name:StringMethod New(name:String)Self.name = nameEndEndOctober 18, 2016 at 8:23 pm #4521Mark, I saw that you modified the List to work with skn3 Post. My question is, Is the only way to use InsertBefore or InsertAfter by using nodes with in the list? I tried to add a removed node to it and it failed. produced an endless for loop. I tried it before the current modifications and after the modifications with same results.
[/crayon]Monkey1234567891011121314151617181920212223242526272829303132333435363738394041[crayon-5cba820c1fee0600061543 inline="true" ]#Import "<std>"Using std..Function Main:Void()Local items := New List<Item>()Local item1 := New Item("one")item1.node = items.AddLast(item1)Local item2 := New Item("two")item2.node = items.AddLast(item2)Local item3 := New Item("three")item3.node = items.AddLast(item3)Print "before"For Local item := Eachin itemsPrint item.nameNextitem1.node.Remove()item2.node.InsertBefore(item1.node)Print "after"For Local item := Eachin itemsPrint item.nameNextPrint ""EndClass ItemField node:List<Item>.NodeField name:StringMethod New(name:String)Self.name = nameEndEndOctober 18, 2016 at 8:49 pm #4523Monkey123item1.node.Remove()item2.node.InsertBefore(item1.node)Not quite sure what you’re expecting this to do – once you remove item1.node, it’s no longer in a list so inserting things before/after it doesn’t have a lot of meaning. Also, note that nodes can only be in one list at a time.
Incidentally, I don’t get an endless loop here (with new List), both item1 and item2 just ‘disappear’ as expected, as you a) remove node1 (effectively ‘moving’ it from the list to ‘no list’) then insert node2 before node1 (so it’s also gone!).
But again, I recommend avoiding lists altogether if possible – sticks to Stacks, sort them if necessary, whatever. Lists are slow, memory intensive and just generally icky, as the above demonstrates.
Still, probably my fault for semi-popularizing list abuse like this in the first place!
October 18, 2016 at 9:29 pm #4527.
October 18, 2016 at 9:52 pm #4528Thanks, I was trying to do some some experimenting and wasn’t sure if it was doable or not. I was just wondering if I could move a node from one list to another by doing it that way.
another question:
are linked Lists slow or just this module? I know arrays are faster but was wonder. I guess I could do some tests.October 18, 2016 at 10:12 pm #4532Didn’t realise that the Stack in monkey2 had been fleshed out so much, compared to monkey1/blitz! Very helpful!
-
AuthorPosts
You must be logged in to reply to this topic.