Map.hx 4.8 KB

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