Table.hx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // TODO: use an abstract here?
  27. @:native("_G.table")
  28. extern class Table<A, B> implements ArrayAccess<B> implements Dynamic<B> {
  29. @:pure public static function create<A, B>(?arr:Array<B>, ?hsh:Dynamic):Table<A, B>;
  30. public inline static function fromArray<T>(arr:Array<T>):Table<Int, T> {
  31. var ret = Table.create();
  32. for (idx in 0...arr.length) {
  33. ret[idx + 1] = arr[idx];
  34. }
  35. return ret;
  36. }
  37. public inline static function fromMap<A, B>(map:Map<A, B>):Table<A, B> {
  38. var ret = Table.create();
  39. for (k in map.keys()) {
  40. ret[untyped k] = map.get(k);
  41. }
  42. return ret;
  43. }
  44. public inline static function fromDynamic<A, B>(dyn:Dynamic):Table<A, B> {
  45. var ret = Table.create();
  46. for (f in Reflect.fields(dyn)) {
  47. ret[untyped f] = Reflect.field(dyn, f);
  48. }
  49. return ret;
  50. }
  51. @:overload(function<A, B>(table:Table<A, B>):Void {})
  52. public static function concat<A, B>(table:Table<A, B>, ?sep:String, ?i:Int, ?j:Int):String;
  53. public static function foreach<A, B>(table:Table<A, B>, f:A->B->Void):Void;
  54. public static function foreachi<A, B>(table:Table<A, B>, f:A->B->Int->Void):Void;
  55. public static function sort<A, B>(table:Table<A, B>, ?order:A->A->Bool):Void;
  56. @:overload(function<B>(table:Table<Int, B>, value:B):Void {})
  57. public static function insert<B>(table:Table<Int, B>, pos:Int, value:B):Void;
  58. @:overload(function<B>(table:Table<Int, B>):Void {})
  59. public static function remove<B>(table:Table<Int, B>, ?pos:Int):Void;
  60. #if (lua_ver >= 5.2)
  61. public static function pack<T>(args:haxe.extern.Rest<T>):Table<Int, T>;
  62. public static function unpack<Int, V>(args:lua.Table<Int, V>, ?min:Int, ?max:Int):Dynamic;
  63. #end
  64. }
  65. typedef AnyTable = Table<Dynamic, Dynamic>;