FieldLookup.hx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 private 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:Array<Int>):Int
  91. {
  92. var min = 0;
  93. var max = hashs.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. }