Warcraft III: MapsModelsSkinsIconsSpellsToolsTutorials
WC3 JASS: Code SnippetsJASS and vJASS Spells and SystemsJass Tutorials
Chat @Discord

Author Topic: [Snippet] StringIndexer  (Read 6204 times)

  • Awesome Global Code Moderator
  • Recognized User
  • Rookie - level 2
  • *
  • Posts: 83
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • WC3 Spells: 0
  • Reputation: 516
  • vJass Incarnate
    • View Profile
[Snippet] StringIndexer
« on: January 11, 2012, 01:13:43 PM »
This system generates an index for any string below 8191.

Jass:

Code: jass  [Select]
  1. /************************************************
  2. *
  3. *   StringIndexer
  4. *   v1.0.0.2
  5. *   By Magtheridon96
  6. *
  7. *   - Retrieves an index for a string below 8191
  8. *   - Helps easy string data attachment
  9. *
  10. *   Optional:
  11. *   ---------
  12. *
  13. *       - Table by Bribe
  14. *           - hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
  15. *
  16. *   API:
  17. *   ----
  18. *
  19. *       - function IndexString takes string s returns integer
  20. *           - Indexes a string and returns the index
  21. *       - function GetStringId takes string s returns integer
  22. *           - Retrieves the index of an indexed string
  23. *       - function GetStringById takes integer id returns string
  24. *           - Returns the string corresponding to a certain Id
  25. *
  26. ************************************************/
  27. library StringIndexer requires optional Table
  28.  
  29.     globals
  30.         private string array strings
  31.         private integer count = 0
  32.     endglobals
  33.    
  34.     static if LIBRARY_Table then
  35.         private module Init
  36.             private static method onInit takes nothing returns nothing
  37.                 set data = Table.create()
  38.             endmethod
  39.         endmodule
  40.     endif
  41.  
  42.     private struct D extends array
  43.         static if LIBRARY_Table then
  44.             static Table data
  45.            
  46.             implement Init
  47.         else
  48.             static hashtable data = InitHashtable()
  49.         endif
  50.     endstruct
  51.    
  52.     function GetStringById takes integer i returns string
  53.         return strings[i]
  54.     endfunction
  55.    
  56.     function GetStringId takes string t returns integer
  57.         static if LIBRARY_Table then
  58.             return D.data[StringHash(t)]
  59.         else
  60.             return LoadInteger(D.data, StringHash(t), 0)
  61.         endif
  62.     endfunction
  63.    
  64.     function IndexString takes string t returns integer
  65.         local integer i = StringHash(t)
  66.        
  67.         static if LIBRARY_Table then
  68.             if D.data[i] == 0 then
  69.                 set count = count + 1
  70.                
  71.                 set D.data[i] = count
  72.  
  73.                 set strings[count] = t
  74.                 return count
  75.             endif
  76.  
  77.             return D.data[i]
  78.         else
  79.             if LoadInteger(D.data, i, 0) == 0 then
  80.                 set count = count + 1
  81.  
  82.                 call SaveInteger(D.data, i, 0, count)
  83.  
  84.                 set strings[count] = t
  85.                 return count
  86.             endif
  87.  
  88.             return LoadInteger(D.data, i, 0)
  89.         endif
  90.     endfunction
  91.    
  92. endlibrary


I think it's quite nifty.

Feel free to comment.

  • Site Owner
  • Administrator
  • Starter - level 4
  • *
  • Posts: 981
  • WC3 Models: 5
  • WC3 Tutorials: 11
  • WC3 Tools: 10
  • WC3 Maps: 11
  • WC3 Skins: 6
  • WC3 Icons: 2
  • WC3 Spells: 5
  • Reputation: 1153
  • Site Admin - I love fix things
    • View Profile
    • Blizzard Modding Information Center
Re: [Snippet] StringIndexer
« Reply #1 on: September 02, 2012, 11:51:03 AM »
Just in case, it's been approved by me :)

We can give you full hosting for your projects, a complete page!!

A custom altered melee map where you can play Naga and Demons. Check it out!!
Use Dropbox...

 

[Snippet] Group Units in line, with distance and optional spread-value

Started by rvonsonsnadtzBoard Codes & Snippets

Replies: 5
Views: 11447
Last post March 13, 2013, 06:52:16 PM
by rvonsonsnadtz
[Snippet] Single Player Detector

Started by moyackBoard Codes & Snippets

Replies: 12
Views: 21008
Last post March 13, 2013, 12:48:49 PM
by Magtheridon96
[Snippet] Simple Unit Indexer

Started by moyackBoard Codes & Snippets

Replies: 17
Views: 25829
Last post September 30, 2013, 08:07:19 AM
by moyack
[Snippet] Disable Transmission Skip

Started by PurgeandfireBoard Codes & Snippets

Replies: 2
Views: 8730
Last post July 02, 2013, 01:34:55 PM
by Purgeandfire
[Snippet] Timed Effects

Started by moyackBoard Codes & Snippets

Replies: 0
Views: 7398
Last post January 09, 2012, 07:03:32 PM
by moyack
Blizzard Modding Information Center Starcraft II Modding Information Center Wacraft III Modding Information Center WC3JASS.com - The JASS Vault Chronicles of Darkness - A Warcraft III mod Jetcraft - A Starcraft II mod Troll Smash - A Warcraft III Arena
  Mod DB - Change the Game Power of Corruption - A Warcraft III altered melee map Chaos Realm - The world of Game modders and wc3 addicts Follow us on Facebook!!