ObjectMap.hx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C)2005-2018 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 untyped {
  38. h[key] = value;
  39. k[key] = true;
  40. }
  41. public inline function get( key : A ) : Null<B> untyped {
  42. return h[key];
  43. }
  44. public inline function exists( key : A ) : Bool untyped {
  45. return k[key] != null;
  46. }
  47. public function remove( key : A ) : Bool untyped {
  48. if ( k[key] == null) return false;
  49. k[key] = null;
  50. h[key] = null;
  51. return true;
  52. }
  53. public function keys() : Iterator<A> untyped {
  54. var cur = next(h,null);
  55. return {
  56. next : function() {
  57. var ret = cur;
  58. cur = untyped next(k, cur);
  59. return ret;
  60. },
  61. hasNext : function() return cur != null
  62. }
  63. }
  64. public function iterator() : Iterator<B> {
  65. var itr = keys();
  66. return untyped {
  67. hasNext : itr.hasNext,
  68. next : function() return h[itr.next()]
  69. };
  70. }
  71. @:runtime public inline function keyValueIterator() : KeyValueIterator<A, B> {
  72. return new haxe.iterators.MapKeyValueIterator(this);
  73. }
  74. public function copy() : ObjectMap<A,B> {
  75. var copied = new ObjectMap();
  76. for(key in keys()) copied.set(key, get(key));
  77. return copied;
  78. }
  79. public function toString() : String {
  80. var s = new StringBuf();
  81. s.add("{");
  82. var it = keys();
  83. for( i in it ) {
  84. s.add(i);
  85. s.add(" => ");
  86. s.add(Std.string(get(i)));
  87. if( it.hasNext() )
  88. s.add(", ");
  89. }
  90. s.add("}");
  91. return s.toString();
  92. }
  93. }