Vector.hx 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 flash;
  23. /**
  24. The Vector class is very similar to Array but is only supported by the Flash Player 10+
  25. **/
  26. @:require(flash10) extern class Vector<T> implements ArrayAccess<T> {
  27. var length:Int;
  28. var fixed:Bool;
  29. function new(?length:UInt, ?fixed:Bool):Void;
  30. function concat(?a:Vector<T>):Vector<T>;
  31. function join(sep:String):String;
  32. function pop():Null<T>;
  33. function push(x:T):Int;
  34. function reverse():Void;
  35. function shift():Null<T>;
  36. function unshift(x:T):Void;
  37. function slice(?pos:Int, ?end:Int):Vector<T>;
  38. function sort(f:T->T->Int):Void;
  39. function splice(pos:Int, len:Int):Vector<T>;
  40. function toString():String;
  41. function indexOf(x:T, ?from:Int):Int;
  42. function lastIndexOf(x:T, ?from:Int):Int;
  43. #if flash19
  44. function insertAt(index:Int, element:T):Void;
  45. #else
  46. inline function insertAt(index:Int, element:T):Void {
  47. (cast this).splice(index, 0, element);
  48. }
  49. #end
  50. @:require(flash19) function removeAt(index:Int):T;
  51. inline static function ofArray<T>(v:Array<T>):Vector<T> {
  52. return untyped __vector__(v);
  53. }
  54. inline static function convert<T, U>(v:Vector<T>):Vector<U> {
  55. return untyped __vector__(v);
  56. }
  57. /**
  58. Get a run-time value referencing the `Vector` class with concrete type parameters.
  59. Normally in Haxe, for most of the types, type parameters are eliminated at run-time,
  60. so there is no way to check if a value is of a type with specific type parameters.
  61. However, on the Flash target, the `flash.Vector<T>` values carry type parameter
  62. information at run-time all the type-checks (such as `Std.isOfType` and `Std.downcast`) on them
  63. must be done using a `Class<T>` value that also carries the type parameters. However,
  64. Haxe syntax does not allow creating such values and this function exists to mitigate
  65. this limitation.
  66. It should be used as such:
  67. ```haxe
  68. var specificVectorType:Class<Vector<Int>> = Vector.typeReference();
  69. trace(Std.isOfType(vec, specificVectorType));
  70. ```
  71. or using the type-check syntax:
  72. ```haxe
  73. trace(Std.isOfType(vec, (Vector.typeReference() : Class<Vector<Int>>)));
  74. ```
  75. It's also helpful when working with native Flash libraries, that receive Class instances:
  76. ```haxe
  77. new Signal((Vector.typeReference() : Class<Vector<Int>>));
  78. ```
  79. **/
  80. inline static function typeReference<T>():Class<Vector<T>> {
  81. return untyped __vector__();
  82. }
  83. }