http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java-templates/org/apache/mahout/math/map/OpenObjectValueTypeHashMap.java.t ---------------------------------------------------------------------- diff --git a/math/src/main/java-templates/org/apache/mahout/math/map/OpenObjectValueTypeHashMap.java.t b/math/src/main/java-templates/org/apache/mahout/math/map/OpenObjectValueTypeHashMap.java.t deleted file mode 100644 index 924c7e2..0000000 --- a/math/src/main/java-templates/org/apache/mahout/math/map/OpenObjectValueTypeHashMap.java.t +++ /dev/null @@ -1,567 +0,0 @@ -/** - * 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. - */ - -/* -Copyright � 1999 CERN - European Organization for Nuclear Research. -Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose -is hereby granted without fee, provided that the above copyright notice appear in all copies and -that both that copyright notice and this permission notice appear in supporting documentation. -CERN makes no representations about the suitability of this software for any purpose. -It is provided "as is" without expressed or implied warranty. -*/ -package org.apache.mahout.math.map; - -import java.util.Arrays; -import java.util.List; - -import org.apache.mahout.math.function.Object${valueTypeCap}Procedure; -import org.apache.mahout.math.function.ObjectProcedure; -import org.apache.mahout.math.list.${valueTypeCap}ArrayList; - -/** - * Open hash map from Object keys to ${valueType} values. - **/ -public class OpenObject${valueTypeCap}HashMap<T> extends AbstractObject${valueTypeCap}Map<T> { - protected static final byte FREE = 0; - protected static final byte FULL = 1; - protected static final byte REMOVED = 2; - protected static final Object NO_KEY_VALUE = null; - - /** The hash table keys. */ - private Object[] table; - - /** The hash table values. */ - private ${valueType}[] values; - - /** The state of each hash table entry (FREE, FULL, REMOVED). */ - private byte[] state; - - /** The number of table entries in state==FREE. */ - private int freeEntries; - - - /** Constructs an empty map with default capacity and default load factors. */ - public OpenObject${valueTypeCap}HashMap() { - this(DEFAULT_CAPACITY); - } - - /** - * Constructs an empty map with the specified initial capacity and default load factors. - * - * @param initialCapacity the initial capacity of the map. - * @throws IllegalArgumentException if the initial capacity is less than zero. - */ - public OpenObject${valueTypeCap}HashMap(int initialCapacity) { - this(initialCapacity, DEFAULT_MIN_LOAD_FACTOR, DEFAULT_MAX_LOAD_FACTOR); - } - - /** - * Constructs an empty map with the specified initial capacity and the specified minimum and maximum load factor. - * - * @param initialCapacity the initial capacity. - * @param minLoadFactor the minimum load factor. - * @param maxLoadFactor the maximum load factor. - * @throws IllegalArgumentException if <tt>initialCapacity < 0 || (minLoadFactor < 0.0 || minLoadFactor >= 1.0) || - * (maxLoadFactor <= 0.0 || maxLoadFactor >= 1.0) || (minLoadFactor >= - * maxLoadFactor)</tt>. - */ - public OpenObject${valueTypeCap}HashMap(int initialCapacity, double minLoadFactor, double maxLoadFactor) { - setUp(initialCapacity, minLoadFactor, maxLoadFactor); - } - - /** Removes all (key,value) associations from the receiver. Implicitly calls <tt>trimToSize()</tt>. */ - @Override - public void clear() { - Arrays.fill(this.state, FREE); - Arrays.fill(this.table, null); - - distinct = 0; - freeEntries = table.length; // delta - trimToSize(); - } - - /** - * Returns a deep copy of the receiver. - * - * @return a deep copy of the receiver. - */ - @Override - @SuppressWarnings("unchecked") - public Object clone() { - OpenObject${valueTypeCap}HashMap copy = (OpenObject${valueTypeCap}HashMap) super.clone(); - copy.table = copy.table.clone(); - copy.values = copy.values.clone(); - copy.state = copy.state.clone(); - return copy; - } - - /** - * Returns <tt>true</tt> if the receiver contains the specified key. - * - * @return <tt>true</tt> if the receiver contains the specified key. - */ - @Override - public boolean containsKey(T key) { - return indexOfKey(key) >= 0; - } - - /** - * Returns <tt>true</tt> if the receiver contains the specified value. - * - * @return <tt>true</tt> if the receiver contains the specified value. - */ - @Override - public boolean containsValue(${valueType} value) { - return indexOfValue(value) >= 0; - } - - /** - * Ensures that the receiver can hold at least the specified number of associations without needing to allocate new - * internal memory. If necessary, allocates new internal memory and increases the capacity of the receiver. <p> This - * method never need be called; it is for performance tuning only. Calling this method before <tt>put()</tt>ing a - * large number of associations boosts performance, because the receiver will grow only once instead of potentially - * many times and hash collisions get less probable. - * - * @param minCapacity the desired minimum capacity. - */ - @Override - public void ensureCapacity(int minCapacity) { - if (table.length < minCapacity) { - int newCapacity = nextPrime(minCapacity); - rehash(newCapacity); - } - } - - /** - * Applies a procedure to each key of the receiver, if any. Note: Iterates over the keys in no particular order. - * Subclasses can define a particular order, for example, "sorted by key". All methods which <i>can</i> be expressed - * in terms of this method (most methods can) <i>must guarantee</i> to use the <i>same</i> order defined by this - * method, even if it is no particular order. This is necessary so that, for example, methods <tt>keys</tt> and - * <tt>values</tt> will yield association pairs, not two uncorrelated lists. - * - * @param procedure the procedure to be applied. Stops iteration if the procedure returns <tt>false</tt>, otherwise - * continues. - * @return <tt>false</tt> if the procedure stopped before all keys where iterated over, <tt>true</tt> otherwise. - */ - @Override - @SuppressWarnings("unchecked") - public boolean forEachKey(ObjectProcedure<T> procedure) { - for (int i = table.length; i-- > 0;) { - if (state[i] == FULL && !procedure.apply((T)table[i])) { - return false; - } - } - return true; - } - - /** - * Applies a procedure to each (key,value) pair of the receiver, if any. Iteration order is guaranteed to be - * <i>identical</i> to the order used by method {@link #forEachKey(ObjectProcedure)}. - * - * @param procedure the procedure to be applied. Stops iteration if the procedure returns <tt>false</tt>, otherwise - * continues. - * @return <tt>false</tt> if the procedure stopped before all keys where iterated over, <tt>true</tt> otherwise. - */ - @Override - @SuppressWarnings("unchecked") - public boolean forEachPair(Object${valueTypeCap}Procedure<T> procedure) { - for (int i = table.length; i-- > 0;) { - if (state[i] == FULL && !procedure.apply((T)table[i], values[i])) { - return false; - } - } - return true; - } - - /** - * Returns the value associated with the specified key. It is often a good idea to first check with - * {@link #containsKey(Object)} whether the given key has a value associated or not, - * i.e. whether there exists an association for the given key or not. - * - * @param key the key to be searched for. - * @return the value associated with the specified key; <tt>0</tt> if no such key is present. - */ - @Override - public ${valueType} get(T key) { - final int i = indexOfKey(key); - if (i < 0) { - return 0; - } //not contained - return values[i]; - } - - /** - * @param key the key to be added to the receiver. - * @return the index where the key would need to be inserted, if it is not already contained. Returns -index-1 if the - * key is already contained at slot index. Therefore, if the returned index < 0, then it is already contained - * at slot -index-1. If the returned index >= 0, then it is NOT already contained and should be inserted at - * slot index. - */ - protected int indexOfInsertion(T key) { - final int length = table.length; - - final int hash = key.hashCode() & 0x7FFFFFFF; - int i = hash % length; - int decrement = hash % (length - 2); // double hashing, see http://www.eece.unm.edu/faculty/heileman/hash/node4.html - //int decrement = (hash / length) % length; - if (decrement == 0) { - decrement = 1; - } - - // stop if we find a removed or free slot, or if we find the key itself - // do NOT skip over removed slots (yes, open addressing is like that...) - while (state[i] == FULL && !equalsMindTheNull(table[i], key)) { - i -= decrement; - //hashCollisions++; - if (i < 0) { - i += length; - } - } - - if (state[i] == REMOVED) { - // stop if we find a free slot, or if we find the key itself. - // do skip over removed slots (yes, open addressing is like that...) - // assertion: there is at least one FREE slot. - final int j = i; - while (state[i] != FREE && (state[i] == REMOVED || !equalsMindTheNull(table[i], key))) { - i -= decrement; - //hashCollisions++; - if (i < 0) { - i += length; - } - } - if (state[i] == FREE) { - i = j; - } - } - - - if (state[i] == FULL) { - // key already contained at slot i. - // return a negative number identifying the slot. - return -i - 1; - } - // not already contained, should be inserted at slot i. - // return a number >= 0 identifying the slot. - return i; - } - - /** - * @param key the key to be searched in the receiver. - * @return the index where the key is contained in the receiver, returns -1 if the key was not found. - */ - protected int indexOfKey(T key) { - final int length = table.length; - - final int hash = key.hashCode() & 0x7FFFFFFF; - int i = hash % length; - int decrement = hash % (length - 2); // double hashing, see http://www.eece.unm.edu/faculty/heileman/hash/node4.html - //int decrement = (hash / length) % length; - if (decrement == 0) { - decrement = 1; - } - - // stop if we find a free slot, or if we find the key itself. - // do skip over removed slots (yes, open addressing is like that...) - while (state[i] != FREE && (state[i] == REMOVED || !equalsMindTheNull(table[i], key))) { - i -= decrement; - //hashCollisions++; - if (i < 0) { - i += length; - } - } - - if (state[i] == FREE) { - return -1; - } // not found - return i; //found, return index where key is contained - } - - /** - * @param value the value to be searched in the receiver. - * @return the index where the value is contained in the receiver, returns -1 if the value was not found. - */ - protected int indexOfValue(${valueType} value) { - ${valueType}[] val = values; - byte[] stat = state; - - for (int i = stat.length; --i >= 0;) { - if (stat[i] == FULL && val[i] == value) { - return i; - } - } - - return -1; // not found - } - - /** - * Fills all keys contained in the receiver into the specified list. Fills the list, starting at index 0. After this - * call returns the specified list has a new size that equals <tt>this.size()</tt>. - * This method can be used - * to iterate over the keys of the receiver. - * - * @param list the list to be filled, can have any size. - */ - @Override - @SuppressWarnings("unchecked") - public void keys(List<T> list) { - list.clear(); - - for (int i = table.length; i-- > 0;) { - if (state[i] == FULL) { - list.add((T)table[i]); - } - } - } - - /** - * Fills all pairs satisfying a given condition into the specified lists. Fills into the lists, starting at index 0. - * After this call returns the specified lists both have a new size, the number of pairs satisfying the condition. - * <p> <b>Example:</b> <br> - * <pre> - * Object${valueTypeCap}Procedure<T> condition = new Object${valueTypeCap}Procedure<T>() { // match even values only - * public boolean apply(T key, ${valueType} value) { return value%2==0; } - * } - * keys = (8,7,6), values = (1,2,2) --> keyList = (6,8), valueList = (2,1)</tt> - * </pre> - * - * @param condition the condition to be matched. Takes the current key as first and the current value as second - * argument. - * @param keyList the list to be filled with keys, can have any size. - * @param valueList the list to be filled with values, can have any size. - */ - @Override - @SuppressWarnings("unchecked") - public void pairsMatching(Object${valueTypeCap}Procedure<T> condition, - List<T> keyList, - ${valueTypeCap}ArrayList valueList) { - keyList.clear(); - valueList.clear(); - - for (int i = table.length; i-- > 0;) { - if (state[i] == FULL && condition.apply((T)table[i], values[i])) { - keyList.add((T)table[i]); - valueList.add(values[i]); - } - } - } - - /** - * Associates the given key with the given value. Replaces any old <tt>(key,someOtherValue)</tt> association, if - * existing. - * - * @param key the key the value shall be associated with. - * @param value the value to be associated. - * @return <tt>true</tt> if the receiver did not already contain such a key; <tt>false</tt> if the receiver did - * already contain such a key - the new value has now replaced the formerly associated value. - */ - @Override - public boolean put(T key, ${valueType} value) { - int i = indexOfInsertion(key); - if (i < 0) { //already contained - i = -i - 1; - this.values[i] = value; - return false; - } - - if (this.distinct > this.highWaterMark) { - int newCapacity = chooseGrowCapacity(this.distinct + 1, this.minLoadFactor, this.maxLoadFactor); - rehash(newCapacity); - return put(key, value); - } - - this.table[i] = key; - this.values[i] = value; - if (this.state[i] == FREE) { - this.freeEntries--; - } - this.state[i] = FULL; - this.distinct++; - - if (this.freeEntries < 1) { //delta - int newCapacity = chooseGrowCapacity(this.distinct + 1, this.minLoadFactor, this.maxLoadFactor); - rehash(newCapacity); - } - - return true; - } - - @Override - public ${valueType} adjustOrPutValue(T key, ${valueType} newValue, ${valueType} incrValue) { - int i = indexOfInsertion(key); - if (i < 0) { //already contained - i = -i - 1; - this.values[i] += incrValue; - return this.values[i]; - } else { - put(key, newValue); - return newValue; - } - } - - /** - * Rehashes the contents of the receiver into a new table with a smaller or larger capacity. This method is called - * automatically when the number of keys in the receiver exceeds the high water mark or falls below the low water - * mark. - */ - @SuppressWarnings("unchecked") - protected void rehash(int newCapacity) { - int oldCapacity = table.length; - //if (oldCapacity == newCapacity) return; - - Object[] oldTable = table; - ${valueType}[] oldValues = values; - byte[] oldState = state; - - this.table = new Object[newCapacity]; - this.values = new ${valueType}[newCapacity]; - this.state = new byte[newCapacity]; - - this.lowWaterMark = chooseLowWaterMark(newCapacity, this.minLoadFactor); - this.highWaterMark = chooseHighWaterMark(newCapacity, this.maxLoadFactor); - - this.freeEntries = newCapacity - this.distinct; // delta - - for (int i = oldCapacity; i-- > 0;) { - if (oldState[i] == FULL) { - Object element = oldTable[i]; - int index = indexOfInsertion((T)element); - this.table[index] = element; - this.values[index] = oldValues[i]; - this.state[index] = FULL; - } - } - } - - /** - * Removes the given key with its associated element from the receiver, if present. - * - * @param key the key to be removed from the receiver. - * @return <tt>true</tt> if the receiver contained the specified key, <tt>false</tt> otherwise. - */ - @Override - public boolean removeKey(T key) { - int i = indexOfKey(key); - if (i < 0) { - return false; - } // key not contained - - this.state[i] = REMOVED; - //this.values[i]=0; // delta - this.distinct--; - - if (this.distinct < this.lowWaterMark) { - int newCapacity = chooseShrinkCapacity(this.distinct, this.minLoadFactor, this.maxLoadFactor); - rehash(newCapacity); - } - - return true; - } - - /** - * Initializes the receiver. - * - * @param initialCapacity the initial capacity of the receiver. - * @param minLoadFactor the minLoadFactor of the receiver. - * @param maxLoadFactor the maxLoadFactor of the receiver. - * @throws IllegalArgumentException if <tt>initialCapacity < 0 || (minLoadFactor < 0.0 || minLoadFactor >= 1.0) || - * (maxLoadFactor <= 0.0 || maxLoadFactor >= 1.0) || (minLoadFactor >= - * maxLoadFactor)</tt>. - */ - @Override - final protected void setUp(int initialCapacity, double minLoadFactor, double maxLoadFactor) { - int capacity = initialCapacity; - super.setUp(capacity, minLoadFactor, maxLoadFactor); - capacity = nextPrime(capacity); - if (capacity == 0) { - capacity = 1; - } // open addressing needs at least one FREE slot at any time. - - this.table = new Object[capacity]; - this.values = new ${valueType}[capacity]; - this.state = new byte[capacity]; - - // memory will be exhausted long before this pathological case happens, anyway. - this.minLoadFactor = minLoadFactor; - if (capacity == PrimeFinder.LARGEST_PRIME) { - this.maxLoadFactor = 1.0; - } else { - this.maxLoadFactor = maxLoadFactor; - } - - this.distinct = 0; - this.freeEntries = capacity; // delta - - // lowWaterMark will be established upon first expansion. - // establishing it now (upon instance construction) would immediately make the table shrink upon first put(...). - // After all the idea of an "initialCapacity" implies violating lowWaterMarks when an object is young. - // See ensureCapacity(...) - this.lowWaterMark = 0; - this.highWaterMark = chooseHighWaterMark(capacity, this.maxLoadFactor); - } - - /** - * Trims the capacity of the receiver to be the receiver's current size. Releases any superfluous internal memory. An - * application can use this operation to minimize the storage of the receiver. - */ - @Override - public void trimToSize() { - // * 1.2 because open addressing's performance exponentially degrades beyond that point - // so that even rehashing the table can take very long - int newCapacity = nextPrime((int) (1 + 1.2 * size())); - if (table.length > newCapacity) { - rehash(newCapacity); - } - } - - /** - * Fills all values contained in the receiver into the specified list. Fills the list, starting at index 0. After this - * call returns the specified list has a new size that equals <tt>this.size()</tt>. - * <p> This method can be used - * to iterate over the values of the receiver. - * - * @param list the list to be filled, can have any size. - */ - @Override - public void values(${valueTypeCap}ArrayList list) { - list.setSize(distinct); - ${valueType}[] elements = list.elements(); - - int j = 0; - for (int i = state.length; i-- > 0;) { - if (state[i] == FULL) { - elements[j++] = values[i]; - } - } - } - - /** - * Access for unit tests. - * @param capacity - * @param minLoadFactor - * @param maxLoadFactor - */ - protected void getInternalFactors(int[] capacity, - double[] minLoadFactor, - double[] maxLoadFactor) { - capacity[0] = table.length; - minLoadFactor[0] = this.minLoadFactor; - maxLoadFactor[0] = this.maxLoadFactor; - } -}
http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java-templates/org/apache/mahout/math/set/AbstractKeyTypeSet.java.t ---------------------------------------------------------------------- diff --git a/math/src/main/java-templates/org/apache/mahout/math/set/AbstractKeyTypeSet.java.t b/math/src/main/java-templates/org/apache/mahout/math/set/AbstractKeyTypeSet.java.t deleted file mode 100644 index 2b451b7..0000000 --- a/math/src/main/java-templates/org/apache/mahout/math/set/AbstractKeyTypeSet.java.t +++ /dev/null @@ -1,181 +0,0 @@ -/** - * 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 org.apache.mahout.math.set; - -import org.apache.mahout.math.function.${keyTypeCap}Procedure; -import org.apache.mahout.math.list.${keyTypeCap}ArrayList; -import java.util.Arrays; -import java.nio.IntBuffer; - -public abstract class Abstract${keyTypeCap}Set extends AbstractSet { - - /** - * Returns <tt>true</tt> if the receiver contains the specified key. - * - * @return <tt>true</tt> if the receiver contains the specified key. - */ - public boolean contains(final ${keyType} key) { - return !forEachKey( - new ${keyTypeCap}Procedure() { - @Override - public boolean apply(${keyType} iterKey) { - return (key != iterKey); - } - } - ); - } - - /** - * Returns a deep copy of the receiver; uses <code>clone()</code> and casts the result. - * - * @return a deep copy of the receiver. - */ - public Abstract${keyTypeCap}Set copy() { - return (Abstract${keyTypeCap}Set) clone(); - } - - public boolean equals(Object obj) { - if (obj == this) { - return true; - } - - if (!(obj instanceof Abstract${keyTypeCap}Set)) { - return false; - } - final Abstract${keyTypeCap}Set other = (Abstract${keyTypeCap}Set) obj; - if (other.size() != size()) { - return false; - } - - return - forEachKey( - new ${keyTypeCap}Procedure() { - @Override - public boolean apply(${keyType} key) { - return other.contains(key); - } - } - ); - } - - public int hashCode() { - final int[] buf = new int[size()]; - forEachKey( - new ${keyTypeCap}Procedure() { - int i = 0; - - @Override - public boolean apply(${keyType} iterKey) { - buf[i++] = HashUtils.hash(iterKey); - return true; - } - } - ); - Arrays.sort(buf); - return IntBuffer.wrap(buf).hashCode(); - } - - /** - * Applies a procedure to each key of the receiver, if any. Note: Iterates over the keys in no particular order. - * Subclasses can define a particular order, for example, "sorted by key". All methods which <i>can</i> be expressed - * in terms of this method (most methods can) <i>must guarantee</i> to use the <i>same</i> order defined by this - * method, even if it is no particular order. This is necessary so that, for example, methods <tt>keys</tt> and - * <tt>values</tt> will yield association pairs, not two uncorrelated lists. - * - * @param procedure the procedure to be applied. Stops iteration if the procedure returns <tt>false</tt>, otherwise - * continues. - * @return <tt>false</tt> if the procedure stopped before all keys where iterated over, <tt>true</tt> otherwise. - */ - public abstract boolean forEachKey(${keyTypeCap}Procedure procedure); - - /** - * Returns a list filled with all keys contained in the receiver. The returned list has a size that equals - * <tt>this.size()</tt>. Iteration order is guaranteed to be <i>identical</i> to the order used by method {@link - * #forEachKey(${keyTypeCap}Procedure)}. <p> This method can be used to iterate over the keys of the receiver. - * - * @return the keys. - */ - public ${keyTypeCap}ArrayList keys() { - ${keyTypeCap}ArrayList list = new ${keyTypeCap}ArrayList(size()); - keys(list); - return list; - } - - /** - * Fills all keys contained in the receiver into the specified list. Fills the list, starting at index 0. After this - * call returns the specified list has a new size that equals <tt>this.size()</tt>. Iteration order is guaranteed to - * be <i>identical</i> to the order used by method {@link #forEachKey(${keyTypeCap}Procedure)}. - * <p> This method can be used to - * iterate over the keys of the receiver. - * - * @param list the list to be filled, can have any size. - */ - public void keys(final ${keyTypeCap}ArrayList list) { - list.clear(); - forEachKey( - new ${keyTypeCap}Procedure() { - @Override - public boolean apply(${keyType} key) { - list.add(key); - return true; - } - } - ); - } - /** - * Associates the given key with the given value. Replaces any old <tt>(key,someOtherValue)</tt> association, if - * existing. - * - * @param key the key the value shall be associated with. - * @return <tt>true</tt> if the receiver did not already contain such a key; <tt>false</tt> if the receiver did - * already contain such a key - the new value has now replaced the formerly associated value. - */ - public abstract boolean add(${keyType} key); - - /** - * Removes the given key with its associated element from the receiver, if present. - * - * @param key the key to be removed from the receiver. - * @return <tt>true</tt> if the receiver contained the specified key, <tt>false</tt> otherwise. - */ - public abstract boolean remove(${keyType} key); - - /** - * Returns a string representation of the receiver, containing the String representation of each key-value pair, - * sorted ascending by key. - */ - public String toString() { - ${keyTypeCap}ArrayList theKeys = keys(); - //theKeys.sort(); - - StringBuilder buf = new StringBuilder(); - buf.append('['); - int maxIndex = theKeys.size() - 1; - for (int i = 0; i <= maxIndex; i++) { - ${keyType} key = theKeys.get(i); - buf.append(String.valueOf(key)); - if (i < maxIndex) { - buf.append(", "); - } - } - buf.append(']'); - return buf.toString(); - } -} http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java-templates/org/apache/mahout/math/set/OpenKeyTypeHashSet.java.t ---------------------------------------------------------------------- diff --git a/math/src/main/java-templates/org/apache/mahout/math/set/OpenKeyTypeHashSet.java.t b/math/src/main/java-templates/org/apache/mahout/math/set/OpenKeyTypeHashSet.java.t deleted file mode 100644 index 8c4c0f0..0000000 --- a/math/src/main/java-templates/org/apache/mahout/math/set/OpenKeyTypeHashSet.java.t +++ /dev/null @@ -1,423 +0,0 @@ -/** - * 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 org.apache.mahout.math.set; - -import java.util.Arrays; - -import org.apache.mahout.math.function.${keyTypeCap}Procedure; -import org.apache.mahout.math.list.${keyTypeCap}ArrayList; -import org.apache.mahout.math.map.HashFunctions; -import org.apache.mahout.math.map.PrimeFinder; - -/** - * Open hash set of ${keyType} items; - **/ -public class Open${keyTypeCap}HashSet extends Abstract${keyTypeCap}Set { - protected static final byte FREE = 0; - protected static final byte FULL = 1; - protected static final byte REMOVED = 2; -#if (${keyTypeFloating} == 'true') -#set ($noKeyComment = "${keyTypeCap}.NaN") - protected static final ${keyType} NO_KEY_VALUE = ${keyTypeCap}.NaN; -#else -#set ($noKeyComment = "0") - protected static final ${keyType} NO_KEY_VALUE = 0; -#end - - /** The hash table keys. */ - private ${keyType}[] table; - - /** The state of each hash table entry (FREE, FULL, REMOVED). */ - private byte[] state; - - /** The number of table entries in state==FREE. */ - private int freeEntries; - - - /** Constructs an empty map with default capacity and default load factors. */ - public Open${keyTypeCap}HashSet() { - this(DEFAULT_CAPACITY); - } - - /** - * Constructs an empty map with the specified initial capacity and default load factors. - * - * @param initialCapacity the initial capacity of the map. - * @throws IllegalArgumentException if the initial capacity is less than zero. - */ - public Open${keyTypeCap}HashSet(int initialCapacity) { - this(initialCapacity, DEFAULT_MIN_LOAD_FACTOR, DEFAULT_MAX_LOAD_FACTOR); - } - - /** - * Constructs an empty map with the specified initial capacity and the specified minimum and maximum load factor. - * - * @param initialCapacity the initial capacity. - * @param minLoadFactor the minimum load factor. - * @param maxLoadFactor the maximum load factor. - * @throws IllegalArgumentException if <tt>initialCapacity < 0 || (minLoadFactor < 0.0 || minLoadFactor >= 1.0) || - * (maxLoadFactor <= 0.0 || maxLoadFactor >= 1.0) || (minLoadFactor >= - * maxLoadFactor)</tt>. - */ - public Open${keyTypeCap}HashSet(int initialCapacity, double minLoadFactor, double maxLoadFactor) { - setUp(initialCapacity, minLoadFactor, maxLoadFactor); - } - - /** Removes all values associations from the receiver. Implicitly calls <tt>trimToSize()</tt>. */ - @Override - public void clear() { - Arrays.fill(this.state, FREE); - distinct = 0; - freeEntries = table.length; // delta - trimToSize(); - } - - /** - * Returns a deep copy of the receiver. - * - * @return a deep copy of the receiver. - */ - @Override - public Object clone() { - Open${keyTypeCap}HashSet copy = (Open${keyTypeCap}HashSet) super.clone(); - copy.table = copy.table.clone(); - copy.state = copy.state.clone(); - return copy; - } - - /** - * Returns <tt>true</tt> if the receiver contains the specified key. - * - * @return <tt>true</tt> if the receiver contains the specified key. - */ - @Override - public boolean contains(${keyType} key) { - return indexOfKey(key) >= 0; - } - - /** - * Ensures that the receiver can hold at least the specified number of associations without needing to allocate new - * internal memory. If necessary, allocates new internal memory and increases the capacity of the receiver. <p> This - * method never need be called; it is for performance tuning only. Calling this method before <tt>add()</tt>ing a - * large number of associations boosts performance, because the receiver will grow only once instead of potentially - * many times and hash collisions get less probable. - * - * @param minCapacity the desired minimum capacity. - */ - @Override - public void ensureCapacity(int minCapacity) { - if (table.length < minCapacity) { - int newCapacity = nextPrime(minCapacity); - rehash(newCapacity); - } - } - - /** - * Applies a procedure to each key of the receiver, if any. Note: Iterates over the keys in no particular order. - * Subclasses can define a particular order, for example, "sorted by key". All methods which <i>can</i> be expressed - * in terms of this method (most methods can) <i>must guarantee</i> to use the <i>same</i> order defined by this - * method, even if it is no particular order. This is necessary so that, for example, methods <tt>keys</tt> and - * <tt>values</tt> will yield association pairs, not two uncorrelated lists. - * - * @param procedure the procedure to be applied. Stops iteration if the procedure returns <tt>false</tt>, otherwise - * continues. - * @return <tt>false</tt> if the procedure stopped before all keys where iterated over, <tt>true</tt> otherwise. - */ - @Override - public boolean forEachKey(${keyTypeCap}Procedure procedure) { - for (int i = table.length; i-- > 0;) { - if (state[i] == FULL) { - if (!procedure.apply(table[i])) { - return false; - } - } - } - return true; - } - - /** - * @param key the key to be added to the receiver. - * @return the index where the key would need to be inserted, if it is not already contained. Returns -index-1 if the - * key is already contained at slot index. Therefore, if the returned index < 0, then it is already contained - * at slot -index-1. If the returned index >= 0, then it is NOT already contained and should be inserted at - * slot index. - */ - protected int indexOfInsertion(${keyType} key) { - final int length = table.length; - - final int hash = HashFunctions.hash(key) & 0x7FFFFFFF; - int i = hash % length; - int decrement = hash % (length - 2); // double hashing, see http://www.eece.unm.edu/faculty/heileman/hash/node4.html - //int decrement = (hash / length) % length; - if (decrement == 0) { - decrement = 1; - } - - // stop if we find a removed or free slot, or if we find the key itself - // do NOT skip over removed slots (yes, open addressing is like that...) - while (state[i] == FULL && table[i] != key) { - i -= decrement; - //hashCollisions++; - if (i < 0) { - i += length; - } - } - - if (state[i] == REMOVED) { - // stop if we find a free slot, or if we find the key itself. - // do skip over removed slots (yes, open addressing is like that...) - // assertion: there is at least one FREE slot. - final int j = i; - while (state[i] != FREE && (state[i] == REMOVED || table[i] != key)) { - i -= decrement; - //hashCollisions++; - if (i < 0) { - i += length; - } - } - if (state[i] == FREE) { - i = j; - } - } - - - if (state[i] == FULL) { - // key already contained at slot i. - // return a negative number identifying the slot. - return -i - 1; - } - // not already contained, should be inserted at slot i. - // return a number >= 0 identifying the slot. - return i; - } - - /** - * @param key the key to be searched in the receiver. - * @return the index where the key is contained in the receiver, returns -1 if the key was not found. - */ - protected int indexOfKey(${keyType} key) { - final int length = table.length; - - final int hash = HashFunctions.hash(key) & 0x7FFFFFFF; - int i = hash % length; - int decrement = hash % (length - 2); // double hashing, see http://www.eece.unm.edu/faculty/heileman/hash/node4.html - //int decrement = (hash / length) % length; - if (decrement == 0) { - decrement = 1; - } - - // stop if we find a free slot, or if we find the key itself. - // do skip over removed slots (yes, open addressing is like that...) - while (state[i] != FREE && (state[i] == REMOVED || table[i] != key)) { - i -= decrement; - //hashCollisions++; - if (i < 0) { - i += length; - } - } - - if (state[i] == FREE) { - return -1; - } // not found - return i; //found, return index where key is contained - } - - /** - * Fills all keys contained in the receiver into the specified list. Fills the list, starting at index 0. After this - * call returns the specified list has a new size that equals <tt>this.size()</tt>. Iteration order is guaranteed to - * be <i>identical</i> to the order used by method {@link #forEachKey(${keyTypeCap}Procedure)}. - * <p> This method can be used - * to iterate over the keys of the receiver. - * - * @param list the list to be filled, can have any size. - */ - @Override - public void keys(${keyTypeCap}ArrayList list) { - list.setSize(distinct); - ${keyType} [] elements = list.elements(); - - int j = 0; - for (int i = table.length; i-- > 0;) { - if (state[i] == FULL) { - elements[j++] = table[i]; - } - } - } - - /** - * Associates the given key with the given value. Replaces any old <tt>(key,someOtherValue)</tt> association, if - * existing. - * - * @param key the key the value shall be associated with. - * @return <tt>true</tt> if the receiver did not already contain such a key; <tt>false</tt> if the receiver did - * already contain such a key - the new value has now replaced the formerly associated value. - */ - @Override - public boolean add(${keyType} key) { - int i = indexOfInsertion(key); - if (i < 0) { //already contained - //i = -i - 1; - return false; - } - - if (this.distinct > this.highWaterMark) { - int newCapacity = chooseGrowCapacity(this.distinct + 1, this.minLoadFactor, this.maxLoadFactor); - rehash(newCapacity); - return add(key); - } - - this.table[i] = key; - if (this.state[i] == FREE) { - this.freeEntries--; - } - this.state[i] = FULL; - this.distinct++; - - if (this.freeEntries < 1) { //delta - int newCapacity = chooseGrowCapacity(this.distinct + 1, this.minLoadFactor, this.maxLoadFactor); - rehash(newCapacity); - } - - return true; - } - - /** - * Rehashes the contents of the receiver into a new table with a smaller or larger capacity. This method is called - * automatically when the number of keys in the receiver exceeds the high water mark or falls below the low water - * mark. - */ - protected void rehash(int newCapacity) { - int oldCapacity = table.length; - //if (oldCapacity == newCapacity) return; - - ${keyType}[] oldTable = table; - byte[] oldState = state; - - this.table = new ${keyType}[newCapacity]; - this.state = new byte[newCapacity]; - - this.lowWaterMark = chooseLowWaterMark(newCapacity, this.minLoadFactor); - this.highWaterMark = chooseHighWaterMark(newCapacity, this.maxLoadFactor); - - this.freeEntries = newCapacity - this.distinct; // delta - - for (int i = oldCapacity; i-- > 0;) { - if (oldState[i] == FULL) { - ${keyType} element = oldTable[i]; - int index = indexOfInsertion(element); - this.table[index] = element; - this.state[index] = FULL; - } - } - } - - /** - * Removes the given key with its associated element from the receiver, if present. - * - * @param key the key to be removed from the receiver. - * @return <tt>true</tt> if the receiver contained the specified key, <tt>false</tt> otherwise. - */ - @Override - public boolean remove(${keyType} key) { - int i = indexOfKey(key); - if (i < 0) { - return false; - } // key not contained - - this.state[i] = REMOVED; - this.distinct--; - - if (this.distinct < this.lowWaterMark) { - int newCapacity = chooseShrinkCapacity(this.distinct, this.minLoadFactor, this.maxLoadFactor); - rehash(newCapacity); - } - - return true; - } - - /** - * Initializes the receiver. - * - * @param initialCapacity the initial capacity of the receiver. - * @param minLoadFactor the minLoadFactor of the receiver. - * @param maxLoadFactor the maxLoadFactor of the receiver. - * @throws IllegalArgumentException if <tt>initialCapacity < 0 || (minLoadFactor < 0.0 || minLoadFactor >= 1.0) || - * (maxLoadFactor <= 0.0 || maxLoadFactor >= 1.0) || (minLoadFactor >= - * maxLoadFactor)</tt>. - */ - @Override - final protected void setUp(int initialCapacity, double minLoadFactor, double maxLoadFactor) { - int capacity = initialCapacity; - super.setUp(capacity, minLoadFactor, maxLoadFactor); - capacity = nextPrime(capacity); - if (capacity == 0) { - capacity = 1; - } // open addressing needs at least one FREE slot at any time. - - this.table = new ${keyType}[capacity]; - this.state = new byte[capacity]; - - // memory will be exhausted long before this pathological case happens, anyway. - this.minLoadFactor = minLoadFactor; - if (capacity == PrimeFinder.LARGEST_PRIME) { - this.maxLoadFactor = 1.0; - } else { - this.maxLoadFactor = maxLoadFactor; - } - - this.distinct = 0; - this.freeEntries = capacity; // delta - - // lowWaterMark will be established upon first expansion. - // establishing it now (upon instance construction) would immediately make the table shrink upon first put(...). - // After all the idea of an "initialCapacity" implies violating lowWaterMarks when an object is young. - // See ensureCapacity(...) - this.lowWaterMark = 0; - this.highWaterMark = chooseHighWaterMark(capacity, this.maxLoadFactor); - } - - /** - * Trims the capacity of the receiver to be the receiver's current size. Releases any superfluous internal memory. An - * application can use this operation to minimize the storage of the receiver. - */ - @Override - public void trimToSize() { - // * 1.2 because open addressing's performance exponentially degrades beyond that point - // so that even rehashing the table can take very long - int newCapacity = nextPrime((int) (1 + 1.2 * size())); - if (table.length > newCapacity) { - rehash(newCapacity); - } - } - - /** - * Access for unit tests. - * @param capacity - * @param minLoadFactor - * @param maxLoadFactor - */ - protected void getInternalFactors(int[] capacity, - double[] minLoadFactor, - double[] maxLoadFactor) { - capacity[0] = table.length; - minLoadFactor[0] = this.minLoadFactor; - maxLoadFactor[0] = this.maxLoadFactor; - } -} http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java/org/apache/mahout/collections/Arithmetic.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/collections/Arithmetic.java b/math/src/main/java/org/apache/mahout/collections/Arithmetic.java deleted file mode 100644 index 18e3200..0000000 --- a/math/src/main/java/org/apache/mahout/collections/Arithmetic.java +++ /dev/null @@ -1,489 +0,0 @@ -/** - * 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. - */ -/* -Copyright 1999 CERN - European Organization for Nuclear Research. -Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose -is hereby granted without fee, provided that the above copyright notice appear in all copies and -that both that copyright notice and this permission notice appear in supporting documentation. -CERN makes no representations about the suitability of this software for any purpose. -It is provided "as is" without expressed or implied warranty. -*/ -package org.apache.mahout.collections; - -/** - * Arithmetic functions. - */ -public final class Arithmetic extends Constants { - // for method STIRLING_CORRECTION(...) - private static final double[] STIRLING_CORRECTION = { - 0.0, - 8.106146679532726e-02, 4.134069595540929e-02, - 2.767792568499834e-02, 2.079067210376509e-02, - 1.664469118982119e-02, 1.387612882307075e-02, - 1.189670994589177e-02, 1.041126526197209e-02, - 9.255462182712733e-03, 8.330563433362871e-03, - 7.573675487951841e-03, 6.942840107209530e-03, - 6.408994188004207e-03, 5.951370112758848e-03, - 5.554733551962801e-03, 5.207655919609640e-03, - 4.901395948434738e-03, 4.629153749334029e-03, - 4.385560249232324e-03, 4.166319691996922e-03, - 3.967954218640860e-03, 3.787618068444430e-03, - 3.622960224683090e-03, 3.472021382978770e-03, - 3.333155636728090e-03, 3.204970228055040e-03, - 3.086278682608780e-03, 2.976063983550410e-03, - 2.873449362352470e-03, 2.777674929752690e-03, - }; - - // for method logFactorial(...) - // log(k!) for k = 0, ..., 29 - private static final double[] LOG_FACTORIALS = { - 0.00000000000000000, 0.00000000000000000, 0.69314718055994531, - 1.79175946922805500, 3.17805383034794562, 4.78749174278204599, - 6.57925121201010100, 8.52516136106541430, 10.60460290274525023, - 12.80182748008146961, 15.10441257307551530, 17.50230784587388584, - 19.98721449566188615, 22.55216385312342289, 25.19122118273868150, - 27.89927138384089157, 30.67186010608067280, 33.50507345013688888, - 36.39544520803305358, 39.33988418719949404, 42.33561646075348503, - 45.38013889847690803, 48.47118135183522388, 51.60667556776437357, - 54.78472939811231919, 58.00360522298051994, 61.26170176100200198, - 64.55753862700633106, 67.88974313718153498, 71.25703896716800901 - }; - - // k! for k = 0, ..., 20 - private static final long[] LONG_FACTORIALS = { - 1L, - 1L, - 2L, - 6L, - 24L, - 120L, - 720L, - 5040L, - 40320L, - 362880L, - 3628800L, - 39916800L, - 479001600L, - 6227020800L, - 87178291200L, - 1307674368000L, - 20922789888000L, - 355687428096000L, - 6402373705728000L, - 121645100408832000L, - 2432902008176640000L - }; - - // k! for k = 21, ..., 170 - private static final double[] DOUBLE_FACTORIALS = { - 5.109094217170944E19, - 1.1240007277776077E21, - 2.585201673888498E22, - 6.204484017332394E23, - 1.5511210043330984E25, - 4.032914611266057E26, - 1.0888869450418352E28, - 3.048883446117138E29, - 8.841761993739701E30, - 2.652528598121911E32, - 8.222838654177924E33, - 2.6313083693369355E35, - 8.68331761881189E36, - 2.952327990396041E38, - 1.0333147966386144E40, - 3.719933267899013E41, - 1.3763753091226346E43, - 5.23022617466601E44, - 2.0397882081197447E46, - 8.15915283247898E47, - 3.34525266131638E49, - 1.4050061177528801E51, - 6.041526306337384E52, - 2.6582715747884495E54, - 1.196222208654802E56, - 5.502622159812089E57, - 2.5862324151116827E59, - 1.2413915592536068E61, - 6.082818640342679E62, - 3.0414093201713376E64, - 1.5511187532873816E66, - 8.06581751709439E67, - 4.274883284060024E69, - 2.308436973392413E71, - 1.2696403353658264E73, - 7.109985878048632E74, - 4.052691950487723E76, - 2.350561331282879E78, - 1.386831185456898E80, - 8.32098711274139E81, - 5.075802138772246E83, - 3.146997326038794E85, - 1.9826083154044396E87, - 1.2688693218588414E89, - 8.247650592082472E90, - 5.443449390774432E92, - 3.6471110918188705E94, - 2.48003554243683E96, - 1.7112245242814127E98, - 1.1978571669969892E100, - 8.504785885678624E101, - 6.123445837688612E103, - 4.470115461512686E105, - 3.307885441519387E107, - 2.4809140811395404E109, - 1.8854947016660506E111, - 1.451830920282859E113, - 1.1324281178206295E115, - 8.94618213078298E116, - 7.15694570462638E118, - 5.797126020747369E120, - 4.7536433370128435E122, - 3.94552396972066E124, - 3.314240134565354E126, - 2.8171041143805494E128, - 2.4227095383672744E130, - 2.107757298379527E132, - 1.854826422573984E134, - 1.6507955160908465E136, - 1.4857159644817605E138, - 1.3520015276784033E140, - 1.2438414054641305E142, - 1.156772507081641E144, - 1.0873661566567426E146, - 1.0329978488239061E148, - 9.916779348709491E149, - 9.619275968248216E151, - 9.426890448883248E153, - 9.332621544394415E155, - 9.332621544394418E157, - 9.42594775983836E159, - 9.614466715035125E161, - 9.902900716486178E163, - 1.0299016745145631E166, - 1.0813967582402912E168, - 1.1462805637347086E170, - 1.2265202031961373E172, - 1.324641819451829E174, - 1.4438595832024942E176, - 1.5882455415227423E178, - 1.7629525510902457E180, - 1.974506857221075E182, - 2.2311927486598138E184, - 2.543559733472186E186, - 2.925093693493014E188, - 3.393108684451899E190, - 3.96993716080872E192, - 4.6845258497542896E194, - 5.574585761207606E196, - 6.689502913449135E198, - 8.094298525273444E200, - 9.875044200833601E202, - 1.2146304367025332E205, - 1.506141741511141E207, - 1.882677176888926E209, - 2.3721732428800483E211, - 3.0126600184576624E213, - 3.856204823625808E215, - 4.974504222477287E217, - 6.466855489220473E219, - 8.471580690878813E221, - 1.1182486511960037E224, - 1.4872707060906847E226, - 1.99294274616152E228, - 2.690472707318049E230, - 3.6590428819525483E232, - 5.0128887482749884E234, - 6.917786472619482E236, - 9.615723196941089E238, - 1.3462012475717523E241, - 1.8981437590761713E243, - 2.6953641378881633E245, - 3.8543707171800694E247, - 5.550293832739308E249, - 8.047926057471989E251, - 1.1749972043909107E254, - 1.72724589045464E256, - 2.5563239178728637E258, - 3.8089226376305687E260, - 5.7133839564458575E262, - 8.627209774233244E264, - 1.3113358856834527E267, - 2.0063439050956838E269, - 3.0897696138473515E271, - 4.789142901463393E273, - 7.471062926282892E275, - 1.1729568794264134E278, - 1.8532718694937346E280, - 2.946702272495036E282, - 4.714723635992061E284, - 7.590705053947223E286, - 1.2296942187394494E289, - 2.0044015765453032E291, - 3.287218585534299E293, - 5.423910666131583E295, - 9.003691705778434E297, - 1.5036165148649983E300, - 2.5260757449731988E302, - 4.2690680090047056E304, - 7.257415615308004E306 - }; - - /** Makes this class non instantiable, but still let's others inherit from it. */ - Arithmetic() { - } - - /** - * Efficiently returns the binomial coefficient, often also referred to as - * "n over k" or "n choose k". The binomial coefficient is defined as - * <tt>(n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k )</tt>. - * <ul> <li><tt>k<0</tt>: <tt>0</tt>.</li> - * <li><tt>k==0</tt>: <tt>1</tt>.</li> - * <li><tt>k==1</tt>: <tt>n</tt>.</li> - * <li>else: <tt>(n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k)</tt>.</li> - * </ul> - * - * @param n - * @param k - * @return the binomial coefficient. - */ - public static double binomial(double n, long k) { - if (k < 0) { - return 0; - } - if (k == 0) { - return 1; - } - if (k == 1) { - return n; - } - - // binomial(n,k) = (n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k ) - double a = n - k + 1; - double b = 1; - double binomial = 1; - for (long i = k; i-- > 0;) { - binomial *= (a++) / (b++); - } - return binomial; - } - - /** - * Efficiently returns the binomial coefficient, often also referred to as "n over k" or "n choose k". The binomial - * coefficient is defined as <ul> <li><tt>k<0</tt>: <tt>0</tt>. <li><tt>k==0 || k==n</tt>: <tt>1</tt>. <li><tt>k==1 || k==n-1</tt>: - * <tt>n</tt>. <li>else: <tt>(n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k )</tt>. </ul> - * - * @return the binomial coefficient. - */ - public static double binomial(long n, long k) { - if (k < 0) { - return 0; - } - if (k == 0 || k == n) { - return 1; - } - if (k == 1 || k == n - 1) { - return n; - } - - // try quick version and see whether we get numeric overflows. - // factorial(..) is O(1); requires no loop; only a table lookup. - if (n > k) { - int max = LONG_FACTORIALS.length + DOUBLE_FACTORIALS.length; - if (n < max) { // if (n! < inf && k! < inf) - double n_fac = factorial((int) n); - double k_fac = factorial((int) k); - double n_minus_k_fac = factorial((int) (n - k)); - double nk = n_minus_k_fac * k_fac; - if (nk != Double.POSITIVE_INFINITY) { // no numeric overflow? - // now this is completely safe and accurate - return n_fac / nk; - } - } - if (k > n / 2) { - k = n - k; - } // quicker - } - - // binomial(n,k) = (n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k ) - long a = n - k + 1; - long b = 1; - double binomial = 1; - for (long i = k; i-- > 0;) { - binomial *= (double) a++ / (b++); - } - return binomial; - } - - /** - * Returns the smallest <code>long >= value</code>. - * <dl><dt>Examples: {@code 1.0 -> 1, 1.2 -> 2, 1.9 -> 2}. This - * method is safer than using (long) Math.ceil(value), because of possible rounding error.</dt></dl> - */ - public static long ceil(double value) { - return Math.round(Math.ceil(value)); - } - - /** - * Evaluates the series of Chebyshev polynomials Ti at argument x/2. The series is given by - * <pre> - * N-1 - * - ' - * y = > coef[i] T (x/2) - * - i - * i=0 - * </pre> - * Coefficients are stored in reverse order, i.e. the zero order term is last in the array. Note N is the number of - * coefficients, not the order. <p> If coefficients are for the interval a to b, x must have been transformed to x -< - * 2(2x - b - a)/(b-a) before entering the routine. This maps x from (a, b) to (-1, 1), over which the Chebyshev - * polynomials are defined. <p> If the coefficients are for the inverted interval, in which (a, b) is mapped to (1/b, - * 1/a), the transformation required is {@code x -> 2(2ab/x - b - a)/(b-a)}. If b is infinity, this becomes {@code x -> 4a/x - 1}. - * <p> SPEED: <p> Taking advantage of the recurrence properties of the Chebyshev polynomials, the routine requires one - * more addition per loop than evaluating a nested polynomial of the same degree. - * - * @param x argument to the polynomial. - * @param coef the coefficients of the polynomial. - * @param N the number of coefficients. - */ - public static double chbevl(double x, double[] coef, int N) { - - int p = 0; - - double b0 = coef[p++]; - double b1 = 0.0; - int i = N - 1; - - double b2; - do { - b2 = b1; - b1 = b0; - b0 = x * b1 - b2 + coef[p++]; - } while (--i > 0); - - return 0.5 * (b0 - b2); - } - - /** - * Instantly returns the factorial <tt>k!</tt>. - * - * @param k must hold <tt>k >= 0</tt>. - */ - private static double factorial(int k) { - if (k < 0) { - throw new IllegalArgumentException(); - } - - int length1 = LONG_FACTORIALS.length; - if (k < length1) { - return LONG_FACTORIALS[k]; - } - - int length2 = DOUBLE_FACTORIALS.length; - if (k < length1 + length2) { - return DOUBLE_FACTORIALS[k - length1]; - } else { - return Double.POSITIVE_INFINITY; - } - } - - /** - * Returns the largest <code>long <= value</code>. - * <dl><dt>Examples: {@code 1.0 -> 1, 1.2 -> 1, 1.9 -> 1 <dt> 2.0 -> 2, 2.2 -> 2, 2.9 -> 2}</dt></dl> - * This method is safer than using (long) Math.floor(value), because of possible rounding error. - */ - public static long floor(double value) { - return Math.round(Math.floor(value)); - } - - /** Returns <tt>log<sub>base</sub>value</tt>. */ - public static double log(double base, double value) { - return Math.log(value) / Math.log(base); - } - - /** Returns <tt>log<sub>10</sub>value</tt>. */ - public static double log10(double value) { - // 1.0 / Math.log(10) == 0.43429448190325176 - return Math.log(value) * 0.43429448190325176; - } - - /** Returns <tt>log<sub>2</sub>value</tt>. */ - public static double log2(double value) { - // 1.0 / Math.log(2) == 1.4426950408889634 - return Math.log(value) * 1.4426950408889634; - } - - /** - * Returns <tt>log(k!)</tt>. Tries to avoid overflows. For <tt>k<30</tt> simply looks up a table in O(1). For - * <tt>k>=30</tt> uses stirlings approximation. - * - * @param k must hold <tt>k >= 0</tt>. - */ - public static double logFactorial(int k) { - if (k >= 30) { - - double r = 1.0 / k; - double rr = r * r; - double C7 = -5.95238095238095238e-04; - double C5 = 7.93650793650793651e-04; - double C3 = -2.77777777777777778e-03; - double C1 = 8.33333333333333333e-02; - double C0 = 9.18938533204672742e-01; - return (k + 0.5) * Math.log(k) - k + C0 + r * (C1 + rr * (C3 + rr * (C5 + rr * C7))); - } else { - return LOG_FACTORIALS[k]; - } - } - - /** - * Instantly returns the factorial <tt>k!</tt>. - * - * @param k must hold {@code k >= 0 && k < 21} - */ - public static long longFactorial(int k) { - if (k < 0) { - throw new IllegalArgumentException("Negative k"); - } - - if (k < LONG_FACTORIALS.length) { - return LONG_FACTORIALS[k]; - } - throw new IllegalArgumentException("Overflow"); - } - - /** - * Returns the StirlingCorrection. <p> Correction term of the Stirling approximation for <tt>log(k!)</tt> (series in - * 1/k, or table values for small k) with int parameter k. </p> <tt> log k! = (k + 1/2)log(k + 1) - (k + 1) + - * (1/2)log(2Pi) + STIRLING_CORRECTION(k + 1) log k! = (k + 1/2)log(k) - k + (1/2)log(2Pi) + - * STIRLING_CORRECTION(k) </tt> - */ - public static double stirlingCorrection(int k) { - - if (k > 30) { - double r = 1.0 / k; - double rr = r * r; - double C7 = -5.95238095238095238e-04; // -1/1680 - double C5 = 7.93650793650793651e-04; // +1/1260 - double C3 = -2.77777777777777778e-03; // -1/360 - double C1 = 8.33333333333333333e-02; // +1/12 - return r * (C1 + rr * (C3 + rr * (C5 + rr * C7))); - } else { - return STIRLING_CORRECTION[k]; - } - } - -} http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java/org/apache/mahout/collections/Constants.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/collections/Constants.java b/math/src/main/java/org/apache/mahout/collections/Constants.java deleted file mode 100644 index 007bd3f..0000000 --- a/math/src/main/java/org/apache/mahout/collections/Constants.java +++ /dev/null @@ -1,75 +0,0 @@ -/** - * 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. - */ - -/* -Copyright 1999 CERN - European Organization for Nuclear Research. -Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose -is hereby granted without fee, provided that the above copyright notice appear in all copies and -that both that copyright notice and this permission notice appear in supporting documentation. -CERN makes no representations about the suitability of this software for any purpose. -It is provided "as is" without expressed or implied warranty. -*/ -package org.apache.mahout.collections; - -/** - * Defines some useful constants. - */ -public class Constants { - /* - * machine constants - */ - protected static final double MACHEP = 1.11022302462515654042E-16; - protected static final double MAXLOG = 7.09782712893383996732E2; - protected static final double MINLOG = -7.451332191019412076235E2; - protected static final double MAXGAM = 171.624376956302725; - protected static final double SQTPI = 2.50662827463100050242E0; - protected static final double SQRTH = 7.07106781186547524401E-1; - protected static final double LOGPI = 1.14472988584940017414; - - protected static final double BIG = 4.503599627370496e15; - protected static final double BIGINV = 2.22044604925031308085e-16; - - - /* - * MACHEP = 1.38777878078144567553E-17 2**-56 - * MAXLOG = 8.8029691931113054295988E1 log(2**127) - * MINLOG = -8.872283911167299960540E1 log(2**-128) - * MAXNUM = 1.701411834604692317316873e38 2**127 - * - * For IEEE arithmetic (IBMPC): - * MACHEP = 1.11022302462515654042E-16 2**-53 - * MAXLOG = 7.09782712893383996843E2 log(2**1024) - * MINLOG = -7.08396418532264106224E2 log(2**-1022) - * MAXNUM = 1.7976931348623158E308 2**1024 - * - * The global symbols for mathematical constants are - * PI = 3.14159265358979323846 pi - * PIO2 = 1.57079632679489661923 pi/2 - * PIO4 = 7.85398163397448309616E-1 pi/4 - * SQRT2 = 1.41421356237309504880 sqrt(2) - * SQRTH = 7.07106781186547524401E-1 sqrt(2)/2 - * LOG2E = 1.4426950408889634073599 1/log(2) - * SQ2OPI = 7.9788456080286535587989E-1 sqrt( 2/pi ) - * LOGE2 = 6.93147180559945309417E-1 log(2) - * LOGSQ2 = 3.46573590279972654709E-1 log(2)/2 - * THPIO4 = 2.35619449019234492885 3*pi/4 - * TWOOPI = 6.36619772367581343075535E-1 2/pi - */ - protected Constants() {} -} http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java/org/apache/mahout/common/RandomUtils.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/common/RandomUtils.java b/math/src/main/java/org/apache/mahout/common/RandomUtils.java deleted file mode 100644 index ba71292..0000000 --- a/math/src/main/java/org/apache/mahout/common/RandomUtils.java +++ /dev/null @@ -1,100 +0,0 @@ -/** - * 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 org.apache.mahout.common; - -import java.util.Collections; -import java.util.Map; -import java.util.Random; -import java.util.WeakHashMap; - -import com.google.common.primitives.Longs; -import org.apache.commons.math3.primes.Primes; - -/** - * <p> - * The source of random stuff for the whole project. This lets us make all randomness in the project - * predictable, if desired, for when we run unit tests, which should be repeatable. - * </p> - */ -public final class RandomUtils { - - /** The largest prime less than 2<sup>31</sup>-1 that is the smaller of a twin prime pair. */ - public static final int MAX_INT_SMALLER_TWIN_PRIME = 2147482949; - - private static final Map<RandomWrapper,Boolean> INSTANCES = - Collections.synchronizedMap(new WeakHashMap<RandomWrapper,Boolean>()); - - private static boolean testSeed = false; - - private RandomUtils() { } - - public static void useTestSeed() { - testSeed = true; - synchronized (INSTANCES) { - for (RandomWrapper rng : INSTANCES.keySet()) { - rng.resetToTestSeed(); - } - } - } - - public static RandomWrapper getRandom() { - RandomWrapper random = new RandomWrapper(); - if (testSeed) { - random.resetToTestSeed(); - } - INSTANCES.put(random, Boolean.TRUE); - return random; - } - - public static Random getRandom(long seed) { - RandomWrapper random = new RandomWrapper(seed); - INSTANCES.put(random, Boolean.TRUE); - return random; - } - - /** @return what {@link Double#hashCode()} would return for the same value */ - public static int hashDouble(double value) { - return Longs.hashCode(Double.doubleToLongBits(value)); - } - - /** @return what {@link Float#hashCode()} would return for the same value */ - public static int hashFloat(float value) { - return Float.floatToIntBits(value); - } - - /** - * <p> - * Finds next-largest "twin primes": numbers p and p+2 such that both are prime. Finds the smallest such p - * such that the smaller twin, p, is greater than or equal to n. Returns p+2, the larger of the two twins. - * </p> - */ - public static int nextTwinPrime(int n) { - if (n > MAX_INT_SMALLER_TWIN_PRIME) { - throw new IllegalArgumentException(); - } - if (n <= 3) { - return 5; - } - int next = Primes.nextPrime(n); - while (!Primes.isPrime(next + 2)) { - next = Primes.nextPrime(next + 4); - } - return next + 2; - } - -} http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java/org/apache/mahout/common/RandomWrapper.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/common/RandomWrapper.java b/math/src/main/java/org/apache/mahout/common/RandomWrapper.java deleted file mode 100644 index 802291b..0000000 --- a/math/src/main/java/org/apache/mahout/common/RandomWrapper.java +++ /dev/null @@ -1,105 +0,0 @@ -/** - * 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 org.apache.mahout.common; - -import org.apache.commons.math3.random.MersenneTwister; -import org.apache.commons.math3.random.RandomGenerator; - -import java.util.Random; - -public final class RandomWrapper extends Random { - - private static final long STANDARD_SEED = 0xCAFEDEADBEEFBABEL; - - private final RandomGenerator random; - - RandomWrapper() { - random = new MersenneTwister(); - random.setSeed(System.currentTimeMillis() + System.identityHashCode(random)); - } - - RandomWrapper(long seed) { - random = new MersenneTwister(seed); - } - - @Override - public void setSeed(long seed) { - // Since this will be called by the java.util.Random() constructor before we construct - // the delegate... and because we don't actually care about the result of this for our - // purpose: - if (random != null) { - random.setSeed(seed); - } - } - - void resetToTestSeed() { - setSeed(STANDARD_SEED); - } - - public RandomGenerator getRandomGenerator() { - return random; - } - - @Override - protected int next(int bits) { - // Ugh, can't delegate this method -- it's protected - // Callers can't use it and other methods are delegated, so shouldn't matter - throw new UnsupportedOperationException(); - } - - @Override - public void nextBytes(byte[] bytes) { - random.nextBytes(bytes); - } - - @Override - public int nextInt() { - return random.nextInt(); - } - - @Override - public int nextInt(int n) { - return random.nextInt(n); - } - - @Override - public long nextLong() { - return random.nextLong(); - } - - @Override - public boolean nextBoolean() { - return random.nextBoolean(); - } - - @Override - public float nextFloat() { - return random.nextFloat(); - } - - @Override - public double nextDouble() { - return random.nextDouble(); - } - - @Override - public double nextGaussian() { - return random.nextGaussian(); - } - -}
