StringMap.hx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 haxe.ds;
  23. import lua.Lua;
  24. class StringMap<T> implements haxe.Constraints.IMap<String, T> {
  25. private var h:lua.Table<String, T>;
  26. static var tnull:Dynamic = lua.Table.create();
  27. public inline function new():Void {
  28. h = lua.Table.create();
  29. }
  30. public inline function set(key:String, value:T):Void
  31. untyped {
  32. if (value == null) {
  33. h[key] = tnull;
  34. } else {
  35. h[key] = value;
  36. }
  37. }
  38. public inline function get(key:String):Null<T>
  39. untyped {
  40. var ret = h[key];
  41. if (ret == tnull) {
  42. return null;
  43. }
  44. return ret;
  45. }
  46. public inline function exists(key:String):Bool
  47. untyped {
  48. return h[key] != null;
  49. }
  50. public function remove(key:String):Bool
  51. untyped {
  52. if (h[key] == null) {
  53. return false;
  54. } else {
  55. h[key] = null;
  56. return true;
  57. }
  58. }
  59. public function keys():Iterator<String> {
  60. var next = Lua.next;
  61. var cur = next(h, null).index;
  62. return {
  63. next: function() {
  64. var ret = cur;
  65. cur = next(h, cur).index;
  66. return cast ret;
  67. },
  68. hasNext: function() return cur != null
  69. }
  70. }
  71. public function iterator():Iterator<T> {
  72. var it = keys();
  73. return untyped {
  74. hasNext: function() return it.hasNext(),
  75. next: function() return h[it.next()]
  76. };
  77. }
  78. @:runtime public inline function keyValueIterator():KeyValueIterator<String, T> {
  79. return new haxe.iterators.MapKeyValueIterator(this);
  80. }
  81. public function copy():StringMap<T> {
  82. var copied = new StringMap();
  83. for (key in keys())
  84. copied.set(key, get(key));
  85. return copied;
  86. }
  87. public function toString():String {
  88. var s = new StringBuf();
  89. s.add("[");
  90. var it = keys();
  91. for (i in it) {
  92. s.add(i);
  93. s.add(" => ");
  94. s.add(Std.string(get(i)));
  95. if (it.hasNext())
  96. s.add(", ");
  97. }
  98. s.add("]");
  99. return s.toString();
  100. }
  101. public inline function clear():Void {
  102. h = lua.Table.create();
  103. }
  104. }