FieldLookup.hx 3.1 KB

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