X-Git-Url: https://git.mdrn.pl/pylucene.git/blobdiff_plain/a2e61f0c04805cfcb8706176758d1283c7e3a55c..aaeed5504b982cf3545252ab528713250aa33eed:/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/util/IntsRef.java?ds=sidebyside diff --git a/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/util/IntsRef.java b/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/util/IntsRef.java new file mode 100644 index 0000000..ee1bd2e --- /dev/null +++ b/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/util/IntsRef.java @@ -0,0 +1,140 @@ +package org.apache.lucene.util; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** Represents int[], as a slice (offset + length) into an + * existing int[]. + * + * @lucene.internal */ +public final class IntsRef implements Comparable { + + public int[] ints; + public int offset; + public int length; + + public IntsRef() { + } + + public IntsRef(int capacity) { + ints = new int[capacity]; + } + + public IntsRef(int[] ints, int offset, int length) { + this.ints = ints; + this.offset = offset; + this.length = length; + } + + public IntsRef(IntsRef other) { + copy(other); + } + + @Override + public Object clone() { + return new IntsRef(this); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 0; + final int end = offset + length; + for(int i = offset; i < end; i++) { + result = prime * result + ints[i]; + } + return result; + } + + @Override + public boolean equals(Object other) { + return this.intsEquals((IntsRef) other); + } + + public boolean intsEquals(IntsRef other) { + if (length == other.length) { + int otherUpto = other.offset; + final int[] otherInts = other.ints; + final int end = offset + length; + for(int upto=offset;upto bInt) { + return 1; + } else if (aInt < bInt) { + return -1; + } + } + + // One is a prefix of the other, or, they are equal: + return this.length - other.length; + } + + public void copy(IntsRef other) { + if (ints == null) { + ints = new int[other.length]; + } else { + ints = ArrayUtil.grow(ints, other.length); + } + System.arraycopy(other.ints, other.offset, ints, 0, other.length); + length = other.length; + offset = 0; + } + + public void grow(int newLength) { + if (ints.length < newLength) { + ints = ArrayUtil.grow(ints, newLength); + } + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append('['); + final int end = offset + length; + for(int i=offset;i offset) { + sb.append(' '); + } + sb.append(Integer.toHexString(ints[i])); + } + sb.append(']'); + return sb.toString(); + } +}