ArrayImpl.hx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 python.internal;
  23. import python.lib.Functools;
  24. @:allow(Array)
  25. class ArrayImpl {
  26. @:ifFeature("dynamic_read.length", "anon_optional_read.length", "python.internal.ArrayImpl.length")
  27. public static inline function get_length<T>(x:Array<T>):Int
  28. return UBuiltins.len(x);
  29. @:ifFeature("dynamic_read.concat", "anon_optional_read.concat", "python.internal.ArrayImpl.concat")
  30. public static inline function concat<T>(a1:Array<T>, a2:Array<T>):Array<T> {
  31. return Syntax.binop(a1, "+", a2);
  32. }
  33. @:ifFeature("dynamic_read.copy", "anon_optional_read.copy", "python.internal.ArrayImpl.copy")
  34. public static inline function copy<T>(x:Array<T>):Array<T> {
  35. return UBuiltins.list(x);
  36. }
  37. @:ifFeature("dynamic_read.iterator", "anon_optional_read.iterator", "python.internal.ArrayImpl.iterator")
  38. public static inline function iterator<T>(x:Array<T>):Iterator<T> {
  39. return new HaxeIterator(Syntax.callField(x, "__iter__"));
  40. }
  41. @:ifFeature("dynamic_read.keyValueIterator", "anon_optional_read.keyValueIterator", "python.internal.ArrayImpl.keyValueIterator")
  42. public static inline function keyValueIterator<T>(x:Array<T>) : KeyValueIterator<Int, T> {
  43. return new haxe.iterators.ArrayKeyValueIterator(x);
  44. }
  45. @:ifFeature("dynamic_read.indexOf", "anon_optional_read.indexOf", "python.internal.ArrayImpl.indexOf")
  46. public static function indexOf<T>(a:Array<T>, x:T, ?fromIndex:Int):Int {
  47. var len = a.length;
  48. var l = if (fromIndex == null) 0 else if (fromIndex < 0) len + fromIndex else fromIndex;
  49. if (l < 0)
  50. l = 0;
  51. for (i in l...len) {
  52. if (unsafeGet(a, i) == x)
  53. return i;
  54. }
  55. return -1;
  56. }
  57. @:ifFeature("dynamic_read.lastIndexOf", "anon_optional_read.lastIndexOf", "python.internal.ArrayImpl.lastIndexOf")
  58. public static function lastIndexOf<T>(a:Array<T>, x:T, ?fromIndex:Int):Int {
  59. var len = a.length;
  60. var l = if (fromIndex == null) len else if (fromIndex < 0) len + fromIndex + 1 else fromIndex + 1;
  61. if (l > len)
  62. l = len;
  63. while (--l > -1) {
  64. if (unsafeGet(a, l) == x)
  65. return l;
  66. }
  67. return -1;
  68. }
  69. @:access(python.Boot)
  70. @:ifFeature("dynamic_read.join", "anon_optional_read.join", "python.internal.ArrayImpl.join")
  71. public static inline function join<T>(x:Array<T>, sep:String):String {
  72. return Boot.arrayJoin(x, sep);
  73. }
  74. @:ifFeature("dynamic_read.toString", "anon_optional_read.toString", "python.internal.ArrayImpl.toString")
  75. public static inline function toString<T>(x:Array<T>):String {
  76. return "[" + join(x, ",") + "]";
  77. }
  78. @:ifFeature("dynamic_read.pop", "anon_optional_read.pop", "python.internal.ArrayImpl.pop")
  79. public static inline function pop<T>(x:Array<T>):Null<T> {
  80. return if (x.length == 0) null else Syntax.callField(x, "pop");
  81. }
  82. @:ifFeature("dynamic_read.push", "anon_optional_read.push", "python.internal.ArrayImpl.push")
  83. public static inline function push<T>(x:Array<T>, e:T):Int {
  84. Syntax.callField(x, "append", e);
  85. return x.length;
  86. }
  87. @:ifFeature("dynamic_read.unshift", "anon_optional_read.unshift", "python.internal.ArrayImpl.unshift")
  88. public static inline function unshift<T>(x:Array<T>, e:T):Void {
  89. x.insert(0, e);
  90. }
  91. @:ifFeature("dynamic_read.remove", "anon_optional_read.remove", "python.internal.ArrayImpl.remove")
  92. public static function remove<T>(x:Array<T>, e:T):Bool {
  93. try {
  94. Syntax.callField(x, "remove", e);
  95. return true;
  96. } catch (e:Dynamic) {
  97. return false;
  98. }
  99. }
  100. @:ifFeature("dynamic_read.contains", "anon_optional_read.contains", "python.internal.ArrayImpl.contains")
  101. public static inline function contains<T>(x:Array<T>,e : T) : Bool {
  102. return Syntax.isIn(e, x);
  103. }
  104. @:ifFeature("dynamic_read.shift", "anon_optional_read.shift", "python.internal.ArrayImpl.shift")
  105. public static inline function shift<T>(x:Array<T>):Null<T> {
  106. if (x.length == 0)
  107. return null;
  108. return Syntax.callField(x, "pop", 0);
  109. }
  110. @:ifFeature("dynamic_read.slice", "anon_optional_read.slice", "python.internal.ArrayImpl.slice")
  111. public static inline function slice<T>(x:Array<T>, pos:Int, ?end:Int):Array<T> {
  112. return Syntax.arrayAccess(x, pos, end);
  113. }
  114. @:ifFeature("dynamic_read.sort", "anon_optional_read.sort", "python.internal.ArrayImpl.sort")
  115. public static inline function sort<T>(x:Array<T>, f:T->T->Int):Void {
  116. Syntax.callNamedUntyped(Syntax.field(x, "sort"), {key: Functools.cmp_to_key(f)});
  117. }
  118. @:ifFeature("dynamic_read.splice", "anon_optional_read.splice", "python.internal.ArrayImpl.splice")
  119. public static inline function splice<T>(x:Array<T>, pos:Int, len:Int):Array<T> {
  120. if (pos < 0)
  121. pos = x.length + pos;
  122. if (pos < 0)
  123. pos = 0;
  124. var res = Syntax.arrayAccess(x, pos, pos + len);
  125. Syntax.delete((Syntax.arrayAccess(x, pos, pos + len)));
  126. return res;
  127. }
  128. @:ifFeature("dynamic_read.map", "anon_optional_read.map", "python.internal.ArrayImpl.map")
  129. public static inline function map<S, T>(x:Array<T>, f:T->S):Array<S> {
  130. return UBuiltins.list(UBuiltins.map(f, cast x));
  131. }
  132. @:ifFeature("dynamic_read.filter", "anon_optional_read.filter", "python.internal.ArrayImpl.filter")
  133. public static inline function filter<T>(x:Array<T>, f:T->Bool):Array<T> {
  134. return UBuiltins.list(UBuiltins.filter(f, cast x));
  135. }
  136. @:ifFeature("dynamic_read.insert", "anon_optional_read.insert", "python.internal.ArrayImpl.insert")
  137. public static inline function insert<T>(a:Array<T>, pos:Int, x:T):Void {
  138. Syntax.callField(a, "insert", pos, x);
  139. }
  140. @:ifFeature("dynamic_read.reverse", "anon_optional_read.reverse", "python.internal.ArrayImpl.reverse")
  141. public static inline function reverse<T>(a:Array<T>):Void {
  142. Syntax.callField(a, "reverse");
  143. }
  144. @:ifFeature("array_read")
  145. private static inline function _get<T>(x:Array<T>, idx:Int):T {
  146. return if (idx > -1 && idx < x.length) unsafeGet(x, idx) else null;
  147. }
  148. @:ifFeature("array_write")
  149. private static inline function _set<T>(x:Array<T>, idx:Int, v:T):T {
  150. var l = x.length;
  151. while (l < idx) {
  152. x.push(null);
  153. l += 1;
  154. }
  155. if (l == idx) {
  156. x.push(v);
  157. } else {
  158. Syntax.assign(Syntax.arrayAccess(x, idx), v);
  159. }
  160. return v;
  161. }
  162. public static inline function unsafeGet<T>(x:Array<T>, idx:Int):T {
  163. return Syntax.arrayAccess(x, idx);
  164. }
  165. public static inline function unsafeSet<T>(x:Array<T>, idx:Int, val:T):T {
  166. Syntax.assign(Syntax.arrayAccess(x, idx), val);
  167. return val;
  168. }
  169. public static inline function resize<T>(x:Array<T>, len:Int):Void {
  170. var l = x.length;
  171. if (l < len) {
  172. _set(x, len - 1, null);
  173. } else if (l > len) {
  174. splice(x, len, l - len);
  175. }
  176. }
  177. }