-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24127e2
commit b8b2eb9
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* | ||
*/ | ||
package au.com.phiware.math.bankers; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import au.com.phiware.math.ring.BigIntegerArithmetic; | ||
import au.com.phiware.math.ring.BitArithmetic; | ||
import au.com.phiware.math.ring.IntegerArithmetic; | ||
import au.com.phiware.math.ring.LongArithmetic; | ||
|
||
/** | ||
* @author Corin Lawson <me@corinlawson.com.au> | ||
* | ||
*/ | ||
public class Bankers { | ||
private BitArithmetic<? extends Number> arithmetic; | ||
private int length; | ||
|
||
private Bankers(int length) { | ||
if (length <= 32) | ||
arithmetic = IntegerArithmetic.getInstance(); | ||
else if (length <= 64) | ||
arithmetic = LongArithmetic.getInstance(); | ||
else | ||
arithmetic = BigIntegerArithmetic.getInstance(); | ||
this.length = length; | ||
} | ||
|
||
public int length() { | ||
return length; | ||
} | ||
|
||
private static Map<Integer, Bankers> instances = new HashMap<Integer, Bankers>(); | ||
public static Bankers getBanker(int length) { | ||
if (!instances.containsKey(length)) | ||
instances.put(length, new Bankers(length)); | ||
return instances.get(length); | ||
} | ||
|
||
public <V extends Number> V next(V b) { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* | ||
*/ | ||
package au.com.phiware.math.bankers; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.BitSet; | ||
|
||
import org.junit.Test; | ||
|
||
/** | ||
* @author Corin Lawson <me@corinlawson.com.au> | ||
* | ||
*/ | ||
public class BankersTest { | ||
|
||
public void testMonotonicity(Bankers bankers) { | ||
int b = 0; | ||
int c = 0; | ||
for (int i = 0; i < 1 << bankers.length(); i++) { | ||
b = bankers.next(b); | ||
assertTrue(b+" at "+i+" should have no less than "+c+" ones", Integer.bitCount(b) >= c); | ||
c = Integer.bitCount(b); | ||
} | ||
} | ||
|
||
/** | ||
* The number of ones in the Banker's sequence should never decrease. | ||
*/ | ||
@Test | ||
public void testMonotonicityUpTo10() { | ||
for (int n = 2; n <= 10; n++) | ||
testMonotonicity(Bankers.getBanker(n)); | ||
} | ||
|
||
public void testIsomorphism(Bankers bankers) { | ||
int b = 0; | ||
BitSet seen = new BitSet(1 << bankers.length()); | ||
for (int i = 0; i < 1 << bankers.length(); i++) { | ||
b = bankers.next(b); | ||
assertFalse(b+" at "+i+" should not have been seen", seen.get(b)); | ||
seen.set(b); | ||
} | ||
} | ||
|
||
/** | ||
* The natural numbers should have a one-to-one mapping to the Banker's numbers. | ||
*/ | ||
@Test | ||
public void testIsomorphismUpTo10() { | ||
for (int n = 2; n <= 10; n++) | ||
testIsomorphism(Bankers.getBanker(n)); | ||
} | ||
|
||
} |