ObjectMap.hx 3.2 KB

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