Map.hx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. import haxe.ds.StringMap;
  23. import haxe.ds.IntMap;
  24. import haxe.ds.HashMap;
  25. import haxe.ds.ObjectMap;
  26. import haxe.ds.WeakMap;
  27. import haxe.ds.EnumValueMap;
  28. /**
  29. Map allows key to value mapping for arbitrary value types, and many key
  30. types.
  31. This is a multi-type abstract, it is instantiated as one of its
  32. specialization types depending on its type parameters.
  33. A Map can be instantiated without explicit type parameters. Type inference
  34. will then determine the type parameters from the usage.
  35. Maps can also be created with `key1 => value1, key2 => value2` syntax.
  36. Map is an abstract type, it is not available at runtime.
  37. **/
  38. @:multiType
  39. abstract Map<K,V>(IMap<K,V> ) {
  40. /**
  41. Creates a new Map.
  42. This becomes a constructor call to one of the specialization types in
  43. the output. The rules for that are as follows:
  44. 1. if K is a `String`, `haxe.ds.StringMap` is used
  45. 2. if K is an `Int`, `haxe.ds.IntMap` is used
  46. 3. if K is an `EnumValue`, `haxe.ds.EnumValueMap` is used
  47. 4. if K is any other class or structure, `haxe.ds.ObjectMap` is used
  48. 5. if K is any other type, it causes a compile-time error
  49. (Cpp) Map does not use weak keys on ObjectMap by default.
  50. **/
  51. public function new();
  52. /**
  53. Maps `key` to `value`.
  54. If `key` already has a mapping, the previous value disappears.
  55. If `key` is null, the result is unspecified.
  56. **/
  57. public inline function set(key:K, value:V) this.set(key, value);
  58. /**
  59. Returns the current mapping of `key`.
  60. If no such mapping exists, null is returned.
  61. Note that a check like `map.get(key) == null` can hold for two reasons:
  62. 1. the map has no mapping for `key`
  63. 2. the map has a mapping with a value of `null`
  64. If it is important to distinguish these cases, `exists()` should be
  65. used.
  66. If `key` is null, the result is unspecified.
  67. **/
  68. @:arrayAccess public inline function get(key:K) return this.get(key);
  69. /**
  70. Returns true if `key` has a mapping, false otherwise.
  71. If `key` is null, the result is unspecified.
  72. **/
  73. public inline function exists(key:K) return this.exists(key);
  74. /**
  75. Removes the mapping of `key` and returns true if such a mapping existed,
  76. false otherwise.
  77. If `key` is null, the result is unspecified.
  78. **/
  79. public inline function remove(key:K) return this.remove(key);
  80. /**
  81. Returns an Iterator over the keys of `this` Map.
  82. The order of keys is undefined.
  83. **/
  84. public inline function keys():Iterator<K> {
  85. return this.keys();
  86. }
  87. /**
  88. Returns an Iterator over the values of `this` Map.
  89. The order of values is undefined.
  90. **/
  91. public inline function iterator():Iterator<V> {
  92. return this.iterator();
  93. }
  94. /**
  95. Returns a String representation of `this` Map.
  96. The exact representation depends on the platform and key-type.
  97. **/
  98. public inline function toString():String {
  99. return this.toString();
  100. }
  101. @:arrayAccess @:noCompletion public inline function arrayWrite(k:K, v:V):V {
  102. this.set(k, v);
  103. return v;
  104. }
  105. @:to static inline function toStringMap(t:IMap<String,V>):StringMap<V> {
  106. return new StringMap<V>();
  107. }
  108. @:to static inline function toIntMap(t:IMap<Int,V>):IntMap<V> {
  109. return new IntMap<V>();
  110. }
  111. @:to static inline function toEnumValueMapMap<K:EnumValue>(t:IMap<K,V>):EnumValueMap<K,V> {
  112. return new EnumValueMap<K, V>();
  113. }
  114. @:to static inline function toObjectMap<K:{ }>(t:IMap<K,V>):ObjectMap<K,V> {
  115. return new ObjectMap<K, V>();
  116. }
  117. @:from static inline function fromStringMap<V>(map:StringMap<V>):Map< String, V > {
  118. return map;
  119. }
  120. @:from static inline function fromIntMap<V>(map:IntMap<V>):Map< Int, V > {
  121. return map;
  122. }
  123. @:from static inline function fromObjectMap<K:{ }, V>(map:ObjectMap<K,V>):Map<K,V> {
  124. return map;
  125. }
  126. }
  127. interface IMap<K,V> {
  128. public function get(k:K):Null<V>;
  129. public function set(k:K, v:V):Void;
  130. public function exists(k:K):Bool;
  131. public function remove(k:K):Bool;
  132. public function keys():Iterator<K>;
  133. public function iterator():Iterator<V>;
  134. public function toString():String;
  135. }
  136. private typedef Hashable = {
  137. function hashCode():Int;
  138. }