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&lt;string, V&gt; 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&lt;string, V&gt; 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  */
104 
105 interface SortedMap(K,V) : Map!(K,V) {
106     /**
107      * Returns the comparator used to order the keys in this map, or
108      * {@code null} if this map uses the {@linkplain Comparable
109      * natural ordering} of its keys.
110      *
111      * @return the comparator used to order the keys in this map,
112      *         or {@code null} if this map uses the natural ordering
113      *         of its keys
114      */
115     Comparator!K comparator();
116 
117     /**
118      * Returns a view of the portion of this map whose keys range from
119      * {@code fromKey}, inclusive, to {@code toKey}, exclusive.  (If
120      * {@code fromKey} and {@code toKey} are equal, the returned map
121      * is empty.)  The returned map is backed by this map, so changes
122      * in the returned map are reflected in this map, and vice-versa.
123      * The returned map supports all optional map operations that this
124      * map supports.
125      *
126      * <p>The returned map will throw an {@code IllegalArgumentException}
127      * on an attempt to insert a key outside its range.
128      *
129      * @param fromKey low endpoint (inclusive) of the keys in the returned map
130      * @param toKey high endpoint (exclusive) of the keys in the returned map
131      * @return a view of the portion of this map whose keys range from
132      *         {@code fromKey}, inclusive, to {@code toKey}, exclusive
133      * @throws ClassCastException if {@code fromKey} and {@code toKey}
134      *         cannot be compared to one another using this map's comparator
135      *         (or, if the map has no comparator, using natural ordering).
136      *         Implementations may, but are not required to, throw this
137      *         exception if {@code fromKey} or {@code toKey}
138      *         cannot be compared to keys currently in the map.
139      * @throws NullPointerException if {@code fromKey} or {@code toKey}
140      *         is null and this map does not permit null keys
141      * @throws IllegalArgumentException if {@code fromKey} is greater than
142      *         {@code toKey}; or if this map itself has a restricted
143      *         range, and {@code fromKey} or {@code toKey} lies
144      *         outside the bounds of the range
145      */
146     SortedMap!(K,V) subMap(K fromKey, K toKey);
147 
148     /**
149      * Returns a view of the portion of this map whose keys are
150      * strictly less than {@code toKey}.  The returned map is backed
151      * by this map, so changes in the returned map are reflected in
152      * this map, and vice-versa.  The returned map supports all
153      * optional map operations that this map supports.
154      *
155      * <p>The returned map will throw an {@code IllegalArgumentException}
156      * on an attempt to insert a key outside its range.
157      *
158      * @param toKey high endpoint (exclusive) of the keys in the returned map
159      * @return a view of the portion of this map whose keys are strictly
160      *         less than {@code toKey}
161      * @throws ClassCastException if {@code toKey} is not compatible
162      *         with this map's comparator (or, if the map has no comparator,
163      *         if {@code toKey} does not implement {@link Comparable}).
164      *         Implementations may, but are not required to, throw this
165      *         exception if {@code toKey} cannot be compared to keys
166      *         currently in the map.
167      * @throws NullPointerException if {@code toKey} is null and
168      *         this map does not permit null keys
169      * @throws IllegalArgumentException if this map itself has a
170      *         restricted range, and {@code toKey} lies outside the
171      *         bounds of the range
172      */
173     SortedMap!(K,V) headMap(K toKey);
174 
175     /**
176      * Returns a view of the portion of this map whose keys are
177      * greater than or equal to {@code fromKey}.  The returned map is
178      * backed by this map, so changes in the returned map are
179      * reflected in this map, and vice-versa.  The returned map
180      * supports all optional map operations that this map supports.
181      *
182      * <p>The returned map will throw an {@code IllegalArgumentException}
183      * on an attempt to insert a key outside its range.
184      *
185      * @param fromKey low endpoint (inclusive) of the keys in the returned map
186      * @return a view of the portion of this map whose keys are greater
187      *         than or equal to {@code fromKey}
188      * @throws ClassCastException if {@code fromKey} is not compatible
189      *         with this map's comparator (or, if the map has no comparator,
190      *         if {@code fromKey} does not implement {@link Comparable}).
191      *         Implementations may, but are not required to, throw this
192      *         exception if {@code fromKey} cannot be compared to keys
193      *         currently in the map.
194      * @throws NullPointerException if {@code fromKey} is null and
195      *         this map does not permit null keys
196      * @throws IllegalArgumentException if this map itself has a
197      *         restricted range, and {@code fromKey} lies outside the
198      *         bounds of the range
199      */
200     SortedMap!(K,V) tailMap(K fromKey);
201 
202     /**
203      * Returns the first (lowest) key currently in this map.
204      *
205      * @return the first (lowest) key currently in this map
206      * @throws NoSuchElementException if this map is empty
207      */
208     K firstKey();
209 
210     /**
211      * Returns the last (highest) key currently in this map.
212      *
213      * @return the last (highest) key currently in this map
214      * @throws NoSuchElementException if this map is empty
215      */
216     K lastKey();
217 
218     /**
219      * Returns a {@link Set} view of the keys contained in this map.
220      * The set's iterator returns the keys in ascending order.
221      * The set is backed by the map, so changes to the map are
222      * reflected in the set, and vice-versa.  If the map is modified
223      * while an iteration over the set is in progress (except through
224      * the iterator's own {@code remove} operation), the results of
225      * the iteration are undefined.  The set supports element removal,
226      * which removes the corresponding mapping from the map, via the
227      * {@code Iterator.remove}, {@code Set.remove},
228      * {@code removeAll}, {@code retainAll}, and {@code clear}
229      * operations.  It does not support the {@code add} or {@code addAll}
230      * operations.
231      *
232      * @return a set view of the keys contained in this map, sorted in
233      *         ascending order
234      */
235     K[] keySet();
236 
237     /**
238      * Returns a {@link Collection} view of the values contained in this map.
239      * The collection's iterator returns the values in ascending order
240      * of the corresponding keys.
241      * The collection is backed by the map, so changes to the map are
242      * reflected in the collection, and vice-versa.  If the map is
243      * modified while an iteration over the collection is in progress
244      * (except through the iterator's own {@code remove} operation),
245      * the results of the iteration are undefined.  The collection
246      * supports element removal, which removes the corresponding
247      * mapping from the map, via the {@code Iterator.remove},
248      * {@code Collection.remove}, {@code removeAll},
249      * {@code retainAll} and {@code clear} operations.  It does not
250      * support the {@code add} or {@code addAll} operations.
251      *
252      * @return a collection view of the values contained in this map,
253      *         sorted in ascending key order
254      */
255     V[] values();
256 
257     /**
258      * Returns a {@link Set} view of the mappings contained in this map.
259      * The set's iterator returns the entries in ascending key order.
260      * The set is backed by the map, so changes to the map are
261      * reflected in the set, and vice-versa.  If the map is modified
262      * while an iteration over the set is in progress (except through
263      * the iterator's own {@code remove} operation, or through the
264      * {@code setValue} operation on a map entry returned by the
265      * iterator) the results of the iteration are undefined.  The set
266      * supports element removal, which removes the corresponding
267      * mapping from the map, via the {@code Iterator.remove},
268      * {@code Set.remove}, {@code removeAll}, {@code retainAll} and
269      * {@code clear} operations.  It does not support the
270      * {@code add} or {@code addAll} operations.
271      *
272      * @return a set view of the mappings contained in this map,
273      *         sorted in ascending key order
274      */
275     // Set<MapEntry<K, V>> entrySet();
276 }