ArrayImpl.hx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. class ArrayImpl {
  27. public static inline function get_length <T>(x:Array<T>):Int return python.lib.Builtin.len(x);
  28. public static inline function concat<T>( a1:Array<T>, a2 : Array<T>) : Array<T>
  29. {
  30. return untyped (untyped a1) + (untyped a2);
  31. }
  32. public static inline function copy<T>(x:Array<T>) : Array<T>
  33. {
  34. return Builtin.list(x);
  35. }
  36. @:keep public static inline function iterator<T>(x:Array<T>) : Iterator<T>
  37. {
  38. return python.Lib.toHaxeIterator(x.__iter__());
  39. }
  40. //public static function insert( pos : Int, x : T ) : Void;
  41. public static function indexOf<T>(a:Array<T>, x : T, ?fromIndex:Int) : Int {
  42. var l =
  43. if (fromIndex == null) 0
  44. else if (fromIndex < 0) a.length + fromIndex
  45. else fromIndex;
  46. if (l < 0) l = 0;
  47. for (i in l...a.length) {
  48. if (a[i] == x) return i;
  49. }
  50. return -1;
  51. }
  52. public static function lastIndexOf<T>(a:Array<T>, x : T, ?fromIndex:Int) : Int {
  53. var l =
  54. if (fromIndex == null) a.length
  55. else if (fromIndex < 0) a.length + fromIndex + 1
  56. else fromIndex+1;
  57. if (l > a.length) l = a.length;
  58. while (--l > -1) {
  59. if (a[l] == x) return l;
  60. }
  61. return -1;
  62. }
  63. public static inline function join<T>(x:Array<T>, sep : String ) : String {
  64. return untyped sep.join(x.map(Std.string));
  65. }
  66. public static inline function toString<T>(x:Array<T>) : String {
  67. return "[" + x.join(",") + "]";
  68. }
  69. public static inline function pop<T>(x:Array<T>) : Null<T> {
  70. return if (x.length == 0) null else untyped __field__(x, "pop")();
  71. }
  72. public static inline function push<T>(x:Array<T>, e:T) : Int {
  73. untyped x.append(e);
  74. return get_length(x);
  75. }
  76. public static inline function unshift<T>(x:Array<T>,e : T) : Void {
  77. return x.insert(0,e);
  78. }
  79. @:keep public static function remove<T>(x:Array<T>,e : T) : Bool {
  80. try {
  81. untyped __field__(x, "remove")(e);
  82. return true;
  83. } catch (e:Dynamic) {
  84. return false;
  85. }
  86. }
  87. // public static function reverse<T>(x:Array<T>) : Void;
  88. public static inline function shift<T>(x:Array<T>) : Null<T> {
  89. if (x.length == 0) return null;
  90. return untyped __field__(x, "pop")(0);
  91. }
  92. public static inline function slice<T>(x:Array<T>, pos : Int, ?end : Int ) : Array<T> {
  93. return untyped __python_array_get__(x, pos, end);
  94. }
  95. public static inline function sort<T>(x:Array<T>, f:T->T->Int) : Void {
  96. return untyped __field__(x, "sort")( (untyped __named_arg__)("key", python.lib.FuncTools.cmp_to_key(f)));
  97. }
  98. /*
  99. b = [i0, i1, i3, i0, i2];
  100. a = b.splice( -2, 2);
  101. b == [i0, i1, i3];
  102. trace(a);
  103. a == [i0, i2];
  104. */
  105. public static inline function splice<T>(x:Array<T>, pos : Int, len : Int ) : Array<T> {
  106. if (pos < 0) pos = x.length+pos;
  107. if (pos < 0) pos = 0;
  108. var res = untyped __python_array_get__(x, pos, pos+len);
  109. untyped __python_del__(untyped __python_array_get__(x, pos, pos+len));
  110. return res;
  111. }
  112. @:keep public static inline function map<S,T>(x:Array<T>, f : T -> S ) : Array<S> {
  113. return Builtin.list(Builtin.map(f,x));
  114. }
  115. @:keep public static inline function filter<T>(x:Array<T>, f : T -> Bool ) : Array<T> {
  116. return Builtin.list(Builtin.filter(f, x));
  117. }
  118. @:keep private static inline function __get<T>(x:Array<T>, idx:Int):T
  119. {
  120. var _hx_a = x;
  121. if (idx >= _hx_a.length || idx < 0)
  122. return null;
  123. else
  124. return x[idx];
  125. }
  126. @:keep private static inline function __set<T>(x:Array<T>, idx:Int, v:T):T
  127. {
  128. var _hx_a = x;
  129. _hx_a[idx] = v;
  130. return v;
  131. }
  132. @:keep private static inline function __unsafe_get<T>(x:Array<T>,idx:Int):T
  133. {
  134. return x[idx];
  135. }
  136. @:keep private static inline function __unsafe_set<T>(x:Array<T>,idx:Int, val:T):T
  137. {
  138. x[idx] = val;
  139. return val;
  140. }
  141. }