IntMap.hx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (C)2005-2015 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 class IntMap<T> implements php.IteratorAggregate<T> implements haxe.Constraints.IMap<Int,T> {
  24. @:analyzer(no_simplification)
  25. private var h : ArrayAccess<Int>;
  26. public function new() : Void {
  27. h = untyped __call__('array');
  28. }
  29. public function set( key : Int, value : T ) : Void {
  30. untyped h[key] = value;
  31. }
  32. public function get( key : Int ) : Null<T> {
  33. if (untyped __call__("array_key_exists", key, h))
  34. return untyped h[key];
  35. else
  36. return null;
  37. }
  38. public function exists( key : Int ) : Bool {
  39. return untyped __call__("array_key_exists", key, h);
  40. }
  41. public function remove( key : Int ) : Bool {
  42. if (untyped __call__("array_key_exists", key, h)) {
  43. untyped __call__("unset", h[key]);
  44. return true;
  45. } else
  46. return false;
  47. }
  48. public function keys() : Iterator<Int> {
  49. return untyped __call__("new _hx_array_iterator", __call__("array_keys", h));
  50. }
  51. public function iterator() : Iterator<T> {
  52. return untyped __call__("new _hx_array_iterator", __call__("array_values", h));
  53. }
  54. public function toString() : String {
  55. var s = "{";
  56. var it = keys();
  57. for( i in it ) {
  58. s += i;
  59. s += " => ";
  60. s += Std.string(get(i));
  61. if( it.hasNext() )
  62. s += ", ";
  63. }
  64. return s + "}";
  65. }
  66. /**
  67. Implement IteratorAggregate for native php iteration
  68. **/
  69. #if php
  70. function getIterator() : Iterator<T> {
  71. return iterator();
  72. }
  73. #end
  74. }