Map.hx 5.1 KB

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