PairTools.hx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package lua;
  2. /**
  3. A set of utility methods for working with the Lua table extern.
  4. **/
  5. class PairTools {
  6. public static function ipairsEach<T>(table:Table<Dynamic,T>, func : Int->T->Void) : Void {
  7. untyped __lua__("for i,v in _G.ipairs(table) do func(i,v) end");
  8. }
  9. public static function pairsEach<A,B>(table:Table<A,B>, func : A->B->Void) : Void {
  10. untyped __lua__("for k,v in _G.pairs(table) do func(k,v) end");
  11. }
  12. public static function ipairsMap<A,B>(table:Table<Dynamic,A>, func : Int->A->B) : Table<Int,B> {
  13. var ret : Table<Int,B> = Table.create();
  14. untyped __lua__( "for i,v in _G.ipairs(table) do ret[i] = func(i,v) end;");
  15. return ret;
  16. }
  17. public static function pairsMap<A,B,C>(table:Table<A,B>, func : A->B->C->C) : Table<A,C> {
  18. var ret : Table<A,C> = Table.create();
  19. untyped __lua__( "for k,v in _G.pairs(table) do ret[k] = func(k,v) end;");
  20. return ret;
  21. }
  22. public static function ipairsFold<A,B>(table:Table<Int,A>, func : Int->A->B->B, seed: B) : B {
  23. untyped __lua__("for i,v in _G.ipairs(table) do seed = func(i,v,seed) end");
  24. return untyped __lua__("seed");
  25. }
  26. public static function pairsFold<A,B,C>(table:Table<A,B>, func : A->B->C->C, seed: C) : C {
  27. untyped __lua__("for k,v in _G.pairs(table) do seed = func(k,v,seed) end");
  28. return untyped __lua__("seed");
  29. }
  30. public static function ipairsConcat<T>(table1:Table<Int,T>, table2:Table<Int,T>){
  31. var ret:Table<Int,T> = Table.create();
  32. ipairsFold(table1, function(a,b,c:Table<Int,T>){ c[a] = b; return c;}, ret);
  33. var size = lua.TableTools.maxn(ret);
  34. ipairsFold(table2, function(a,b,c:Table<Int,T>){ c[a + size] = b; return c;}, ret);
  35. return ret;
  36. }
  37. public static function pairsMerge<A,B>(table1:Table<A,B>, table2:Table<A,B>){
  38. var ret = copy(table1);
  39. pairsEach(table2, function(a,b:B) ret[cast a] = b);
  40. return ret;
  41. }
  42. public static function ipairsExist<T>(table:Table<Int,T>, func: Int->T->Bool) {
  43. untyped __lua__("for k,v in _G.ipairs(table) do if func(k,v) then return true end end");
  44. }
  45. public static function pairsExist<A,B>(table:Table<A,B>, func: A->B->Bool) {
  46. untyped __lua__("for k,v in _G.pairs(table) do if func(k,v) then return true end end");
  47. }
  48. public static function copy<A,B>(table1:Table<A,B>) : Table<A,B> {
  49. var ret : Table<A,B> = Table.create();
  50. untyped __lua__("for k,v in _G.pairs(table1) do ret[k] = v end");
  51. return ret;
  52. }
  53. public static function pairsIterator<A,B>(table:Table<A,B>) : Iterator<{index:A, value:B}> {
  54. var p = Lua.pairs(table);
  55. var next = p.next;
  56. var i = p.index;
  57. return {
  58. next : function(){
  59. var res = next(table,i);
  60. i = res.index;
  61. return {index : res.index, value : res.value};
  62. },
  63. hasNext : function(){
  64. return Lua.next(table, i).value != null;
  65. }
  66. }
  67. }
  68. public static function ipairsIterator<A,B>(table:Table<A,B>) : Iterator<{index:Int, value:B}> {
  69. var p = Lua.ipairs(table);
  70. var next = p.next;
  71. var i = p.index;
  72. return {
  73. next : function(){
  74. var res = next(table,i);
  75. i = res.index;
  76. return {index : res.index, value : res.value};
  77. },
  78. hasNext : function(){
  79. return next(table, i).value != null;
  80. }
  81. }
  82. }
  83. }