Table.hx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 lua;
  23. /**
  24. This library provides generic functions for table manipulation.
  25. **/
  26. @:native("_G.table")
  27. extern class Table<A, B> implements ArrayAccess<B> implements Dynamic<B> {
  28. @:pure public static function create<A, B>(?arr:Array<B>, ?hsh:Dynamic):Table<A, B>;
  29. public inline static function fromArray<T>(arr:Array<T>):Table<Int, T> {
  30. var ret = Table.create();
  31. for (idx in 0...arr.length) {
  32. ret[idx + 1] = arr[idx];
  33. }
  34. return ret;
  35. }
  36. public inline static function fromMap<A, B>(map:Map<A, B>):Table<A, B> {
  37. var ret = Table.create();
  38. for (k in map.keys()) {
  39. ret[untyped k] = map.get(k);
  40. }
  41. return ret;
  42. }
  43. public inline static function fromDynamic<A, B>(dyn:Dynamic):Table<A, B> {
  44. var ret = Table.create();
  45. for (f in Reflect.fields(dyn)) {
  46. ret[untyped f] = Reflect.field(dyn, f);
  47. }
  48. return ret;
  49. }
  50. public inline static function toMap<A,B>(tbl : Table<A,B>) : Map<A,B> {
  51. var res = new haxe.ds.ObjectMap();
  52. foreach(tbl , (k:A,v:B) -> {
  53. res.set(k,v);
  54. });
  55. return cast res;
  56. }
  57. public inline static function toArray<T>(tbl : Table<Int,T>) : Array<T> {
  58. var res = [];
  59. foreachi(tbl, (k:Int, v:T, i:Int) ->{
  60. res.push(v);
  61. });
  62. return res;
  63. }
  64. @:overload(function<A, B>(table:Table<A, B>):Void {})
  65. public static function concat<A, B>(table:Table<A, B>, ?sep:String, ?i:Int, ?j:Int):String;
  66. public static function foreach<A, B>(table:Table<A, B>, f:A->B->Void):Void;
  67. public static function foreachi<A, B>(table:Table<A, B>, f:A->B->Int->Void):Void;
  68. public static function sort<A, B>(table:Table<A, B>, ?order:A->A->Bool):Void;
  69. @:overload(function<B>(table:Table<Int, B>, value:B):Void {})
  70. public static function insert<B>(table:Table<Int, B>, pos:Int, value:B):Void;
  71. @:overload(function<B>(table:Table<Int, B>):Void {})
  72. public static function remove<B>(table:Table<Int, B>, ?pos:Int):Void;
  73. #if (lua_ver >= 5.2)
  74. public static function pack<T>(args:haxe.extern.Rest<T>):Table<Int, T>;
  75. public static function unpack<Int, V>(args:lua.Table<Int, V>, ?min:Int, ?max:Int):Dynamic;
  76. #end
  77. }
  78. typedef AnyTable = Table<Dynamic, Dynamic>;