Array.hx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. @:coreApi
  23. class Array<T> {
  24. public var length(default,null) : Int;
  25. public function new() : Void {
  26. lua.Boot.defArray(this,0);
  27. }
  28. public function concat( a : Array<T> ) : Array<T> {
  29. var ret = this.copy();
  30. for (i in a) ret.push(i);
  31. return ret;
  32. }
  33. public function join( sep : String ) : String {
  34. var sb = new StringBuf();
  35. var first = true;
  36. for (i in iterator()){
  37. if (first) first = false;
  38. else sb.add(sep);
  39. sb.add(Std.string(i));
  40. }
  41. return sb.toString();
  42. }
  43. public function pop() : Null<T> {
  44. return this.length == 0 ? null : this[this.length-- -1];
  45. }
  46. public function push(x : T) : Int {
  47. this[this.length++] = x;
  48. return this.length;
  49. }
  50. public function reverse() : Void {
  51. var tmp:T;
  52. var i = 0;
  53. while(i < Std.int(this.length/2)){
  54. tmp = this[i];
  55. this[i] = this[this.length-i-1];
  56. this[this.length-i-1] = tmp;
  57. i++;
  58. }
  59. }
  60. public function shift() : Null<T> return null;
  61. public function slice( pos : Int, ?end : Int ) : Array<T> return [];
  62. public function sort( f : T -> T -> Int ) : Void return;
  63. public function splice( pos : Int, len : Int ) : Array<T> return [];
  64. public function toString() : String {
  65. var sb = new StringBuf();
  66. sb.add("[");
  67. sb.add(join(", "));
  68. sb.add("]");
  69. return sb.toString();
  70. }
  71. public function unshift( x : T ) : Void return;
  72. public inline function insert( pos : Int, x : T ) : Void {
  73. (untyped this).splice(pos,0,x);
  74. }
  75. public function remove( x : T ) : Bool {
  76. for (i in 0...this.length){
  77. var a = this[i];
  78. if (a == x){
  79. lua.TableTools.remove(cast this, i+1);
  80. this.length-=1;
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. public function indexOf( x : T, ?fromIndex:Int ) : Int return 1;
  87. public function lastIndexOf( x : T, ?fromIndex:Int ) : Int return 1;
  88. public inline function copy() : Array<T> {
  89. return [for (i in this) i];
  90. }
  91. public function map<S>(f:T->S):Array<S> {
  92. return [for (i in this) f(i)];
  93. }
  94. public function filter(f:T->Bool):Array<T> {
  95. return [for (i in this) if (f(i)) i];
  96. }
  97. public inline function iterator() : Iterator<T> {
  98. var cur_length = 0;
  99. return {
  100. hasNext : function() return cur_length < length,
  101. next : function() return untyped this[cur_length++]
  102. }
  103. }
  104. }