ObjectMap.hx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. import php.*;
  24. @:coreApi
  25. class ObjectMap <K:{ }, V> implements haxe.Constraints.IMap<K,V> {
  26. var _keys:NativeAssocArray<K>;
  27. var _values:NativeAssocArray<V>;
  28. public function new():Void {
  29. _keys = new NativeAssocArray();
  30. _values = new NativeAssocArray();
  31. }
  32. public function set(key:K, value:V):Void {
  33. var id = Global.spl_object_hash(key);
  34. _keys[id] = key;
  35. _values[id] = value;
  36. }
  37. public function get(key:K):Null<V> {
  38. var id = Global.spl_object_hash(key);
  39. return Global.isset(_values[id]) ? _values[id] : null;
  40. }
  41. public function exists(key:K):Bool {
  42. return Global.array_key_exists(Global.spl_object_hash(key), _values);
  43. }
  44. public function remove( key : K ) : Bool {
  45. var id = Global.spl_object_hash(key);
  46. if (Global.array_key_exists(id, _values)) {
  47. Global.unset(_keys[id], _values[id]);
  48. return true;
  49. } else {
  50. return false;
  51. }
  52. }
  53. public inline function keys() : Iterator<K> {
  54. return _keys.iterator();
  55. }
  56. public inline function iterator() : Iterator<V> {
  57. return _values.iterator();
  58. }
  59. @:runtime public inline function keyValueIterator() : KeyValueIterator<K, V> {
  60. return new haxe.iterators.MapKeyValueIterator(this);
  61. }
  62. public inline function copy() : ObjectMap<K,V> {
  63. return Syntax.clone(this);
  64. }
  65. public function toString() : String {
  66. var s = "{";
  67. var it = keys();
  68. for( i in it ) {
  69. s += Std.string(i);
  70. s += " => ";
  71. s += Std.string(get(i));
  72. if( it.hasNext() )
  73. s += ", ";
  74. }
  75. return s + "}";
  76. }
  77. }