📄 doubleintindex.java
字号:
/* Copyright (c) 2001-2005, The HSQL Development Group * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the HSQL Development Group nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG, * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package org.hsqldb.lib;import java.util.NoSuchElementException;/** * Maintains an ordered integer->integer lookup table, consisting of two * columns, one for keys, the other for values. * * The table is sorted on either the key or value column, depending on the calls to * setKeysSearchTarget() or setValuesSearchTarget(). By default, the table is * sorted on values.<p> * * findXXX() methods return the array index into the list * pair containing a matching key or value, or or -1 if not found.<p> * * Sorting methods originally contributed by Tony Lai. * * @author fredt@users * @version 1.8.0 * @since 1.8.0 */public class DoubleIntIndex implements IntLookup { private int count = 0; private int capacity; private boolean sorted = true; private boolean sortOnValues = true; private boolean hasChanged; private final boolean fixedSize; private int[] keys; private int[] values;// private int targetSearchValue; public DoubleIntIndex(int capacity, boolean fixedSize) { this.capacity = capacity; keys = new int[capacity]; values = new int[capacity]; this.fixedSize = fixedSize; hasChanged = true; } public synchronized int getKey(int i) { if (i < 0 || i >= count) { throw new IndexOutOfBoundsException(); } return keys[i]; } public synchronized int getValue(int i) { if (i < 0 || i >= count) { throw new IndexOutOfBoundsException(); } return values[i]; } /** * Modifies an existing pair. * @param i the index * @param key the key */ public synchronized void setKey(int i, int key) { if (i < 0 || i >= count) { throw new IndexOutOfBoundsException(); } if (!sortOnValues) { sorted = false; } keys[i] = key; } /** * Modifies an existing pair. * @param i the index * @param value the value */ public synchronized void setValue(int i, int value) { if (i < 0 || i >= count) { throw new IndexOutOfBoundsException(); } if (sortOnValues) { sorted = false; } values[i] = value; } public synchronized int size() { return count; } public synchronized int capacity() { return capacity; } /** * Adds a pair into the table. * * @param key the key * @param value the value * @return true or false depending on success */ public synchronized boolean addUnsorted(int key, int value) { if (count == capacity) { if (fixedSize) { return false; } else { doubleCapacity(); } } if (sorted && count != 0) { if (sortOnValues) { if (value < values[count - 1]) { sorted = false; } } else { if (value < keys[count - 1]) { sorted = false; } } } hasChanged = true; keys[count] = key; values[count] = value; count++; return true; } /** * Adds a key, value pair into the table with the guarantee that the key * is equal or larger than the largest existing key. This prevents a sort * from taking place on next call to find() * * @param key the key * @param value the value * @return true or false depending on success */ public synchronized boolean addSorted(int key, int value) { if (count == capacity) { if (fixedSize) { return false; } else { doubleCapacity(); } } if (count != 0 && value < values[count - 1]) { return false; } hasChanged = true; keys[count] = key; values[count] = value; count++; return true; } /** * Adds a pair, ensuring no duplicate key xor value already exists in the * current search target column. * @param key the key * @param value the value * @return true or false depending on success */ public synchronized boolean addUnique(int key, int value) { if (count == capacity) { if (fixedSize) { return false; } else { doubleCapacity(); } } if (!sorted) { fastQuickSort(); } targetSearchValue = sortOnValues ? value : key; int i = binaryEmptySlotSearch(); if (i == -1) { return false; } hasChanged = true; if (count != i) { moveRows(i, i + 1, count - i); } keys[i] = key; values[i] = value; count++; return true; } /** * Adds a pair, maintaining sorted order * current search target column. * @param key the key * @param value the value * @return true or false depending on success */ public synchronized boolean add(int key, int value) { if (count == capacity) { if (fixedSize) { return false; } else { doubleCapacity(); } } if (!sorted) { fastQuickSort(); } targetSearchValue = sortOnValues ? value : key; int i = binarySlotSearch(); if (i == -1) { return false; } hasChanged = true; if (count != i) { moveRows(i, i + 1, count - i); } keys[i] = key; values[i] = value; count++; return true; } public int lookupFirstEqual(int key) throws NoSuchElementException { if (sortOnValues) { sorted = false; sortOnValues = false; } int i = findFirstEqualKeyIndex(key); if (i == -1) { throw new NoSuchElementException(); } return getValue(i); } public int lookupFirstGreaterEqual(int key) throws NoSuchElementException { if (sortOnValues) { sorted = false; sortOnValues = false; } int i = findFirstGreaterEqualKeyIndex(key); if (i == -1) { throw new NoSuchElementException(); } return getValue(i); } public synchronized void setValuesSearchTarget() { if (!sortOnValues) { sorted = false; } sortOnValues = true; } public synchronized void setKeysSearchTarget() { if (sortOnValues) { sorted = false; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -