1 /*
2 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
104 /**
105 * The index corresponding to unseen in the elements array.
106 */
107 int unseenIndex = 0;
108
109 /**
110 * The bit representing the last element returned by this iterator
111 * but not removed, or zero if no such element exists.
112 */
113 long lastReturned = 0;
114
115 /**
116 * The index corresponding to lastReturned in the elements array.
117 */
118 int lastReturnedIndex = 0;
119
120 EnumSetIterator() {
121 unseen = elements[0];
122 }
123
124 public boolean hasNext() {
125 while (unseen == 0 && unseenIndex < elements.length - 1)
126 unseen = elements[++unseenIndex];
127 return unseen != 0;
128 }
129
130 @Override
131 public E next() {
132 if (!hasNext())
133 throw new NoSuchElementException();
134 lastReturned = unseen & -unseen;
135 lastReturnedIndex = unseenIndex;
136 unseen -= lastReturned;
137 return (E) universe[(lastReturnedIndex << 6)
138 + Long.numberOfTrailingZeros(lastReturned)];
139 }
140
141 public void remove() {
142 if (lastReturned == 0)
143 throw new IllegalStateException();
144 final long oldElements = elements[lastReturnedIndex];
145 elements[lastReturnedIndex] &= ~lastReturned;
146 if (oldElements != elements[lastReturnedIndex]) {
147 size--;
148 }
149 lastReturned = 0;
150 }
151 }
152
153 /**
154 * Returns the number of elements in this set.
155 *
156 * @return the number of elements in this set
157 */
158 public int size() {
159 return size;
160 }
|
1 /*
2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
104 /**
105 * The index corresponding to unseen in the elements array.
106 */
107 int unseenIndex = 0;
108
109 /**
110 * The bit representing the last element returned by this iterator
111 * but not removed, or zero if no such element exists.
112 */
113 long lastReturned = 0;
114
115 /**
116 * The index corresponding to lastReturned in the elements array.
117 */
118 int lastReturnedIndex = 0;
119
120 EnumSetIterator() {
121 unseen = elements[0];
122 }
123
124 @Override
125 public boolean hasNext() {
126 while (unseen == 0 && unseenIndex < elements.length - 1)
127 unseen = elements[++unseenIndex];
128 return unseen != 0;
129 }
130
131 @Override
132 @SuppressWarnings("unchecked")
133 public E next() {
134 if (!hasNext())
135 throw new NoSuchElementException();
136 lastReturned = unseen & -unseen;
137 lastReturnedIndex = unseenIndex;
138 unseen -= lastReturned;
139 return (E) universe[(lastReturnedIndex << 6)
140 + Long.numberOfTrailingZeros(lastReturned)];
141 }
142
143 @Override
144 public void remove() {
145 if (lastReturned == 0)
146 throw new IllegalStateException();
147 final long oldElements = elements[lastReturnedIndex];
148 elements[lastReturnedIndex] &= ~lastReturned;
149 if (oldElements != elements[lastReturnedIndex]) {
150 size--;
151 }
152 lastReturned = 0;
153 }
154 }
155
156 /**
157 * Returns the number of elements in this set.
158 *
159 * @return the number of elements in this set
160 */
161 public int size() {
162 return size;
163 }
|