You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by sr...@apache.org on 2009/11/25 04:36:02 UTC
svn commit: r883973 [9/14] - in
/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix: ./
bitvector/ buffer/ function/ list/ list/adapter/ map/
Modified: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/list/ByteArrayList.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/list/ByteArrayList.java?rev=883973&r1=883972&r2=883973&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/list/ByteArrayList.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/list/ByteArrayList.java Wed Nov 25 03:35:59 2009
@@ -18,17 +18,17 @@
*/
@Deprecated
public class ByteArrayList extends AbstractByteList {
- /**
- * The array buffer into which the elements of the list are stored.
- * The capacity of the list is the length of this array buffer.
- * @serial
- */
- protected byte[] elements;
+ /**
+ * The array buffer into which the elements of the list are stored.
+ * The capacity of the list is the length of this array buffer.
+ * @serial
+ */
+ protected byte[] elements;
/**
* Constructs an empty list.
*/
public ByteArrayList() {
- this(10);
+ this(10);
}
/**
* Constructs a list containing the specified elements.
@@ -40,7 +40,7 @@
* @param elements the array to be backed by the the constructed list
*/
public ByteArrayList(byte[] elements) {
- elements(elements);
+ elements(elements);
}
/**
* Constructs an empty list with the specified initial capacity.
@@ -48,8 +48,8 @@
* @param initialCapacity the number of elements the receiver can hold without auto-expanding itself by allocating new internal memory.
*/
public ByteArrayList(int initialCapacity) {
- this(new byte[initialCapacity]);
- setSizeRaw(0);
+ this(new byte[initialCapacity]);
+ setSizeRaw(0);
}
/**
* Appends the specified element to the end of this list.
@@ -57,11 +57,11 @@
* @param element element to be appended to this list.
*/
public void add(byte element) {
- // overridden for performance only.
- if (size == elements.length) {
- ensureCapacity(size + 1);
- }
- elements[size++] = element;
+ // overridden for performance only.
+ if (size == elements.length) {
+ ensureCapacity(size + 1);
+ }
+ elements[size++] = element;
}
/**
* Inserts the specified element before the specified position into the receiver.
@@ -73,13 +73,13 @@
* @exception IndexOutOfBoundsException index is out of range (<tt>index < 0 || index > size()</tt>).
*/
public void beforeInsert(int index, byte element) {
- // overridden for performance only.
- if (index > size || index < 0)
- throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
- ensureCapacity(size + 1);
- System.arraycopy(elements, index, elements, index+1, size-index);
- elements[index] = element;
- size++;
+ // overridden for performance only.
+ if (index > size || index < 0)
+ throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
+ ensureCapacity(size + 1);
+ System.arraycopy(elements, index, elements, index+1, size-index);
+ elements[index] = element;
+ size++;
}
/**
* Searches the receiver for the specified value using
@@ -94,18 +94,18 @@
* @param from the leftmost search position, inclusive.
* @param to the rightmost search position, inclusive.
* @return index of the search key, if it is contained in the receiver;
- * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The <i>insertion
- * point</i> is defined as the the point at which the value would
- * be inserted into the receiver: the index of the first
- * element greater than the key, or <tt>receiver.size()</tt>, if all
- * elements in the receiver are less than the specified key. Note
- * that this guarantees that the return value will be >= 0 if
- * and only if the key is found.
+ * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The <i>insertion
+ * point</i> is defined as the the point at which the value would
+ * be inserted into the receiver: the index of the first
+ * element greater than the key, or <tt>receiver.size()</tt>, if all
+ * elements in the receiver are less than the specified key. Note
+ * that this guarantees that the return value will be >= 0 if
+ * and only if the key is found.
* @see org.apache.mahout.matrix.Sorting
* @see java.util.Arrays
*/
public int binarySearchFromTo(byte key, int from, int to) {
- return org.apache.mahout.matrix.Sorting.binarySearchFromTo(this.elements,key,from,to);
+ return org.apache.mahout.matrix.Sorting.binarySearchFromTo(this.elements,key,from,to);
}
/**
* Returns a deep copy of the receiver.
@@ -113,10 +113,10 @@
* @return a deep copy of the receiver.
*/
public Object clone() {
- // overridden for performance only.
- ByteArrayList clone = new ByteArrayList((byte[]) elements.clone());
- clone.setSizeRaw(size);
- return clone;
+ // overridden for performance only.
+ ByteArrayList clone = new ByteArrayList((byte[]) elements.clone());
+ clone.setSizeRaw(size);
+ return clone;
}
/**
* Returns a deep copy of the receiver; uses <code>clone()</code> and casts the result.
@@ -124,7 +124,7 @@
* @return a deep copy of the receiver.
*/
public ByteArrayList copy() {
- return (ByteArrayList) clone();
+ return (ByteArrayList) clone();
}
/**
* Sorts the specified range of the receiver into ascending numerical order.
@@ -136,29 +136,29 @@
* @param to the index of the last element (inclusive) to be sorted.
*/
public void countSortFromTo(int from, int to) {
- if (size==0) return;
- checkRangeFromTo(from, to, size);
+ if (size==0) return;
+ checkRangeFromTo(from, to, size);
- final int min = - (int)Byte.MIN_VALUE;
- final int range = min + Byte.MAX_VALUE + 1;
- byte[] theElements = elements;
- int[] counts = new int[range];
-
- for (int i=from; i<=to; i++) counts[theElements[i]+min]++;
-
- int fromIndex = from;
- byte val = Byte.MIN_VALUE;
- for (int i=0; i<range; i++, val++) {
- int c=counts[i];
- if (c>0) {
- if (c==1) theElements[fromIndex++]=val;
- else {
- int toIndex = fromIndex+c-1;
- fillFromToWith(fromIndex,toIndex,val);
- fromIndex = toIndex+1;
- }
- }
- }
+ final int min = - (int)Byte.MIN_VALUE;
+ final int range = min + Byte.MAX_VALUE + 1;
+ byte[] theElements = elements;
+ int[] counts = new int[range];
+
+ for (int i=from; i<=to; i++) counts[theElements[i]+min]++;
+
+ int fromIndex = from;
+ byte val = Byte.MIN_VALUE;
+ for (int i=0; i<range; i++, val++) {
+ int c=counts[i];
+ if (c>0) {
+ if (c==1) theElements[fromIndex++]=val;
+ else {
+ int toIndex = fromIndex+c-1;
+ fillFromToWith(fromIndex,toIndex,val);
+ fromIndex = toIndex+1;
+ }
+ }
+ }
}
/**
* Sorts the specified range of the receiver into ascending numerical order.
@@ -175,28 +175,28 @@
* @param max the largest element contained in the range.
*/
protected void countSortFromTo(int from, int to, byte min, byte max) {
- if (size==0) return;
- checkRangeFromTo(from, to, size);
+ if (size==0) return;
+ checkRangeFromTo(from, to, size);
- final int width = (int) (max-min+1);
-
- int[] counts = new int[width];
- byte[] theElements = elements;
- for (int i=from; i<=to; ) counts[(int)(theElements[i++]-min)]++;
-
- int fromIndex = from;
- byte val = min;
- for (int i=0; i<width; i++, val++) {
- int c = counts[i];
- if (c>0) {
- if (c==1) theElements[fromIndex++]=val;
- else {
- int toIndex = fromIndex + c - 1;
- fillFromToWith(fromIndex,toIndex,val);
- fromIndex = toIndex + 1;
- }
- }
- }
+ final int width = (int) (max-min+1);
+
+ int[] counts = new int[width];
+ byte[] theElements = elements;
+ for (int i=from; i<=to; ) counts[(int)(theElements[i++]-min)]++;
+
+ int fromIndex = from;
+ byte val = min;
+ for (int i=0; i<width; i++, val++) {
+ int c = counts[i];
+ if (c>0) {
+ if (c==1) theElements[fromIndex++]=val;
+ else {
+ int toIndex = fromIndex + c - 1;
+ fillFromToWith(fromIndex,toIndex,val);
+ fromIndex = toIndex + 1;
+ }
+ }
+ }
}
/**
* Returns the elements currently stored, including invalid elements between size and capacity, if any.
@@ -207,7 +207,7 @@
* @return the elements currently stored.
*/
public byte[] elements() {
- return elements;
+ return elements;
}
/**
* Sets the receiver's elements to be the specified array (not a copy of it).
@@ -220,9 +220,9 @@
* @return the receiver itself.
*/
public AbstractByteList elements(byte[] elements) {
- this.elements=elements;
- this.size=elements.length;
- return this;
+ this.elements=elements;
+ this.size=elements.length;
+ return this;
}
/**
* Ensures that the receiver can hold at least the specified number of elements without needing to allocate new internal memory.
@@ -231,7 +231,7 @@
* @param minCapacity the desired minimum capacity.
*/
public void ensureCapacity(int minCapacity) {
- elements = org.apache.mahout.matrix.Arrays.ensureCapacity(elements,minCapacity);
+ elements = org.apache.mahout.matrix.Arrays.ensureCapacity(elements,minCapacity);
}
/**
* Compares the specified Object with the receiver.
@@ -244,19 +244,19 @@
* @return true if the specified Object is equal to the receiver.
*/
public boolean equals(Object otherObj) { //delta
- // overridden for performance only.
- if (! (otherObj instanceof ByteArrayList)) return super.equals(otherObj);
- if (this==otherObj) return true;
- if (otherObj==null) return false;
- ByteArrayList other = (ByteArrayList) otherObj;
- if (size()!=other.size()) return false;
-
- byte[] theElements = elements();
- byte[] otherElements = other.elements();
- for (int i=size(); --i >= 0; ) {
- if (theElements[i] != otherElements[i]) return false;
- }
- return true;
+ // overridden for performance only.
+ if (! (otherObj instanceof ByteArrayList)) return super.equals(otherObj);
+ if (this==otherObj) return true;
+ if (otherObj==null) return false;
+ ByteArrayList other = (ByteArrayList) otherObj;
+ if (size()!=other.size()) return false;
+
+ byte[] theElements = elements();
+ byte[] otherElements = other.elements();
+ for (int i=size(); --i >= 0; ) {
+ if (theElements[i] != otherElements[i]) return false;
+ }
+ return true;
}
/**
* Applies a procedure to each element of the receiver, if any.
@@ -265,25 +265,25 @@
* @return <tt>false</tt> if the procedure stopped before all elements where iterated over, <tt>true</tt> otherwise.
*/
public boolean forEach(ByteProcedure procedure) {
- // overridden for performance only.
- byte[] theElements = elements;
- int theSize = size;
-
- for (int i=0; i<theSize;) if (! procedure.apply(theElements[i++])) return false;
- return true;
+ // overridden for performance only.
+ byte[] theElements = elements;
+ int theSize = size;
+
+ for (int i=0; i<theSize;) if (! procedure.apply(theElements[i++])) return false;
+ return true;
}
/**
* Returns the element at the specified position in the receiver.
*
* @param index index of element to return.
* @exception IndexOutOfBoundsException index is out of range (index
- * < 0 || index >= size()).
+ * < 0 || index >= size()).
*/
public byte get(int index) {
- // overridden for performance only.
- if (index >= size || index < 0)
- throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
- return elements[index];
+ // overridden for performance only.
+ if (index >= size || index < 0)
+ throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
+ return elements[index];
}
/**
* Returns the element at the specified position in the receiver; <b>WARNING:</b> Does not check preconditions.
@@ -294,7 +294,7 @@
* @param index index of element to return.
*/
public byte getQuick(int index) {
- return elements[index];
+ return elements[index];
}
/**
* Returns the index of the first occurrence of the specified
@@ -309,15 +309,15 @@
* @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>).
*/
public int indexOfFromTo(byte element, int from, int to) {
- // overridden for performance only.
- if (size==0) return -1;
- checkRangeFromTo(from, to, size);
-
- byte[] theElements = elements;
- for (int i = from ; i <= to; i++) {
- if (element==theElements[i]) {return i;} //found
- }
- return -1; //not found
+ // overridden for performance only.
+ if (size==0) return -1;
+ checkRangeFromTo(from, to, size);
+
+ byte[] theElements = elements;
+ for (int i = from ; i <= to; i++) {
+ if (element==theElements[i]) {return i;} //found
+ }
+ return -1; //not found
}
/**
* Returns the index of the last occurrence of the specified
@@ -332,15 +332,15 @@
* @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>).
*/
public int lastIndexOfFromTo(byte element, int from, int to) {
- // overridden for performance only.
- if (size==0) return -1;
- checkRangeFromTo(from, to, size);
-
- byte[] theElements = elements;
- for (int i = to ; i >= from; i--) {
- if (element==theElements[i]) {return i;} //found
- }
- return -1; //not found
+ // overridden for performance only.
+ if (size==0) return -1;
+ checkRangeFromTo(from, to, size);
+
+ byte[] theElements = elements;
+ for (int i = to ; i >= from; i--) {
+ if (element==theElements[i]) {return i;} //found
+ }
+ return -1; //not found
}
/**
* Returns a new list of the part of the receiver between <code>from</code>, inclusive, and <code>to</code>, inclusive.
@@ -350,13 +350,13 @@
* @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>).
*/
public AbstractByteList partFromTo(int from, int to) {
- if (size==0) return new ByteArrayList(0);
+ if (size==0) return new ByteArrayList(0);
- checkRangeFromTo(from, to, size);
+ checkRangeFromTo(from, to, size);
- byte[] part = new byte[to-from+1];
- System.arraycopy(elements, from, part, 0, to-from+1);
- return new ByteArrayList(part);
+ byte[] part = new byte[to-from+1];
+ System.arraycopy(elements, from, part, 0, to-from+1);
+ return new ByteArrayList(part);
}
/**
* Removes from the receiver all elements that are contained in the specified list.
@@ -366,46 +366,46 @@
* @return <code>true</code> if the receiver changed as a result of the call.
*/
public boolean removeAll(AbstractByteList other) {
- // overridden for performance only.
- if (! (other instanceof ByteArrayList)) return super.removeAll(other);
-
- /* There are two possibilities to do the thing
- a) use other.indexOf(...)
- b) sort other, then use other.binarySearch(...)
-
- Let's try to figure out which one is faster. Let M=size, N=other.size, then
- a) takes O(M*N) steps
- b) takes O(N*logN + M*logN) steps (sorting is O(N*logN) and binarySearch is O(logN))
+ // overridden for performance only.
+ if (! (other instanceof ByteArrayList)) return super.removeAll(other);
+
+ /* There are two possibilities to do the thing
+ a) use other.indexOf(...)
+ b) sort other, then use other.binarySearch(...)
+
+ Let's try to figure out which one is faster. Let M=size, N=other.size, then
+ a) takes O(M*N) steps
+ b) takes O(N*logN + M*logN) steps (sorting is O(N*logN) and binarySearch is O(logN))
- Hence, if N*logN + M*logN < M*N, we use b) otherwise we use a).
- */
- if (other.size()==0) {return false;} //nothing to do
- int limit = other.size()-1;
- int j=0;
- byte[] theElements = elements;
- int mySize = size();
-
- double N=(double) other.size();
- double M=(double) mySize;
- if ( (N+M)* org.apache.mahout.jet.math.Arithmetic.log2(N) < M*N ) {
- // it is faster to sort other before searching in it
- ByteArrayList sortedList = (ByteArrayList) other.clone();
- sortedList.quickSort();
-
- for (int i=0; i<mySize ; i++) {
- if (sortedList.binarySearchFromTo(theElements[i], 0, limit) < 0) theElements[j++]=theElements[i];
- }
- }
- else {
- // it is faster to search in other without sorting
- for (int i=0; i<mySize ; i++) {
- if (other.indexOfFromTo(theElements[i], 0, limit) < 0) theElements[j++]=theElements[i];
- }
- }
-
- boolean modified = (j!=mySize);
- setSize(j);
- return modified;
+ Hence, if N*logN + M*logN < M*N, we use b) otherwise we use a).
+ */
+ if (other.size()==0) {return false;} //nothing to do
+ int limit = other.size()-1;
+ int j=0;
+ byte[] theElements = elements;
+ int mySize = size();
+
+ double N=(double) other.size();
+ double M=(double) mySize;
+ if ( (N+M)* org.apache.mahout.jet.math.Arithmetic.log2(N) < M*N ) {
+ // it is faster to sort other before searching in it
+ ByteArrayList sortedList = (ByteArrayList) other.clone();
+ sortedList.quickSort();
+
+ for (int i=0; i<mySize ; i++) {
+ if (sortedList.binarySearchFromTo(theElements[i], 0, limit) < 0) theElements[j++]=theElements[i];
+ }
+ }
+ else {
+ // it is faster to search in other without sorting
+ for (int i=0; i<mySize ; i++) {
+ if (other.indexOfFromTo(theElements[i], 0, limit) < 0) theElements[j++]=theElements[i];
+ }
+ }
+
+ boolean modified = (j!=mySize);
+ setSize(j);
+ return modified;
}
/**
* Replaces a number of elements in the receiver with the same number of elements of another list.
@@ -418,18 +418,18 @@
* @param otherFrom position of first element within other list to be copied.
*/
public void replaceFromToWithFrom(int from, int to, AbstractByteList other, int otherFrom) {
- // overridden for performance only.
- if (! (other instanceof ByteArrayList)) {
- // slower
- super.replaceFromToWithFrom(from,to,other,otherFrom);
- return;
- }
- int length=to-from+1;
- if (length>0) {
- checkRangeFromTo(from, to, size());
- checkRangeFromTo(otherFrom,otherFrom+length-1,other.size());
- System.arraycopy(((ByteArrayList) other).elements, otherFrom, elements, from, length);
- }
+ // overridden for performance only.
+ if (! (other instanceof ByteArrayList)) {
+ // slower
+ super.replaceFromToWithFrom(from,to,other,otherFrom);
+ return;
+ }
+ int length=to-from+1;
+ if (length>0) {
+ checkRangeFromTo(from, to, size());
+ checkRangeFromTo(otherFrom,otherFrom+length-1,other.size());
+ System.arraycopy(((ByteArrayList) other).elements, otherFrom, elements, from, length);
+ }
}
/**
* Retains (keeps) only the elements in the receiver that are contained in the specified other list.
@@ -439,62 +439,62 @@
* @return <code>true</code> if the receiver changed as a result of the call.
*/
public boolean retainAll(AbstractByteList other) {
- // overridden for performance only.
- if (! (other instanceof ByteArrayList)) return super.retainAll(other);
-
- /* There are two possibilities to do the thing
- a) use other.indexOf(...)
- b) sort other, then use other.binarySearch(...)
-
- Let's try to figure out which one is faster. Let M=size, N=other.size, then
- a) takes O(M*N) steps
- b) takes O(N*logN + M*logN) steps (sorting is O(N*logN) and binarySearch is O(logN))
-
- Hence, if N*logN + M*logN < M*N, we use b) otherwise we use a).
- */
- int limit = other.size()-1;
- int j=0;
- byte[] theElements = elements;
- int mySize = size();
-
- double N=(double) other.size();
- double M=(double) mySize;
- if ( (N+M)* org.apache.mahout.jet.math.Arithmetic.log2(N) < M*N ) {
- // it is faster to sort other before searching in it
- ByteArrayList sortedList = (ByteArrayList) other.clone();
- sortedList.quickSort();
-
- for (int i=0; i<mySize ; i++) {
- if (sortedList.binarySearchFromTo(theElements[i], 0, limit) >= 0) theElements[j++]=theElements[i];
- }
- }
- else {
- // it is faster to search in other without sorting
- for (int i=0; i<mySize ; i++) {
- if (other.indexOfFromTo(theElements[i], 0, limit) >= 0) theElements[j++]=theElements[i];
- }
- }
-
- boolean modified = (j!=mySize);
- setSize(j);
- return modified;
+ // overridden for performance only.
+ if (! (other instanceof ByteArrayList)) return super.retainAll(other);
+
+ /* There are two possibilities to do the thing
+ a) use other.indexOf(...)
+ b) sort other, then use other.binarySearch(...)
+
+ Let's try to figure out which one is faster. Let M=size, N=other.size, then
+ a) takes O(M*N) steps
+ b) takes O(N*logN + M*logN) steps (sorting is O(N*logN) and binarySearch is O(logN))
+
+ Hence, if N*logN + M*logN < M*N, we use b) otherwise we use a).
+ */
+ int limit = other.size()-1;
+ int j=0;
+ byte[] theElements = elements;
+ int mySize = size();
+
+ double N=(double) other.size();
+ double M=(double) mySize;
+ if ( (N+M)* org.apache.mahout.jet.math.Arithmetic.log2(N) < M*N ) {
+ // it is faster to sort other before searching in it
+ ByteArrayList sortedList = (ByteArrayList) other.clone();
+ sortedList.quickSort();
+
+ for (int i=0; i<mySize ; i++) {
+ if (sortedList.binarySearchFromTo(theElements[i], 0, limit) >= 0) theElements[j++]=theElements[i];
+ }
+ }
+ else {
+ // it is faster to search in other without sorting
+ for (int i=0; i<mySize ; i++) {
+ if (other.indexOfFromTo(theElements[i], 0, limit) >= 0) theElements[j++]=theElements[i];
+ }
+ }
+
+ boolean modified = (j!=mySize);
+ setSize(j);
+ return modified;
}
/**
* Reverses the elements of the receiver.
* Last becomes first, second last becomes second first, and so on.
*/
public void reverse() {
- // overridden for performance only.
- byte tmp;
- int limit=size/2;
- int j=size-1;
-
- byte[] theElements = elements;
- for (int i=0; i<limit;) { //swap
- tmp=theElements[i];
- theElements[i++]=theElements[j];
- theElements[j--]=tmp;
- }
+ // overridden for performance only.
+ byte tmp;
+ int limit=size/2;
+ int j=size-1;
+
+ byte[] theElements = elements;
+ for (int i=0; i<limit;) { //swap
+ tmp=theElements[i];
+ theElements[i++]=theElements[j];
+ theElements[j--]=tmp;
+ }
}
/**
* Replaces the element at the specified position in the receiver with the specified element.
@@ -502,13 +502,13 @@
* @param index index of element to replace.
* @param element element to be stored at the specified position.
* @exception IndexOutOfBoundsException index is out of range (index
- * < 0 || index >= size()).
+ * < 0 || index >= size()).
*/
public void set(int index, byte element) {
- // overridden for performance only.
- if (index >= size || index < 0)
- throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
- elements[index] = element;
+ // overridden for performance only.
+ if (index >= size || index < 0)
+ throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
+ elements[index] = element;
}
/**
* Replaces the element at the specified position in the receiver with the specified element; <b>WARNING:</b> Does not check preconditions.
@@ -520,7 +520,7 @@
* @param element element to be stored at the specified position.
*/
public void setQuick(int index, byte element) {
- elements[index] = element;
+ elements[index] = element;
}
/**
* Randomly permutes the part of the receiver between <code>from</code> (inclusive) and <code>to</code> (inclusive).
@@ -529,22 +529,22 @@
* @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>).
*/
public void shuffleFromTo(int from, int to) {
- // overridden for performance only.
- if (size==0) {return;}
- checkRangeFromTo(from, to, size);
-
- org.apache.mahout.jet.random.Uniform gen = new org.apache.mahout.jet.random.Uniform(new org.apache.mahout.jet.random.engine.DRand(new java.util.Date()));
- byte tmpElement;
- byte[] theElements = elements;
- int random;
- for (int i=from; i<to; i++) {
- random = gen.nextIntFromTo(i, to);
-
- //swap(i, random)
- tmpElement = theElements[random];
- theElements[random]=theElements[i];
- theElements[i]=tmpElement;
- }
+ // overridden for performance only.
+ if (size==0) {return;}
+ checkRangeFromTo(from, to, size);
+
+ org.apache.mahout.jet.random.Uniform gen = new org.apache.mahout.jet.random.Uniform(new org.apache.mahout.jet.random.engine.DRand(new java.util.Date()));
+ byte tmpElement;
+ byte[] theElements = elements;
+ int random;
+ for (int i=from; i<to; i++) {
+ random = gen.nextIntFromTo(i, to);
+
+ //swap(i, random)
+ tmpElement = theElements[random];
+ theElements[random]=theElements[i];
+ theElements[i]=tmpElement;
+ }
}
/**
* Sorts the specified range of the receiver into ascending order.
@@ -556,19 +556,19 @@
* @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>).
*/
public void sortFromTo(int from, int to) {
- // try to figure out which option is fastest.
- double N = to - from + 1;
- double quickSortEstimate = N * Math.log(N)/0.6931471805599453; // O(N*log(N,base=2)) ; ln(2)=0.6931471805599453
-
- double width = 256;
- double countSortEstimate = Math.max(width,N); // O(Max(width,N))
-
- if (countSortEstimate < quickSortEstimate) {
- countSortFromTo(from, to);
- }
- else {
- quickSortFromTo(from, to);
- }
+ // try to figure out which option is fastest.
+ double N = to - from + 1;
+ double quickSortEstimate = N * Math.log(N)/0.6931471805599453; // O(N*log(N,base=2)) ; ln(2)=0.6931471805599453
+
+ double width = 256;
+ double countSortEstimate = Math.max(width,N); // O(Max(width,N))
+
+ if (countSortEstimate < quickSortEstimate) {
+ countSortFromTo(from, to);
+ }
+ else {
+ quickSortFromTo(from, to);
+ }
}
/**
* Trims the capacity of the receiver to be the receiver's current
@@ -576,6 +576,6 @@
* storage of the receiver.
*/
public void trimToSize() {
- elements = org.apache.mahout.matrix.Arrays.trimToCapacity(elements,size());
+ elements = org.apache.mahout.matrix.Arrays.trimToCapacity(elements,size());
}
}
Modified: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/list/CharArrayList.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/list/CharArrayList.java?rev=883973&r1=883972&r2=883973&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/list/CharArrayList.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/list/CharArrayList.java Wed Nov 25 03:35:59 2009
@@ -18,17 +18,17 @@
*/
@Deprecated
public class CharArrayList extends AbstractCharList {
- /**
- * The array buffer into which the elements of the list are stored.
- * The capacity of the list is the length of this array buffer.
- * @serial
- */
- protected char[] elements;
+ /**
+ * The array buffer into which the elements of the list are stored.
+ * The capacity of the list is the length of this array buffer.
+ * @serial
+ */
+ protected char[] elements;
/**
* Constructs an empty list.
*/
public CharArrayList() {
- this(10);
+ this(10);
}
/**
* Constructs a list containing the specified elements.
@@ -40,7 +40,7 @@
* @param elements the array to be backed by the the constructed list
*/
public CharArrayList(char[] elements) {
- elements(elements);
+ elements(elements);
}
/**
* Constructs an empty list with the specified initial capacity.
@@ -48,8 +48,8 @@
* @param initialCapacity the number of elements the receiver can hold without auto-expanding itself by allocating new internal memory.
*/
public CharArrayList(int initialCapacity) {
- this(new char[initialCapacity]);
- setSizeRaw(0);
+ this(new char[initialCapacity]);
+ setSizeRaw(0);
}
/**
* Appends the specified element to the end of this list.
@@ -57,11 +57,11 @@
* @param element element to be appended to this list.
*/
public void add(char element) {
- // overridden for performance only.
- if (size == elements.length) {
- ensureCapacity(size + 1);
- }
- elements[size++] = element;
+ // overridden for performance only.
+ if (size == elements.length) {
+ ensureCapacity(size + 1);
+ }
+ elements[size++] = element;
}
/**
* Inserts the specified element before the specified position into the receiver.
@@ -73,13 +73,13 @@
* @exception IndexOutOfBoundsException index is out of range (<tt>index < 0 || index > size()</tt>).
*/
public void beforeInsert(int index, char element) {
- // overridden for performance only.
- if (index > size || index < 0)
- throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
- ensureCapacity(size + 1);
- System.arraycopy(elements, index, elements, index+1, size-index);
- elements[index] = element;
- size++;
+ // overridden for performance only.
+ if (index > size || index < 0)
+ throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
+ ensureCapacity(size + 1);
+ System.arraycopy(elements, index, elements, index+1, size-index);
+ elements[index] = element;
+ size++;
}
/**
* Searches the receiver for the specified value using
@@ -94,18 +94,18 @@
* @param from the leftmost search position, inclusive.
* @param to the rightmost search position, inclusive.
* @return index of the search key, if it is contained in the receiver;
- * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The <i>insertion
- * point</i> is defined as the the point at which the value would
- * be inserted into the receiver: the index of the first
- * element greater than the key, or <tt>receiver.size()</tt>, if all
- * elements in the receiver are less than the specified key. Note
- * that this guarantees that the return value will be >= 0 if
- * and only if the key is found.
+ * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The <i>insertion
+ * point</i> is defined as the the point at which the value would
+ * be inserted into the receiver: the index of the first
+ * element greater than the key, or <tt>receiver.size()</tt>, if all
+ * elements in the receiver are less than the specified key. Note
+ * that this guarantees that the return value will be >= 0 if
+ * and only if the key is found.
* @see org.apache.mahout.matrix.Sorting
* @see java.util.Arrays
*/
public int binarySearchFromTo(char key, int from, int to) {
- return org.apache.mahout.matrix.Sorting.binarySearchFromTo(this.elements,key,from,to);
+ return org.apache.mahout.matrix.Sorting.binarySearchFromTo(this.elements,key,from,to);
}
/**
* Returns a deep copy of the receiver.
@@ -113,10 +113,10 @@
* @return a deep copy of the receiver.
*/
public Object clone() {
- // overridden for performance only.
- CharArrayList clone = new CharArrayList((char[]) elements.clone());
- clone.setSizeRaw(size);
- return clone;
+ // overridden for performance only.
+ CharArrayList clone = new CharArrayList((char[]) elements.clone());
+ clone.setSizeRaw(size);
+ return clone;
}
/**
* Returns a deep copy of the receiver; uses <code>clone()</code> and casts the result.
@@ -124,7 +124,7 @@
* @return a deep copy of the receiver.
*/
public CharArrayList copy() {
- return (CharArrayList) clone();
+ return (CharArrayList) clone();
}
/**
* Sorts the specified range of the receiver into ascending numerical order.
@@ -141,28 +141,28 @@
* @param max the largest element contained in the range.
*/
protected void countSortFromTo(int from, int to, char min, char max) {
- if (size==0) return;
- checkRangeFromTo(from, to, size);
+ if (size==0) return;
+ checkRangeFromTo(from, to, size);
- final int width = (int) (max-min+1);
-
- int[] counts = new int[width];
- char[] theElements = elements;
- for (int i=from; i<=to; ) counts[(int)(theElements[i++]-min)]++;
-
- int fromIndex = from;
- char val = min;
- for (int i=0; i<width; i++, val++) {
- int c = counts[i];
- if (c>0) {
- if (c==1) theElements[fromIndex++]=val;
- else {
- int toIndex = fromIndex + c - 1;
- fillFromToWith(fromIndex,toIndex,val);
- fromIndex = toIndex + 1;
- }
- }
- }
+ final int width = (int) (max-min+1);
+
+ int[] counts = new int[width];
+ char[] theElements = elements;
+ for (int i=from; i<=to; ) counts[(int)(theElements[i++]-min)]++;
+
+ int fromIndex = from;
+ char val = min;
+ for (int i=0; i<width; i++, val++) {
+ int c = counts[i];
+ if (c>0) {
+ if (c==1) theElements[fromIndex++]=val;
+ else {
+ int toIndex = fromIndex + c - 1;
+ fillFromToWith(fromIndex,toIndex,val);
+ fromIndex = toIndex + 1;
+ }
+ }
+ }
}
/**
* Returns the elements currently stored, including invalid elements between size and capacity, if any.
@@ -173,7 +173,7 @@
* @return the elements currently stored.
*/
public char[] elements() {
- return elements;
+ return elements;
}
/**
* Sets the receiver's elements to be the specified array (not a copy of it).
@@ -186,9 +186,9 @@
* @return the receiver itself.
*/
public AbstractCharList elements(char[] elements) {
- this.elements=elements;
- this.size=elements.length;
- return this;
+ this.elements=elements;
+ this.size=elements.length;
+ return this;
}
/**
* Ensures that the receiver can hold at least the specified number of elements without needing to allocate new internal memory.
@@ -197,7 +197,7 @@
* @param minCapacity the desired minimum capacity.
*/
public void ensureCapacity(int minCapacity) {
- elements = org.apache.mahout.matrix.Arrays.ensureCapacity(elements,minCapacity);
+ elements = org.apache.mahout.matrix.Arrays.ensureCapacity(elements,minCapacity);
}
/**
* Compares the specified Object with the receiver.
@@ -210,19 +210,19 @@
* @return true if the specified Object is equal to the receiver.
*/
public boolean equals(Object otherObj) { //delta
- // overridden for performance only.
- if (! (otherObj instanceof CharArrayList)) return super.equals(otherObj);
- if (this==otherObj) return true;
- if (otherObj==null) return false;
- CharArrayList other = (CharArrayList) otherObj;
- if (size()!=other.size()) return false;
-
- char[] theElements = elements();
- char[] otherElements = other.elements();
- for (int i=size(); --i >= 0; ) {
- if (theElements[i] != otherElements[i]) return false;
- }
- return true;
+ // overridden for performance only.
+ if (! (otherObj instanceof CharArrayList)) return super.equals(otherObj);
+ if (this==otherObj) return true;
+ if (otherObj==null) return false;
+ CharArrayList other = (CharArrayList) otherObj;
+ if (size()!=other.size()) return false;
+
+ char[] theElements = elements();
+ char[] otherElements = other.elements();
+ for (int i=size(); --i >= 0; ) {
+ if (theElements[i] != otherElements[i]) return false;
+ }
+ return true;
}
/**
* Applies a procedure to each element of the receiver, if any.
@@ -231,25 +231,25 @@
* @return <tt>false</tt> if the procedure stopped before all elements where iterated over, <tt>true</tt> otherwise.
*/
public boolean forEach(CharProcedure procedure) {
- // overridden for performance only.
- char[] theElements = elements;
- int theSize = size;
-
- for (int i=0; i<theSize;) if (! procedure.apply(theElements[i++])) return false;
- return true;
+ // overridden for performance only.
+ char[] theElements = elements;
+ int theSize = size;
+
+ for (int i=0; i<theSize;) if (! procedure.apply(theElements[i++])) return false;
+ return true;
}
/**
* Returns the element at the specified position in the receiver.
*
* @param index index of element to return.
* @exception IndexOutOfBoundsException index is out of range (index
- * < 0 || index >= size()).
+ * < 0 || index >= size()).
*/
public char get(int index) {
- // overridden for performance only.
- if (index >= size || index < 0)
- throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
- return elements[index];
+ // overridden for performance only.
+ if (index >= size || index < 0)
+ throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
+ return elements[index];
}
/**
* Returns the element at the specified position in the receiver; <b>WARNING:</b> Does not check preconditions.
@@ -260,7 +260,7 @@
* @param index index of element to return.
*/
public char getQuick(int index) {
- return elements[index];
+ return elements[index];
}
/**
* Returns the index of the first occurrence of the specified
@@ -275,15 +275,15 @@
* @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>).
*/
public int indexOfFromTo(char element, int from, int to) {
- // overridden for performance only.
- if (size==0) return -1;
- checkRangeFromTo(from, to, size);
-
- char[] theElements = elements;
- for (int i = from ; i <= to; i++) {
- if (element==theElements[i]) {return i;} //found
- }
- return -1; //not found
+ // overridden for performance only.
+ if (size==0) return -1;
+ checkRangeFromTo(from, to, size);
+
+ char[] theElements = elements;
+ for (int i = from ; i <= to; i++) {
+ if (element==theElements[i]) {return i;} //found
+ }
+ return -1; //not found
}
/**
* Returns the index of the last occurrence of the specified
@@ -298,15 +298,15 @@
* @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>).
*/
public int lastIndexOfFromTo(char element, int from, int to) {
- // overridden for performance only.
- if (size==0) return -1;
- checkRangeFromTo(from, to, size);
-
- char[] theElements = elements;
- for (int i = to ; i >= from; i--) {
- if (element==theElements[i]) {return i;} //found
- }
- return -1; //not found
+ // overridden for performance only.
+ if (size==0) return -1;
+ checkRangeFromTo(from, to, size);
+
+ char[] theElements = elements;
+ for (int i = to ; i >= from; i--) {
+ if (element==theElements[i]) {return i;} //found
+ }
+ return -1; //not found
}
/**
* Returns a new list of the part of the receiver between <code>from</code>, inclusive, and <code>to</code>, inclusive.
@@ -316,13 +316,13 @@
* @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>).
*/
public AbstractCharList partFromTo(int from, int to) {
- if (size==0) return new CharArrayList(0);
+ if (size==0) return new CharArrayList(0);
- checkRangeFromTo(from, to, size);
+ checkRangeFromTo(from, to, size);
- char[] part = new char[to-from+1];
- System.arraycopy(elements, from, part, 0, to-from+1);
- return new CharArrayList(part);
+ char[] part = new char[to-from+1];
+ System.arraycopy(elements, from, part, 0, to-from+1);
+ return new CharArrayList(part);
}
/**
* Removes from the receiver all elements that are contained in the specified list.
@@ -332,46 +332,46 @@
* @return <code>true</code> if the receiver changed as a result of the call.
*/
public boolean removeAll(AbstractCharList other) {
- // overridden for performance only.
- if (! (other instanceof CharArrayList)) return super.removeAll(other);
-
- /* There are two possibilities to do the thing
- a) use other.indexOf(...)
- b) sort other, then use other.binarySearch(...)
-
- Let's try to figure out which one is faster. Let M=size, N=other.size, then
- a) takes O(M*N) steps
- b) takes O(N*logN + M*logN) steps (sorting is O(N*logN) and binarySearch is O(logN))
+ // overridden for performance only.
+ if (! (other instanceof CharArrayList)) return super.removeAll(other);
+
+ /* There are two possibilities to do the thing
+ a) use other.indexOf(...)
+ b) sort other, then use other.binarySearch(...)
+
+ Let's try to figure out which one is faster. Let M=size, N=other.size, then
+ a) takes O(M*N) steps
+ b) takes O(N*logN + M*logN) steps (sorting is O(N*logN) and binarySearch is O(logN))
- Hence, if N*logN + M*logN < M*N, we use b) otherwise we use a).
- */
- if (other.size()==0) {return false;} //nothing to do
- int limit = other.size()-1;
- int j=0;
- char[] theElements = elements;
- int mySize = size();
-
- double N=(double) other.size();
- double M=(double) mySize;
- if ( (N+M)* org.apache.mahout.jet.math.Arithmetic.log2(N) < M*N ) {
- // it is faster to sort other before searching in it
- CharArrayList sortedList = (CharArrayList) other.clone();
- sortedList.quickSort();
-
- for (int i=0; i<mySize ; i++) {
- if (sortedList.binarySearchFromTo(theElements[i], 0, limit) < 0) theElements[j++]=theElements[i];
- }
- }
- else {
- // it is faster to search in other without sorting
- for (int i=0; i<mySize ; i++) {
- if (other.indexOfFromTo(theElements[i], 0, limit) < 0) theElements[j++]=theElements[i];
- }
- }
-
- boolean modified = (j!=mySize);
- setSize(j);
- return modified;
+ Hence, if N*logN + M*logN < M*N, we use b) otherwise we use a).
+ */
+ if (other.size()==0) {return false;} //nothing to do
+ int limit = other.size()-1;
+ int j=0;
+ char[] theElements = elements;
+ int mySize = size();
+
+ double N=(double) other.size();
+ double M=(double) mySize;
+ if ( (N+M)* org.apache.mahout.jet.math.Arithmetic.log2(N) < M*N ) {
+ // it is faster to sort other before searching in it
+ CharArrayList sortedList = (CharArrayList) other.clone();
+ sortedList.quickSort();
+
+ for (int i=0; i<mySize ; i++) {
+ if (sortedList.binarySearchFromTo(theElements[i], 0, limit) < 0) theElements[j++]=theElements[i];
+ }
+ }
+ else {
+ // it is faster to search in other without sorting
+ for (int i=0; i<mySize ; i++) {
+ if (other.indexOfFromTo(theElements[i], 0, limit) < 0) theElements[j++]=theElements[i];
+ }
+ }
+
+ boolean modified = (j!=mySize);
+ setSize(j);
+ return modified;
}
/**
* Replaces a number of elements in the receiver with the same number of elements of another list.
@@ -384,18 +384,18 @@
* @param otherFrom position of first element within other list to be copied.
*/
public void replaceFromToWithFrom(int from, int to, AbstractCharList other, int otherFrom) {
- // overridden for performance only.
- if (! (other instanceof CharArrayList)) {
- // slower
- super.replaceFromToWithFrom(from,to,other,otherFrom);
- return;
- }
- int length=to-from+1;
- if (length>0) {
- checkRangeFromTo(from, to, size());
- checkRangeFromTo(otherFrom,otherFrom+length-1,other.size());
- System.arraycopy(((CharArrayList) other).elements, otherFrom, elements, from, length);
- }
+ // overridden for performance only.
+ if (! (other instanceof CharArrayList)) {
+ // slower
+ super.replaceFromToWithFrom(from,to,other,otherFrom);
+ return;
+ }
+ int length=to-from+1;
+ if (length>0) {
+ checkRangeFromTo(from, to, size());
+ checkRangeFromTo(otherFrom,otherFrom+length-1,other.size());
+ System.arraycopy(((CharArrayList) other).elements, otherFrom, elements, from, length);
+ }
}
/**
* Retains (keeps) only the elements in the receiver that are contained in the specified other list.
@@ -405,62 +405,62 @@
* @return <code>true</code> if the receiver changed as a result of the call.
*/
public boolean retainAll(AbstractCharList other) {
- // overridden for performance only.
- if (! (other instanceof CharArrayList)) return super.retainAll(other);
-
- /* There are two possibilities to do the thing
- a) use other.indexOf(...)
- b) sort other, then use other.binarySearch(...)
-
- Let's try to figure out which one is faster. Let M=size, N=other.size, then
- a) takes O(M*N) steps
- b) takes O(N*logN + M*logN) steps (sorting is O(N*logN) and binarySearch is O(logN))
-
- Hence, if N*logN + M*logN < M*N, we use b) otherwise we use a).
- */
- int limit = other.size()-1;
- int j=0;
- char[] theElements = elements;
- int mySize = size();
-
- double N=(double) other.size();
- double M=(double) mySize;
- if ( (N+M)* org.apache.mahout.jet.math.Arithmetic.log2(N) < M*N ) {
- // it is faster to sort other before searching in it
- CharArrayList sortedList = (CharArrayList) other.clone();
- sortedList.quickSort();
-
- for (int i=0; i<mySize ; i++) {
- if (sortedList.binarySearchFromTo(theElements[i], 0, limit) >= 0) theElements[j++]=theElements[i];
- }
- }
- else {
- // it is faster to search in other without sorting
- for (int i=0; i<mySize ; i++) {
- if (other.indexOfFromTo(theElements[i], 0, limit) >= 0) theElements[j++]=theElements[i];
- }
- }
-
- boolean modified = (j!=mySize);
- setSize(j);
- return modified;
+ // overridden for performance only.
+ if (! (other instanceof CharArrayList)) return super.retainAll(other);
+
+ /* There are two possibilities to do the thing
+ a) use other.indexOf(...)
+ b) sort other, then use other.binarySearch(...)
+
+ Let's try to figure out which one is faster. Let M=size, N=other.size, then
+ a) takes O(M*N) steps
+ b) takes O(N*logN + M*logN) steps (sorting is O(N*logN) and binarySearch is O(logN))
+
+ Hence, if N*logN + M*logN < M*N, we use b) otherwise we use a).
+ */
+ int limit = other.size()-1;
+ int j=0;
+ char[] theElements = elements;
+ int mySize = size();
+
+ double N=(double) other.size();
+ double M=(double) mySize;
+ if ( (N+M)* org.apache.mahout.jet.math.Arithmetic.log2(N) < M*N ) {
+ // it is faster to sort other before searching in it
+ CharArrayList sortedList = (CharArrayList) other.clone();
+ sortedList.quickSort();
+
+ for (int i=0; i<mySize ; i++) {
+ if (sortedList.binarySearchFromTo(theElements[i], 0, limit) >= 0) theElements[j++]=theElements[i];
+ }
+ }
+ else {
+ // it is faster to search in other without sorting
+ for (int i=0; i<mySize ; i++) {
+ if (other.indexOfFromTo(theElements[i], 0, limit) >= 0) theElements[j++]=theElements[i];
+ }
+ }
+
+ boolean modified = (j!=mySize);
+ setSize(j);
+ return modified;
}
/**
* Reverses the elements of the receiver.
* Last becomes first, second last becomes second first, and so on.
*/
public void reverse() {
- // overridden for performance only.
- char tmp;
- int limit=size/2;
- int j=size-1;
-
- char[] theElements = elements;
- for (int i=0; i<limit;) { //swap
- tmp=theElements[i];
- theElements[i++]=theElements[j];
- theElements[j--]=tmp;
- }
+ // overridden for performance only.
+ char tmp;
+ int limit=size/2;
+ int j=size-1;
+
+ char[] theElements = elements;
+ for (int i=0; i<limit;) { //swap
+ tmp=theElements[i];
+ theElements[i++]=theElements[j];
+ theElements[j--]=tmp;
+ }
}
/**
* Replaces the element at the specified position in the receiver with the specified element.
@@ -468,13 +468,13 @@
* @param index index of element to replace.
* @param element element to be stored at the specified position.
* @exception IndexOutOfBoundsException index is out of range (index
- * < 0 || index >= size()).
+ * < 0 || index >= size()).
*/
public void set(int index, char element) {
- // overridden for performance only.
- if (index >= size || index < 0)
- throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
- elements[index] = element;
+ // overridden for performance only.
+ if (index >= size || index < 0)
+ throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
+ elements[index] = element;
}
/**
* Replaces the element at the specified position in the receiver with the specified element; <b>WARNING:</b> Does not check preconditions.
@@ -486,7 +486,7 @@
* @param element element to be stored at the specified position.
*/
public void setQuick(int index, char element) {
- elements[index] = element;
+ elements[index] = element;
}
/**
* Randomly permutes the part of the receiver between <code>from</code> (inclusive) and <code>to</code> (inclusive).
@@ -495,22 +495,22 @@
* @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>).
*/
public void shuffleFromTo(int from, int to) {
- // overridden for performance only.
- if (size==0) {return;}
- checkRangeFromTo(from, to, size);
-
- org.apache.mahout.jet.random.Uniform gen = new org.apache.mahout.jet.random.Uniform(new org.apache.mahout.jet.random.engine.DRand(new java.util.Date()));
- char tmpElement;
- char[] theElements = elements;
- int random;
- for (int i=from; i<to; i++) {
- random = gen.nextIntFromTo(i, to);
-
- //swap(i, random)
- tmpElement = theElements[random];
- theElements[random]=theElements[i];
- theElements[i]=tmpElement;
- }
+ // overridden for performance only.
+ if (size==0) {return;}
+ checkRangeFromTo(from, to, size);
+
+ org.apache.mahout.jet.random.Uniform gen = new org.apache.mahout.jet.random.Uniform(new org.apache.mahout.jet.random.engine.DRand(new java.util.Date()));
+ char tmpElement;
+ char[] theElements = elements;
+ int random;
+ for (int i=from; i<to; i++) {
+ random = gen.nextIntFromTo(i, to);
+
+ //swap(i, random)
+ tmpElement = theElements[random];
+ theElements[random]=theElements[i];
+ theElements[i]=tmpElement;
+ }
}
/**
* Sorts the specified range of the receiver into ascending order.
@@ -529,39 +529,39 @@
* @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>).
*/
public void sortFromTo(int from, int to) {
- /*
- * Computes min and max and decides on this basis.
- * In practice the additional overhead is very small compared to the potential gains.
- */
- final int widthThreshold = 10000; // never consider options resulting in outrageous memory allocations.
-
- if (size==0) return;
- checkRangeFromTo(from, to, size);
-
- // determine minimum and maximum.
- char min=elements[from];
- char max=elements[from];
-
- char[] theElements = elements;
- for (int i=from+1; i<=to; ) {
- char elem = theElements[i++];
- if (elem>max) max=elem;
- else if (elem<min) min=elem;
- }
-
- // try to figure out which option is fastest.
- double N = (double)to - (double)from + 1.0;
- double quickSortEstimate = N * Math.log(N)/0.6931471805599453; // O(N*log(N,base=2)) ; ln(2)=0.6931471805599453
-
- double width = (double)max - (double)min + 1.0;
- double countSortEstimate = Math.max(width,N); // O(Max(width,N))
-
- if (width < widthThreshold && countSortEstimate < quickSortEstimate) {
- countSortFromTo(from, to, min, max);
- }
- else {
- quickSortFromTo(from, to);
- }
+ /*
+ * Computes min and max and decides on this basis.
+ * In practice the additional overhead is very small compared to the potential gains.
+ */
+ final int widthThreshold = 10000; // never consider options resulting in outrageous memory allocations.
+
+ if (size==0) return;
+ checkRangeFromTo(from, to, size);
+
+ // determine minimum and maximum.
+ char min=elements[from];
+ char max=elements[from];
+
+ char[] theElements = elements;
+ for (int i=from+1; i<=to; ) {
+ char elem = theElements[i++];
+ if (elem>max) max=elem;
+ else if (elem<min) min=elem;
+ }
+
+ // try to figure out which option is fastest.
+ double N = (double)to - (double)from + 1.0;
+ double quickSortEstimate = N * Math.log(N)/0.6931471805599453; // O(N*log(N,base=2)) ; ln(2)=0.6931471805599453
+
+ double width = (double)max - (double)min + 1.0;
+ double countSortEstimate = Math.max(width,N); // O(Max(width,N))
+
+ if (width < widthThreshold && countSortEstimate < quickSortEstimate) {
+ countSortFromTo(from, to, min, max);
+ }
+ else {
+ quickSortFromTo(from, to);
+ }
}
/**
* Trims the capacity of the receiver to be the receiver's current
@@ -569,6 +569,6 @@
* storage of the receiver.
*/
public void trimToSize() {
- elements = org.apache.mahout.matrix.Arrays.trimToCapacity(elements,size());
+ elements = org.apache.mahout.matrix.Arrays.trimToCapacity(elements,size());
}
}
Modified: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/list/DistinctNumberList.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/list/DistinctNumberList.java?rev=883973&r1=883972&r2=883973&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/list/DistinctNumberList.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/list/DistinctNumberList.java Wed Nov 25 03:35:59 2009
@@ -51,16 +51,14 @@
* @see MinMaxNumberList
* @see java.lang.Float
* @see java.lang.Double
- * @author wolfgang.hoschek@cern.ch
- * @version 1.0, 09/24/99
*/
/**
* @deprecated until unit tests are in place. Until this time, this class/interface is unsupported.
*/
@Deprecated
public class DistinctNumberList extends org.apache.mahout.matrix.list.AbstractLongList {
- protected long[] distinctValues;
- protected MinMaxNumberList elements;
+ protected long[] distinctValues;
+ protected MinMaxNumberList elements;
/**
* Constructs an empty list with the specified initial capacity and the specified distinct values allowed to be hold in this list.
*
@@ -68,7 +66,7 @@
* @param initialCapacity the number of elements the receiver can hold without auto-expanding itself by allocating new internal memory.
*/
public DistinctNumberList(long[] distinctValues, int initialCapacity) {
- setUp(distinctValues,initialCapacity);
+ setUp(distinctValues,initialCapacity);
}
/**
* Appends the specified element to the end of this list.
@@ -76,17 +74,17 @@
* @param element element to be appended to this list.
*/
public void add(long element) {
- //overridden for performance only.
- elements.add(codeOf(element));
- size++;
+ //overridden for performance only.
+ elements.add(codeOf(element));
+ size++;
}
/**
* Returns the code that shall be stored for the given element.
*/
protected int codeOf(long element) {
- int index = java.util.Arrays.binarySearch(distinctValues,element);
- if (index<0) throw new IllegalArgumentException("Element="+element+" not contained in distinct elements.");
- return index;
+ int index = java.util.Arrays.binarySearch(distinctValues,element);
+ if (index<0) throw new IllegalArgumentException("Element="+element+" not contained in distinct elements.");
+ return index;
}
/**
* Ensures that the receiver can hold at least the specified number of elements without needing to allocate new internal memory.
@@ -95,7 +93,7 @@
* @param minCapacity the desired minimum capacity.
*/
public void ensureCapacity(int minCapacity) {
- elements.ensureCapacity(minCapacity);
+ elements.ensureCapacity(minCapacity);
}
/**
* Returns the element at the specified position in the receiver; <b>WARNING:</b> Does not check preconditions.
@@ -106,7 +104,7 @@
* @param index index of element to return.
*/
public long getQuick(int index) {
- return distinctValues[(int)(elements.getQuick(index))];
+ return distinctValues[(int)(elements.getQuick(index))];
}
/**
* Removes from the receiver all elements whose index is between
@@ -119,8 +117,8 @@
* @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>).
*/
public void removeFromTo(int from, int to) {
- elements.removeFromTo(from,to);
- size -= to-from+1;
+ elements.removeFromTo(from,to);
+ size -= to-from+1;
}
/**
* Replaces the element at the specified position in the receiver with the specified element; <b>WARNING:</b> Does not check preconditions.
@@ -132,15 +130,15 @@
* @param element element to be stored at the specified position.
*/
public void setQuick(int index, long element) {
- elements.setQuick(index,codeOf(element));
+ elements.setQuick(index,codeOf(element));
}
/**
* Sets the size of the receiver without modifying it otherwise.
* This method should not release or allocate new memory but simply set some instance variable like <tt>size</tt>.
*/
protected void setSizeRaw(int newSize) {
- super.setSizeRaw(newSize);
- elements.setSizeRaw(newSize);
+ super.setSizeRaw(newSize);
+ elements.setSizeRaw(newSize);
}
/**
* Sets the receiver to an empty list with the specified initial capacity and the specified distinct values allowed to be hold in this list.
@@ -149,9 +147,9 @@
* @param initialCapacity the number of elements the receiver can hold without auto-expanding itself by allocating new internal memory.
*/
protected void setUp(long[] distinctValues, int initialCapacity) {
- this.distinctValues = distinctValues;
- //java.util.Arrays.sort(this.distinctElements);
- this.elements = new MinMaxNumberList(0,distinctValues.length-1,initialCapacity);
+ this.distinctValues = distinctValues;
+ //java.util.Arrays.sort(this.distinctElements);
+ this.elements = new MinMaxNumberList(0,distinctValues.length-1,initialCapacity);
}
/**
* Trims the capacity of the receiver to be the receiver's current
@@ -159,6 +157,6 @@
* storage of the receiver.
*/
public void trimToSize() {
- elements.trimToSize();
+ elements.trimToSize();
}
}
Modified: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/list/DoubleArrayList.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/list/DoubleArrayList.java?rev=883973&r1=883972&r2=883973&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/list/DoubleArrayList.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/list/DoubleArrayList.java Wed Nov 25 03:35:59 2009
@@ -18,17 +18,17 @@
*/
@Deprecated
public class DoubleArrayList extends AbstractDoubleList {
- /**
- * The array buffer into which the elements of the list are stored.
- * The capacity of the list is the length of this array buffer.
- * @serial
- */
- protected double[] elements;
+ /**
+ * The array buffer into which the elements of the list are stored.
+ * The capacity of the list is the length of this array buffer.
+ * @serial
+ */
+ protected double[] elements;
/**
* Constructs an empty list.
*/
public DoubleArrayList() {
- this(10);
+ this(10);
}
/**
* Constructs a list containing the specified elements.
@@ -40,7 +40,7 @@
* @param elements the array to be backed by the the constructed list
*/
public DoubleArrayList(double[] elements) {
- elements(elements);
+ elements(elements);
}
/**
* Constructs an empty list with the specified initial capacity.
@@ -48,8 +48,8 @@
* @param initialCapacity the number of elements the receiver can hold without auto-expanding itself by allocating new internal memory.
*/
public DoubleArrayList(int initialCapacity) {
- this(new double[initialCapacity]);
- setSizeRaw(0);
+ this(new double[initialCapacity]);
+ setSizeRaw(0);
}
/**
* Appends the specified element to the end of this list.
@@ -57,9 +57,9 @@
* @param element element to be appended to this list.
*/
public void add(double element) {
- // overridden for performance only.
- if (size == elements.length) ensureCapacity(size + 1);
- elements[size++] = element;
+ // overridden for performance only.
+ if (size == elements.length) ensureCapacity(size + 1);
+ elements[size++] = element;
}
/**
* Inserts the specified element before the specified position into the receiver.
@@ -71,17 +71,17 @@
* @exception IndexOutOfBoundsException index is out of range (<tt>index < 0 || index > size()</tt>).
*/
public void beforeInsert(int index, double element) {
- // overridden for performance only.
- if (size == index) {
- add(element);
- return;
- }
- if (index > size || index < 0)
- throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
- ensureCapacity(size + 1);
- System.arraycopy(elements, index, elements, index+1, size-index);
- elements[index] = element;
- size++;
+ // overridden for performance only.
+ if (size == index) {
+ add(element);
+ return;
+ }
+ if (index > size || index < 0)
+ throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
+ ensureCapacity(size + 1);
+ System.arraycopy(elements, index, elements, index+1, size-index);
+ elements[index] = element;
+ size++;
}
/**
* Searches the receiver for the specified value using
@@ -96,18 +96,18 @@
* @param from the leftmost search position, inclusive.
* @param to the rightmost search position, inclusive.
* @return index of the search key, if it is contained in the receiver;
- * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The <i>insertion
- * point</i> is defined as the the point at which the value would
- * be inserted into the receiver: the index of the first
- * element greater than the key, or <tt>receiver.size()</tt>, if all
- * elements in the receiver are less than the specified key. Note
- * that this guarantees that the return value will be >= 0 if
- * and only if the key is found.
+ * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The <i>insertion
+ * point</i> is defined as the the point at which the value would
+ * be inserted into the receiver: the index of the first
+ * element greater than the key, or <tt>receiver.size()</tt>, if all
+ * elements in the receiver are less than the specified key. Note
+ * that this guarantees that the return value will be >= 0 if
+ * and only if the key is found.
* @see org.apache.mahout.matrix.Sorting
* @see java.util.Arrays
*/
public int binarySearchFromTo(double key, int from, int to) {
- return org.apache.mahout.matrix.Sorting.binarySearchFromTo(this.elements,key,from,to);
+ return org.apache.mahout.matrix.Sorting.binarySearchFromTo(this.elements,key,from,to);
}
/**
* Returns a deep copy of the receiver.
@@ -115,10 +115,10 @@
* @return a deep copy of the receiver.
*/
public Object clone() {
- // overridden for performance only.
- DoubleArrayList clone = new DoubleArrayList((double[]) elements.clone());
- clone.setSizeRaw(size);
- return clone;
+ // overridden for performance only.
+ DoubleArrayList clone = new DoubleArrayList((double[]) elements.clone());
+ clone.setSizeRaw(size);
+ return clone;
}
/**
* Returns a deep copy of the receiver; uses <code>clone()</code> and casts the result.
@@ -126,7 +126,7 @@
* @return a deep copy of the receiver.
*/
public DoubleArrayList copy() {
- return (DoubleArrayList) clone();
+ return (DoubleArrayList) clone();
}
/**
* Returns the elements currently stored, including invalid elements between size and capacity, if any.
@@ -137,7 +137,7 @@
* @return the elements currently stored.
*/
public double[] elements() {
- return elements;
+ return elements;
}
/**
* Sets the receiver's elements to be the specified array (not a copy of it).
@@ -150,9 +150,9 @@
* @return the receiver itself.
*/
public AbstractDoubleList elements(double[] elements) {
- this.elements=elements;
- this.size=elements.length;
- return this;
+ this.elements=elements;
+ this.size=elements.length;
+ return this;
}
/**
* Ensures that the receiver can hold at least the specified number of elements without needing to allocate new internal memory.
@@ -161,7 +161,7 @@
* @param minCapacity the desired minimum capacity.
*/
public void ensureCapacity(int minCapacity) {
- elements = org.apache.mahout.matrix.Arrays.ensureCapacity(elements,minCapacity);
+ elements = org.apache.mahout.matrix.Arrays.ensureCapacity(elements,minCapacity);
}
/**
* Compares the specified Object with the receiver.
@@ -174,19 +174,19 @@
* @return true if the specified Object is equal to the receiver.
*/
public boolean equals(Object otherObj) { //delta
- // overridden for performance only.
- if (! (otherObj instanceof DoubleArrayList)) return super.equals(otherObj);
- if (this==otherObj) return true;
- if (otherObj==null) return false;
- DoubleArrayList other = (DoubleArrayList) otherObj;
- if (size()!=other.size()) return false;
-
- double[] theElements = elements();
- double[] otherElements = other.elements();
- for (int i=size(); --i >= 0; ) {
- if (theElements[i] != otherElements[i]) return false;
- }
- return true;
+ // overridden for performance only.
+ if (! (otherObj instanceof DoubleArrayList)) return super.equals(otherObj);
+ if (this==otherObj) return true;
+ if (otherObj==null) return false;
+ DoubleArrayList other = (DoubleArrayList) otherObj;
+ if (size()!=other.size()) return false;
+
+ double[] theElements = elements();
+ double[] otherElements = other.elements();
+ for (int i=size(); --i >= 0; ) {
+ if (theElements[i] != otherElements[i]) return false;
+ }
+ return true;
}
/**
* Applies a procedure to each element of the receiver, if any.
@@ -195,25 +195,25 @@
* @return <tt>false</tt> if the procedure stopped before all elements where iterated over, <tt>true</tt> otherwise.
*/
public boolean forEach(DoubleProcedure procedure) {
- // overridden for performance only.
- double[] theElements = elements;
- int theSize = size;
-
- for (int i=0; i<theSize;) if (! procedure.apply(theElements[i++])) return false;
- return true;
+ // overridden for performance only.
+ double[] theElements = elements;
+ int theSize = size;
+
+ for (int i=0; i<theSize;) if (! procedure.apply(theElements[i++])) return false;
+ return true;
}
/**
* Returns the element at the specified position in the receiver.
*
* @param index index of element to return.
* @exception IndexOutOfBoundsException index is out of range (index
- * < 0 || index >= size()).
+ * < 0 || index >= size()).
*/
public double get(int index) {
- // overridden for performance only.
- if (index >= size || index < 0)
- throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
- return elements[index];
+ // overridden for performance only.
+ if (index >= size || index < 0)
+ throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
+ return elements[index];
}
/**
* Returns the element at the specified position in the receiver; <b>WARNING:</b> Does not check preconditions.
@@ -224,7 +224,7 @@
* @param index index of element to return.
*/
public double getQuick(int index) {
- return elements[index];
+ return elements[index];
}
/**
* Returns the index of the first occurrence of the specified
@@ -239,15 +239,15 @@
* @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>).
*/
public int indexOfFromTo(double element, int from, int to) {
- // overridden for performance only.
- if (size==0) return -1;
- checkRangeFromTo(from, to, size);
-
- double[] theElements = elements;
- for (int i = from ; i <= to; i++) {
- if (element==theElements[i]) {return i;} //found
- }
- return -1; //not found
+ // overridden for performance only.
+ if (size==0) return -1;
+ checkRangeFromTo(from, to, size);
+
+ double[] theElements = elements;
+ for (int i = from ; i <= to; i++) {
+ if (element==theElements[i]) {return i;} //found
+ }
+ return -1; //not found
}
/**
* Returns the index of the last occurrence of the specified
@@ -262,15 +262,15 @@
* @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>).
*/
public int lastIndexOfFromTo(double element, int from, int to) {
- // overridden for performance only.
- if (size==0) return -1;
- checkRangeFromTo(from, to, size);
-
- double[] theElements = elements;
- for (int i = to ; i >= from; i--) {
- if (element==theElements[i]) {return i;} //found
- }
- return -1; //not found
+ // overridden for performance only.
+ if (size==0) return -1;
+ checkRangeFromTo(from, to, size);
+
+ double[] theElements = elements;
+ for (int i = to ; i >= from; i--) {
+ if (element==theElements[i]) {return i;} //found
+ }
+ return -1; //not found
}
/**
* Returns a new list of the part of the receiver between <code>from</code>, inclusive, and <code>to</code>, inclusive.
@@ -280,13 +280,13 @@
* @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>).
*/
public AbstractDoubleList partFromTo(int from, int to) {
- if (size==0) return new DoubleArrayList(0);
+ if (size==0) return new DoubleArrayList(0);
- checkRangeFromTo(from, to, size);
+ checkRangeFromTo(from, to, size);
- double[] part = new double[to-from+1];
- System.arraycopy(elements, from, part, 0, to-from+1);
- return new DoubleArrayList(part);
+ double[] part = new double[to-from+1];
+ System.arraycopy(elements, from, part, 0, to-from+1);
+ return new DoubleArrayList(part);
}
/**
* Removes from the receiver all elements that are contained in the specified list.
@@ -296,46 +296,46 @@
* @return <code>true</code> if the receiver changed as a result of the call.
*/
public boolean removeAll(AbstractDoubleList other) {
- // overridden for performance only.
- if (! (other instanceof DoubleArrayList)) return super.removeAll(other);
-
- /* There are two possibilities to do the thing
- a) use other.indexOf(...)
- b) sort other, then use other.binarySearch(...)
-
- Let's try to figure out which one is faster. Let M=size, N=other.size, then
- a) takes O(M*N) steps
- b) takes O(N*logN + M*logN) steps (sorting is O(N*logN) and binarySearch is O(logN))
+ // overridden for performance only.
+ if (! (other instanceof DoubleArrayList)) return super.removeAll(other);
+
+ /* There are two possibilities to do the thing
+ a) use other.indexOf(...)
+ b) sort other, then use other.binarySearch(...)
+
+ Let's try to figure out which one is faster. Let M=size, N=other.size, then
+ a) takes O(M*N) steps
+ b) takes O(N*logN + M*logN) steps (sorting is O(N*logN) and binarySearch is O(logN))
- Hence, if N*logN + M*logN < M*N, we use b) otherwise we use a).
- */
- if (other.size()==0) {return false;} //nothing to do
- int limit = other.size()-1;
- int j=0;
- double[] theElements = elements;
- int mySize = size();
-
- double N=(double) other.size();
- double M=(double) mySize;
- if ( (N+M)* org.apache.mahout.jet.math.Arithmetic.log2(N) < M*N ) {
- // it is faster to sort other before searching in it
- DoubleArrayList sortedList = (DoubleArrayList) other.clone();
- sortedList.quickSort();
-
- for (int i=0; i<mySize ; i++) {
- if (sortedList.binarySearchFromTo(theElements[i], 0, limit) < 0) theElements[j++]=theElements[i];
- }
- }
- else {
- // it is faster to search in other without sorting
- for (int i=0; i<mySize ; i++) {
- if (other.indexOfFromTo(theElements[i], 0, limit) < 0) theElements[j++]=theElements[i];
- }
- }
-
- boolean modified = (j!=mySize);
- setSize(j);
- return modified;
+ Hence, if N*logN + M*logN < M*N, we use b) otherwise we use a).
+ */
+ if (other.size()==0) {return false;} //nothing to do
+ int limit = other.size()-1;
+ int j=0;
+ double[] theElements = elements;
+ int mySize = size();
+
+ double N=(double) other.size();
+ double M=(double) mySize;
+ if ( (N+M)* org.apache.mahout.jet.math.Arithmetic.log2(N) < M*N ) {
+ // it is faster to sort other before searching in it
+ DoubleArrayList sortedList = (DoubleArrayList) other.clone();
+ sortedList.quickSort();
+
+ for (int i=0; i<mySize ; i++) {
+ if (sortedList.binarySearchFromTo(theElements[i], 0, limit) < 0) theElements[j++]=theElements[i];
+ }
+ }
+ else {
+ // it is faster to search in other without sorting
+ for (int i=0; i<mySize ; i++) {
+ if (other.indexOfFromTo(theElements[i], 0, limit) < 0) theElements[j++]=theElements[i];
+ }
+ }
+
+ boolean modified = (j!=mySize);
+ setSize(j);
+ return modified;
}
/**
* Replaces a number of elements in the receiver with the same number of elements of another list.
@@ -348,18 +348,18 @@
* @param otherFrom position of first element within other list to be copied.
*/
public void replaceFromToWithFrom(int from, int to, AbstractDoubleList other, int otherFrom) {
- // overridden for performance only.
- if (! (other instanceof DoubleArrayList)) {
- // slower
- super.replaceFromToWithFrom(from,to,other,otherFrom);
- return;
- }
- int length=to-from+1;
- if (length>0) {
- checkRangeFromTo(from, to, size());
- checkRangeFromTo(otherFrom,otherFrom+length-1,other.size());
- System.arraycopy(((DoubleArrayList) other).elements, otherFrom, elements, from, length);
- }
+ // overridden for performance only.
+ if (! (other instanceof DoubleArrayList)) {
+ // slower
+ super.replaceFromToWithFrom(from,to,other,otherFrom);
+ return;
+ }
+ int length=to-from+1;
+ if (length>0) {
+ checkRangeFromTo(from, to, size());
+ checkRangeFromTo(otherFrom,otherFrom+length-1,other.size());
+ System.arraycopy(((DoubleArrayList) other).elements, otherFrom, elements, from, length);
+ }
}
/**
* Retains (keeps) only the elements in the receiver that are contained in the specified other list.
@@ -369,62 +369,62 @@
* @return <code>true</code> if the receiver changed as a result of the call.
*/
public boolean retainAll(AbstractDoubleList other) {
- // overridden for performance only.
- if (! (other instanceof DoubleArrayList)) return super.retainAll(other);
-
- /* There are two possibilities to do the thing
- a) use other.indexOf(...)
- b) sort other, then use other.binarySearch(...)
-
- Let's try to figure out which one is faster. Let M=size, N=other.size, then
- a) takes O(M*N) steps
- b) takes O(N*logN + M*logN) steps (sorting is O(N*logN) and binarySearch is O(logN))
-
- Hence, if N*logN + M*logN < M*N, we use b) otherwise we use a).
- */
- int limit = other.size()-1;
- int j=0;
- double[] theElements = elements;
- int mySize = size();
-
- double N=(double) other.size();
- double M=(double) mySize;
- if ( (N+M)* org.apache.mahout.jet.math.Arithmetic.log2(N) < M*N ) {
- // it is faster to sort other before searching in it
- DoubleArrayList sortedList = (DoubleArrayList) other.clone();
- sortedList.quickSort();
-
- for (int i=0; i<mySize ; i++) {
- if (sortedList.binarySearchFromTo(theElements[i], 0, limit) >= 0) theElements[j++]=theElements[i];
- }
- }
- else {
- // it is faster to search in other without sorting
- for (int i=0; i<mySize ; i++) {
- if (other.indexOfFromTo(theElements[i], 0, limit) >= 0) theElements[j++]=theElements[i];
- }
- }
-
- boolean modified = (j!=mySize);
- setSize(j);
- return modified;
+ // overridden for performance only.
+ if (! (other instanceof DoubleArrayList)) return super.retainAll(other);
+
+ /* There are two possibilities to do the thing
+ a) use other.indexOf(...)
+ b) sort other, then use other.binarySearch(...)
+
+ Let's try to figure out which one is faster. Let M=size, N=other.size, then
+ a) takes O(M*N) steps
+ b) takes O(N*logN + M*logN) steps (sorting is O(N*logN) and binarySearch is O(logN))
+
+ Hence, if N*logN + M*logN < M*N, we use b) otherwise we use a).
+ */
+ int limit = other.size()-1;
+ int j=0;
+ double[] theElements = elements;
+ int mySize = size();
+
+ double N=(double) other.size();
+ double M=(double) mySize;
+ if ( (N+M)* org.apache.mahout.jet.math.Arithmetic.log2(N) < M*N ) {
+ // it is faster to sort other before searching in it
+ DoubleArrayList sortedList = (DoubleArrayList) other.clone();
+ sortedList.quickSort();
+
+ for (int i=0; i<mySize ; i++) {
+ if (sortedList.binarySearchFromTo(theElements[i], 0, limit) >= 0) theElements[j++]=theElements[i];
+ }
+ }
+ else {
+ // it is faster to search in other without sorting
+ for (int i=0; i<mySize ; i++) {
+ if (other.indexOfFromTo(theElements[i], 0, limit) >= 0) theElements[j++]=theElements[i];
+ }
+ }
+
+ boolean modified = (j!=mySize);
+ setSize(j);
+ return modified;
}
/**
* Reverses the elements of the receiver.
* Last becomes first, second last becomes second first, and so on.
*/
public void reverse() {
- // overridden for performance only.
- double tmp;
- int limit=size/2;
- int j=size-1;
-
- double[] theElements = elements;
- for (int i=0; i<limit;) { //swap
- tmp=theElements[i];
- theElements[i++]=theElements[j];
- theElements[j--]=tmp;
- }
+ // overridden for performance only.
+ double tmp;
+ int limit=size/2;
+ int j=size-1;
+
+ double[] theElements = elements;
+ for (int i=0; i<limit;) { //swap
+ tmp=theElements[i];
+ theElements[i++]=theElements[j];
+ theElements[j--]=tmp;
+ }
}
/**
* Replaces the element at the specified position in the receiver with the specified element.
@@ -432,13 +432,13 @@
* @param index index of element to replace.
* @param element element to be stored at the specified position.
* @exception IndexOutOfBoundsException index is out of range (index
- * < 0 || index >= size()).
+ * < 0 || index >= size()).
*/
public void set(int index, double element) {
- // overridden for performance only.
- if (index >= size || index < 0)
- throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
- elements[index] = element;
+ // overridden for performance only.
+ if (index >= size || index < 0)
+ throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
+ elements[index] = element;
}
/**
* Replaces the element at the specified position in the receiver with the specified element; <b>WARNING:</b> Does not check preconditions.
@@ -450,7 +450,7 @@
* @param element element to be stored at the specified position.
*/
public void setQuick(int index, double element) {
- elements[index] = element;
+ elements[index] = element;
}
/**
* Randomly permutes the part of the receiver between <code>from</code> (inclusive) and <code>to</code> (inclusive).
@@ -459,22 +459,22 @@
* @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>).
*/
public void shuffleFromTo(int from, int to) {
- // overridden for performance only.
- if (size==0) {return;}
- checkRangeFromTo(from, to, size);
-
- org.apache.mahout.jet.random.Uniform gen = new org.apache.mahout.jet.random.Uniform(new org.apache.mahout.jet.random.engine.DRand(new java.util.Date()));
- double tmpElement;
- double[] theElements = elements;
- int random;
- for (int i=from; i<to; i++) {
- random = gen.nextIntFromTo(i, to);
-
- //swap(i, random)
- tmpElement = theElements[random];
- theElements[random]=theElements[i];
- theElements[i]=tmpElement;
- }
+ // overridden for performance only.
+ if (size==0) {return;}
+ checkRangeFromTo(from, to, size);
+
+ org.apache.mahout.jet.random.Uniform gen = new org.apache.mahout.jet.random.Uniform(new org.apache.mahout.jet.random.engine.DRand(new java.util.Date()));
+ double tmpElement;
+ double[] theElements = elements;
+ int random;
+ for (int i=from; i<to; i++) {
+ random = gen.nextIntFromTo(i, to);
+
+ //swap(i, random)
+ tmpElement = theElements[random];
+ theElements[random]=theElements[i];
+ theElements[i]=tmpElement;
+ }
}
/**
* Trims the capacity of the receiver to be the receiver's current
@@ -482,6 +482,6 @@
* storage of the receiver.
*/
public void trimToSize() {
- elements = org.apache.mahout.matrix.Arrays.trimToCapacity(elements,size());
+ elements = org.apache.mahout.matrix.Arrays.trimToCapacity(elements,size());
}
}