FieldLookup.hx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (C)2005-2017 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 java.internal;
  23. import java.lang.System;
  24. @:native('haxe.lang.FieldLookup')
  25. @:keep
  26. @:static private class FieldLookup
  27. {
  28. @:functionCode('
  29. return s.hashCode();
  30. ')
  31. public static function hash(s:String):Int
  32. {
  33. return 0;
  34. }
  35. public static function findHash(hash:String, hashs:java.NativeArray<String>, length:Int):Int
  36. {
  37. var min = 0;
  38. var max = length;
  39. while (min < max)
  40. {
  41. var mid = Std.int((max + min) / 2); //overflow safe
  42. var classify = untyped hash.compareTo(hashs[mid]);
  43. if (classify < 0)
  44. {
  45. max = mid;
  46. } else if (classify > 0) {
  47. min = mid + 1;
  48. } else {
  49. return mid;
  50. }
  51. }
  52. //if not found, return a negative value of where it should be inserted
  53. return ~min;
  54. }
  55. static function removeString(a:java.NativeArray<String>, length:Int, pos:Int) {
  56. System.arraycopy(a, pos + 1, a, pos, length - pos - 1);
  57. a[length - 1] = null;
  58. }
  59. static function removeFloat(a:java.NativeArray<Float>, length:Int, pos:Int) {
  60. System.arraycopy(a, pos + 1, a, pos, length - pos - 1);
  61. a[length - 1] = 0;
  62. }
  63. static function removeDynamic(a:java.NativeArray<Dynamic>, length:Int, pos:Int) {
  64. System.arraycopy(a, pos + 1, a, pos, length - pos - 1);
  65. a[length - 1] = null;
  66. }
  67. @:extern
  68. static inline function __insert<T>(a:java.NativeArray<T>, length:Int, pos:Int, x:T):java.NativeArray<T>
  69. {
  70. var capacity = a.length;
  71. if (pos == length)
  72. {
  73. if (capacity == length)
  74. {
  75. var newarr = new NativeArray((length << 1) + 1);
  76. System.arraycopy(a, 0, newarr, 0, a.length);
  77. a = newarr;
  78. }
  79. }
  80. else if (pos == 0)
  81. {
  82. if (capacity == length)
  83. {
  84. var newarr = new NativeArray((length << 1) + 1);
  85. System.arraycopy(a, 0, newarr, 1, length);
  86. a = newarr;
  87. }
  88. else
  89. {
  90. System.arraycopy(a, 0, a, 1, length);
  91. }
  92. }
  93. else
  94. {
  95. if (capacity == length)
  96. {
  97. var newarr = new NativeArray((length << 1) + 1);
  98. System.arraycopy(a, 0, newarr, 0, pos);
  99. System.arraycopy(a, pos, newarr, pos + 1, length - pos);
  100. a = newarr;
  101. }
  102. else
  103. {
  104. System.arraycopy(a, pos, a, pos + 1, length - pos);
  105. System.arraycopy(a, 0, a, 0, pos);
  106. }
  107. }
  108. a[pos] = x;
  109. return a;
  110. }
  111. static function insertString(a:java.NativeArray<String>, length:Int, pos:Int, x:String):java.NativeArray<String> return __insert(a, length, pos, x);
  112. static function insertFloat(a:java.NativeArray<Float>, length:Int, pos:Int, x:Float):java.NativeArray<Float> return __insert(a, length, pos, x);
  113. static function insertDynamic(a:java.NativeArray<Dynamic>, length:Int, pos:Int, x:Dynamic):java.NativeArray<Dynamic> return __insert(a, length, pos, x);
  114. }