July 4, 2016 at 6:50 pm #1728
I have been trying to get this code to work for a while:Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788#import "<std>"Using std..Global carStore:Store<StoreObject> = New Store<StoreObject>(50)Global list:List<StoreObject> = New List<StoreObject>Function Main()For Local i:Int = 0 Until 20Local car := carStore.GetItem()list.AddLast(car)NextLocal link := list.FirstNode()While linkLocal storeObject :=Cast<StoreObject>(link.Value)link = link.SuccWendEnd functionClass Store<T>Field last:TField total:IntMethod New()End MethodMethod New(count:Int)Fill(count)End MethodMethod Fill:Void(total:Int)For Local i:Int = 0 Until totalLocal c:T = New T()c._pred = lastlast = cNextSelf.total = totalEnd MethodMethod Count:Int()Return totalEnd MethodMethod GetItem:T()If lastLocal c:T = lastlast = Cast<T>(last._pred)c._pred = Nulltotal -= 1Return cEndifReturn New T()End MethodMethod ReturnItem:Void(obj:T)obj._succ = Nullobj._pred = lastlast = objtotal += 1End MethodEnd ClassClass StoreObjectField _pred:StoreObjectField _succ:StoreObjectMethod New()End methodFunction Separate:Void(obj:StoreObject)If obj._predIf obj._succobj._pred._succ = obj._succobj._succ._pred = obj._predElseobj._pred._succ = NullEndifElseif obj._succobj._succ._pred = NullEndifEnd FunctionEnd Class
but it goes into an endless loop. I am not sure if that’s a bug or I am not doing it right?
it works fine in monkey1 but not here.July 4, 2016 at 7:05 pm #1729
This might be a better way of doing it now:Monkey12345678910111213Function Main()For Local i:Int = 0 Until 20Local car := carStore.GetItem()list.AddLast(car)NextLocal items:=list.All()While Not items.AtEndLocal link:=items.CurrentLocal storeObject :=Cast<StoreObject>(link)items.Bump()WendEnd functionJuly 4, 2016 at 7:16 pm #1730
OK Thanks Peter. I don’t get the need of Pred and Succ anymore. I guess I have a lot of re coding to do now.
Personally I don’t think that’s a better way. i hate the fact that it goes trough a lot of processes in the background. I think the direct approach was better.July 4, 2016 at 7:32 pm #1731
I alternatively figured it out with a minor change:Monkey1234567891011121314Function Main()For Local i:Int = 0 Until 20Local car := carStore.GetItem()list.AddLast(car)NextLocal link := list.FirstNode()While link.Value <> NullPrint "ok"Local storeObject :=Cast<StoreObject>(link.Value)link = link.SuccWendEnd function
this works as expected.July 4, 2016 at 9:27 pm #1732
I’ll have to try that if it’s quicker. The other method is safe if you need to add and remove things from the list while you’re looping through but only as opposed to using a foreach, so maybe using it directly like that is slightly faster. But then given how exotic the c++ compiler seems to be these days it’s anyone’s guess!
You must be logged in to reply to this topic.