1
package org.apache.lucene.util.collections;
3
import java.util.Arrays;
6
* Licensed to the Apache Software Foundation (ASF) under one or more
7
* contributor license agreements. See the NOTICE file distributed with
8
* this work for additional information regarding copyright ownership.
9
* The ASF licenses this file to You under the Apache License, Version 2.0
10
* (the "License"); you may not use this file except in compliance with
11
* the License. You may obtain a copy of the License at
13
* http://www.apache.org/licenses/LICENSE-2.0
15
* Unless required by applicable law or agreed to in writing, software
16
* distributed under the License is distributed on an "AS IS" BASIS,
17
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
* See the License for the specific language governing permissions and
19
* limitations under the License.
23
* A Class wrapper for a grow-able int[] which can be sorted and intersect with
26
* @lucene.experimental
28
public class IntArray {
31
* The int[] which holds the data
36
* Holds the number of items in the array.
41
* A flag which indicates whether a sort should occur of the array is
44
private boolean shouldSort;
47
* Construct a default IntArray, size 0 and surly a sort should not occur.
53
private void init(boolean realloc) {
62
* Intersects the data with a given {@link IntHashSet}.
65
* A given ArrayHashSetInt which holds the data to be intersected
68
public void intersect(IntHashSet set) {
70
for (int i = 0; i < size; ++i) {
71
if (set.contains(data[i])) {
72
data[newSize] = data[i];
80
* Intersects the data with a given IntArray
83
* A given IntArray which holds the data to be intersected agains
85
public void intersect(IntArray other) {
92
if (this.size > other.size) {
93
while (otherIndex < other.size && myIndex < size) {
94
while (otherIndex < other.size
95
&& other.data[otherIndex] < data[myIndex]) {
98
if (otherIndex == other.size) {
101
while (myIndex < size && other.data[otherIndex] > data[myIndex]) {
104
if (other.data[otherIndex] == data[myIndex]) {
105
data[newSize++] = data[myIndex];
111
while (otherIndex < other.size && myIndex < size) {
112
while (myIndex < size && other.data[otherIndex] > data[myIndex]) {
115
if (myIndex == size) {
118
while (otherIndex < other.size
119
&& other.data[otherIndex] < data[myIndex]) {
122
if (other.data[otherIndex] == data[myIndex]) {
123
data[newSize++] = data[myIndex];
133
* Return the size of the Array. Not the allocated size, but the number of
134
* values actually set.
136
* @return the (filled) size of the array
143
* Adds a value to the array.
148
public void addToArray(int value) {
149
if (size == data.length) {
150
int[] newArray = new int[2 * size + 1];
151
System.arraycopy(data, 0, newArray, 0, size);
160
* Equals method. Checking the sizes, than the values from the last index to
161
* the first (Statistically for random should be the same but for our
162
* specific use would find differences faster).
165
public boolean equals(Object o) {
166
if (!(o instanceof IntArray)) {
170
IntArray array = (IntArray) o;
171
if (array.size != size) {
178
boolean equal = true;
180
for (int i = size; i > 0 && equal;) {
182
equal = (array.data[i] == this.data[i]);
189
* Sorts the data. If it is needed.
194
Arrays.sort(data, 0, size);
199
* Calculates a hash-code for HashTables
202
public int hashCode() {
204
for (int i = 0; i < size; ++i) {
205
hash = data[i] ^ (hash * 31);
211
* Get an element from a specific index.
214
* index of which element should be retrieved.
216
public int get(int i) {
218
throw new ArrayIndexOutOfBoundsException(i);
223
public void set(int idx, int value) {
225
throw new ArrayIndexOutOfBoundsException(idx);
227
this.data[idx] = value;
231
* toString or not toString. That is the question!
234
public String toString() {
235
String s = "(" + size + ") ";
236
for (int i = 0; i < size; ++i) {
237
s += "" + data[i] + ", ";
243
* Clear the IntArray (set all elements to zero).
244
* @param resize - if resize is true, then clear actually allocates
245
* a new array of size 0, essentially 'clearing' the array and freeing
248
public void clear(boolean resize) {
b'\\ No newline at end of file'