MCPcopy Index your code
hub / github.com/antlr/codebuff / construct

Method construct

output/java_guava/1.4.17/ImmutableSet.java:148–196  ·  view source on GitHub ↗

Constructs an ImmutableSet from the first n elements of the specified array. If k is the size of the returned ImmutableSet, then the unique elements of elements will be in the first k positions, and elements[i] == null for k <= i < n.

(int n, Object... elements)

Source from the content-addressed store, hash-verified

146 */
147
148 private static <E> ImmutableSet<E> construct(int n, Object... elements) {
149 switch (n) {
150 case 0:
151 return of();
152 case 1:
153 @SuppressWarnings("unchecked") // safe; elements contains only E's
154 E elem = (E) elements[0];
155 return of(elem);
156 default:
157 // continue below to handle the general case
158
159 }
160 int tableSize = chooseTableSize(n);
161 Object[] table = new Object[tableSize];
162 int mask = tableSize - 1;
163 int hashCode = 0;
164 int uniques = 0;
165 for (int i = 0; i < n; i++) {
166 Object element = checkElementNotNull(elements[i], i);
167 int hash = element.hashCode();
168 for (int j = Hashing.smear(hash); ; j++) {
169 int index = j & mask;
170 Object value = table[index];
171 if (value == null) {
172 // Came to an empty slot. Put the element here.
173 elements[uniques++] = element;
174 table[index] = element;
175 hashCode += hash;
176 break;
177 } else if (value.equals(element)) {
178 break;
179 }
180 }
181 }
182 Arrays.fill(elements, uniques, n, null);
183 if (uniques == 1) {
184 // There is only one element or elements are all duplicates
185 @SuppressWarnings("unchecked") // we are careful to only pass in E
186 E element = (E) elements[0];
187 return new SingletonImmutableSet<E>(element, hashCode);
188 } else if (tableSize != chooseTableSize(uniques)) {
189 // Resize the table when the array includes too many duplicates.
190 // when this happens, we have already made a copy
191 return construct(uniques, elements);
192 } else {
193 Object[] uniqueElements = (uniques < elements.length) ? ObjectArrays.arraysCopyOf(elements, uniques) : elements;
194 return new RegularImmutableSet<E>(uniqueElements, hashCode, table, mask);
195 }
196 }
197
198 // We use power-of-2 tables, and this is the highest int that's a power of 2
199

Callers 3

ofMethod · 0.95
copyOfMethod · 0.95
buildMethod · 0.45

Calls 7

ofMethod · 0.95
chooseTableSizeMethod · 0.95
smearMethod · 0.95
arraysCopyOfMethod · 0.95
hashCodeMethod · 0.65
equalsMethod · 0.65
checkElementNotNullMethod · 0.45

Tested by

no test coverage detected