Rest.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package haxe;
  2. import haxe.iterators.RestIterator;
  3. import haxe.iterators.RestKeyValueIterator;
  4. import java.NativeArray;
  5. import java.lang.System;
  6. import java.lang.Object;
  7. import java.util.Arrays;
  8. import java.StdTypes;
  9. private typedef NativeRest<T> = NativeArray<T>;
  10. @:coreApi
  11. abstract Rest<T>(NativeRest<T>) {
  12. public var length(get,never):Int;
  13. inline function get_length():Int
  14. return this.length;
  15. #if jvm
  16. @:from static public function of<T>(array:Array<T>):Rest<T> {
  17. return new Rest(@:privateAccess array.__a);
  18. }
  19. #else
  20. @:from extern inline static public function of<T>(array:Array<T>):Rest<T> {
  21. var result = createNative(array.length);
  22. var src:NativeArray<Object> = cast @:privateAccess array.__a;
  23. for(i in 0...src.length)
  24. result[i] = cast src[i];
  25. return new Rest(result);
  26. }
  27. #end
  28. @:noDoc
  29. @:generic
  30. static function ofNativePrimitive<TBoxed,TRest>(result:NativeRest<TBoxed>, collection:NativeArray<TRest>):Rest<TRest> {
  31. for(i in 0...collection.length)
  32. result[i] = cast collection[i];
  33. return new Rest(cast result);
  34. }
  35. @:noDoc
  36. @:from static function ofNativeInt(collection:NativeArray<Int>):Rest<Int>
  37. return ofNativePrimitive(new NativeRest<java.lang.Integer>(collection.length), collection);
  38. @:noDoc
  39. @:from static function ofNativeFloat(collection:NativeArray<Float>):Rest<Float>
  40. return ofNativePrimitive(new NativeRest<java.lang.Double>(collection.length), collection);
  41. @:noDoc
  42. @:from static function ofNativeBool(collection:NativeArray<Bool>):Rest<Bool>
  43. return ofNativePrimitive(new NativeRest<java.lang.Boolean>(collection.length), collection);
  44. @:noDoc
  45. @:from static function ofNativeInt8(collection:NativeArray<Int8>):Rest<Int8>
  46. return ofNativePrimitive(new NativeRest<java.lang.Byte>(collection.length), collection);
  47. @:noDoc
  48. @:from static function ofNativeInt16(collection:NativeArray<Int16>):Rest<Int16>
  49. return ofNativePrimitive(new NativeRest<java.lang.Short>(collection.length), collection);
  50. @:noDoc
  51. @:from static function ofNativeChar16(collection:NativeArray<Char16>):Rest<Char16>
  52. return ofNativePrimitive(new NativeRest<java.lang.Character>(collection.length), collection);
  53. @:noDoc
  54. @:from static function ofNativeHaxeInt64(collection:NativeArray<haxe.Int64>):Rest<haxe.Int64>
  55. return ofNativePrimitive(new NativeRest<java.lang.Long>(collection.length), collection);
  56. @:noDoc
  57. @:from static function ofNativeInt64(collection:NativeArray<Int64>):Rest<Int64>
  58. return ofNativePrimitive(new NativeRest<java.lang.Long>(collection.length), collection);
  59. @:noDoc
  60. @:from static function ofNative<T>(collection:NativeArray<T>):Rest<T> {
  61. return new Rest(collection);
  62. }
  63. inline function new(a:NativeRest<T>):Void
  64. this = a;
  65. /**
  66. * Implemented in genjvm (to auto-box primitive types) and genjava
  67. */
  68. static function createNative<T>(length:Int):NativeRest<T>
  69. return new NativeRest<T>(length);
  70. @:arrayAccess inline function get(index:Int):T
  71. return this[index];
  72. @:to public function toArray():Array<T> {
  73. return [for(i in 0...this.length) this[i]];
  74. }
  75. public inline function iterator():RestIterator<T>
  76. return new RestIterator<T>(this);
  77. public inline function keyValueIterator():RestKeyValueIterator<T>
  78. return new RestKeyValueIterator<T>(this);
  79. extern inline public function append(item:T):Rest<T> {
  80. return _append(createNative(this.length + 1), item);
  81. }
  82. function _append(result:NativeRest<T>, item:T):Rest<T> {
  83. System.arraycopy(this, 0, result, 0, this.length);
  84. result[this.length] = cast item;
  85. return new Rest(result);
  86. }
  87. extern inline public function prepend(item:T):Rest<T> {
  88. return _prepend(createNative(this.length + 1), item);
  89. }
  90. function _prepend(result:NativeRest<T>, item:T):Rest<T> {
  91. System.arraycopy(this, 0, result, 1, this.length);
  92. result[0] = cast item;
  93. return new Rest(result);
  94. }
  95. public function toString():String {
  96. return toArray().toString();
  97. }
  98. }