HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

ListModule

04-14-2009, 11:46 PM#1
grim001
This library provides the List module, which allows you to easily create a linked list of all of the allocated instances of a struct-type. Iterating through a linked list is about 12% faster than iteratating through an array in JASS. There is no faster method to iterate through a list of structs than the method used by this module. Aside from the marginal speed gain, the best use of this library is to hide some ugly low-level code from your structs. Rather than manually building and maintaining a list of struct instances, just implement the List module, and your code will become much prettier.

Expand JASS:
04-15-2009, 04:13 AM#2
akolyt0r
looks much better than DataTable ;)

however in your first example the "LoopThroughAllYourStructs" method most probably should be static ;)
04-15-2009, 04:46 AM#3
Here-b-Trollz
StackModule too plx.
04-15-2009, 05:51 AM#4
grim001
Updated, it regained the ability to destroy structs attached to units leaving the game.
04-15-2009, 07:05 AM#5
akolyt0r
Quote:
Originally Posted by grim001
Updated, it regained the ability to destroy structs attached to units leaving the game.
that should be optional .. add a boolean to UnitList...
04-15-2009, 07:15 AM#6
grim001
You mean give the user the option to leak?
04-15-2009, 10:55 AM#7
dead_or_alivex
Great, I was just thinking of something like this. I was toying with the idea of making structs extend a linked list struct, then thought to use the new modules, and then came across this topic.

Quote:
Originally Posted by Documentation
The "List" module allows you to create a linked list containing all of the allocated instances of a struct.
This is slightly misleading. You can choose which structs go into the list, right? When I first read it I got the impression that you couldn't.
04-15-2009, 11:21 AM#8
grim001
You can leave structs out of the list, but I can't think of any application where it makes sense to do so. Documentation gets long enough without describing unintended ways of using it. If Vex adds module-specific onCreate, onDestroy, etc., I could make the adding/removing automatic.
04-15-2009, 11:54 AM#9
Tide-Arc Ephemera
Grim, change your user title to "Requires vJass" so you don't have to type it for everything you do at WC3C.
04-16-2009, 05:24 AM#10
grim001
Quote:
Originally Posted by Here-b-Trollz
StackModule too plx.

Do you actually need fast random access? Otherwise linked list is superior.
04-16-2009, 03:36 PM#11
PipeDream
It doesn't exactly implement what we usually call lists, since there's two extra invariants: uniqueness of entries and it's static in the single instance sense. Because of the uniqueness the base name should be Set. I'm uncertain what to do about the single instantiability - consider SingletonSet, StaticSet.

I'm skeptical of the claim that it's faster than iterating over an array, but I like the style.
04-17-2009, 02:27 AM#12
grim001
Let's compare array iteration and linked list iteration:

Array style:
Collapse JASS:
function DoLoop takes nothing returns nothing
    local integer n = MyStruct.stack_n //global read
    local MyStruct ms
        loop
            exitwhen n < 0 //var read and comparison
            set ms = MyStruct[n] //var set, var read, array read
            //do stuff
            set n = n - 1 //var read, subtraction, var set
        endloop
endfunction
Requires 2 local integer declarations, 1 global read
Requires 1 comparison, 1 global array read, 3 local read, 2 local sets, 1 subtraction per loop

Linked list style:
Collapse JASS:
function DoLoop takes nothing returns nothing
    local MyStruct ms = MyStruct.first //global read
        loop
            exitwhen ms == 0 //var read and comparison
            //do stuff
            set ms = ms.next //var set, var read, array read
        endloop
endfunction
Requires 1 local integer declaration, 1 global read
Requires 1 comparison, 1 global array read, 2 local read, 1 local set

The difference comes out to 1 fewer local declaration in the function, and 1 less local read/local set/subtraction per loop (all from getting rid of the n = n - 1 line). It's not that big of a deal but it should please the "speed wanker" group as they have been referred to lately.
04-17-2009, 05:26 AM#13
PipeDream
Count literal values as well in ops

The subtlety I missed is the extra layer of indirection required by an array implementation which is absorbed naturally by linked lists.

Not a criticism:
You don't have to have indirection at the cost of copying the entire struct contents. Since iteration is more frequent than modification, some structs are small, etc, it's not nonsensical. It could even happen in a system like this w vjass support for automatic struct assignment by value, or a requirement that people write a copy or move by value method.
04-17-2009, 04:49 PM#14
grim001
I'm going to split this into two libraries since UnitList has become much bigger.
04-20-2009, 02:39 PM#15
Vexorian
Could you move UnitListModule to another thread? I'll review ListModule:
...
Ok, ListModule has been approved, but I'd rather you move UnitListModule to another thread, so I can just approve this one and review that other one later instead of having to wait till reviewing UnitListModule before approving this one.