Map.hx 5.2 KB

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