FieldLookup.hx 3.6 KB

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