ObjectMap.hx 2.9 KB

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