Author: luc
Date: Fri Jan 2 09:53:44 2009
New Revision: 730801
URL: http://svn.apache.org/viewvc?rev=730801&view=rev
Log:
added a configurable value for missing entries in OpenIntToDoubleHashMap
Modified:
commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealMatrix.java
commons/proper/math/trunk/src/java/org/apache/commons/math/util/OpenIntToDoubleHashMap.java
commons/proper/math/trunk/src/test/org/apache/commons/math/util/OpenIntToDoubleHashMapTest.java
Modified:
commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealMatrix.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealMatrix.java?rev=730801&r1=730800&r2=730801&view=diff
==============================================================================
---
commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealMatrix.java
(original)
+++
commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealMatrix.java
Fri Jan 2 09:53:44 2009
@@ -48,7 +48,7 @@
super(rowDimension, columnDimension);
this.rowDimension = rowDimension;
this.columnDimension = columnDimension;
- this.entries = new OpenIntToDoubleHashMap();
+ this.entries = new OpenIntToDoubleHashMap(0.0);
}
/**
Modified:
commons/proper/math/trunk/src/java/org/apache/commons/math/util/OpenIntToDoubleHashMap.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/util/OpenIntToDoubleHashMap.java?rev=730801&r1=730800&r2=730801&view=diff
==============================================================================
---
commons/proper/math/trunk/src/java/org/apache/commons/math/util/OpenIntToDoubleHashMap.java
(original)
+++
commons/proper/math/trunk/src/java/org/apache/commons/math/util/OpenIntToDoubleHashMap.java
Fri Jan 2 09:53:44 2009
@@ -73,6 +73,9 @@
/** States table. */
private byte[] states;
+ /** Return value for missing entries. */
+ private final double missingEntries;
+
/** Current size of the map. */
private int size;
@@ -83,21 +86,40 @@
private transient int count;
/**
- * Build an empty map with default size.
+ * Build an empty map with default size and using NaN for missing entries.
*/
public OpenIntToDoubleHashMap() {
- this(DEFAULT_EXPECTED_SIZE);
+ this(DEFAULT_EXPECTED_SIZE, Double.NaN);
}
/**
- * Build an empty map with specified size.
+ * Build an empty map with default size
+ * @param missingEntries value to return when a missing entry is fetched
+ */
+ public OpenIntToDoubleHashMap(final double missingEntries) {
+ this(DEFAULT_EXPECTED_SIZE, missingEntries);
+ }
+
+ /**
+ * Build an empty map with specified size and using NaN for missing
entries.
* @param expectedSize expected number of elements in the map
*/
public OpenIntToDoubleHashMap(final int expectedSize) {
+ this(expectedSize, Double.NaN);
+ }
+
+ /**
+ * Build an empty map with specified size.
+ * @param expectedSize expected number of elements in the map
+ * @param missingEntries value to return when a missing entry is fetched
+ */
+ public OpenIntToDoubleHashMap(final int expectedSize,
+ final double missingEntries) {
final int capacity = computeCapacity(expectedSize);
keys = new int[capacity];
values = new double[capacity];
states = new byte[capacity];
+ this.missingEntries = missingEntries;
mask = capacity - 1;
}
@@ -113,6 +135,7 @@
System.arraycopy(source.values, 0, values, 0, length);
states = new byte[length];
System.arraycopy(source.states, 0, states, 0, length);
+ missingEntries = source.missingEntries;
size = source.size;
mask = source.mask;
count = source.count;
@@ -158,7 +181,7 @@
}
if (states[index] == FREE) {
- return 0.0;
+ return missingEntries;
}
for (int perturb = perturb(hash), j = index; states[index] != FREE;
perturb >>= PERTURB_SHIFT) {
@@ -169,7 +192,7 @@
}
}
- return 0.0;
+ return missingEntries;
}
@@ -329,7 +352,7 @@
}
if (states[index] == FREE) {
- return 0.0;
+ return missingEntries;
}
for (int perturb = perturb(hash), j = index; states[index] != FREE;
perturb >>= PERTURB_SHIFT) {
@@ -340,7 +363,7 @@
}
}
- return 0.0;
+ return missingEntries;
}
@@ -364,7 +387,7 @@
keys[index] = 0;
states[index] = REMOVED;
final double previous = values[index];
- values[index] = 0;
+ values[index] = missingEntries;
--size;
++count;
return previous;
@@ -378,7 +401,7 @@
*/
public double put(final int key, final double value) {
int index = findInsertionIndex(key);
- double previous = 0.0;
+ double previous = missingEntries;
boolean newMapping = true;
if (index < 0) {
index = changeIndexSign(index);
Modified:
commons/proper/math/trunk/src/test/org/apache/commons/math/util/OpenIntToDoubleHashMapTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/util/OpenIntToDoubleHashMapTest.java?rev=730801&r1=730800&r2=730801&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/org/apache/commons/math/util/OpenIntToDoubleHashMapTest.java
(original)
+++
commons/proper/math/trunk/src/test/org/apache/commons/math/util/OpenIntToDoubleHashMapTest.java
Fri Jan 2 09:53:44 2009
@@ -124,14 +124,14 @@
OpenIntToDoubleHashMap map = createFromJavaMap();
for (Map.Entry<Integer, Double> mapEntry : generated.entrySet())
- assertEquals(0.0, map.get(mapEntry.getKey()));
+ assertTrue(Double.isNaN(map.get(mapEntry.getKey())));
}
public void testGetFromEmpty() {
OpenIntToDoubleHashMap map = new OpenIntToDoubleHashMap();
- assertEquals(0.0, map.get(5));
- assertEquals(0.0, map.get(0));
- assertEquals(0.0, map.get(50));
+ assertTrue(Double.isNaN(map.get(5)));
+ assertTrue(Double.isNaN(map.get(0)));
+ assertTrue(Double.isNaN(map.get(50)));
}
public void testRemove() {
@@ -141,7 +141,7 @@
for (Map.Entry<Integer, Double> mapEntry : javaMap.entrySet()) {
map.remove(mapEntry.getKey());
assertEquals(--mapSize, map.size());
- assertEquals(0.0, map.get(mapEntry.getKey()));
+ assertTrue(Double.isNaN(map.get(mapEntry.getKey())));
}
/* Ensure that put and get still work correctly after removals */
@@ -158,7 +158,7 @@
keysInMap.remove(mapEntry.getKey());
map.remove(mapEntry.getKey());
assertEquals(--mapSize, map.size());
- assertEquals(0.0, map.get(mapEntry.getKey()));
+ assertTrue(Double.isNaN(map.get(mapEntry.getKey())));
if (count++ > 5)
break;
}
@@ -169,7 +169,7 @@
public void testRemoveFromEmpty() {
OpenIntToDoubleHashMap map = new OpenIntToDoubleHashMap();
- assertEquals(0.0, map.remove(50));
+ assertTrue(Double.isNaN(map.remove(50)));
}
public void testRemoveAbsent() {
@@ -181,7 +181,7 @@
for (Map.Entry<Integer, Double> mapEntry : generated.entrySet()) {
map.remove(mapEntry.getKey());
assertEquals(mapSize, map.size());
- assertEquals(0.0, map.get(mapEntry.getKey()));
+ assertTrue(Double.isNaN(map.get(mapEntry.getKey())));
}
}