49873
|
1 |
/*
|
58877
|
2 |
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
|
49873
|
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
|
50609
|
7 |
* published by the Free Software Foundation.
|
49873
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
52578
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
49873
|
41 |
package jdk.internal.vm.compiler.collections;
|
|
42 |
|
|
43 |
import java.util.Iterator;
|
|
44 |
|
|
45 |
/**
|
|
46 |
* Memory efficient set data structure.
|
|
47 |
*
|
55509
|
48 |
* @since 19.0
|
49873
|
49 |
*/
|
|
50 |
public interface EconomicSet<E> extends UnmodifiableEconomicSet<E> {
|
|
51 |
|
|
52 |
/**
|
|
53 |
* Adds {@code element} to this set if it is not already present.
|
|
54 |
*
|
|
55 |
* @return {@code true} if this set did not already contain {@code element}.
|
55509
|
56 |
* @since 19.0
|
49873
|
57 |
*/
|
|
58 |
boolean add(E element);
|
|
59 |
|
|
60 |
/**
|
|
61 |
* Removes {@code element} from this set if it is present. This set will not contain
|
|
62 |
* {@code element} once the call returns.
|
|
63 |
*
|
55509
|
64 |
* @since 19.0
|
49873
|
65 |
*/
|
|
66 |
void remove(E element);
|
|
67 |
|
|
68 |
/**
|
|
69 |
* Removes all of the elements from this set. The set will be empty after this call returns.
|
|
70 |
*
|
55509
|
71 |
* @since 19.0
|
49873
|
72 |
*/
|
|
73 |
void clear();
|
|
74 |
|
|
75 |
/**
|
|
76 |
* Adds all of the elements in {@code other} to this set if they're not already present.
|
|
77 |
*
|
55509
|
78 |
* @since 19.0
|
49873
|
79 |
*/
|
|
80 |
default void addAll(EconomicSet<E> other) {
|
|
81 |
addAll(other.iterator());
|
|
82 |
}
|
|
83 |
|
|
84 |
/**
|
|
85 |
* Adds all of the elements in {@code values} to this set if they're not already present.
|
|
86 |
*
|
55509
|
87 |
* @since 19.0
|
49873
|
88 |
*/
|
|
89 |
default void addAll(Iterable<E> values) {
|
|
90 |
addAll(values.iterator());
|
|
91 |
}
|
|
92 |
|
|
93 |
/**
|
|
94 |
* Adds all of the elements enumerated by {@code iterator} to this set if they're not already
|
|
95 |
* present.
|
|
96 |
*
|
55509
|
97 |
* @since 19.0
|
49873
|
98 |
*/
|
|
99 |
default void addAll(Iterator<E> iterator) {
|
|
100 |
while (iterator.hasNext()) {
|
|
101 |
add(iterator.next());
|
|
102 |
}
|
|
103 |
}
|
|
104 |
|
|
105 |
/**
|
|
106 |
* Removes from this set all of its elements that are contained in {@code other}.
|
|
107 |
*
|
55509
|
108 |
* @since 19.0
|
49873
|
109 |
*/
|
|
110 |
default void removeAll(EconomicSet<E> other) {
|
|
111 |
removeAll(other.iterator());
|
|
112 |
}
|
|
113 |
|
|
114 |
/**
|
|
115 |
* Removes from this set all of its elements that are contained in {@code values}.
|
|
116 |
*
|
55509
|
117 |
* @since 19.0
|
49873
|
118 |
*/
|
|
119 |
default void removeAll(Iterable<E> values) {
|
|
120 |
removeAll(values.iterator());
|
|
121 |
}
|
|
122 |
|
|
123 |
/**
|
|
124 |
* Removes from this set all of its elements that are enumerated by {@code iterator}.
|
|
125 |
*
|
55509
|
126 |
* @since 19.0
|
49873
|
127 |
*/
|
|
128 |
default void removeAll(Iterator<E> iterator) {
|
|
129 |
while (iterator.hasNext()) {
|
|
130 |
remove(iterator.next());
|
|
131 |
}
|
|
132 |
}
|
|
133 |
|
|
134 |
/**
|
|
135 |
* Removes from this set all of its elements that are not contained in {@code other}.
|
|
136 |
*
|
55509
|
137 |
* @since 19.0
|
49873
|
138 |
*/
|
|
139 |
default void retainAll(EconomicSet<E> other) {
|
|
140 |
Iterator<E> iterator = iterator();
|
|
141 |
while (iterator.hasNext()) {
|
|
142 |
E key = iterator.next();
|
|
143 |
if (!other.contains(key)) {
|
|
144 |
iterator.remove();
|
|
145 |
}
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* Creates a new set guaranteeing insertion order when iterating over its elements with the
|
|
151 |
* default {@link Equivalence#DEFAULT} comparison strategy.
|
|
152 |
*
|
55509
|
153 |
* @since 19.0
|
49873
|
154 |
*/
|
|
155 |
static <E> EconomicSet<E> create() {
|
|
156 |
return EconomicSet.create(Equivalence.DEFAULT);
|
|
157 |
}
|
|
158 |
|
|
159 |
/**
|
|
160 |
* Creates a new set guaranteeing insertion order when iterating over its elements.
|
|
161 |
*
|
55509
|
162 |
* @since 19.0
|
49873
|
163 |
*/
|
|
164 |
static <E> EconomicSet<E> create(Equivalence strategy) {
|
|
165 |
return EconomicMapImpl.create(strategy, true);
|
|
166 |
}
|
|
167 |
|
|
168 |
/**
|
|
169 |
* Creates a new set guaranteeing insertion order when iterating over its elements with the
|
|
170 |
* default {@link Equivalence#DEFAULT} comparison strategy and inserts all elements of the
|
|
171 |
* specified collection.
|
|
172 |
*
|
55509
|
173 |
* @since 19.0
|
49873
|
174 |
*/
|
|
175 |
static <E> EconomicSet<E> create(int initialCapacity) {
|
|
176 |
return EconomicSet.create(Equivalence.DEFAULT, initialCapacity);
|
|
177 |
}
|
|
178 |
|
|
179 |
/**
|
|
180 |
* Creates a new set guaranteeing insertion order when iterating over its elements with the
|
|
181 |
* default {@link Equivalence#DEFAULT} comparison strategy and inserts all elements of the
|
|
182 |
* specified collection.
|
|
183 |
*
|
55509
|
184 |
* @since 19.0
|
49873
|
185 |
*/
|
|
186 |
static <E> EconomicSet<E> create(UnmodifiableEconomicSet<E> c) {
|
|
187 |
return EconomicSet.create(Equivalence.DEFAULT, c);
|
|
188 |
}
|
|
189 |
|
|
190 |
/**
|
|
191 |
* Creates a new set guaranteeing insertion order when iterating over its elements and
|
|
192 |
* initializes with the given capacity.
|
|
193 |
*
|
55509
|
194 |
* @since 19.0
|
49873
|
195 |
*/
|
|
196 |
static <E> EconomicSet<E> create(Equivalence strategy, int initialCapacity) {
|
|
197 |
return EconomicMapImpl.create(strategy, initialCapacity, true);
|
|
198 |
}
|
|
199 |
|
|
200 |
/**
|
|
201 |
* Creates a new set guaranteeing insertion order when iterating over its elements and inserts
|
|
202 |
* all elements of the specified collection.
|
|
203 |
*
|
55509
|
204 |
* @since 19.0
|
49873
|
205 |
*/
|
|
206 |
static <E> EconomicSet<E> create(Equivalence strategy, UnmodifiableEconomicSet<E> c) {
|
|
207 |
return EconomicMapImpl.create(strategy, c, true);
|
|
208 |
}
|
|
209 |
}
|