StringMap.hx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 lua.Lua;
  24. class StringMap<T> implements haxe.Constraints.IMap<String,T> {
  25. private var h : lua.Table<String,T>;
  26. static var tnull : Dynamic = lua.Table.create();
  27. public inline function new() : Void {
  28. h = lua.Table.create();
  29. }
  30. public inline function set( key : String, value : T ) : Void untyped {
  31. if (value == null){
  32. h[key] = tnull;
  33. } else {
  34. h[key] = value;
  35. }
  36. }
  37. public inline function get( key : String ) : Null<T> untyped {
  38. var ret = h[key];
  39. if (ret == tnull){
  40. ret = null;
  41. }
  42. return ret;
  43. }
  44. public inline function exists( key : String ) : Bool untyped {
  45. return h[key] != null;
  46. }
  47. public function remove( key : String ) : Bool untyped {
  48. if (h[key] == null){
  49. return false;
  50. } else {
  51. h[key] = null;
  52. return true;
  53. }
  54. }
  55. public function keys() : Iterator<String> {
  56. var next = Lua.next;
  57. var cur = next(h,null).index;
  58. return {
  59. next : function() {
  60. var ret = cur;
  61. cur = next(h,cur).index;
  62. return cast ret;
  63. },
  64. hasNext : function() return cur != null
  65. }
  66. }
  67. public function iterator() : Iterator<T> {
  68. var it = keys();
  69. return untyped {
  70. hasNext : function() return it.hasNext(),
  71. next : function() return h[it.next()]
  72. };
  73. }
  74. @:runtime public inline function keyValueIterator() : KeyValueIterator<String, T> {
  75. return new haxe.iterators.MapKeyValueIterator(this);
  76. }
  77. public function copy() : StringMap<T> {
  78. var copied = new StringMap();
  79. for(key in keys()) copied.set(key, get(key));
  80. return copied;
  81. }
  82. public function toString() : String {
  83. var s = new StringBuf();
  84. s.add("{");
  85. var it = keys();
  86. for( i in it ) {
  87. s.add(i);
  88. s.add(" => ");
  89. s.add(Std.string(get(i)));
  90. if( it.hasNext() )
  91. s.add(", ");
  92. }
  93. s.add("}");
  94. return s.toString();
  95. }
  96. }