FieldLookup.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. @:native('haxe.lang.FieldLookup')
  23. @:static private class FieldLookup
  24. {
  25. @:private private static var fieldIds:Array<Int>;
  26. @:private private static var fields:Array<String>;
  27. //s cannot be null here
  28. private static inline function doHash(s:String):Int
  29. {
  30. var acc = 0; //alloc_int
  31. for (i in 0...s.length)
  32. {
  33. acc = (( 223 * (acc >> 1) + s.charCodeAt(i) ) << 1);
  34. }
  35. return acc >>> 1; //always positive
  36. }
  37. public static function lookupHash(key:Int):String
  38. {
  39. //start of binary search algorithm
  40. var ids = fieldIds;
  41. var min = 0;
  42. var max = ids.length;
  43. while (min < max)
  44. {
  45. var mid = Std.int(min + (max - min) / 2); //overflow safe
  46. var imid = ids[mid];
  47. if (key < imid)
  48. {
  49. max = mid;
  50. } else if (key > imid) {
  51. min = mid + 1;
  52. } else {
  53. return fields[mid];
  54. }
  55. }
  56. //if not found, it's definately an error
  57. throw "Field not found for hash " + key;
  58. }
  59. public static function hash(s:String):Int
  60. {
  61. if (s == null) return 0;
  62. var key = doHash(s);
  63. //start of binary search algorithm
  64. var ids = fieldIds;
  65. var min = 0;
  66. var max = ids.length;
  67. while (min < max)
  68. {
  69. var mid = Std.int(min + (max - min) / 2); //overflow safe
  70. var imid = ids[mid];
  71. if (key < imid)
  72. {
  73. max = mid;
  74. } else if (key > imid) {
  75. min = mid + 1;
  76. } else {
  77. var field = fields[mid];
  78. if (field != s)
  79. return -(key + 1); //special case
  80. return key;
  81. }
  82. }
  83. //if not found, min holds the value where we should insert the key
  84. ids.insert(min, key);
  85. fields.insert(min, s);
  86. return key;
  87. }
  88. public static function findHash(hash:Int, hashs:Array<Int>):Int
  89. {
  90. var min = 0;
  91. var max = hashs.length;
  92. while (min < max)
  93. {
  94. var mid = Std.int((max + min) / 2); //overflow safe
  95. var imid = hashs[mid];
  96. if (hash < imid)
  97. {
  98. max = mid;
  99. } else if (hash > imid) {
  100. min = mid + 1;
  101. } else {
  102. return min;
  103. }
  104. }
  105. //if not found, return a negative value of where it should be inserted
  106. return ~min;
  107. }
  108. }