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

vJASS & Zinc Documentation
For the latest documentation about how it works vJASS and Zinc language layers for Warcraft III, please follow these links:
Jasshelper documentation - Zinc documentation

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

  • Awesome Global Code Moderator
  • Recognized User
  • Rookie - level 2
  • *****
  • Posts: 84
  • 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 »
Category: String handling
Language: vJASS

Related Topics or Resources


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. *
  15. *   API:
  16. *   ----
  17. *
  18. *       - function IndexString takes string s returns integer
  19. *           - Indexes a string and returns the index
  20. *       - function GetStringId takes string s returns integer
  21. *           - Retrieves the index of an indexed string
  22. *       - function GetStringById takes integer id returns string
  23. *           - Returns the string corresponding to a certain Id
  24. *
  25. ************************************************/
  26. library StringIndexer requires optional Table
  27.  
  28.     globals
  29.         private string array strings
  30.         private integer count = 0
  31.     endglobals
  32.    
  33.     static if LIBRARY_Table then
  34.         private module Init
  35.             private static method onInit takes nothing returns nothing
  36.                 set data = Table.create()
  37.             endmethod
  38.         endmodule
  39.     endif
  40.  
  41.     private struct D extends array
  42.         static if LIBRARY_Table then
  43.             static Table data
  44.            
  45.             implement Init
  46.         else
  47.             static hashtable data = InitHashtable()
  48.         endif
  49.     endstruct
  50.    
  51.     function GetStringById takes integer i returns string
  52.         return strings[i]
  53.     endfunction
  54.    
  55.     function GetStringId takes string t returns integer
  56.         static if LIBRARY_Table then
  57.             return D.data[StringHash(t)]
  58.         else
  59.             return LoadInteger(D.data, StringHash(t), 0)
  60.         endif
  61.     endfunction
  62.    
  63.     function IndexString takes string t returns integer
  64.         local integer i = StringHash(t)
  65.        
  66.         static if LIBRARY_Table then
  67.             if D.data[i] == 0 then
  68.                 set count = count + 1
  69.                
  70.                 set D.data[i] = count
  71.  
  72.                 set strings[count] = t
  73.                 return count
  74.             endif
  75.  
  76.             return D.data[i]
  77.         else
  78.             if LoadInteger(D.data, i, 0) == 0 then
  79.                 set count = count + 1
  80.  
  81.                 call SaveInteger(D.data, i, 0, count)
  82.  
  83.                 set strings[count] = t
  84.                 return count
  85.             endif
  86.  
  87.             return LoadInteger(D.data, i, 0)
  88.         endif
  89.     endfunction
  90.    
  91. endlibrary


I think it's quite nifty.

Feel free to comment.
« Last Edit: December 18, 2017, 06:24:25 AM by moyack »

  • Site Owner
  • Administrator
  • Starter - level 4
  • *
  • Posts: 951
  • WC3 Models: 59
  • WC3 Tutorials: 13
  • WC3 Tools: 11
  • WC3 Maps: 12
  • WC3 Skins: 6
  • WC3 Icons: 2
  • WC3 Spells: 8
  • 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: 12216
Last post March 13, 2013, 06:52:16 PM
by rvonsonsnadtz
[Snippet] Single Player Detector

Started by moyackBoard Codes & Snippets

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

Started by moyackBoard Codes & Snippets

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

Started by PurgeandfireBoard Codes & Snippets

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

Started by moyackBoard Codes & Snippets

Replies: 0
Views: 7953
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!!