Skip to content

Commit b437328

Browse files
committed
Use IntVec fewer places (prefer native arrays)
1 parent 1682c1b commit b437328

File tree

6 files changed

+37
-38
lines changed

6 files changed

+37
-38
lines changed

Count/src/com/mzlabs/count/divideandconquer/SplitNode.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import com.mzlabs.count.NonNegativeIntegralCounter;
66
import com.mzlabs.count.op.IntFunc;
7-
import com.mzlabs.count.op.IntVecFn;
7+
import com.mzlabs.count.op.CachableCalculation;
88
import com.mzlabs.count.op.Reducer;
99
import com.mzlabs.count.op.Sequencer;
1010
import com.mzlabs.count.op.SolnCache;
@@ -14,7 +14,7 @@
1414
import com.mzlabs.count.op.iter.SeqLE;
1515
import com.mzlabs.count.util.IntVec;
1616

17-
final class SplitNode implements NonNegativeIntegralCounter,IntVecFn {
17+
final class SplitNode implements NonNegativeIntegralCounter,CachableCalculation {
1818
private final NonNegativeIntegralCounter leftSubSystem;
1919
private final NonNegativeIntegralCounter rightSubSystem;
2020
private final int[][] A;
@@ -64,11 +64,11 @@ public boolean obviouslyEmpty(final int[] bIn) {
6464
}
6565

6666
@Override
67-
public BigInteger eval(final IntVec b) {
67+
public BigInteger eval(final int[] b) {
6868
final int[] bound = new int[nEntangled];
6969
for(int ii=0;ii<nEntangled;++ii) {
7070
final int i = entangledRows[ii];
71-
bound[ii] = b.get(i);
71+
bound[ii] = b[i];
7272
if(zeroOne) {
7373
int rowSum = 0;
7474
for(int j=0;j<n;++j) {
@@ -88,10 +88,10 @@ public BigInteger f(final int[] x) {
8888
final int[] b2 = new int[m];
8989
for(int i=0;i<m;++i) {
9090
if(usesRow[0][i]) {
91-
b1[i] = b.get(i);
91+
b1[i] = b[i];
9292
}
9393
if(usesRow[1][i]) {
94-
b2[i] = b.get(i);
94+
b2[i] = b[i];
9595
}
9696
}
9797
final int[] counter = new int[nEntangled];
@@ -100,7 +100,7 @@ public BigInteger f(final int[] x) {
100100
for(int ii=0;ii<nEntangled;++ii) {
101101
final int i = entangledRows[ii];
102102
b1[i] = counter[ii];
103-
b2[i] = b.get(i) - counter[ii];
103+
b2[i] = b[i] - counter[ii];
104104
}
105105
// b1 + b2 == b
106106
// add sub1*sub2 terms, but try to avoid calculating sub(i) if sub(1-i) is obviously zero
@@ -126,7 +126,7 @@ public BigInteger f(final int[] x) {
126126

127127
@Override
128128
public BigInteger countNonNegativeSolutions(final int[] b) {
129-
return cache.evalCached(this,new IntVec(b));
129+
return cache.evalCached(this,b);
130130
}
131131

132132

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.mzlabs.count.op;
2+
3+
import java.math.BigInteger;
4+
5+
public interface CachableCalculation {
6+
BigInteger eval(int[] x);
7+
}

Count/src/com/mzlabs/count/op/IntVecFn.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

Count/src/com/mzlabs/count/op/SolnCache.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.mzlabs.count.op;
22

33
import java.math.BigInteger;
4+
import java.util.Arrays;
45

56
import com.mzlabs.count.op.impl.RecNode;
6-
import com.mzlabs.count.util.IntVec;
77

88
public final class SolnCache {
99
private final int nsub = 100;
@@ -19,16 +19,16 @@ public SolnCache() {
1919
/**
2020
* Look for a cached value of f(x), if none such create a record, block on the record and compute f(x) (so only one attempt to compute f(x))
2121
* @param f
22-
* @param x
22+
* @param x not null, x.length>0
2323
* @return
2424
*/
25-
public BigInteger evalCached(final IntVecFn f, final IntVec x) {
26-
int subi = x.hashCode%nsub;
25+
public BigInteger evalCached(final CachableCalculation f, final int[] x) {
26+
int subi = Arrays.hashCode(x)%nsub;
2727
if(subi<0) {
2828
subi += nsub;
2929
}
3030
final RecNode store = stores[subi];
31-
final RecNode newHolder = new RecNode(x.get(x.dim()-1));
31+
final RecNode newHolder = new RecNode(x[x.length-1]);
3232
final RecNode cached;
3333
synchronized (newHolder) {
3434
synchronized(store) {

Count/src/com/mzlabs/count/op/impl/RecNode.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.math.BigInteger;
44

5-
import com.mzlabs.count.util.IntVec;
65

76
/**
87
* prefix tree on integer vectors (trying to cut down number of flyweight objects)
@@ -86,17 +85,17 @@ private void put(final RecNode x) {
8685
* @param newNode not null with key==x.get(x.dim()-1) (possible mutex locked)
8786
* @return terminal node to hold value (newNode if there was allocation)
8887
*/
89-
private static RecNode lookupAlloc(RecNode nd, final IntVec x, final RecNode newNode) {
90-
final int n = x.dim();
88+
private static RecNode lookupAlloc(RecNode nd, final int[] x, final RecNode newNode) {
89+
final int n = x.length;
9190
if(n<=0) {
9291
throw new IllegalArgumentException("empty x");
9392
}
94-
if(newNode.key!=x.get(n-1)) {
93+
if(newNode.key!=x[n-1]) {
9594
throw new IllegalArgumentException("bad new key");
9695
}
9796
int firstPosn = 0;
9897
while(true) {
99-
final int key = x.get(firstPosn);
98+
final int key = x[firstPosn];
10099
RecNode sub = nd.get(key);
101100
if(firstPosn>=n-1) {
102101
// at terminal case
@@ -123,7 +122,7 @@ private static RecNode lookupAlloc(RecNode nd, final IntVec x, final RecNode new
123122
* @param newNode
124123
* @return
125124
*/
126-
public RecNode lookupAlloc(final IntVec x, final RecNode newNode) {
125+
public RecNode lookupAlloc(final int[] x, final RecNode newNode) {
127126
return lookupAlloc(this,x,newNode);
128127
}
129128

Count/src/com/mzlabs/count/zeroone/ZeroOneCounter.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import com.mzlabs.count.CountingProblem;
99
import com.mzlabs.count.NonNegativeIntegralCounter;
1010
import com.mzlabs.count.divideandconquer.DivideAndConquerCounter;
11-
import com.mzlabs.count.op.IntVecFn;
11+
import com.mzlabs.count.op.CachableCalculation;
1212
import com.mzlabs.count.op.SolnCache;
1313
import com.mzlabs.count.op.iter.SeqLE;
1414
import com.mzlabs.count.op.iter.SeqLT;
@@ -47,7 +47,7 @@
4747
* @author johnmount
4848
*
4949
*/
50-
public final class ZeroOneCounter implements NonNegativeIntegralCounter,IntVecFn {
50+
public final class ZeroOneCounter implements NonNegativeIntegralCounter,CachableCalculation {
5151
private final CountingProblem prob;
5252
private final int m;
5353
private final ZeroOneStore zeroOneCounts;
@@ -175,17 +175,17 @@ public String toString() {
175175
* @return number of non-negative integer solutions x to A x == b
176176
*/
177177
@Override
178-
public BigInteger eval(final IntVec b) {
178+
public BigInteger eval(final int[] b) {
179179
BigInteger result = BigInteger.ZERO;
180-
final IBPair[] group = zeroOneCounts.lookup(b);
180+
final IBPair[] group = zeroOneCounts.lookup(new IntVec(b));
181181
if(null!=group) {
182182
final int[] bprime = new int[m];
183183
for(final IBPair gi: group) {
184184
final IntVec r = gi.key;
185185
boolean goodR = true;
186186
int sum = 0;
187187
for(int i=0;i<m;++i) {
188-
final int diff = b.get(i) - r.get(i);
188+
final int diff = b[i] - r.get(i);
189189
if(diff<0) {
190190
goodR = false;
191191
break;
@@ -195,14 +195,16 @@ public BigInteger eval(final IntVec b) {
195195
sum += bpi;
196196
}
197197
if(goodR) {
198-
final BigInteger nzone = gi.value;
198+
final BigInteger nzone = gi.value; // not zero by cache conditions
199199
if(sum<=0) { // A x = 0, has one non-negative solution (since a second positive solution would give us a ray of solutions, and we know we are bounded).
200200
result = result.add(nzone);
201201
} else {
202202
final Permutation tobprimeNorm = prob.toNormalForm(bprime);
203203
final int[] bprimeNorm = tobprimeNorm.apply(bprime);
204204
final BigInteger subsoln = countNonNegativeSolutions(bprimeNorm);
205-
result = result.add(nzone.multiply(subsoln));
205+
if(subsoln.compareTo(BigInteger.ZERO)!=0) {
206+
result = result.add(nzone.multiply(subsoln));
207+
}
206208
}
207209
}
208210
}
@@ -228,14 +230,14 @@ public BigInteger countNonNegativeSolutions(final int[] bIn) {
228230
}
229231
sum += bi;
230232
}
231-
if(obviouslyEmpty(bIn)) {
233+
if(!prob.admissableB(bIn)) {
232234
return BigInteger.ZERO;
233235
}
234-
if(sum<=0) {
236+
if(sum<=0) { // if A x = 0 has either 1, zero or +infinity solutions- we are bounded it is zero or one (and therefore 1 in this case).
235237
return BigInteger.ONE;
236238
}
237239
final Permutation perm = prob.toNormalForm(bIn);
238-
final IntVec bNormal = new IntVec(perm.apply(bIn));
240+
final int[] bNormal = perm.apply(bIn);
239241
final BigInteger result = cache.evalCached(this,bNormal);
240242
return result;
241243
}

0 commit comments

Comments
 (0)