1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. Don’t forget to sign up for the Hive Cup. There’s a 555 EUR prize pool. Sign up now!
    Dismiss Notice
  4. The Hive Workshop Cup contest results have been announced! See the maps that'll be featured in the Hive Workshop Cup tournament!
    Dismiss Notice
  5. The results are out! Check them out.
    Dismiss Notice
  6. The poll for Hive's 12th Concept Art Contest is up! Go cast your vote for your favourite genie!
    Dismiss Notice
  7. The raddest synthwave tracks were chosen - Check out our Music Contest #12 - Results and congratulate the winners!
    Dismiss Notice
  8. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

Arrays and hashtables

Discussion in 'Triggers & Scripts' started by Krogoth, Oct 4, 2012.

  1. Krogoth

    Krogoth

    Joined:
    Apr 5, 2011
    Messages:
    247
    Resources:
    0
    Resources:
    0
    I am noob and I want to know how big is difference between using arrays with unit indexing and hashtables.

    For example, I want each unit having stamina. So, I can use array stamina[] or hashtable.

    ?
     
  2. claptomanic

    claptomanic

    Joined:
    Jul 10, 2011
    Messages:
    562
    Resources:
    0
    Resources:
    0
    for your example id recommend hashs because that way your able to easily connect the stamina value to each unit....would recommend 2 arrays otherwise.

    for the difference and all informations read this
     
  3. Krogoth

    Krogoth

    Joined:
    Apr 5, 2011
    Messages:
    247
    Resources:
    0
    Resources:
    0
    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.

    So, my idea is to take PUI and make a lot of arrays (because stamina was just an example). Am I right? And am I right if the most of properties are very special, only for 1% of units? Should I make separated indexing for them? Or should I use hashtable? ...
     
  4. claptomanic

    claptomanic

    Joined:
    Jul 10, 2011
    Messages:
    562
    Resources:
    0
    Resources:
    0
    1. i dont know PUI....just know that its an unit indexing.

    2. why making masses of arrays instead of 1 hash?

    3. tell me what you wanna do exactly and ill tell you the (at least in my opinion) best way to do it ^^
     
  5. Yixx

    Yixx

    Joined:
    Oct 12, 2008
    Messages:
    1,492
    Resources:
    3
    Spells:
    3
    Resources:
    3
    The first big difference is that arrays can only hold indices from 0 to 8191, while hashtables can hold any value that fits in a 32-bit integer..

    Then there is the way of handling it. Though, if you would use a hashtable, I would strongly suggest to use Table by Bribe, since it makes you not use one hashtable per system/resource, but rather one hashtables for everything you need..
     
  6. defskull

    defskull

    Joined:
    Mar 27, 2008
    Messages:
    7,978
    Resources:
    17
    Spells:
    17
    Resources:
    17
    Let me give you a morning lecture about the difference between Indexing and Hashtable (well, it's morning at my time).

    INTRODUCTIONS
    Both 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.


    HASHTABLE
    By 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.



    INDEXING
    The 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.


    The way I see it, the main difference you should know is that Indexing offers data stacking while Hashtable offers data overwriting, that's it. Don't compare speeds, just focus on the goal you want to achieve (either Stacking or Overwriting).

    Well, that is my point of view, I did not say that every statement I made there is correct, but that is from my experience dealing with Hashtable and Indexing.
    If there are mistakes, kindly point it to me and explain the real definitions based from my statements, thank you :)
     
  7. Adiktuz

    Adiktuz

    Joined:
    Oct 16, 2008
    Messages:
    9,674
    Resources:
    23
    Models:
    2
    Packs:
    1
    Maps:
    1
    Spells:
    16
    Tutorials:
    1
    JASS:
    2
    Resources:
    23
    for this, I'd suggest using arrays + the GUI unit indexer to easily set and track the array index of each unit... mainly because it's easier to write an array line compared to a hashtable line...

    or if ur using vJASS, bribe's Table will be fine, just use the unit's HandleID as the key...
     
  8. defskull

    defskull

    Joined:
    Mar 27, 2008
    Messages:
    7,978
    Resources:
    17
    Spells:
    17
    Resources:
    17
    Many people often confused Indexing and Unit Indexer.
    Let me give you an equation for you to understand this a bit.

    Indexing IS NOT Unit Indexer
    Indexing IS NOT HASHTABLE

    BUT

    Hashtable IS Unit Indexer

    Now you must be confused, how can Hashtable = Unit Indexer (it has the word "Index" in it).

    Basically, Unit Indexer is a Hashtable which uses Custom Value to replace parent key which will make it to become a 1D array instead of 2D.

    Basically, the application is the same method as Hashtable (Unit Indexer overwrites data) but it is said to be more faster than Hashtable.

    So, what is the use of Hashtable if we already have Unit Indexer ?
    This is where Hashtable beats Indexing.

    Like I said at my previous post, Hashtable allows you to save values to an object that does not even exist in the map yet, right ?

    However, Unit Indexer can't do that, it can't save values to an object that does not exist in the map yet because it assigns value when unit enters the map or initially in the map, but not from the map's Object Editor.

    Hashtable allows you to save '200' to a unit named Paladin so when Paladin is summoned in the map, you can load the value from the UNIT TYPE of that UNIT to get the value 200.
     
  9. Adiktuz

    Adiktuz

    Joined:
    Oct 16, 2008
    Messages:
    9,674
    Resources:
    23
    Models:
    2
    Packs:
    1
    Maps:
    1
    Spells:
    16
    Tutorials:
    1
    JASS:
    2
    Resources:
    23
    actually you can save things for non-existent things into arrays too [using normal indexing, not UNIT indexing because unit indexing is obviously for units existent on the map already]... but of course hashes do it directly so it is better to use hashes for that...

    but assuming he's using vJASS, then Bribe's Table will be a better way of doing that... shorter to write too because you use it like an array...

    afaik, unit indexer systems just sets the unit's custom value to it's "map index" value (most probably based on the order that it appeared on the map for unit's dynamically added) which is why its faster (uses CV directly, no arrays or hashes needed)... that is before index recycling...

    example: if there is no unit preplaced on the map, then the first unit to be created will be given a custom value of 1, the next one will have a cv of 2 and so on...
     
  10. defskull

    defskull

    Joined:
    Mar 27, 2008
    Messages:
    7,978
    Resources:
    17
    Spells:
    17
    Resources:
    17
    Lol no idea he was a vJASSer ;p

    Well, if my point of view (GUI Side), I would base this system from a Hashtable because it allows a data overwriting, instead of data stacking (why would you want to stack stamina lol).

    And by means of Hashtable, you can use Unit Indexer in it, because the "stamina" is assigned to units when they enter the map and initially on the map, right ?
     
  11. Adiktuz

    Adiktuz

    Joined:
    Oct 16, 2008
    Messages:
    9,674
    Resources:
    23
    Models:
    2
    Packs:
    1
    Maps:
    1
    Spells:
    16
    Tutorials:
    1
    JASS:
    2
    Resources:
    23
    yes, though using hashes for just one purpose is not really good... but if he's gonna use it for multiple things then hashes will be better...

    but why won't you be able to overwrite data in an array and why won't hashes be able to stack data??? just curious...

    or do you actually mean stacking in a sense that you can have multiple instances of the thing (like stamina) on a single unit?

    but anyway, you can overwrite data in an array and "stack" data in a hash...
     
  12. defskull

    defskull

    Joined:
    Mar 27, 2008
    Messages:
    7,978
    Resources:
    17
    Spells:
    17
    Resources:
    17
    The only way I see to stack data in Hashtable is by spawning dummy for each instance (tested before and it works like Indexing).

    Problem is, it requires unit to hold that the data, while Indexing requires none of it at all and serves the same purpose, that's why Indexing is favored for data stacking.
     
  13. mckill2009

    mckill2009

    Joined:
    Mar 10, 2009
    Messages:
    4,696
    Resources:
    34
    Maps:
    5
    Spells:
    27
    JASS:
    2
    Resources:
    34
    Technically, hashtables are also considered as arrays but can hold huge millions of instances using an ID of a handle (unit, timers, locations, destructables etc...)...

    The problem of HT is the 'recalling' or loading of the saved data, since there are only a few functions that can recall data of that handle (e.g. unit==PIck Every UNit, timer==getexpiredtimer)...HT is best suitable on systems, not spells...

    vJass on the other hand uses structs which is indexing itself, thus combining Table (multi-hashtable) makes it suitable to anything with unlimited instances...
     
  14. defskull

    defskull

    Joined:
    Mar 27, 2008
    Messages:
    7,978
    Resources:
    17
    Spells:
    17
    Resources:
    17
    And I was explaining on the GUI Side, not on vJASS Side.
    Well, if you use vJASS, go on use Table, or so they call it.
     
  15. PurplePoot

    PurplePoot

    Joined:
    Dec 14, 2005
    Messages:
    11,161
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    The speed difference is negligible. Use whichever you feel more comfortable with; both work just fine. Personally, I prefer arrays since you generally want to be looping through all indices on a timer, which makes them convenient, but there are arguments and situations for both.
     
  16. defskull

    defskull

    Joined:
    Mar 27, 2008
    Messages:
    7,978
    Resources:
    17
    Spells:
    17
    Resources:
    17
    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.
     
  17. Krogoth

    Krogoth

    Joined:
    Apr 5, 2011
    Messages:
    247
    Resources:
    0
    Resources:
    0
    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?
     
  18. PurplePoot

    PurplePoot

    Joined:
    Dec 14, 2005
    Messages:
    11,161
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    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.

    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.
     
  19. Krogoth

    Krogoth

    Joined:
    Apr 5, 2011
    Messages:
    247
    Resources:
    0
    Resources:
    0
    Can I make something like C++ union in vJASS?
     
  20. PurplePoot

    PurplePoot

    Joined:
    Dec 14, 2005
    Messages:
    11,161
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    No (thank god).