Y_Less

y_stringhash 1.0

Aug 19th, 2010
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.58 KB | None | 0 0
  1. /*----------------------------------------------------------------------------*-
  2.                     ========================================
  3.                      y_stringhash - Fast string comparisons
  4.                     ========================================
  5. Description:
  6.     Allows you to hash strings at compile time to use them in a switch.
  7. Legal:
  8.     Version: MPL 1.1
  9.    
  10.     The contents of this file are subject to the Mozilla Public License Version
  11.     1.1 (the "License"); you may not use this file except in compliance with
  12.     the License. You may obtain a copy of the License at
  13.     http://www.mozilla.org/MPL/
  14.    
  15.     Software distributed under the License is distributed on an "AS IS" basis,
  16.     WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  17.     for the specific language governing rights and limitations under the
  18.     License.
  19.    
  20.     The Original Code is the SA:MP compiler time string hashing include.
  21.    
  22.     The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  23.     Portions created by the Initial Developer are Copyright (C) 2008
  24.     the Initial Developer. All Rights Reserved.
  25.    
  26.     Special Thanks to:
  27.    
  28.     SA:MP Team past, present and future
  29. Version:
  30.     1.0
  31. Changelog:
  32.     19/08/10:
  33.         First version.
  34. Functions:
  35.     stock:
  36.         Hash - Generate a string hash at run time.
  37. Definitions:
  38.     _H - Generate a string hash at compile time.
  39. -*----------------------------------------------------------------------------*/
  40.  
  41. // The bit that actually does the work.
  42. #define _DOHASH(%0,%1,%2) (_HASH%1(%2)*33+%0)
  43.  
  44. // Space.
  45. #define _HASH(%0) _DOHASH(32,%0)
  46.  
  47. // Numbers.
  48. #define _HASH0(%0) _DOHASH(48,%0)
  49. #define _HASH1(%0) _DOHASH(49,%0)
  50. #define _HASH2(%0) _DOHASH(50,%0)
  51. #define _HASH3(%0) _DOHASH(51,%0)
  52. #define _HASH4(%0) _DOHASH(52,%0)
  53. #define _HASH5(%0) _DOHASH(53,%0)
  54. #define _HASH6(%0) _DOHASH(54,%0)
  55. #define _HASH7(%0) _DOHASH(55,%0)
  56. #define _HASH8(%0) _DOHASH(56,%0)
  57. #define _HASH9(%0) _DOHASH(57,%0)
  58.  
  59. // Upper case letters.
  60. #define _HASHA(%0) _DOHASH(65,%0)
  61. #define _HASHB(%0) _DOHASH(66,%0)
  62. #define _HASHC(%0) _DOHASH(67,%0)
  63. #define _HASHD(%0) _DOHASH(68,%0)
  64. #define _HASHE(%0) _DOHASH(69,%0)
  65. #define _HASHF(%0) _DOHASH(70,%0)
  66. #define _HASHG(%0) _DOHASH(71,%0)
  67. #define _HASHH(%0) _DOHASH(72,%0)
  68. #define _HASHI(%0) _DOHASH(73,%0)
  69. #define _HASHJ(%0) _DOHASH(74,%0)
  70. #define _HASHK(%0) _DOHASH(75,%0)
  71. #define _HASHL(%0) _DOHASH(76,%0)
  72. #define _HASHM(%0) _DOHASH(77,%0)
  73. #define _HASHN(%0) _DOHASH(78,%0)
  74. #define _HASHO(%0) _DOHASH(79,%0)
  75. #define _HASHP(%0) _DOHASH(80,%0)
  76. #define _HASHQ(%0) _DOHASH(81,%0)
  77. #define _HASHR(%0) _DOHASH(82,%0)
  78. #define _HASHS(%0) _DOHASH(83,%0)
  79. #define _HASHT(%0) _DOHASH(84,%0)
  80. #define _HASHU(%0) _DOHASH(85,%0)
  81. #define _HASHV(%0) _DOHASH(86,%0)
  82. #define _HASHW(%0) _DOHASH(87,%0)
  83. #define _HASHX(%0) _DOHASH(88,%0)
  84. #define _HASHY(%0) _DOHASH(89,%0)
  85. #define _HASHZ(%0) _DOHASH(90,%0)
  86.  
  87. // Underscore.
  88. #define _HASH_(%0) _DOHASH(95,%0)
  89.  
  90. // Lower case letters.
  91. #define _HASHa(%0) _DOHASH(97,%0)
  92. #define _HASHb(%0) _DOHASH(98,%0)
  93. #define _HASHc(%0) _DOHASH(99,%0)
  94. #define _HASHd(%0) _DOHASH(100,%0)
  95. #define _HASHe(%0) _DOHASH(101,%0)
  96. #define _HASHf(%0) _DOHASH(102,%0)
  97. #define _HASHg(%0) _DOHASH(103,%0)
  98. #define _HASHh(%0) _DOHASH(104,%0)
  99. #define _HASHi(%0) _DOHASH(105,%0)
  100. #define _HASHj(%0) _DOHASH(106,%0)
  101. #define _HASHk(%0) _DOHASH(107,%0)
  102. #define _HASHl(%0) _DOHASH(108,%0)
  103. #define _HASHm(%0) _DOHASH(109,%0)
  104. #define _HASHn(%0) _DOHASH(110,%0)
  105. #define _HASHo(%0) _DOHASH(111,%0)
  106. #define _HASHp(%0) _DOHASH(112,%0)
  107. #define _HASHq(%0) _DOHASH(113,%0)
  108. #define _HASHr(%0) _DOHASH(114,%0)
  109. #define _HASHs(%0) _DOHASH(115,%0)
  110. #define _HASHt(%0) _DOHASH(116,%0)
  111. #define _HASHu(%0) _DOHASH(117,%0)
  112. #define _HASHv(%0) _DOHASH(118,%0)
  113. #define _HASHw(%0) _DOHASH(119,%0)
  114. #define _HASHx(%0) _DOHASH(120,%0)
  115. #define _HASHy(%0) _DOHASH(121,%0)
  116. #define _HASHz(%0) _DOHASH(122,%0)
  117.  
  118. // String end.
  119. #define _HASH@(%0) -1
  120.  
  121. // Internal call.
  122. #define _REHASH(%0,%1) _HASH%0(%1)
  123.  
  124. // Signify the end with two "@" symbols.
  125. #define HASH(%0) _REHASH(%0,@,@)
  126.  
  127. #define _H(%0) _REHASH(%0,@,@)
  128.  
  129. /*----------------------------------------------------------------------------*-
  130. Function:
  131.     Hash
  132. Params:
  133.     str - The string to hash.
  134. Return:
  135.     The reverse Bernstein hash of the string.
  136. Notes:
  137.     Based on Bernstein hash, but backwards to match the macros.  The only
  138.     characters which can be used in the compile time version of this code are:
  139.     a-z, A-Z, 0-9, _ and space.
  140.  
  141. native Hash(str[]);
  142.  
  143. -*----------------------------------------------------------------------------*/
  144.  
  145. stock Hash(str[])
  146. {
  147.     new
  148.         len = strlen(str),
  149.         hash = -1;
  150.     while (len--)
  151.     {
  152.         hash = hash * 33 + str[len];
  153.     }
  154.     return hash;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment