1 package org.apache.lucene.util;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 /** Represents int[], as a slice (offset + length) into an
24 public final class IntsRef implements Comparable<IntsRef> {
33 public IntsRef(int capacity) {
34 ints = new int[capacity];
37 public IntsRef(int[] ints, int offset, int length) {
43 public IntsRef(IntsRef other) {
48 public Object clone() {
49 return new IntsRef(this);
53 public int hashCode() {
56 final int end = offset + length;
57 for(int i = offset; i < end; i++) {
58 result = prime * result + ints[i];
64 public boolean equals(Object other) {
65 return this.intsEquals((IntsRef) other);
68 public boolean intsEquals(IntsRef other) {
69 if (length == other.length) {
70 int otherUpto = other.offset;
71 final int[] otherInts = other.ints;
72 final int end = offset + length;
73 for(int upto=offset;upto<end;upto++,otherUpto++) {
74 if (ints[upto] != otherInts[otherUpto]) {
84 /** Signed int order comparison */
85 public int compareTo(IntsRef other) {
86 if (this == other) return 0;
88 final int[] aInts = this.ints;
89 int aUpto = this.offset;
90 final int[] bInts = other.ints;
91 int bUpto = other.offset;
93 final int aStop = aUpto + Math.min(this.length, other.length);
95 while(aUpto < aStop) {
96 int aInt = aInts[aUpto++];
97 int bInt = bInts[bUpto++];
100 } else if (aInt < bInt) {
105 // One is a prefix of the other, or, they are equal:
106 return this.length - other.length;
109 public void copy(IntsRef other) {
111 ints = new int[other.length];
113 ints = ArrayUtil.grow(ints, other.length);
115 System.arraycopy(other.ints, other.offset, ints, 0, other.length);
116 length = other.length;
120 public void grow(int newLength) {
121 if (ints.length < newLength) {
122 ints = ArrayUtil.grow(ints, newLength);
127 public String toString() {
128 StringBuilder sb = new StringBuilder();
130 final int end = offset + length;
131 for(int i=offset;i<end;i++) {
135 sb.append(Integer.toHexString(ints[i]));
138 return sb.toString();