ObjectMap.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C)2005-2013 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. @:coreApi
  24. class ObjectMap <K:{ }, V> {
  25. static var count = 0;
  26. static inline function assignId(obj: { } ):Int {
  27. return untyped obj.__id__ = ++count;
  28. }
  29. static inline function getId(obj: { } ):Int {
  30. return untyped obj.__id__;
  31. }
  32. var h: { };
  33. public function new(?weakKeys:Bool = false):Void {
  34. h = untyped __new__(_global["Object"]);
  35. untyped h.__keys__ = untyped __new__(_global["Object"]);
  36. }
  37. public function set(key:K, value:V):Void untyped {
  38. var id = "$" + (key.__id__ != null ? key.__id__ : assignId(key));
  39. h[id] = value;
  40. h.__keys__[id] = key;
  41. }
  42. public inline function get(key:K):Null<V> {
  43. return untyped h["$" +getId(key)];
  44. }
  45. public inline function exists(key:K):Bool {
  46. return untyped h["hasOwnProperty"]("$" +getId(key));
  47. }
  48. public function remove( key : K ) : Bool {
  49. var key = "$" + getId(key);
  50. if( untyped !h["hasOwnProperty"](key) ) return false;
  51. untyped __delete__(h,key);
  52. untyped __delete__(h.__keys__,key);
  53. return true;
  54. }
  55. public function keys() : Iterator<K> {
  56. var a = [];
  57. untyped {
  58. var keys:Iterator<String> = __hkeys__(h.__keys__)["iterator"]();
  59. for (key in keys) {
  60. if( h.hasOwnProperty("$" +key) )
  61. a.push(h.__keys__["$" +key]);
  62. }
  63. }
  64. return a.iterator();
  65. }
  66. public function iterator() : Iterator<V> {
  67. return untyped {
  68. ref : h,
  69. it : __hkeys__(h.__keys__)["iterator"](),
  70. hasNext : function() { return __this__.it[__unprotect__("hasNext")](); },
  71. next : function() { var i = __this__.it[__unprotect__("next")](); return __this__.ref["$" +i]; }
  72. };
  73. }
  74. public function toString() : String {
  75. var s = new StringBuf();
  76. s.add("{");
  77. var it = keys();
  78. for( i in it ) {
  79. s.add(i);
  80. s.add(" => ");
  81. s.add(Std.string(get(i)));
  82. if( it.hasNext() )
  83. s.add(", ");
  84. }
  85. s.add("}");
  86. return s.toString();
  87. }
  88. }