ObjectMap.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 function new(weakKeys:Bool = false) {
  33. this = untyped __new__(_global["Object"]);
  34. untyped this.__keys__ = untyped __new__(_global["Object"]);
  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 key = "$" + getId(key);
  49. if( untyped !this["hasOwnProperty"](key) ) return false;
  50. untyped __delete__(this,key);
  51. untyped __delete__(this.__keys__,key);
  52. return true;
  53. }
  54. public function keys() : Iterator<K> {
  55. var a = [];
  56. untyped {
  57. var keys:Iterator<String> = __hkeys__(this.__keys__)["iterator"]();
  58. for (key in keys) {
  59. if( this.hasOwnProperty("$" +key) )
  60. a.push(this.__keys__["$" +key]);
  61. }
  62. }
  63. return a.iterator();
  64. }
  65. public function iterator() : Iterator<V> {
  66. return untyped {
  67. ref : this,
  68. it : __hkeys__(this.__keys__)["iterator"](),
  69. hasNext : function() { return __this__.it[__unprotect__("hasNext")](); },
  70. next : function() { var i = __this__.it[__unprotect__("next")](); return __this__.ref["$" +i]; }
  71. };
  72. }
  73. public function toString() : String {
  74. var s = new StringBuf();
  75. s.add("{");
  76. var it = keys(this);
  77. for( i in it ) {
  78. s.add(i);
  79. s.add(" => ");
  80. s.add(Std.string(get(this,i)));
  81. if( it.hasNext() )
  82. s.add(", ");
  83. }
  84. s.add("}");
  85. return s.toString();
  86. }
  87. }