Github user nzw0301 commented on a diff in the pull request:

    https://github.com/apache/incubator-hivemall/pull/111#discussion_r136694290
  
    --- Diff: 
core/src/main/java/hivemall/utils/collections/maps/Int2DoubleOpenHashTable.java 
---
    @@ -0,0 +1,417 @@
    +/*
    + * 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.
    + */
    +package hivemall.utils.collections.maps;
    +
    +import hivemall.utils.math.Primes;
    +
    +import java.io.Externalizable;
    +import java.io.IOException;
    +import java.io.ObjectInput;
    +import java.io.ObjectOutput;
    +import java.util.Arrays;
    +
    +/**
    + * An open-addressing hash table with double hashing
    + *
    + * @see http://en.wikipedia.org/wiki/Double_hashing
    + */
    +public class Int2DoubleOpenHashTable implements Externalizable {
    +    protected static final byte FREE = 0;
    +    protected static final byte FULL = 1;
    +    protected static final byte REMOVED = 2;
    +
    +    private static final float DEFAULT_LOAD_FACTOR = 0.7f;
    +    private static final float DEFAULT_GROW_FACTOR = 2.0f;
    +
    +    protected final transient float _loadFactor;
    +    protected final transient float _growFactor;
    +
    +    protected int _used = 0;
    +    protected int _threshold;
    +    protected double defaultReturnValue = -1.d;
    +
    +    protected int[] _keys;
    +    protected double[] _values;
    +    protected byte[] _states;
    +
    +    protected Int2DoubleOpenHashTable(int size, float loadFactor, float 
growFactor,
    +            boolean forcePrime) {
    +        if (size < 1) {
    +            throw new IllegalArgumentException();
    +        }
    +        this._loadFactor = loadFactor;
    +        this._growFactor = growFactor;
    +        int actualSize = forcePrime ? Primes.findLeastPrimeNumber(size) : 
size;
    +        this._keys = new int[actualSize];
    +        this._values = new double[actualSize];
    +        this._states = new byte[actualSize];
    +        this._threshold = (int) (actualSize * _loadFactor);
    +    }
    +
    +    public Int2DoubleOpenHashTable(int size, float loadFactor, float 
growFactor) {
    +        this(size, loadFactor, growFactor, true);
    +    }
    +
    +    public Int2DoubleOpenHashTable(int size) {
    +        this(size, DEFAULT_LOAD_FACTOR, DEFAULT_GROW_FACTOR, true);
    +    }
    +
    +    /**
    +     * Only for {@link Externalizable}
    +     */
    +    public Int2DoubleOpenHashTable() {// required for serialization
    +        this._loadFactor = DEFAULT_LOAD_FACTOR;
    +        this._growFactor = DEFAULT_GROW_FACTOR;
    +    }
    +
    +    public void defaultReturnValue(double v) {
    +        this.defaultReturnValue = v;
    +    }
    +
    +    public boolean containsKey(int key) {
    +        return findKey(key) >= 0;
    +    }
    +
    +    /**
    +     * @return -1.d if not found
    +     */
    +    public double get(int key) {
    +        int i = findKey(key);
    +        if (i < 0) {
    +            return defaultReturnValue;
    +        }
    +        return _values[i];
    +    }
    +
    +    public double put(int key, double value) {
    +        int hash = keyHash(key);
    +        int keyLength = _keys.length;
    +        int keyIdx = hash % keyLength;
    +
    +        boolean expanded = preAddEntry(keyIdx);
    +        if (expanded) {
    +            keyLength = _keys.length;
    +            keyIdx = hash % keyLength;
    +        }
    +
    +        int[] keys = _keys;
    +        double[] values = _values;
    +        byte[] states = _states;
    +
    +        if (states[keyIdx] == FULL) {// double hashing
    +            if (keys[keyIdx] == key) {
    +                double old = values[keyIdx];
    +                values[keyIdx] = value;
    +                return old;
    +            }
    +            // try second hash
    +            int decr = 1 + (hash % (keyLength - 2));
    +            for (;;) {
    +                keyIdx -= decr;
    +                if (keyIdx < 0) {
    +                    keyIdx += keyLength;
    +                }
    +                if (isFree(keyIdx, key)) {
    +                    break;
    +                }
    +                if (states[keyIdx] == FULL && keys[keyIdx] == key) {
    +                    double old = values[keyIdx];
    +                    values[keyIdx] = value;
    +                    return old;
    +                }
    +            }
    +        }
    +        keys[keyIdx] = key;
    +        values[keyIdx] = value;
    +        states[keyIdx] = FULL;
    +        ++_used;
    +        return defaultReturnValue;
    +    }
    +
    +    /** Return weather the required slot is free for new entry */
    +    protected boolean isFree(int index, int key) {
    +        byte stat = _states[index];
    +        if (stat == FREE) {
    +            return true;
    +        }
    +        if (stat == REMOVED && _keys[index] == key) {
    +            return true;
    +        }
    +        return false;
    +    }
    +
    +    /** @return expanded or not */
    +    protected boolean preAddEntry(int index) {
    --- End diff --
    
    I see. Thanks.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to