forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueueReader.java
More file actions
202 lines (183 loc) · 4.96 KB
/
QueueReader.java
File metadata and controls
202 lines (183 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package soot.util.queue;
import java.util.Collection;
import java.util.Collections;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 2003 Ondrej Lhotak
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
import java.util.NoSuchElementException;
import soot.util.Invalidable;
/**
* A queue of Object's. One can add objects to the queue, and they are later read by a QueueReader. One can create arbitrary
* numbers of QueueReader's for a queue, and each one receives all the Object's that are added. Only objects that have not
* been read by all the QueueReader's are kept. A QueueReader only receives the Object's added to the queue <b>after</b> the
* QueueReader was created.
*
* @author Ondrej Lhotak
*/
public class QueueReader<E> implements java.util.Iterator<E> {
protected E[] q;
protected int index;
protected QueueReader(E[] q, int index) {
this.q = q;
this.index = index;
}
/**
* Returns (and removes) the next object in the queue, or null if there are none.
*/
@SuppressWarnings("unchecked")
public E next() {
Object ret = null;
do {
if (q[index] == null) {
throw new NoSuchElementException();
}
if (index == q.length - 1) {
q = (E[]) q[index];
index = 0;
if (q[index] == null) {
throw new NoSuchElementException();
}
}
ret = q[index];
if (ret == ChunkedQueue.NULL_CONST) {
ret = null;
}
index++;
} while (skip(ret));
return (E) ret;
}
protected boolean skip(Object ret) {
if (ret instanceof Invalidable) {
final Invalidable invalidable = (Invalidable) ret;
if (invalidable.isInvalid()) {
return true;
}
}
return ret == ChunkedQueue.DELETED_CONST;
}
/** Returns true iff there is currently another object in the queue. */
@SuppressWarnings("unchecked")
public boolean hasNext() {
do {
E ret = q[index];
if (ret == null) {
return false;
}
if (index == q.length - 1) {
q = (E[]) ret;
index = 0;
if (q[index] == null) {
return false;
}
}
if (skip(ret)) {
index++;
} else {
return true;
}
} while (true);
}
/**
* Removes an element from the underlying queue. This operation can only delete elements that have not yet been consumed by
* this reader.
*
* @param o
* The element to remove
*/
public void remove(E o) {
if (o instanceof Invalidable) {
((Invalidable) o).invalidate();
return;
}
remove(Collections.singleton(o));
}
/**
* Removes elements from the underlying queue. This operation can only delete elements that have not yet been consumed by
* this reader.
*
* @param toRemove
* The elements to remove
*/
@SuppressWarnings("unchecked")
public void remove(Collection<E> toRemove) {
boolean allInvalidable = true;
for (E o : toRemove) {
if (!(o instanceof Invalidable)) {
allInvalidable = false;
continue;
}
((Invalidable) o).invalidate();
}
if (allInvalidable) {
return;
}
int idx = 0;
Object[] curQ = q;
while (curQ[idx] != null) {
// Do we need to switch to a new list?
if (idx == curQ.length - 1) {
curQ = (E[]) curQ[idx];
idx = 0;
}
// Is this the element to delete?
if (toRemove.contains(curQ[idx])) {
curQ[idx] = ChunkedQueue.DELETED_CONST;
}
// Next element
idx++;
}
}
@SuppressWarnings("unchecked")
public void remove() {
q[index - 1] = (E) ChunkedQueue.DELETED_CONST;
}
public QueueReader<E> clone() {
return new QueueReader<E>(q, index);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
boolean isFirst = true;
int idx = index;
Object[] curArray = q;
while (idx < curArray.length) {
Object curObj = curArray[idx];
if (curObj == null) {
break;
}
if (isFirst) {
isFirst = false;
} else {
sb.append(", ");
}
if (curObj instanceof Object[]) {
curArray = (Object[]) curObj;
idx = 0;
} else {
sb.append(curObj.toString());
idx++;
}
}
sb.append("]");
return sb.toString();
}
}