ArrayImpl.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package python.internal;
  2. /*
  3. * Copyright (C)2005-2012 Haxe Foundation
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. import python.lib.FuncTools;
  24. import python.lib.Builtin;
  25. @:allow(Array)
  26. @:keep
  27. class ArrayImpl {
  28. public static inline function get_length <T>(x:Array<T>):Int return python.lib.Builtin.len(x);
  29. public static inline function concat<T>( a1:Array<T>, a2 : Array<T>) : Array<T>
  30. {
  31. return untyped (untyped a1) + (untyped a2);
  32. }
  33. public static inline function copy<T>(x:Array<T>) : Array<T>
  34. {
  35. return Builtin.list(x);
  36. }
  37. @:keep public static inline function iterator<T>(x:Array<T>) : Iterator<T>
  38. {
  39. return python.Lib.toHaxeIterator(untyped x.__iter__());
  40. }
  41. //public static function insert( pos : Int, x : T ) : Void;
  42. public static function indexOf<T>(a:Array<T>, x : T, ?fromIndex:Int) : Int {
  43. var l =
  44. if (fromIndex == null) 0
  45. else if (fromIndex < 0) a.length + fromIndex
  46. else fromIndex;
  47. if (l < 0) l = 0;
  48. for (i in l...a.length) {
  49. if (a[i] == x) return i;
  50. }
  51. return -1;
  52. }
  53. public static function lastIndexOf<T>(a:Array<T>, x : T, ?fromIndex:Int) : Int {
  54. var l =
  55. if (fromIndex == null) a.length
  56. else if (fromIndex < 0) a.length + fromIndex + 1
  57. else fromIndex+1;
  58. if (l > a.length) l = a.length;
  59. while (--l > -1) {
  60. if (a[l] == x) return l;
  61. }
  62. return -1;
  63. }
  64. public static inline function join<T>(x:Array<T>, sep : String ) : String {
  65. return untyped sep.join(x.map(Std.string));
  66. }
  67. public static inline function toString<T>(x:Array<T>) : String {
  68. return "[" + x.join(",") + "]";
  69. }
  70. public static inline function pop<T>(x:Array<T>) : Null<T> {
  71. return if (x.length == 0) null else untyped __field__(x, "pop")();
  72. }
  73. public static inline function push<T>(x:Array<T>, e:T) : Int {
  74. untyped x.append(e);
  75. return get_length(x);
  76. }
  77. public static inline function unshift<T>(x:Array<T>,e : T) : Void {
  78. return x.insert(0,e);
  79. }
  80. @:keep public static function remove<T>(x:Array<T>,e : T) : Bool {
  81. try {
  82. untyped __field__(x, "remove")(e);
  83. return true;
  84. } catch (e:Dynamic) {
  85. return false;
  86. }
  87. }
  88. // public static function reverse<T>(x:Array<T>) : Void;
  89. public static inline function shift<T>(x:Array<T>) : Null<T> {
  90. if (x.length == 0) return null;
  91. return untyped __field__(x, "pop")(0);
  92. }
  93. public static inline function slice<T>(x:Array<T>, pos : Int, ?end : Int ) : Array<T> {
  94. return untyped __python_array_get__(x, pos, end);
  95. }
  96. public static inline function sort<T>(x:Array<T>, f:T->T->Int) : Void {
  97. return untyped __field__(x, "sort")( (untyped __named_arg__)("key", python.lib.FuncTools.cmp_to_key(f)));
  98. }
  99. /*
  100. b = [i0, i1, i3, i0, i2];
  101. a = b.splice( -2, 2);
  102. b == [i0, i1, i3];
  103. trace(a);
  104. a == [i0, i2];
  105. */
  106. public static inline function splice<T>(x:Array<T>, pos : Int, len : Int ) : Array<T> {
  107. if (pos < 0) pos = x.length+pos;
  108. if (pos < 0) pos = 0;
  109. var res = untyped __python_array_get__(x, pos, pos+len);
  110. untyped __python_del__(untyped __python_array_get__(x, pos, pos+len));
  111. return res;
  112. }
  113. @:keep public static inline function map<S,T>(x:Array<T>, f : T -> S ) : Array<S> {
  114. return Builtin.list(Builtin.map(f,cast x));
  115. }
  116. @:keep public static inline function filter<T>(x:Array<T>, f : T -> Bool ) : Array<T> {
  117. return Builtin.list(Builtin.filter(f, x));
  118. }
  119. @:keep private static inline function __get<T>(x:Array<T>, idx:Int):T
  120. {
  121. var _hx_a = x;
  122. if (idx >= _hx_a.length || idx < 0)
  123. return null;
  124. else
  125. return x[idx];
  126. }
  127. @:keep private static inline function __set<T>(x:Array<T>, idx:Int, v:T):T
  128. {
  129. var _hx_a = x;
  130. _hx_a[idx] = v;
  131. return v;
  132. }
  133. @:keep private static inline function __unsafe_get<T>(x:Array<T>,idx:Int):T
  134. {
  135. return x[idx];
  136. }
  137. @:keep private static inline function __unsafe_set<T>(x:Array<T>,idx:Int, val:T):T
  138. {
  139. x[idx] = val;
  140. return val;
  141. }
  142. }