ObjectMap.hx 2.8 KB

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