Best way to swap TList objects?

BlitzMax Forums/BlitzMax Programming/Best way to swap TList objects?

Grey Alien(Posted April) [#1]
Hi all, forum search has been down for a while and I googled using site:blitzmax.com but couldn't come up with a result. Also searching the code archives manually is a pain.

Anyway, if I want to swap two list items in a TList, what is the best way please?

Do I need to use a temporary variable like when swapping array slots or can I just use FindLink() to get the two relevant links and then simply change what they point at (assuming I already have variables pointing to objects A and B which are in the list)?

EDIT
OK this seems to work:
Function ccSwapListLinks(linkA:TLink, linkB:TLink)
	Local tempObject:Object = linkA._value
	linkA._value = linkB._value
	linkB._value = tempObject	
End Function



Derron(Posted April) [#2]
Lists contain "TLink"-instances (as you surely know).

TLink contains the property "_value:object".

Maybe just switch these two _value-properties of the tlink-instances of interest.


Of course this needs:
- a new call to sort the list (only if you sort it)
- it might become troublesome of something else tampers the tlink-entries of a list.


bye
Ron


Derron(Posted April) [#3]
Here is a sample:




Output:
[ 80%] Processing:linkedlist_switch.bmx
[ 90%] Compiling:linkedlist_switch.bmx.console.release.linux.x86.s
flat assembler  version 1.68  (32768 kilobytes memory)
3 passes, 2827 bytes.
[100%] Linking:linkedlist_switch
Executing:linkedlist_switch

Original list contains:
- Item 1
- Item 2
- Item 3
- Item 4
- Item 5
switching first and last links objects now...
modified list contains:
- Item 5
- Item 2
- Item 3
- Item 4
- Item 1


bye
Ron


Grey Alien(Posted April) [#4]
Aha excellent thanks. I came up with the same thing so it's good to have that verified. It would surely make a good TList Method for BlitzMax. I expect Brucey has already added it to his version.

And yes I did need to resort it (and update on screen coords as that was relevant in my case), so good catch.