ArrayImpl.hx 5.0 KB

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