~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/src/java/org/apache/lucene/util/IntsRef.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.util;
2
 
 
3
 
/**
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
10
 
 *
11
 
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
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.
18
 
 */
19
 
 
20
 
/** Represents int[], as a slice (offset + length) into an
21
 
 *  existing int[].
22
 
 *
23
 
 *  @lucene.internal */
24
 
public final class IntsRef implements Comparable<IntsRef> {
25
 
 
26
 
  public int[] ints;
27
 
  public int offset;
28
 
  public int length;
29
 
 
30
 
  public IntsRef() {
31
 
  }
32
 
 
33
 
  public IntsRef(int capacity) {
34
 
    ints = new int[capacity];
35
 
  }
36
 
 
37
 
  public IntsRef(int[] ints, int offset, int length) {
38
 
    this.ints = ints;
39
 
    this.offset = offset;
40
 
    this.length = length;
41
 
  }
42
 
 
43
 
  public IntsRef(IntsRef other) {
44
 
    copy(other);
45
 
  }
46
 
 
47
 
  @Override
48
 
  public Object clone() {
49
 
    return new IntsRef(this);
50
 
  }
51
 
 
52
 
  @Override
53
 
  public int hashCode() {
54
 
    final int prime = 31;
55
 
    int result = 0;
56
 
    final int end = offset + length;
57
 
    for(int i = offset; i < end; i++) {
58
 
      result = prime * result + ints[i];
59
 
    }
60
 
    return result;
61
 
  }
62
 
  
63
 
  @Override
64
 
  public boolean equals(Object other) {
65
 
    return this.intsEquals((IntsRef) other);
66
 
  }
67
 
 
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]) {
75
 
          return false;
76
 
        }
77
 
      }
78
 
      return true;
79
 
    } else {
80
 
      return false;
81
 
    }
82
 
  }
83
 
 
84
 
  /** Signed int order comparison */
85
 
  public int compareTo(IntsRef other) {
86
 
    if (this == other) return 0;
87
 
 
88
 
    final int[] aInts = this.ints;
89
 
    int aUpto = this.offset;
90
 
    final int[] bInts = other.ints;
91
 
    int bUpto = other.offset;
92
 
 
93
 
    final int aStop = aUpto + Math.min(this.length, other.length);
94
 
 
95
 
    while(aUpto < aStop) {
96
 
      int aInt = aInts[aUpto++];
97
 
      int bInt = bInts[bUpto++];
98
 
      if (aInt > bInt) {
99
 
        return 1;
100
 
      } else if (aInt < bInt) {
101
 
        return -1;
102
 
      }
103
 
    }
104
 
 
105
 
    // One is a prefix of the other, or, they are equal:
106
 
    return this.length - other.length;
107
 
  }
108
 
 
109
 
  public void copy(IntsRef other) {
110
 
    if (ints == null) {
111
 
      ints = new int[other.length];
112
 
    } else {
113
 
      ints = ArrayUtil.grow(ints, other.length);
114
 
    }
115
 
    System.arraycopy(other.ints, other.offset, ints, 0, other.length);
116
 
    length = other.length;
117
 
    offset = 0;
118
 
  }
119
 
 
120
 
  public void grow(int newLength) {
121
 
    if (ints.length < newLength) {
122
 
      ints = ArrayUtil.grow(ints, newLength);
123
 
    }
124
 
  }
125
 
 
126
 
  @Override
127
 
  public String toString() {
128
 
    StringBuilder sb = new StringBuilder();
129
 
    sb.append('[');
130
 
    final int end = offset + length;
131
 
    for(int i=offset;i<end;i++) {
132
 
      if (i > offset) {
133
 
        sb.append(' ');
134
 
      }
135
 
      sb.append(Integer.toHexString(ints[i]));
136
 
    }
137
 
    sb.append(']');
138
 
    return sb.toString();
139
 
  }
140
 
}