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.NavigableSet; 13 14 import hunt.collection.Set; 15 import hunt.collection.SortedSet; 16 import hunt.collection.Iterator; 17 18 /** 19 * A {@link SortedSet} extended with navigation methods reporting 20 * closest matches for given search targets. Methods {@code lower}, 21 * {@code floor}, {@code ceiling}, and {@code higher} return elements 22 * respectively less than, less than or equal, greater than or equal, 23 * and greater than a given element, returning {@code null} if there 24 * is no such element. A {@code NavigableSet} may be accessed and 25 * traversed in either ascending or descending order. The {@code 26 * descendingSet} method returns a view of the set with the senses of 27 * all relational and directional methods inverted. The performance of 28 * ascending operations and views is likely to be faster than that of 29 * descending ones. This interface additionally defines methods 30 * {@code pollFirst} and {@code pollLast} that return and remove the 31 * lowest and highest element, if one exists, else returning {@code 32 * null}. Methods {@code subSet}, {@code headSet}, 33 * and {@code tailSet} differ from the like-named {@code 34 * SortedSet} methods in accepting additional arguments describing 35 * whether lower and upper bounds are inclusive versus exclusive. 36 * Subsets of any {@code NavigableSet} must implement the {@code 37 * NavigableSet} interface. 38 * 39 * <p> The return values of navigation methods may be ambiguous in 40 * implementations that permit {@code null} elements. However, even 41 * in this case the result can be disambiguated by checking 42 * {@code contains(null)}. To avoid such issues, implementations of 43 * this interface are encouraged to <em>not</em> permit insertion of 44 * {@code null} elements. (Note that sorted sets of {@link 45 * Comparable} elements intrinsically do not permit {@code null}.) 46 * 47 * <p>Methods 48 * {@link #subSet(Object, Object) subSet(E, E)}, 49 * {@link #headSet(Object) headSet(E)}, and 50 * {@link #tailSet(Object) tailSet(E)} 51 * are specified to return {@code SortedSet} to allow existing 52 * implementations of {@code SortedSet} to be compatibly retrofitted to 53 * implement {@code NavigableSet}, but extensions and implementations 54 * of this interface are encouraged to override these methods to return 55 * {@code NavigableSet}. 56 * 57 * <p>This interface is a member of the 58 * <a href="{@docRoot}/../technotes/guides/collections/index.html"> 59 * Java Collections Framework</a>. 60 * 61 * @author Doug Lea 62 * @author Josh Bloch 63 * @param (E) the type of elements maintained by this set 64 */ 65 interface NavigableSet(E) : SortedSet!(E) { 66 /** 67 * Returns the greatest element in this set strictly less than the 68 * given element, or {@code null} if there is no such element. 69 * 70 * @param e the value to match 71 * @return the greatest element less than {@code e}, 72 * or {@code null} if there is no such element 73 * @throws ClassCastException if the specified element cannot be 74 * compared with the elements currently in the set 75 * @throws NullPointerException if the specified element is null 76 * and this set does not permit null elements 77 */ 78 E lower(E e); 79 80 /** 81 * Returns the greatest element in this set less than or equal to 82 * the given element, or {@code null} if there is no such element. 83 * 84 * @param e the value to match 85 * @return the greatest element less than or equal to {@code e}, 86 * or {@code null} if there is no such element 87 * @throws ClassCastException if the specified element cannot be 88 * compared with the elements currently in the set 89 * @throws NullPointerException if the specified element is null 90 * and this set does not permit null elements 91 */ 92 E floor(E e); 93 94 /** 95 * Returns the least element in this set greater than or equal to 96 * the given element, or {@code null} if there is no such element. 97 * 98 * @param e the value to match 99 * @return the least element greater than or equal to {@code e}, 100 * or {@code null} if there is no such element 101 * @throws ClassCastException if the specified element cannot be 102 * compared with the elements currently in the set 103 * @throws NullPointerException if the specified element is null 104 * and this set does not permit null elements 105 */ 106 E ceiling(E e); 107 108 /** 109 * Returns the least element in this set strictly greater than the 110 * given element, or {@code null} if there is no such element. 111 * 112 * @param e the value to match 113 * @return the least element greater than {@code e}, 114 * or {@code null} if there is no such element 115 * @throws ClassCastException if the specified element cannot be 116 * compared with the elements currently in the set 117 * @throws NullPointerException if the specified element is null 118 * and this set does not permit null elements 119 */ 120 E higher(E e); 121 122 /** 123 * Retrieves and removes the first (lowest) element, 124 * or returns {@code null} if this set is empty. 125 * 126 * @return the first element, or {@code null} if this set is empty 127 */ 128 E pollFirst(); 129 130 /** 131 * Retrieves and removes the last (highest) element, 132 * or returns {@code null} if this set is empty. 133 * 134 * @return the last element, or {@code null} if this set is empty 135 */ 136 E pollLast(); 137 138 /** 139 * Returns an iterator over the elements in this set, in ascending order. 140 * 141 * @return an iterator over the elements in this set, in ascending order 142 */ 143 // Iterator!(E) iterator(); 144 145 /** 146 * Returns a reverse order view of the elements contained in this set. 147 * The descending set is backed by this set, so changes to the set are 148 * reflected in the descending set, and vice-versa. If either set is 149 * modified while an iteration over either set is in progress (except 150 * through the iterator's own {@code remove} operation), the results of 151 * the iteration are undefined. 152 * 153 * <p>The returned set has an ordering equivalent to 154 * <tt>{@link Collections#reverseOrder(Comparator) Collections.reverseOrder}(comparator())</tt>. 155 * The expression {@code s.descendingSet().descendingSet()} returns a 156 * view of {@code s} essentially equivalent to {@code s}. 157 * 158 * @return a reverse order view of this set 159 */ 160 // NavigableSet!(E) descendingSet(); 161 162 /** 163 * Returns an iterator over the elements in this set, in descending order. 164 * Equivalent in effect to {@code descendingSet().iterator()}. 165 * 166 * @return an iterator over the elements in this set, in descending order 167 */ 168 // Iterator!(E) descendingIterator(); 169 170 /** 171 * Returns a view of the portion of this set whose elements range from 172 * {@code fromElement} to {@code toElement}. If {@code fromElement} and 173 * {@code toElement} are equal, the returned set is empty unless {@code 174 * fromInclusive} and {@code toInclusive} are both true. The returned set 175 * is backed by this set, so changes in the returned set are reflected in 176 * this set, and vice-versa. The returned set supports all optional set 177 * operations that this set supports. 178 * 179 * <p>The returned set will throw an {@code IllegalArgumentException} 180 * on an attempt to insert an element outside its range. 181 * 182 * @param fromElement low endpoint of the returned set 183 * @param fromInclusive {@code true} if the low endpoint 184 * is to be included in the returned view 185 * @param toElement high endpoint of the returned set 186 * @param toInclusive {@code true} if the high endpoint 187 * is to be included in the returned view 188 * @return a view of the portion of this set whose elements range from 189 * {@code fromElement}, inclusive, to {@code toElement}, exclusive 190 * @throws ClassCastException if {@code fromElement} and 191 * {@code toElement} cannot be compared to one another using this 192 * set's comparator (or, if the set has no comparator, using 193 * natural ordering). Implementations may, but are not required 194 * to, throw this exception if {@code fromElement} or 195 * {@code toElement} cannot be compared to elements currently in 196 * the set. 197 * @throws NullPointerException if {@code fromElement} or 198 * {@code toElement} is null and this set does 199 * not permit null elements 200 * @throws IllegalArgumentException if {@code fromElement} is 201 * greater than {@code toElement}; or if this set itself 202 * has a restricted range, and {@code fromElement} or 203 * {@code toElement} lies outside the bounds of the range. 204 */ 205 NavigableSet!(E) subSet(E fromElement, bool fromInclusive, 206 E toElement, bool toInclusive); 207 208 /** 209 * Returns a view of the portion of this set whose elements are less than 210 * (or equal to, if {@code inclusive} is true) {@code toElement}. The 211 * returned set is backed by this set, so changes in the returned set are 212 * reflected in this set, and vice-versa. The returned set supports all 213 * optional set operations that this set supports. 214 * 215 * <p>The returned set will throw an {@code IllegalArgumentException} 216 * on an attempt to insert an element outside its range. 217 * 218 * @param toElement high endpoint of the returned set 219 * @param inclusive {@code true} if the high endpoint 220 * is to be included in the returned view 221 * @return a view of the portion of this set whose elements are less than 222 * (or equal to, if {@code inclusive} is true) {@code toElement} 223 * @throws ClassCastException if {@code toElement} is not compatible 224 * with this set's comparator (or, if the set has no comparator, 225 * if {@code toElement} does not implement {@link Comparable}). 226 * Implementations may, but are not required to, throw this 227 * exception if {@code toElement} cannot be compared to elements 228 * currently in the set. 229 * @throws NullPointerException if {@code toElement} is null and 230 * this set does not permit null elements 231 * @throws IllegalArgumentException if this set itself has a 232 * restricted range, and {@code toElement} lies outside the 233 * bounds of the range 234 */ 235 NavigableSet!(E) headSet(E toElement, bool inclusive); 236 237 /** 238 * Returns a view of the portion of this set whose elements are greater 239 * than (or equal to, if {@code inclusive} is true) {@code fromElement}. 240 * The returned set is backed by this set, so changes in the returned set 241 * are reflected in this set, and vice-versa. The returned set supports 242 * all optional set operations that this set supports. 243 * 244 * <p>The returned set will throw an {@code IllegalArgumentException} 245 * on an attempt to insert an element outside its range. 246 * 247 * @param fromElement low endpoint of the returned set 248 * @param inclusive {@code true} if the low endpoint 249 * is to be included in the returned view 250 * @return a view of the portion of this set whose elements are greater 251 * than or equal to {@code fromElement} 252 * @throws ClassCastException if {@code fromElement} is not compatible 253 * with this set's comparator (or, if the set has no comparator, 254 * if {@code fromElement} does not implement {@link Comparable}). 255 * Implementations may, but are not required to, throw this 256 * exception if {@code fromElement} cannot be compared to elements 257 * currently in the set. 258 * @throws NullPointerException if {@code fromElement} is null 259 * and this set does not permit null elements 260 * @throws IllegalArgumentException if this set itself has a 261 * restricted range, and {@code fromElement} lies outside the 262 * bounds of the range 263 */ 264 NavigableSet!(E) tailSet(E fromElement, bool inclusive); 265 266 /** 267 * {@inheritDoc} 268 * 269 * <p>Equivalent to {@code subSet(fromElement, true, toElement, false)}. 270 * 271 * @throws ClassCastException {@inheritDoc} 272 * @throws NullPointerException {@inheritDoc} 273 * @throws IllegalArgumentException {@inheritDoc} 274 */ 275 SortedSet!(E) subSet(E fromElement, E toElement); 276 277 /** 278 * {@inheritDoc} 279 * 280 * <p>Equivalent to {@code headSet(toElement, false)}. 281 * 282 * @throws ClassCastException {@inheritDoc} 283 * @throws NullPointerException {@inheritDoc} 284 * @throws IllegalArgumentException {@inheritDoc} 285 */ 286 SortedSet!(E) headSet(E toElement); 287 288 /** 289 * {@inheritDoc} 290 * 291 * <p>Equivalent to {@code tailSet(fromElement, true)}. 292 * 293 * @throws ClassCastException {@inheritDoc} 294 * @throws NullPointerException {@inheritDoc} 295 * @throws IllegalArgumentException {@inheritDoc} 296 */ 297 SortedSet!(E) tailSet(E fromElement); 298 }