| 1 |
/////////////////////////////////////////////////////////////////////////////// |
| 2 |
// Copyright (c) 2001, Eric D. Friedman All Rights Reserved. |
| 3 |
// |
| 4 |
// This library is free software; you can redistribute it and/or |
| 5 |
// modify it under the terms of the GNU Lesser General Public |
| 6 |
// License as published by the Free Software Foundation; either |
| 7 |
// version 2.1 of the License, or (at your option) any later version. |
| 8 |
// |
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU Lesser General Public |
| 15 |
// License along with this program; if not, write to the Free Software |
| 16 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 |
/////////////////////////////////////////////////////////////////////////////// |
| 18 |
// THIS FILE IS AUTOGENERATED, PLEASE DO NOT EDIT OR ELSE |
| 19 |
package gnu.trove; |
| 20 |
|
| 21 |
import java.util.ConcurrentModificationException; |
| 22 |
|
| 23 |
/** |
| 24 |
* Iterator for maps of type #e# and Object. |
| 25 |
* <p/> |
| 26 |
* <p>The iterator semantics for Trove's primitive maps is slightly different |
| 27 |
* from those defined in <tt>java.util.Iterator</tt>, but still well within |
| 28 |
* the scope of the pattern, as defined by Gamma, et al.</p> |
| 29 |
* <p/> |
| 30 |
* <p>This iterator does <b>not</b> implicitly advance to the next entry when |
| 31 |
* the value at the current position is retrieved. Rather, you must explicitly |
| 32 |
* ask the iterator to <tt>advance()</tt> and then retrieve either the <tt>key()</tt>, |
| 33 |
* the <tt>value()</tt> or both. This is done so that you have the option, but not |
| 34 |
* the obligation, to retrieve keys and/or values as your application requires, and |
| 35 |
* without introducing wrapper objects that would carry both. As the iteration is |
| 36 |
* stateful, access to the key/value parts of the current map entry happens in |
| 37 |
* constant time.</p> |
| 38 |
* <p/> |
| 39 |
* <p>In practice, the iterator is akin to a "search finger" that you move from |
| 40 |
* position to position. Read or write operations affect the current entry only and |
| 41 |
* do not assume responsibility for moving the finger.</p> |
| 42 |
* <p/> |
| 43 |
* <p>Here are some sample scenarios for this class of iterator:</p> |
| 44 |
* <p/> |
| 45 |
* <pre> |
| 46 |
* // accessing keys/values through an iterator: |
| 47 |
* for (T#E#ObjectIterator it = map.iterator(); |
| 48 |
* it.hasNext();) { |
| 49 |
* it.forward(); |
| 50 |
* if (satisfiesCondition(it.key()) { |
| 51 |
* doSomethingWithValue(it.value()); |
| 52 |
* } |
| 53 |
* } |
| 54 |
* </pre> |
| 55 |
* <p/> |
| 56 |
* <pre> |
| 57 |
* // modifying values in-place through iteration: |
| 58 |
* for (T#E#ObjectIterator it = map.iterator(); |
| 59 |
* it.hasNext();) { |
| 60 |
* it.forward(); |
| 61 |
* if (satisfiesCondition(it.key()) { |
| 62 |
* it.setValue(newValueForKey(it.key())); |
| 63 |
* } |
| 64 |
* } |
| 65 |
* </pre> |
| 66 |
* <p/> |
| 67 |
* <pre> |
| 68 |
* // deleting entries during iteration: |
| 69 |
* for (T#E#ObjectIterator it = map.iterator(); |
| 70 |
* it.hasNext();) { |
| 71 |
* it.forward(); |
| 72 |
* if (satisfiesCondition(it.key()) { |
| 73 |
* it.remove(); |
| 74 |
* } |
| 75 |
* } |
| 76 |
* </pre> |
| 77 |
* <p/> |
| 78 |
* <pre> |
| 79 |
* // faster iteration by avoiding hasNext(): |
| 80 |
* T#E#ObjectIterator iterator = map.iterator(); |
| 81 |
* for (int i = map.size(); i-- > 0;) { |
| 82 |
* iterator.advance(); |
| 83 |
* doSomethingWithKeyAndValue(iterator.key(), iterator.value()); |
| 84 |
* } |
| 85 |
* </pre> |
| 86 |
* |
| 87 |
* @author Eric D. Friedman |
| 88 |
*/ |
| 89 |
public class T#E#ObjectIterator <V> extends TIterator { |
| 90 |
/** |
| 91 |
* the collection being iterated over |
| 92 |
*/ |
| 93 |
private final T#E#ObjectHashMap<V> _map; |
| 94 |
|
| 95 |
/** |
| 96 |
* Creates an iterator over the specified map |
| 97 |
*/ |
| 98 |
public T#E#ObjectIterator(T#E#ObjectHashMap<V> map) { |
| 99 |
super(map); |
| 100 |
_map = map; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Returns the index of the next value in the data structure |
| 105 |
* or a negative value if the iterator is exhausted. |
| 106 |
* |
| 107 |
* @return an <code>int</code> value |
| 108 |
* @exception ConcurrentModificationException if the underlying collection's |
| 109 |
* size has been modified since the iterator was created. |
| 110 |
*/ |
| 111 |
@Override |
| 112 |
protected final int nextIndex() { |
| 113 |
if (_expectedSize != _map.size()) { |
| 114 |
throw new ConcurrentModificationException(); |
| 115 |
} |
| 116 |
|
| 117 |
Object[] values = _map._values; |
| 118 |
int i = _index; |
| 119 |
while (i-- > 0 && !T#E#ObjectHashMap.isFull(values, i)); |
| 120 |
return i; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Moves the iterator forward to the next entry in the underlying map. |
| 125 |
* |
| 126 |
* @throws java.util.NoSuchElementException |
| 127 |
* if the iterator is already exhausted |
| 128 |
*/ |
| 129 |
public void advance() { |
| 130 |
moveToNextIndex(); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Provides access to the key of the mapping at the iterator's position. |
| 135 |
* Note that you must <tt>advance()</tt> the iterator at least once |
| 136 |
* before invoking this method. |
| 137 |
* |
| 138 |
* @return the key of the entry at the iterator's current position. |
| 139 |
*/ |
| 140 |
public #e# key() { |
| 141 |
return _map._set[_index]; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Provides access to the value of the mapping at the iterator's position. |
| 146 |
* Note that you must <tt>advance()</tt> the iterator at least once |
| 147 |
* before invoking this method. |
| 148 |
* |
| 149 |
* @return the value of the entry at the iterator's current position. |
| 150 |
*/ |
| 151 |
public V value() { |
| 152 |
return _map._values[_index]; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Replace the value of the mapping at the iterator's position with the |
| 157 |
* specified value. Note that you must <tt>advance()</tt> the iterator at |
| 158 |
* least once before invoking this method. |
| 159 |
* |
| 160 |
* @param val the value to set in the current entry |
| 161 |
* @return the old value of the entry. |
| 162 |
*/ |
| 163 |
public V setValue(V val) { |
| 164 |
V old = value(); |
| 165 |
_map._values[_index] = val; |
| 166 |
return old; |
| 167 |
} |
| 168 |
}// T#E#ObjectIterator |