PairTools.hx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. A set of utility methods for working with the Lua table extern.
  25. **/
  26. class PairTools {
  27. public static function ipairsEach<T>(table:Table<Dynamic, T>, func:Int->T->Void):Void {
  28. untyped __lua__("for i,v in _G.ipairs(table) do func(i,v) end");
  29. }
  30. public static function pairsEach<A, B>(table:Table<A, B>, func:A->B->Void):Void {
  31. untyped __lua__("for k,v in _G.pairs(table) do func(k,v) end");
  32. }
  33. public static function ipairsMap<A, B>(table:Table<Dynamic, A>, func:Int->A->B):Table<Int, B> {
  34. var ret:Table<Int, B> = Table.create();
  35. untyped __lua__("for i,v in _G.ipairs(table) do ret[i] = func(i,v) end;");
  36. return ret;
  37. }
  38. public static function pairsMap<A, B, C>(table:Table<A, B>, func:A->B->C->C):Table<A, C> {
  39. var ret:Table<A, C> = Table.create();
  40. untyped __lua__("for k,v in _G.pairs(table) do ret[k] = func(k,v) end;");
  41. return ret;
  42. }
  43. public static function ipairsFold<A, B>(table:Table<Int, A>, func:Int->A->B->B, seed:B):B {
  44. untyped __lua__("for i,v in _G.ipairs(table) do seed = func(i,v,seed) end");
  45. return untyped __lua__("seed");
  46. }
  47. public static function pairsFold<A, B, C>(table:Table<A, B>, func:A->B->C->C, seed:C):C {
  48. untyped __lua__("for k,v in _G.pairs(table) do seed = func(k,v,seed) end");
  49. return untyped __lua__("seed");
  50. }
  51. public static function ipairsConcat<T>(table1:Table<Int, T>, table2:Table<Int, T>) {
  52. var ret:Table<Int, T> = Table.create();
  53. ipairsFold(table1, function(a, b, c:Table<Int, T>) {
  54. c[a] = b;
  55. return c;
  56. }, ret);
  57. var size = lua.TableTools.maxn(ret);
  58. ipairsFold(table2, function(a, b, c:Table<Int, T>) {
  59. c[a + size] = b;
  60. return c;
  61. }, ret);
  62. return ret;
  63. }
  64. public static function pairsMerge<A, B>(table1:Table<A, B>, table2:Table<A, B>) {
  65. var ret = copy(table1);
  66. pairsEach(table2, function(a, b:B) ret[cast a] = b);
  67. return ret;
  68. }
  69. public static function ipairsExist<T>(table:Table<Int, T>, func:Int->T->Bool) {
  70. untyped __lua__("for k,v in _G.ipairs(table) do if func(k,v) then return true end end");
  71. }
  72. public static function pairsExist<A, B>(table:Table<A, B>, func:A->B->Bool) {
  73. untyped __lua__("for k,v in _G.pairs(table) do if func(k,v) then return true end end");
  74. }
  75. public static function copy<A, B>(table1:Table<A, B>):Table<A, B> {
  76. var ret:Table<A, B> = Table.create();
  77. untyped __lua__("for k,v in _G.pairs(table1) do ret[k] = v end");
  78. return ret;
  79. }
  80. public static function pairsIterator<A, B>(table:Table<A, B>):Iterator<{index:A, value:B}> {
  81. var p = Lua.pairs(table);
  82. var next = p.next;
  83. var i = p.index;
  84. return {
  85. next: function() {
  86. var res = next(table, i);
  87. i = res.index;
  88. return {index: res.index, value: res.value};
  89. },
  90. hasNext: function() {
  91. return Lua.next(table, i).value != null;
  92. }
  93. }
  94. }
  95. public static function ipairsIterator<A, B>(table:Table<A, B>):Iterator<{index:Int, value:B}> {
  96. var p = Lua.ipairs(table);
  97. var next = p.next;
  98. var i = p.index;
  99. return {
  100. next: function() {
  101. var res = next(table, i);
  102. i = res.index;
  103. return {index: res.index, value: res.value};
  104. },
  105. hasNext: function() {
  106. return next(table, i).value != null;
  107. }
  108. }
  109. }
  110. }