⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tintdoublehashmap.java~2~

📁 垃圾邮件过滤器源代码
💻 JAVA~2~
📖 第 1 页 / 共 2 页
字号:
        HashProcedure p = new HashProcedure();        forEachEntry(p);        return p.getHashCode();    }    private final class HashProcedure implements TIntDoubleProcedure {        private int h = 0;        public int getHashCode() {            return h;        }        public final boolean execute(int key, double value) {            h += (_hashingStrategy.computeHashCode(key) ^ HashFunctions.hash(value));            return true;        }    }    private static final class EqProcedure implements TIntDoubleProcedure {        private final TIntDoubleHashMap _otherMap;        EqProcedure(TIntDoubleHashMap otherMap) {            _otherMap = otherMap;        }        public final boolean execute(int key, double value) {            int index = _otherMap.index(key);            if (index >= 0 && eq(value, _otherMap.get(key))) {                return true;            }            return false;        }        /**         * Compare two doubles for equality.         */        private final boolean eq(double v1, double v2) {            return v1 == v2;        }    }    /**     * removes the mapping at <tt>index</tt> from the map.     *     * @param index an <code>int</code> value     */    protected void removeAt(int index) {        super.removeAt(index);  // clear key, state; adjust size        _values[index] = (double)0;    }    /**     * Returns the values of the map.     * 返回映射中的值     * @return a <code>Collection</code> value     */    public double[] getValues() {        double[] vals = new double[size()];        double[] v = _values;        byte[] states = _states;        for (int i = v.length, j = 0; i-- > 0;) {          if (states[i] == FULL) {            vals[j++] = v[i];          }        }        return vals;    }    /**     * returns the keys of the map.     * 取得映射的键值     * @return a <code>Set</code> value     */    public int[] keys() {        int[] keys = new int[size()];        int[] k = _set;        byte[] states = _states;        for (int i = k.length, j = 0; i-- > 0;) {          if (states[i] == FULL) {            keys[j++] = k[i];          }        }        return keys;    }    /**     * checks for the presence of <tt>val</tt> in the values of the map.     *     * @param val an <code>double</code> value     * @return a <code>boolean</code> value     */    public boolean containsValue(double val) {        byte[] states = _states;        double[] vals = _values;        for (int i = vals.length; i-- > 0;) {            if (states[i] == FULL && val == vals[i]) {                return true;            }        }        return false;    }    /**     * checks for the present of <tt>key</tt> in the keys of the map.     * 检查一个键值是否出现在HASH表中     * @param key an <code>int</code> value     * @return a <code>boolean</code> value     */    public boolean containsKey(int key) {        return contains(key);    }    /**     * Executes <tt>procedure</tt> for each key in the map.     *     * @param procedure a <code>TIntProcedure</code> value     * @return false if the loop over the keys terminated because     * the procedure returned false for some key.     */    public boolean forEachKey(TIntProcedure procedure) {        return forEach(procedure);    }    /**     * Executes <tt>procedure</tt> for each value in the map.     *     * @param procedure a <code>TDoubleProcedure</code> value     * @return false if the loop over the values terminated because     * the procedure returned false for some value.     */    public boolean forEachValue(TDoubleProcedure procedure) {        byte[] states = _states;        double[] values = _values;        for (int i = values.length; i-- > 0;) {            if (states[i] == FULL && ! procedure.execute(values[i])) {                return false;            }        }        return true;    }    /**     * Executes <tt>procedure</tt> for each key/value entry in the map.     * 对HASH表中的每个入口执行同一过程     * @param procedure a <code>TOIntDoubleProcedure</code> value     * @return false if the loop over the entries terminated because     * the procedure returned false for some entry.     */    public boolean forEachEntry(TIntDoubleProcedure procedure) {        byte[] states = _states;        int[] keys = _set;        double[] values = _values;        for (int i = keys.length; i-- > 0;) {            if (states[i] == FULL && ! procedure.execute(keys[i],values[i])) {                return false;            }        }        return true;    }    /**     * Retains only those entries in the map for which the procedure     * returns a true value.     *     * @param procedure determines which entries to keep     * @return true if the map was modified.     */    public boolean retainEntries(TIntDoubleProcedure procedure) {        boolean modified = false;        byte[] states = _states;        int[] keys = _set;        double[] values = _values;        for (int i = keys.length; i-- > 0;) {            if (states[i] == FULL && ! procedure.execute(keys[i],values[i])) {                removeAt(i);                modified = true;            }        }        return modified;    }    /**     * Transform the values in this map using <tt>function</tt>.     *     * @param function a <code>TDoubleFunction</code> value     */    public void transformValues(TDoubleFunction function) {        byte[] states = _states;        double[] values = _values;        for (int i = values.length; i-- > 0;) {            if (states[i] == FULL) {                values[i] = function.execute(values[i]);            }        }    }    /**     * Increments the primitive value mapped to key by 1     *     * @param key the key of the value to increment     * @return true if a mapping was found and modified.     */    public boolean increment(int key) {        return adjustValue(key, (double)1);    }    /**     * Adjusts the primitive value mapped to key.     *     * @param key the key of the value to increment     * @param amount the amount to adjust the value by.     * @return true if a mapping was found and modified.     */    public boolean adjustValue(int key, double amount) {        int index = index(key);        if (index < 0) {            return false;        } else {            _values[index] += amount;            return true;        }    }    private void writeObject(ObjectOutputStream stream)        throws IOException {        stream.defaultWriteObject();        // number of entries        stream.writeInt(_size);        SerializationProcedure writeProcedure = new SerializationProcedure(stream);        if (! forEachEntry(writeProcedure)) {            throw writeProcedure.exception;        }    }    private void readObject(ObjectInputStream stream)        throws IOException, ClassNotFoundException {        stream.defaultReadObject();        int size = stream.readInt();        setUp(size);        while (size-- > 0) {            int key = stream.readInt();            double val = stream.readDouble();            put(key, val);        }    }} // TIntDoubleHashMap

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -