1 /* 2 * Hunt - A refined core library for D programming language. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.collection.SortedMap; 13 14 import hunt.collection.Collection; 15 import hunt.collection.Map; 16 import hunt.collection.Set; 17 18 import hunt.util.Comparator; 19 import hunt.Exceptions; 20 21 /** 22 * A {@link Map} that further provides a <em>total ordering</em> on its keys. 23 * The map is ordered according to the {@linkplain Comparable natural 24 * ordering} of its keys, or by a {@link Comparator} typically 25 * provided at sorted map creation time. This order is reflected when 26 * iterating over the sorted map's collection views (returned by the 27 * {@code entrySet}, {@code keySet} and {@code values} methods). 28 * Several additional operations are provided to take advantage of the 29 * ordering. (This interface is the map analogue of {@link SortedSet}.) 30 * 31 * <p>All keys inserted into a sorted map must implement the {@code Comparable} 32 * interface (or be accepted by the specified comparator). Furthermore, all 33 * such keys must be <em>mutually comparable</em>: {@code k1.compareTo(k2)} (or 34 * {@code comparator.compare(k1, k2)}) must not throw a 35 * {@code ClassCastException} for any keys {@code k1} and {@code k2} in 36 * the sorted map. Attempts to violate this restriction will cause the 37 * offending method or constructor invocation to throw a 38 * {@code ClassCastException}. 39 * 40 * <p>Note that the ordering maintained by a sorted map (whether or not an 41 * explicit comparator is provided) must be <em>consistent with equals</em> if 42 * the sorted map is to correctly implement the {@code Map} interface. (See 43 * the {@code Comparable} interface or {@code Comparator} interface for a 44 * precise definition of <em>consistent with equals</em>.) This is so because 45 * the {@code Map} interface is defined in terms of the {@code equals} 46 * operation, but a sorted map performs all key comparisons using its 47 * {@code compareTo} (or {@code compare}) method, so two keys that are 48 * deemed equal by this method are, from the standpoint of the sorted map, 49 * equal. The behavior of a tree map <em>is</em> well-defined even if its 50 * ordering is inconsistent with equals; it just fails to obey the general 51 * contract of the {@code Map} interface. 52 * 53 * <p>All general-purpose sorted map implementation classes should provide four 54 * "standard" constructors. It is not possible to enforce this recommendation 55 * though as required constructors cannot be specified by interfaces. The 56 * expected "standard" constructors for all sorted map implementations are: 57 * <ol> 58 * <li>A void (no arguments) constructor, which creates an empty sorted map 59 * sorted according to the natural ordering of its keys.</li> 60 * <li>A constructor with a single argument of type {@code Comparator}, which 61 * creates an empty sorted map sorted according to the specified comparator.</li> 62 * <li>A constructor with a single argument of type {@code Map}, which creates 63 * a new map with the same key-value mappings as its argument, sorted 64 * according to the keys' natural ordering.</li> 65 * <li>A constructor with a single argument of type {@code SortedMap}, which 66 * creates a new sorted map with the same key-value mappings and the same 67 * ordering as the input sorted map.</li> 68 * </ol> 69 * 70 * <p><strong>Note</strong>: several methods return submaps with restricted key 71 * ranges. Such ranges are <em>half-open</em>, that is, they include their low 72 * endpoint but not their high endpoint (where applicable). If you need a 73 * <em>closed range</em> (which includes both endpoints), and the key type 74 * allows for calculation of the successor of a given key, merely request 75 * the subrange from {@code lowEndpoint} to 76 * {@code successor(highEndpoint)}. For example, suppose that {@code m} 77 * is a map whose keys are strings. The following idiom obtains a view 78 * containing all of the key-value mappings in {@code m} whose keys are 79 * between {@code low} and {@code high}, inclusive:<pre> 80 * SortedMap<string, V> sub = m.subMap(low, high+"\0");</pre> 81 * 82 * A similar technique can be used to generate an <em>open range</em> 83 * (which contains neither endpoint). The following idiom obtains a 84 * view containing all of the key-value mappings in {@code m} whose keys 85 * are between {@code low} and {@code high}, exclusive:<pre> 86 * SortedMap<string, V> sub = m.subMap(low+"\0", high);</pre> 87 * 88 * <p>This interface is a member of the 89 * <a href="{@docRoot}/../technotes/guides/collections/index.html"> 90 * Java Collections Framework</a>. 91 * 92 * @param !K the type of keys maintained by this map 93 * @param !V the type of mapped values 94 * 95 * @author Josh Bloch 96 * @see Map 97 * @see TreeMap 98 * @see SortedSet 99 * @see Comparator 100 * @see Comparable 101 * @see Collection 102 * @see ClassCastException 103 * @since 1.2 104 */ 105 106 interface SortedMap(K,V) : Map!(K,V) { 107 /** 108 * Returns the comparator used to order the keys in this map, or 109 * {@code null} if this map uses the {@linkplain Comparable 110 * natural ordering} of its keys. 111 * 112 * @return the comparator used to order the keys in this map, 113 * or {@code null} if this map uses the natural ordering 114 * of its keys 115 */ 116 Comparator!K comparator(); 117 118 /** 119 * Returns a view of the portion of this map whose keys range from 120 * {@code fromKey}, inclusive, to {@code toKey}, exclusive. (If 121 * {@code fromKey} and {@code toKey} are equal, the returned map 122 * is empty.) The returned map is backed by this map, so changes 123 * in the returned map are reflected in this map, and vice-versa. 124 * The returned map supports all optional map operations that this 125 * map supports. 126 * 127 * <p>The returned map will throw an {@code IllegalArgumentException} 128 * on an attempt to insert a key outside its range. 129 * 130 * @param fromKey low endpoint (inclusive) of the keys in the returned map 131 * @param toKey high endpoint (exclusive) of the keys in the returned map 132 * @return a view of the portion of this map whose keys range from 133 * {@code fromKey}, inclusive, to {@code toKey}, exclusive 134 * @throws ClassCastException if {@code fromKey} and {@code toKey} 135 * cannot be compared to one another using this map's comparator 136 * (or, if the map has no comparator, using natural ordering). 137 * Implementations may, but are not required to, throw this 138 * exception if {@code fromKey} or {@code toKey} 139 * cannot be compared to keys currently in the map. 140 * @throws NullPointerException if {@code fromKey} or {@code toKey} 141 * is null and this map does not permit null keys 142 * @throws IllegalArgumentException if {@code fromKey} is greater than 143 * {@code toKey}; or if this map itself has a restricted 144 * range, and {@code fromKey} or {@code toKey} lies 145 * outside the bounds of the range 146 */ 147 SortedMap!(K,V) subMap(K fromKey, K toKey); 148 149 /** 150 * Returns a view of the portion of this map whose keys are 151 * strictly less than {@code toKey}. The returned map is backed 152 * by this map, so changes in the returned map are reflected in 153 * this map, and vice-versa. The returned map supports all 154 * optional map operations that this map supports. 155 * 156 * <p>The returned map will throw an {@code IllegalArgumentException} 157 * on an attempt to insert a key outside its range. 158 * 159 * @param toKey high endpoint (exclusive) of the keys in the returned map 160 * @return a view of the portion of this map whose keys are strictly 161 * less than {@code toKey} 162 * @throws ClassCastException if {@code toKey} is not compatible 163 * with this map's comparator (or, if the map has no comparator, 164 * if {@code toKey} does not implement {@link Comparable}). 165 * Implementations may, but are not required to, throw this 166 * exception if {@code toKey} cannot be compared to keys 167 * currently in the map. 168 * @throws NullPointerException if {@code toKey} is null and 169 * this map does not permit null keys 170 * @throws IllegalArgumentException if this map itself has a 171 * restricted range, and {@code toKey} lies outside the 172 * bounds of the range 173 */ 174 SortedMap!(K,V) headMap(K toKey); 175 176 /** 177 * Returns a view of the portion of this map whose keys are 178 * greater than or equal to {@code fromKey}. The returned map is 179 * backed by this map, so changes in the returned map are 180 * reflected in this map, and vice-versa. The returned map 181 * supports all optional map operations that this map supports. 182 * 183 * <p>The returned map will throw an {@code IllegalArgumentException} 184 * on an attempt to insert a key outside its range. 185 * 186 * @param fromKey low endpoint (inclusive) of the keys in the returned map 187 * @return a view of the portion of this map whose keys are greater 188 * than or equal to {@code fromKey} 189 * @throws ClassCastException if {@code fromKey} is not compatible 190 * with this map's comparator (or, if the map has no comparator, 191 * if {@code fromKey} does not implement {@link Comparable}). 192 * Implementations may, but are not required to, throw this 193 * exception if {@code fromKey} cannot be compared to keys 194 * currently in the map. 195 * @throws NullPointerException if {@code fromKey} is null and 196 * this map does not permit null keys 197 * @throws IllegalArgumentException if this map itself has a 198 * restricted range, and {@code fromKey} lies outside the 199 * bounds of the range 200 */ 201 SortedMap!(K,V) tailMap(K fromKey); 202 203 /** 204 * Returns the first (lowest) key currently in this map. 205 * 206 * @return the first (lowest) key currently in this map 207 * @throws NoSuchElementException if this map is empty 208 */ 209 K firstKey(); 210 211 /** 212 * Returns the last (highest) key currently in this map. 213 * 214 * @return the last (highest) key currently in this map 215 * @throws NoSuchElementException if this map is empty 216 */ 217 K lastKey(); 218 219 /** 220 * Returns a {@link Set} view of the keys contained in this map. 221 * The set's iterator returns the keys in ascending order. 222 * The set is backed by the map, so changes to the map are 223 * reflected in the set, and vice-versa. If the map is modified 224 * while an iteration over the set is in progress (except through 225 * the iterator's own {@code remove} operation), the results of 226 * the iteration are undefined. The set supports element removal, 227 * which removes the corresponding mapping from the map, via the 228 * {@code Iterator.remove}, {@code Set.remove}, 229 * {@code removeAll}, {@code retainAll}, and {@code clear} 230 * operations. It does not support the {@code add} or {@code addAll} 231 * operations. 232 * 233 * @return a set view of the keys contained in this map, sorted in 234 * ascending order 235 */ 236 K[] keySet(); 237 238 /** 239 * Returns a {@link Collection} view of the values contained in this map. 240 * The collection's iterator returns the values in ascending order 241 * of the corresponding keys. 242 * The collection is backed by the map, so changes to the map are 243 * reflected in the collection, and vice-versa. If the map is 244 * modified while an iteration over the collection is in progress 245 * (except through the iterator's own {@code remove} operation), 246 * the results of the iteration are undefined. The collection 247 * supports element removal, which removes the corresponding 248 * mapping from the map, via the {@code Iterator.remove}, 249 * {@code Collection.remove}, {@code removeAll}, 250 * {@code retainAll} and {@code clear} operations. It does not 251 * support the {@code add} or {@code addAll} operations. 252 * 253 * @return a collection view of the values contained in this map, 254 * sorted in ascending key order 255 */ 256 V[] values(); 257 258 /** 259 * Returns a {@link Set} view of the mappings contained in this map. 260 * The set's iterator returns the entries in ascending key order. 261 * The set is backed by the map, so changes to the map are 262 * reflected in the set, and vice-versa. If the map is modified 263 * while an iteration over the set is in progress (except through 264 * the iterator's own {@code remove} operation, or through the 265 * {@code setValue} operation on a map entry returned by the 266 * iterator) the results of the iteration are undefined. The set 267 * supports element removal, which removes the corresponding 268 * mapping from the map, via the {@code Iterator.remove}, 269 * {@code Set.remove}, {@code removeAll}, {@code retainAll} and 270 * {@code clear} operations. It does not support the 271 * {@code add} or {@code addAll} operations. 272 * 273 * @return a set view of the mappings contained in this map, 274 * sorted in ascending key order 275 */ 276 // Set<MapEntry<K, V>> entrySet(); 277 }