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  */
38 interface Iterator(E) {
39     /**
40      * Returns {@code true} if the iteration has more elements.
41      * (In other words, returns {@code true} if {@link #next} would
42      * return an element rather than throwing an exception.)
43      *
44      * @return {@code true} if the iteration has more elements
45      */
46     bool hasNext();
47 
48     /**
49      * Returns the next element in the iteration.
50      *
51      * @return the next element in the iteration
52      * @throws NoSuchElementException if the iteration has no more elements
53      */
54     E next();
55 
56     /**
57      * Removes from the underlying collection the last element returned
58      * by this iterator (optional operation).  This method can be called
59      * only once per call to {@link #next}.  The behavior of an iterator
60      * is unspecified if the underlying collection is modified while the
61      * iteration is in progress in any way other than by calling this
62      * method.
63      *
64      * @implSpec
65      * The default implementation throws an instance of
66      * {@link UnsupportedOperationException} and performs no other action.
67      *
68      * @throws UnsupportedOperationException if the {@code remove}
69      *         operation is not supported by this iterator
70      *
71      * @throws IllegalStateException if the {@code next} method has not
72      *         yet been called, or the {@code remove} method has already
73      *         been called after the last call to the {@code next}
74      *         method
75      */
76     // default void remove() {
77     //     throw new UnsupportedOperationException("remove");
78     // }
79 
80     /**
81      * Performs the given action for each remaining element until all elements
82      * have been processed or the action throws an exception.  Actions are
83      * performed in the order of iteration, if that order is specified.
84      * Exceptions thrown by the action are relayed to the caller.
85      *
86      * @implSpec
87      * <p>The default implementation behaves as if:
88      * <pre>{@code
89      *     while (hasNext())
90      *         action.accept(next());
91      * }</pre>
92      *
93      * @param action The action to be performed for each element
94      * @throws NullPointerException if the specified action is null
95      */
96     // default void forEachRemaining(Consumer<E> action) {
97     //     Objects.requireNonNull(action);
98     //     while (hasNext())
99     //         action.accept(next());
100     // }
101 }
102 
103 import std.range;
104 
105 /**
106 */
107 class RangeIterator(T) : Iterator!(T)
108 {
109     private InputRange!T _range;
110 
111     this(InputRange!T range)
112     {
113         _range = range;
114     }
115 
116 	// implement Iterator
117 	
118 	bool hasNext() {
119         return !_range.empty();
120 	}
121 	
122 	T next() {
123         T r = _range.front();
124         _range.popFront();
125         return r;
126 	}
127 }
128