|
| 1 | +/*+***************************************************************************** |
| 2 | + * ___ _ ____ ____ |
| 3 | + * / _ \ _ _ ___ ___| |_| _ \| __ ) |
| 4 | + * | | | | | | |/ _ \/ __| __| | | | _ \ |
| 5 | + * | |_| | |_| | __/\__ \ |_| |_| | |_) | |
| 6 | + * \__\_\\__,_|\___||___/\__|____/|____/ |
| 7 | + * |
| 8 | + * Copyright (c) 2014-2019 Appsicle |
| 9 | + * Copyright (c) 2019-2026 QuestDB |
| 10 | + * |
| 11 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 12 | + * you may not use this file except in compliance with the License. |
| 13 | + * You may obtain a copy of the License at |
| 14 | + * |
| 15 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | + * |
| 17 | + * Unless required by applicable law or agreed to in writing, software |
| 18 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | + * See the License for the specific language governing permissions and |
| 21 | + * limitations under the License. |
| 22 | + * |
| 23 | + ******************************************************************************/ |
| 24 | + |
| 25 | +package io.questdb.client.std; |
| 26 | + |
| 27 | +import java.util.Arrays; |
| 28 | + |
| 29 | +public abstract class AbstractLowerCaseCharSequenceHashSet implements Mutable { |
| 30 | + protected static final int MIN_INITIAL_CAPACITY = 16; |
| 31 | + protected static final CharSequence noEntryKey = null; |
| 32 | + protected final double loadFactor; |
| 33 | + protected int capacity; |
| 34 | + protected int free; |
| 35 | + protected CharSequence[] keys; |
| 36 | + protected int mask; |
| 37 | + |
| 38 | + public AbstractLowerCaseCharSequenceHashSet(int initialCapacity, double loadFactor) { |
| 39 | + if (loadFactor <= 0d || loadFactor >= 1d) { |
| 40 | + throw new IllegalArgumentException("0 < loadFactor < 1"); |
| 41 | + } |
| 42 | + |
| 43 | + free = this.capacity = Math.max(initialCapacity, MIN_INITIAL_CAPACITY); |
| 44 | + this.loadFactor = loadFactor; |
| 45 | + keys = new CharSequence[Numbers.ceilPow2((int) (this.capacity / loadFactor))]; |
| 46 | + mask = keys.length - 1; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void clear() { |
| 51 | + Arrays.fill(keys, noEntryKey); |
| 52 | + free = capacity; |
| 53 | + } |
| 54 | + |
| 55 | + public int keyIndex(CharSequence key) { |
| 56 | + int index = Chars.lowerCaseHashCode(key) & mask; |
| 57 | + |
| 58 | + if (keys[index] == noEntryKey) { |
| 59 | + return index; |
| 60 | + } |
| 61 | + |
| 62 | + if (Chars.equalsIgnoreCase(key, keys[index])) { |
| 63 | + return -index - 1; |
| 64 | + } |
| 65 | + |
| 66 | + return probe(key, index); |
| 67 | + } |
| 68 | + |
| 69 | + public int size() { |
| 70 | + return capacity - free; |
| 71 | + } |
| 72 | + |
| 73 | + private int probe(CharSequence key, int index) { |
| 74 | + do { |
| 75 | + index = (index + 1) & mask; |
| 76 | + if (keys[index] == noEntryKey) { |
| 77 | + return index; |
| 78 | + } |
| 79 | + if (Chars.equalsIgnoreCase(key, keys[index])) { |
| 80 | + return -index - 1; |
| 81 | + } |
| 82 | + } while (true); |
| 83 | + } |
| 84 | +} |
0 commit comments