2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.lucene.search.grouping;
20 import java.util.Arrays;
22 /** A native int set where one value is reserved to mean "EMPTY" */
23 class SentinelIntSet {
26 public final int emptyVal;
27 public int rehashCount; // the count at which a rehash should be done
29 public SentinelIntSet(int size, int emptyVal) {
30 this.emptyVal = emptyVal;
31 int tsize = Math.max(org.apache.lucene.util.BitUtil.nextHighestPowerOfTwo(size), 1);
32 rehashCount = tsize - (tsize>>2);
33 if (size >= rehashCount) { // should be able to hold "size" w/o rehashing
35 rehashCount = tsize - (tsize>>2);
37 keys = new int[tsize];
43 Arrays.fill(keys, emptyVal);
47 public int hash(int key) {
51 public int size() { return count; }
53 /** returns the slot for this key */
54 public int getSlot(int key) {
55 assert key != emptyVal;
57 int s = h & (keys.length-1);
58 if (keys[s] == key || keys[s]== emptyVal) return s;
60 int increment = (h>>7)|1;
62 s = (s + increment) & (keys.length-1);
63 } while (keys[s] != key && keys[s] != emptyVal);
67 /** returns the slot for this key, or -slot-1 if not found */
68 public int find(int key) {
69 assert key != emptyVal;
71 int s = h & (keys.length-1);
72 if (keys[s] == key) return s;
73 if (keys[s] == emptyVal) return -s-1;
75 int increment = (h>>7)|1;
77 s = (s + increment) & (keys.length-1);
78 if (keys[s] == key) return s;
79 if (keys[s] == emptyVal) return -s-1;
83 public boolean exists(int key) {
84 return find(key) >= 0;
87 public int put(int key) {
90 if (count >= rehashCount) {
102 public void rehash() {
103 int newSize = keys.length << 1;
104 int[] oldKeys = keys;
105 keys = new int[newSize];
106 if (emptyVal != 0) Arrays.fill(keys, emptyVal);
108 for (int i=0; i<oldKeys.length; i++) {
109 int key = oldKeys[i];
110 if (key == emptyVal) continue;
111 int newSlot = getSlot(key);
114 rehashCount = newSize - (newSize>>2);