ObjectMap.hx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (C)2005-2019 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. @:ifFeature("dynamic_read.iterator", "anon_optional_read.iterator", "anon_read.iterator")
  57. public inline function iterator():Iterator<V> {
  58. return _values.iterator();
  59. }
  60. @:ifFeature("dynamic_read.keyValueIterator", "anon_optional_read.keyValueIterator", "anon_read.keyValueIterator")
  61. public inline function keyValueIterator():KeyValueIterator<K, V> {
  62. return new haxe.iterators.MapKeyValueIterator(this);
  63. }
  64. public inline function copy():ObjectMap<K, V> {
  65. return Syntax.clone(this);
  66. }
  67. public function toString():String {
  68. var s = "{";
  69. var it = keys();
  70. for (i in it) {
  71. s += Std.string(i);
  72. s += " => ";
  73. s += Std.string(get(i));
  74. if (it.hasNext())
  75. s += ", ";
  76. }
  77. return s + "}";
  78. }
  79. public inline function clear():Void {
  80. _keys = new NativeAssocArray();
  81. _values = new NativeAssocArray();
  82. }
  83. }