FieldLookup.hx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package cs.internal;
  23. @:native('haxe.lang.FieldLookup')
  24. @:final @:nativeGen
  25. @:keep @:static class FieldLookup
  26. {
  27. @:private private static var fieldIds:Array<Int>;
  28. @:private private static var fields:Array<String>;
  29. //s cannot be null here
  30. private static inline function doHash(s:String):Int
  31. {
  32. var acc = 0; //alloc_int
  33. for (i in 0...s.length)
  34. {
  35. acc = (( 223 * (acc >> 1) + cast(s[i], Int)) << 1);
  36. }
  37. return acc >>> 1; //always positive
  38. }
  39. public static function lookupHash(key:Int):String
  40. {
  41. //start of binary search algorithm
  42. var ids = fieldIds;
  43. var min = 0;
  44. var max = ids.length;
  45. while (min < max)
  46. {
  47. var mid = min + Std.int((max - min) / 2);
  48. var imid = ids[mid];
  49. if (key < imid)
  50. {
  51. max = mid;
  52. } else if (key > imid) {
  53. min = mid + 1;
  54. } else {
  55. return fields[mid];
  56. }
  57. }
  58. //if not found, it's definately an error
  59. throw "Field not found for hash " + key;
  60. }
  61. public static function hash(s:String):Int
  62. {
  63. if (s == null) return 0;
  64. var key = doHash(s);
  65. //start of binary search algorithm
  66. var ids = fieldIds;
  67. var min = 0;
  68. var max = ids.length;
  69. while (min < max)
  70. {
  71. var mid = Std.int(min + (max - min) / 2); //overflow safe
  72. var imid = ids[mid];
  73. if (key < imid)
  74. {
  75. max = mid;
  76. } else if (key > imid) {
  77. min = mid + 1;
  78. } else {
  79. var field = fields[mid];
  80. if (field != s)
  81. return ~key; //special case
  82. return key;
  83. }
  84. }
  85. //if not found, min holds the value where we should insert the key
  86. ids.insert(min, key);
  87. fields.insert(min, s);
  88. return key;
  89. }
  90. public static function findHash(hash:Int, hashs:cs.NativeArray<Int>, length:Int):Int
  91. {
  92. var min = 0;
  93. var max = length;
  94. while (min < max)
  95. {
  96. var mid = Std.int((max + min) / 2); //overflow safe
  97. var imid = hashs[mid];
  98. if (hash < imid)
  99. {
  100. max = mid;
  101. } else if (hash > imid) {
  102. min = mid + 1;
  103. } else {
  104. return mid;
  105. }
  106. }
  107. //if not found, return a negative value of where it should be inserted
  108. return ~min;
  109. }
  110. #if !erase_generics
  111. static function remove<T>(a:cs.NativeArray<T>, length:Int, pos:Int)
  112. {
  113. cs.system.Array.Copy(a, pos + 1, a, pos, length - pos - 1);
  114. a[length - 1] = null;
  115. }
  116. static function insert<T>(a:cs.Ref<cs.NativeArray<T>>, length:Int, pos:Int, x:T)
  117. {
  118. var capacity = a.Length;
  119. if (pos == length)
  120. {
  121. if (capacity == length)
  122. {
  123. var newarr = new NativeArray((length << 1) + 1);
  124. a.CopyTo(newarr, 0);
  125. a = newarr;
  126. }
  127. }
  128. else if (pos == 0)
  129. {
  130. if (capacity == length)
  131. {
  132. var newarr = new NativeArray((length << 1) + 1);
  133. cs.system.Array.Copy(a, 0, newarr, 1, length);
  134. a = newarr;
  135. }
  136. else
  137. {
  138. cs.system.Array.Copy(a, 0, a, 1, length);
  139. }
  140. }
  141. else
  142. {
  143. if (capacity == length)
  144. {
  145. var newarr = new NativeArray((length << 1) + 1);
  146. cs.system.Array.Copy(a, 0, newarr, 0, pos);
  147. cs.system.Array.Copy(a, pos, newarr, pos + 1, length - pos);
  148. a = newarr;
  149. }
  150. else
  151. {
  152. cs.system.Array.Copy(a, pos, a, pos + 1, length - pos);
  153. cs.system.Array.Copy(a, 0, a, 0, pos);
  154. }
  155. }
  156. a[pos] = x;
  157. }
  158. #else
  159. static function removeInt(a:cs.NativeArray<Int>, length:Int, pos:Int)
  160. {
  161. cs.system.Array.Copy(a, pos + 1, a, pos, length - pos - 1);
  162. a[length - 1] = 0;
  163. }
  164. static function removeFloat(a:cs.NativeArray<Float>, length:Int, pos:Int)
  165. {
  166. cs.system.Array.Copy(a, pos + 1, a, pos, length - pos - 1);
  167. a[length - 1] = 0;
  168. }
  169. static function removeDynamic(a:cs.NativeArray<Dynamic>, length:Int, pos:Int)
  170. {
  171. cs.system.Array.Copy(a, pos + 1, a, pos, length - pos - 1);
  172. a[length - 1] = null;
  173. }
  174. @:extern
  175. static inline function __insert<T>(a:cs.NativeArray<T>, length:Int, pos:Int, x:T):cs.NativeArray<T>
  176. {
  177. var capacity = a.Length;
  178. if (pos == length)
  179. {
  180. if (capacity == length)
  181. {
  182. var newarr = new NativeArray((length << 1) + 1);
  183. a.CopyTo(newarr, 0);
  184. a = newarr;
  185. }
  186. }
  187. else if (pos == 0)
  188. {
  189. if (capacity == length)
  190. {
  191. var newarr = new NativeArray((length << 1) + 1);
  192. cs.system.Array.Copy(a, 0, newarr, 1, length);
  193. a = newarr;
  194. }
  195. else
  196. {
  197. cs.system.Array.Copy(a, 0, a, 1, length);
  198. }
  199. }
  200. else
  201. {
  202. if (capacity == length)
  203. {
  204. var newarr = new NativeArray((length << 1) + 1);
  205. cs.system.Array.Copy(a, 0, newarr, 0, pos);
  206. cs.system.Array.Copy(a, pos, newarr, pos + 1, length - pos);
  207. a = newarr;
  208. }
  209. else
  210. {
  211. cs.system.Array.Copy(a, pos, a, pos + 1, length - pos);
  212. cs.system.Array.Copy(a, 0, a, 0, pos);
  213. }
  214. }
  215. a[pos] = x;
  216. return a;
  217. }
  218. static function insertInt(a:cs.NativeArray<Int>, length:Int, pos:Int, x:Int):cs.NativeArray<Int> return __insert(a, length, pos, x);
  219. static function insertFloat(a:cs.NativeArray<Float>, length:Int, pos:Int, x:Float):cs.NativeArray<Float> return __insert(a, length, pos, x);
  220. static function insertDynamic(a:cs.NativeArray<Dynamic>, length:Int, pos:Int, x:Dynamic):cs.NativeArray<Dynamic> return __insert(a, length, pos, x);
  221. #end
  222. }