132
|
1 /*
|
|
2 File: AbstractIterator.d
|
|
3
|
|
4 Originally written by Doug Lea and released into the public domain.
|
|
5 Thanks for the assistance and support of Sun Microsystems Labs, Agorics
|
|
6 Inc, Loral, and everyone contributing, testing, and using this code.
|
|
7
|
|
8 History:
|
|
9 Date Who What
|
|
10 24Sep95 dl@cs.oswego.edu Create from tango.util.collection.d working file
|
|
11 13Oct95 dl Changed protection statuses
|
|
12 9Apr97 dl made class public
|
|
13 */
|
|
14
|
|
15
|
|
16 module tango.util.collection.impl.AbstractIterator;
|
|
17
|
|
18 private import tango.core.Exception;
|
|
19
|
|
20 private import tango.util.collection.model.View,
|
|
21 tango.util.collection.model.GuardIterator;
|
|
22
|
|
23
|
|
24
|
|
25 /**
|
|
26 *
|
|
27 * A convenient base class for implementations of GuardIterator
|
|
28 *
|
|
29 author: Doug Lea
|
|
30 * @version 0.93
|
|
31 *
|
|
32 * <P> For an introduction to this package see <A HREF="index.html"> Overview </A>.
|
|
33 **/
|
|
34
|
|
35 public abstract class AbstractIterator(T) : GuardIterator!(T)
|
|
36 {
|
|
37 /**
|
|
38 * The collection being enumerated
|
|
39 **/
|
|
40
|
|
41 private View!(T) view;
|
|
42
|
|
43 /**
|
|
44 * The version number of the collection we got upon construction
|
|
45 **/
|
|
46
|
|
47 private uint mutation;
|
|
48
|
|
49 /**
|
|
50 * The number of elements we think we have left.
|
|
51 * Initialized to view.size() upon construction
|
|
52 **/
|
|
53
|
|
54 private uint togo;
|
|
55
|
|
56
|
|
57 protected this (View!(T) v)
|
|
58 {
|
|
59 view = v;
|
|
60 togo = v.size();
|
|
61 mutation = v.mutation();
|
|
62 }
|
|
63
|
|
64 /**
|
|
65 * Implements tango.util.collection.impl.Collection.CollectionIterator.corrupted.
|
|
66 * Claim corruption if version numbers differ
|
|
67 * See_Also: tango.util.collection.impl.Collection.CollectionIterator.corrupted
|
|
68 **/
|
|
69
|
|
70 public final bool corrupted()
|
|
71 {
|
|
72 return mutation != view.mutation;
|
|
73 }
|
|
74
|
|
75 /**
|
|
76 * Implements tango.util.collection.impl.Collection.CollectionIterator.numberOfRemaingingElements.
|
|
77 * See_Also: tango.util.collection.impl.Collection.CollectionIterator.remaining
|
|
78 **/
|
|
79 public final uint remaining()
|
|
80 {
|
|
81 return togo;
|
|
82 }
|
|
83
|
|
84 /**
|
|
85 * Implements tango.util.collection.model.Iterator.more.
|
|
86 * Return true if remaining > 0 and not corrupted
|
|
87 * See_Also: tango.util.collection.model.Iterator.more
|
|
88 **/
|
|
89 public final bool more()
|
|
90 {
|
|
91 return togo > 0 && mutation is view.mutation;
|
|
92 }
|
|
93
|
|
94 /**
|
|
95 * Subclass utility.
|
|
96 * Tries to decrement togo, raising exceptions
|
|
97 * if it is already zero or if corrupted()
|
|
98 * Always call as the first line of get.
|
|
99 **/
|
|
100 protected final void decRemaining()
|
|
101 {
|
|
102 if (mutation != view.mutation)
|
|
103 throw new CorruptedIteratorException ("Collection modified during iteration");
|
|
104
|
|
105 if (togo is 0)
|
|
106 throw new NoSuchElementException ("exhausted enumeration");
|
|
107
|
|
108 --togo;
|
|
109 }
|
|
110 }
|
|
111
|
|
112
|
|
113 public abstract class AbstractMapIterator(K, V) : AbstractIterator!(V), PairIterator!(K, V)
|
|
114 {
|
|
115 abstract V get (inout K key);
|
|
116
|
|
117 protected this (View!(V) c)
|
|
118 {
|
|
119 super (c);
|
|
120 }
|
|
121 }
|