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.Iterator; 13 14 15 /** 16 * An iterator over a collection. {@code Iterator} takes the place of 17 * {@link Enumeration} in the Java Collections Framework. Iterators 18 * differ from enumerations in two ways: 19 * 20 * <ul> 21 * <li> Iterators allow the caller to remove elements from the 22 * underlying collection during the iteration with well-defined 23 * semantics. 24 * <li> Method names have been improved. 25 * </ul> 26 * 27 * <p>This interface is a member of the 28 * <a href="{@docRoot}/../technotes/guides/collections/index.html"> 29 * Java Collections Framework</a>. 30 * 31 * @param <E> the type of elements returned by this iterator 32 * 33 * @author Josh Bloch 34 * @see Collection 35 * @see ListIterator 36 * @see Iterable 37 * @since 1.2 38 */ 39 interface Iterator(E) { 40 /** 41 * Returns {@code true} if the iteration has more elements. 42 * (In other words, returns {@code true} if {@link #next} would 43 * return an element rather than throwing an exception.) 44 * 45 * @return {@code true} if the iteration has more elements 46 */ 47 bool hasNext(); 48 49 /** 50 * Returns the next element in the iteration. 51 * 52 * @return the next element in the iteration 53 * @throws NoSuchElementException if the iteration has no more elements 54 */ 55 E next(); 56 57 /** 58 * Removes from the underlying collection the last element returned 59 * by this iterator (optional operation). This method can be called 60 * only once per call to {@link #next}. The behavior of an iterator 61 * is unspecified if the underlying collection is modified while the 62 * iteration is in progress in any way other than by calling this 63 * method. 64 * 65 * @implSpec 66 * The default implementation throws an instance of 67 * {@link UnsupportedOperationException} and performs no other action. 68 * 69 * @throws UnsupportedOperationException if the {@code remove} 70 * operation is not supported by this iterator 71 * 72 * @throws IllegalStateException if the {@code next} method has not 73 * yet been called, or the {@code remove} method has already 74 * been called after the last call to the {@code next} 75 * method 76 */ 77 // default void remove() { 78 // throw new UnsupportedOperationException("remove"); 79 // } 80 81 /** 82 * Performs the given action for each remaining element until all elements 83 * have been processed or the action throws an exception. Actions are 84 * performed in the order of iteration, if that order is specified. 85 * Exceptions thrown by the action are relayed to the caller. 86 * 87 * @implSpec 88 * <p>The default implementation behaves as if: 89 * <pre>{@code 90 * while (hasNext()) 91 * action.accept(next()); 92 * }</pre> 93 * 94 * @param action The action to be performed for each element 95 * @throws NullPointerException if the specified action is null 96 * @since 1.8 97 */ 98 // default void forEachRemaining(Consumer<E> action) { 99 // Objects.requireNonNull(action); 100 // while (hasNext()) 101 // action.accept(next()); 102 // } 103 } 104 105 import std.range; 106 107 /** 108 */ 109 class RangeIterator(T) : Iterator!(T) 110 { 111 private InputRange!T _range; 112 113 this(InputRange!T range) 114 { 115 _range = range; 116 } 117 118 // implement Iterator 119 120 bool hasNext() { 121 return !_range.empty(); 122 } 123 124 T next() { 125 T r = _range.front(); 126 _range.popFront(); 127 return r; 128 } 129 } 130