PageRenderTime 23ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/std/haxe/ds/Vector.hx

https://bitbucket.org/kodetech/haxe_bin_lfs
Haxe | 336 lines | 193 code | 16 blank | 127 comment | 23 complexity | 153753306f90a6377dcde4a618e2f77e MD5 | raw file
Possible License(s): AGPL-3.0, MPL-2.0-no-copyleft-exception
  1. /*
  2. * Copyright (C)2005-2017 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 haxe.ds;
  23. #if cpp
  24. using cpp.NativeArray;
  25. #end
  26. private typedef VectorData<T> = #if flash10
  27. flash.Vector<T>
  28. #elseif neko
  29. neko.NativeArray<T>
  30. #elseif cs
  31. cs.NativeArray<T>
  32. #elseif java
  33. java.NativeArray<T>
  34. #elseif lua
  35. lua.Table<Int,T>
  36. #else
  37. Array<T>
  38. #end
  39. /**
  40. A Vector is a storage of fixed size. It can be faster than Array on some
  41. targets, and is never slower.
  42. @see https://haxe.org/manual/std-vector.html
  43. **/
  44. abstract Vector<T>(VectorData<T>) {
  45. /**
  46. Creates a new Vector of length `length`.
  47. Initially `this` Vector contains `length` neutral elements:
  48. - always null on dynamic targets
  49. - 0, 0.0 or false for Int, Float and Bool respectively on static targets
  50. - null for other types on static targets
  51. If `length` is less than or equal to 0, the result is unspecified.
  52. **/
  53. public inline function new(length:Int) {
  54. #if flash10
  55. this = new flash.Vector<T>(length, true);
  56. #elseif neko
  57. this = untyped __dollar__amake(length);
  58. #elseif js
  59. this = untyped __new__(Array, length);
  60. #elseif cs
  61. this = new cs.NativeArray(length);
  62. #elseif java
  63. this = new java.NativeArray(length);
  64. #elseif cpp
  65. this = NativeArray.create(length);
  66. #elseif python
  67. this = python.Syntax.pythonCode("[{0}]*{1}", null, length);
  68. #elseif lua
  69. this = untyped __lua_table__({length:length});
  70. #else
  71. this = [];
  72. untyped this.length = length;
  73. #end
  74. }
  75. /**
  76. Returns the value at index `index`.
  77. If `index` is negative or exceeds `this.length`, the result is
  78. unspecified.
  79. **/
  80. @:op([]) public inline function get(index:Int):T {
  81. #if cpp
  82. return this.unsafeGet(index);
  83. #elseif python
  84. return python.internal.ArrayImpl.unsafeGet(this, index);
  85. #else
  86. return this[index];
  87. #end
  88. }
  89. /**
  90. Sets the value at index `index` to `val`.
  91. If `index` is negative or exceeds `this.length`, the result is
  92. unspecified.
  93. **/
  94. @:op([]) public inline function set(index:Int, val:T):T {
  95. #if cpp
  96. return this.unsafeSet(index,val);
  97. #elseif python
  98. return python.internal.ArrayImpl.unsafeSet(this, index, val);
  99. #else
  100. return this[index] = val;
  101. #end
  102. }
  103. /**
  104. Returns the length of `this` Vector.
  105. **/
  106. public var length(get, never):Int;
  107. inline function get_length():Int {
  108. #if neko
  109. return untyped __dollar__asize(this);
  110. #elseif cs
  111. return this.Length;
  112. #elseif java
  113. return this.length;
  114. #elseif python
  115. return this.length;
  116. #else
  117. return untyped this.length;
  118. #end
  119. }
  120. /**
  121. Copies `length` of elements from `src` Vector, beginning at `srcPos` to
  122. `dest` Vector, beginning at `destPos`
  123. The results are unspecified if `length` results in out-of-bounds access,
  124. or if `src` or `dest` are null
  125. **/
  126. public static #if (cs || java || neko || cpp) inline #end function blit<T>(src:Vector<T>, srcPos:Int, dest:Vector<T>, destPos:Int, len:Int):Void
  127. {
  128. #if neko
  129. untyped __dollar__ablit(dest,destPos,src,srcPos,len);
  130. #elseif java
  131. java.lang.System.arraycopy(src, srcPos, dest, destPos, len);
  132. #elseif cs
  133. cs.system.Array.Copy(cast src, srcPos,cast dest, destPos, len);
  134. #elseif cpp
  135. dest.toData().blit(destPos,src.toData(), srcPos,len);
  136. #else
  137. if (src == dest) {
  138. if (srcPos < destPos) {
  139. var i = srcPos + len;
  140. var j = destPos + len;
  141. for (k in 0...len) {
  142. i--;
  143. j--;
  144. src[j] = src[i];
  145. }
  146. } else if (srcPos > destPos) {
  147. var i = srcPos;
  148. var j = destPos;
  149. for (k in 0...len) {
  150. src[j] = src[i];
  151. i++;
  152. j++;
  153. }
  154. }
  155. } else {
  156. for (i in 0...len) {
  157. dest[destPos + i] = src[srcPos + i];
  158. }
  159. }
  160. #end
  161. }
  162. /**
  163. Creates a new Array, copy the content from the Vector to it, and returns it.
  164. **/
  165. public #if (flash || cpp || js || java) inline #end function toArray():Array<T> {
  166. #if cpp
  167. return this.copy();
  168. #elseif python
  169. return this.copy();
  170. #elseif js
  171. return this.slice(0);
  172. #else
  173. var a = new Array();
  174. var len = length;
  175. #if (neko)
  176. // prealloc good size
  177. if( len > 0 ) a[len - 1] = get(0);
  178. #end
  179. for( i in 0...len )
  180. a[i] = get(i);
  181. return a;
  182. #end
  183. }
  184. /**
  185. Extracts the data of `this` Vector.
  186. This returns the internal representation type.
  187. **/
  188. public inline function toData():VectorData<T>
  189. return cast this;
  190. /**
  191. Initializes a new Vector from `data`.
  192. Since `data` is the internal representation of Vector, this is a no-op.
  193. If `data` is null, the corresponding Vector is also `null`.
  194. **/
  195. static public inline function fromData<T>(data:VectorData<T>):Vector<T>
  196. return cast data;
  197. /**
  198. Creates a new Vector by copying the elements of `array`.
  199. This always creates a copy, even on platforms where the internal
  200. representation is Array.
  201. The elements are not copied and retain their identity, so
  202. `a[i] == Vector.fromArrayCopy(a).get(i)` is true for any valid i.
  203. If `array` is null, the result is unspecified.
  204. **/
  205. #if as3 @:extern #end
  206. static public inline function fromArrayCopy<T>(array:Array<T>):Vector<T> {
  207. #if python
  208. return cast array.copy();
  209. #elseif flash10
  210. return fromData(flash.Vector.ofArray(array));
  211. #elseif java
  212. return fromData(java.Lib.nativeArray(array,false));
  213. #elseif cs
  214. return fromData(cs.Lib.nativeArray(array,false));
  215. #elseif cpp
  216. return cast array.copy();
  217. #elseif js
  218. return fromData(array.slice(0));
  219. #else
  220. // TODO: Optimize this for others?
  221. var vec = new Vector<T>(array.length);
  222. for (i in 0...array.length)
  223. vec.set(i, array[i]);
  224. return vec;
  225. #end
  226. }
  227. /**
  228. Returns a shallow copy of `this` Vector.
  229. The elements are not copied and retain their identity, so
  230. `a[i] == a.copy()[i]` is true for any valid `i`. However,
  231. `a == a.copy()` is always false.
  232. **/
  233. #if cs @:extern #end public inline function copy<T>():Vector<T> {
  234. var r = new Vector<T>(length);
  235. Vector.blit(cast this, 0, r, 0, length);
  236. return r;
  237. }
  238. /**
  239. Returns a string representation of `this` Vector, with `sep` separating
  240. each element.
  241. The result of this operation is equal to `Std.string(this[0]) + sep +
  242. Std.string(this[1]) + sep + ... + sep + Std.string(this[this.length-1])`
  243. If `this` Vector has length 0, the result is the empty String `""`.
  244. If `this` has exactly one element, the result is equal to a call to
  245. `Std.string(this[0])`.
  246. If `sep` is null, the result is unspecified.
  247. **/
  248. #if cs @:extern #end public inline function join<T>(sep:String):String {
  249. #if (flash10||cpp)
  250. return this.join(sep);
  251. #else
  252. var b = new StringBuf();
  253. var i = 0;
  254. var len = length;
  255. for(i in 0...len) {
  256. b.add( Std.string(get(i)) );
  257. if(i < len-1) {
  258. b.add(sep);
  259. }
  260. }
  261. return b.toString();
  262. #end
  263. }
  264. /**
  265. Creates a new Vector by applying function `f` to all elements of `this`.
  266. The order of elements is preserved.
  267. If `f` is null, the result is unspecified.
  268. **/
  269. #if cs @:extern #end public inline function map<S>(f:T->S):Vector<S> {
  270. var length = length;
  271. var r = new Vector<S>(length);
  272. var i = 0;
  273. var len = length;
  274. for(i in 0...len) {
  275. var v = f(get(i));
  276. r.set(i, v);
  277. }
  278. return r;
  279. }
  280. /**
  281. Sorts `this` Vector according to the comparison function `f`, where
  282. `f(x,y)` returns 0 if x == y, a positive Int if x > y and a
  283. negative Int if x < y.
  284. This operation modifies `this` Vector in place.
  285. The sort operation is not guaranteed to be stable, which means that the
  286. order of equal elements may not be retained.
  287. If `f` is null, the result is unspecified.
  288. **/
  289. public inline function sort<T>(f:T->T->Int):Void {
  290. #if (neko || cs || java)
  291. throw "not yet supported";
  292. #elseif lua
  293. haxe.ds.ArraySort.sort(cast this, f);
  294. #else
  295. this.sort(f);
  296. #end
  297. }
  298. }