Map.hx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 enum, 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 used.
  65. If [key] is null, the result is unspecified.
  66. **/
  67. @:arrayAccess public inline function get(key:K) return this.get(key);
  68. /**
  69. Returns true if [key] has a mapping, false otherwise.
  70. If [key] is null, the result is unspecified.
  71. **/
  72. public inline function exists(key:K) return this.exists(key);
  73. /**
  74. Removes the mapping of [key] and returns true if such a mapping existed,
  75. false otherwise.
  76. If [key] is null, the result is unspecified.
  77. **/
  78. public inline function remove(key:K) return this.remove(key);
  79. /**
  80. Returns an Iterator over the keys of [this] Map.
  81. The order of keys is undefined.
  82. **/
  83. public inline function keys():Iterator<K> {
  84. return this.keys();
  85. }
  86. /**
  87. Returns an Iterator over the values of [this] Map.
  88. The order of values is undefined.
  89. **/
  90. public inline function iterator():Iterator<V> {
  91. return this.iterator();
  92. }
  93. /**
  94. Returns a String representation of [this] Map.
  95. The exact representation depends on the platform and key-type.
  96. **/
  97. public inline function toString():String {
  98. return this.toString();
  99. }
  100. @:arrayAccess @:noCompletion public inline function arrayWrite(k:K, v:V):V {
  101. this.set(k, v);
  102. return v;
  103. }
  104. @:to static inline function toStringMap(t:IMap<String,V>):StringMap<V> {
  105. return new StringMap<V>();
  106. }
  107. @:to static inline function toIntMap(t:IMap<Int,V>):IntMap<V> {
  108. return new IntMap<V>();
  109. }
  110. @:to static inline function toEnumValueMapMap<K:EnumValue>(t:IMap<K,V>):EnumValueMap<K,V> {
  111. return new EnumValueMap<K, V>();
  112. }
  113. @:to static inline function toObjectMap<K:{ }>(t:IMap<K,V>):ObjectMap<K,V> {
  114. return new ObjectMap<K, V>();
  115. }
  116. @:from static inline function fromStringMap<V>(map:StringMap<V>):Map< String, V > {
  117. return map;
  118. }
  119. @:from static inline function fromIntMap<V>(map:IntMap<V>):Map< Int, V > {
  120. return map;
  121. }
  122. @:from static inline function fromObjectMap<K:{ }, V>(map:ObjectMap<K,V>):Map<K,V> {
  123. return map;
  124. }
  125. }
  126. interface IMap<K,V> {
  127. public function get(k:K):Null<V>;
  128. public function set(k:K, v:V):Void;
  129. public function exists(k:K):Bool;
  130. public function remove(k:K):Bool;
  131. public function keys():Iterator<K>;
  132. public function iterator():Iterator<V>;
  133. public function toString():String;
  134. }
  135. private typedef Hashable = {
  136. function hashCode():Int;
  137. }