Let me give you a morning lecture about the difference between Indexing and Hashtable (well, it's morning at my time).There is almost nothing about difference as I see. "Hashtables are slower than arrays" - and that's all. But what is really wondering me, it's JASSers use them both often.
INTRODUCTIONSBoth method (Indexing and Hashtable) uses arrays in the process. Indexing is a 1D array (Unit[0]) while Hashtable is a 2D arrays (Unit[0][0]) which uses child key and parent key to determine which unit is which while Indexing uses only 1 key (Integer) to assign a value (Index) to a unit.
HASHTABLEBy using this method, it allows you to overwrite a data to a certain unit, it is useful enough if you're making a trigger that needs to be overwritten, for example, a buff trigger. Well, you know that buffs are reset each time you are affected to it while it's still on that unit, right ? For instance, you have Roar buff that lasts for 10 seconds. At the last remaining 3 seconds, you would cast Roar again, what would happen ? Ah yes, the buff is reset to 10 seconds, right ? This is where Hashtable comes in handy.
It also allows you to use a Direct Search Algorithm which would speed up the performance compared to Indexing which uses Binary Search Algorithm - the larger the index , the heavier the process (it depends on the object's index value too).
Hashtable can save values (Integer, Real, Boolean, etc) to an object that does not even exist in the map. Let's say you want to save a value of '200' to Item A, so when you pick up Item A (not initially on the map), and once you load to the correct path, you will get the 200 value from that Item. Basically, hashtable saves value to that Item Type, which will affect the Item associated with it.
INDEXINGThe main difference between Indexing and Hashtable, is that Indexing allows you to stack a data to a single object while Hashtable overwrites it. Imagine you have a spell that deals 50 damage per second when applied to a target unit and lasts for 10 seconds. Let's say you have 3 units having this ability and all of them cast at separately at interval of 1 second each. The damage dealt to that unit per second would (50 + 50 + 50) 150 damage per second, and each of the duration has its own timer. Like for the first cast, it would lasts 10 seconds, while the second cast would end 9 seconds after first cast, while the third cast would end 8 seconds after first cast.
Indexing as I said, uses a Binary Search Algorithm, it will loop until you hit the correct index value. Imagine your object has index value of 30, meaning that it will loop 30 times before it finds the index value for it, you can terminate the loop by setting the current looping integer to maxIndex.
It is faster than Hashtable because the fact it is only 1D array compared to 2D array (Hashtable). The Binary Search Algorithm can be ignored if compared to arrays that has different dimensions.
Looping through all indices on a timer is a fine thing, but when it comes to search which object is which, indexing is bad.Personally, I prefer arrays since you generally want to be looping through all indices on a timer
Of course, hence why I said it depends on the situation. That said, I rarely want to do that with anything other than timers, in which case I just attach a struct to the timer with TimerUtils since it's easier.Looping through all indices on a timer is a fine thing, but when it comes to search which object is which, indexing is bad.
You know what I mean, right ?
Yeah well, both has pros and cons, truly depends on the situation.
I would either just have the extra fields even when I wasn't using them, or (in vJass) have a place to attach a struct with additional data.Thank you, guys! I have understood almost everything I wanted.
And the last question: is it stupid to make separated unit indexing for special units having such parameters that nobody else have and no having some of other have? What's more significant, lesser array and its full use or bigger array and faster unit indexing, how do you think?
No (thank god).Can I make something like C++ union in vJASS?
What is this supposed to do? Just use hashtables.Who said indexing cannot be O(1) search? If we refer to GUI just use some sort of real variable event and set a temporary integer variable into the index you are currently playing with and voila you got the index but then you must remember to store it somewhere.
What is this supposed to do? Just use hashtables.
You can still use hashtables in vJass.Never (not that I have the need to do this anyway), vJASS/ZINC is much more simple thank god.
You can still use hashtables in vJass.
edo494 said:Also I suggest using hashtables over arrays as arrays eat space(yes this is another what, its only some bytes but when you have hundreds of array variables its getting some size) because you can add multiple parameters to the same unit using some indexing system and just inserting index of given unit to parent key and changing(overwriting as Defskull calls it and its more right in terms of what happens) the values to whatever you want.
Remind me again when all spells in GUI form uses only 1 hashtable for all of them instead of 1 hashtable/spell. And yes I do know that arrays takes shitload of space compared to a non-arrayed variable that's why I try to prevent it by saying that it's useless to store things like this:
Real[1] = X
Real[2] = XX
Real[3] = XXX
And waste 8187 indexes and instead set it Real[CurrentIndex] = lvl*x. However I find myself going rather off-topic at this very moment.
globals
real array x
endglobals
function Test takes nothing returns nothing
set x[1000] = 5
endfunction
GetHandleId(h) - 0x100000
)This should not be true.they say JASS arrays always get all 8191 indexes upped...
That's how many elements it initializes (since GUI tries to avoid forcing the user to create most objects explicitly). The array max size is 8192. That said, warcraft 3 doesn't allocate all the memory for the array at once; it increases its size as needed (by powers of 2, if I recall correctly).I do wonder about this PAF 'cause then we can rather set the array size in the globals block as well, don't you agree?
That's how many elements it initializes (since GUI tries to avoid forcing the user to create most objects explicitly). The array max size is 8192. That said, warcraft 3 doesn't allocate all the memory for the array at once; it increases its size as needed (by powers of 2, if I recall correctly).