Table.hx 3.4 KB

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