Mercurial > hg > truffle
comparison graal/com.oracle.max.base/src/com/sun/max/collect/PoolSet.java @ 3733:e233f5660da4
Added Java files from Maxine project.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Sat, 17 Dec 2011 19:59:18 +0100 |
parents | |
children | bc8527f3071c |
comparison
equal
deleted
inserted
replaced
3732:3e2e8b8abdaf | 3733:e233f5660da4 |
---|---|
1 /* | |
2 * Copyright (c) 2007, 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. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.sun.max.collect; | |
24 | |
25 import java.util.*; | |
26 | |
27 /** | |
28 * A representation for a subset of objects in a {@linkplain Pool pool}. The recommended mechanism for creating a pool | |
29 * set is by calling {@link #noneOf(Pool)}, {@link #allOf(Pool)}, {@link #of(Pool, PoolObject[])} or | |
30 * {@link #of(Pool, PoolObject, PoolObject...)}. These methods ensure that the most efficient and compact pool set | |
31 * object is created based on the length of the underlying pool. | |
32 */ | |
33 public abstract class PoolSet<T extends PoolObject> implements Cloneable, Iterable<T> { | |
34 | |
35 protected final Pool<T> pool; | |
36 | |
37 protected PoolSet(Pool<T> pool) { | |
38 this.pool = pool; | |
39 } | |
40 | |
41 /** | |
42 * Gets the number of objects in this set. | |
43 */ | |
44 public abstract int size(); | |
45 | |
46 /** | |
47 * Adds a value to this set. The value must be in the {@linkplain #pool() underlying pool}. | |
48 */ | |
49 public abstract void add(T value); | |
50 | |
51 /** | |
52 * Determines if a given value is in this set. The value must be in the {@linkplain #pool() underlying pool}. | |
53 */ | |
54 public abstract boolean contains(T value); | |
55 | |
56 /** | |
57 * Determines if this set contains all the values present in a given set. | |
58 */ | |
59 public boolean containsAll(PoolSet<T> others) { | |
60 for (T value : others) { | |
61 if (!contains(value)) { | |
62 return false; | |
63 } | |
64 } | |
65 return true; | |
66 } | |
67 | |
68 /** | |
69 * Removes a given value from this set. The value must be in the {@linkplain #pool() underlying pool}. | |
70 * | |
71 * @return true if this set contained {@code value} | |
72 */ | |
73 public abstract boolean remove(T value); | |
74 | |
75 /** | |
76 * Removes an arbitrary value from this set. The value must be in the {@linkplain #pool() underlying pool}. | |
77 * | |
78 * @throws NoSuchElementException if this set is empty | |
79 */ | |
80 public abstract T removeOne() throws NoSuchElementException; | |
81 | |
82 /** | |
83 * Gets the pool containing the values that are in or may be added to this set. | |
84 */ | |
85 public Pool<T> pool() { | |
86 return pool; | |
87 } | |
88 | |
89 /** | |
90 * Clears all entries from this set. | |
91 */ | |
92 public abstract void clear(); | |
93 | |
94 /** | |
95 * Adds all entries from the pool to this set. | |
96 * | |
97 * @return this set | |
98 */ | |
99 public abstract PoolSet<T> addAll(); | |
100 | |
101 /** | |
102 * Adds all entries from another pool set to this set. | |
103 */ | |
104 public void or(PoolSet<T> others) { | |
105 for (T element : others) { | |
106 add(element); | |
107 } | |
108 } | |
109 | |
110 /** | |
111 * Removes all the entries from this set that are not in a given set. | |
112 */ | |
113 public abstract void and(PoolSet<T> others); | |
114 | |
115 /** | |
116 * Creates a copy of this pool set. The copy has the same pool object as this pool set. | |
117 */ | |
118 @Override | |
119 public abstract PoolSet<T> clone(); | |
120 | |
121 /** | |
122 * @return true if there are no values in this set | |
123 */ | |
124 public abstract boolean isEmpty(); | |
125 | |
126 /** | |
127 * @see #toString(PoolSet) | |
128 */ | |
129 @Override | |
130 public String toString() { | |
131 return toString(this); | |
132 } | |
133 | |
134 public T[] toArray(T[] a) { | |
135 assert a.length == size(); | |
136 int i = 0; | |
137 for (T element : this) { | |
138 a[i++] = element; | |
139 } | |
140 return a; | |
141 } | |
142 | |
143 /** | |
144 * Creates an empty pool set for a given pool. | |
145 * | |
146 * @param <T> the type of objects in {@code pool} | |
147 * @param pool the pool of objects that the returned set provides a view upon | |
148 * @return an empty pool set that can be subsequently modified to contain objects from {@code pool} | |
149 */ | |
150 public static <T extends PoolObject> PoolSet<T> noneOf(Pool<T> pool) { | |
151 if (pool.length() <= PoolSet64.MAX_POOL_SIZE) { | |
152 return new PoolSet64<T>(pool); | |
153 } | |
154 if (pool.length() <= PoolSet128.MAX_POOL_SIZE) { | |
155 return new PoolSet128<T>(pool); | |
156 } | |
157 return new PoolBitSet<T>(pool); | |
158 } | |
159 | |
160 /** | |
161 * Creates a pool set initially containing all the objects in a given pool. | |
162 * | |
163 * @param <T> the type of objects in {@code pool} | |
164 * @param pool the pool of objects that the returned set provides a view upon | |
165 * @return a pool set containing all the objects in {@code pool} | |
166 */ | |
167 public static <T extends PoolObject> PoolSet<T> allOf(Pool<T> pool) { | |
168 return noneOf(pool).addAll(); | |
169 } | |
170 | |
171 /** | |
172 * Creates a pool set initially containing one or more objects from a given pool. | |
173 * | |
174 * @param <T> the type of objects in {@code pool} | |
175 * @param <S> the type of objects that can be added to the pool by this method | |
176 * @param pool the pool of objects that the returned set provides a view upon | |
177 * @param first an object that will be in the returned set | |
178 * @param rest zero or more objects that will be in the returned set | |
179 * @return a pool set containing {@code first} and all the objects in {@code rest} | |
180 */ | |
181 public static <T extends PoolObject, S extends T> PoolSet<T> of(Pool<T> pool, S first, S... rest) { | |
182 final PoolSet<T> poolSet = noneOf(pool); | |
183 poolSet.add(first); | |
184 for (T object : rest) { | |
185 poolSet.add(object); | |
186 } | |
187 return poolSet; | |
188 } | |
189 | |
190 /** | |
191 * Creates a pool set initially containing all the objects specified by a given array that are also in a given pool. | |
192 * | |
193 * @param <T> the type of objects in {@code pool} | |
194 * @param <S> the type of objects that can be added to the pool by this method | |
195 * @param pool the pool of objects that the returned set provides a view upon | |
196 * @param objects zero or more objects that will be in the returned set | |
197 * @return a pool set containing all the objects in {@code objects} | |
198 */ | |
199 public static <T extends PoolObject, S extends T> PoolSet<T> of(Pool<T> pool, S[] objects) { | |
200 final PoolSet<T> poolSet = noneOf(pool); | |
201 for (T object : objects) { | |
202 poolSet.add(object); | |
203 } | |
204 return poolSet; | |
205 } | |
206 | |
207 /** | |
208 * Adds all objects returned by a given iterable's iterator to a given pool set. | |
209 * | |
210 * @param <T> the type of objects in {@code poolSet} | |
211 * @param poolSet the set to which the objects are added | |
212 * @param elements a collection of objects | |
213 */ | |
214 public static <T extends PoolObject> void addAll(PoolSet<T> poolSet, Iterable<T> elements) { | |
215 for (T element : elements) { | |
216 poolSet.add(element); | |
217 } | |
218 } | |
219 | |
220 public static <T extends PoolObject> boolean match(PoolSet<T> poolSet1, PoolSet<T> poolSet2) { | |
221 if (!poolSet1.pool().equals(poolSet2.pool())) { | |
222 return false; | |
223 } | |
224 final Iterator<T> iterator1 = poolSet1.iterator(); | |
225 final Iterator<T> iterator2 = poolSet2.iterator(); | |
226 while (iterator1.hasNext() && iterator2.hasNext()) { | |
227 if (!iterator1.next().equals(iterator2.next())) { | |
228 return false; | |
229 } | |
230 } | |
231 return iterator1.hasNext() == iterator2.hasNext(); | |
232 } | |
233 | |
234 /** | |
235 * Gets a string representation of a given pool set. The returned string is delimited by "{" and "}". For each | |
236 * object in the given pool set, its {@code toString()} representation is added to the string followed by ", " if it | |
237 * is not the last object in the set. | |
238 * | |
239 * @param poolSet a pool set for which a string representation is required | |
240 */ | |
241 public static String toString(PoolSet poolSet) { | |
242 String s = "{"; | |
243 String delimiter = ""; | |
244 for (Object value : poolSet) { | |
245 s += delimiter + value; | |
246 delimiter = ", "; | |
247 } | |
248 s += "}"; | |
249 return s; | |
250 } | |
251 } |