int numRecord;
AdtreeNode rootNodePtr;
- public Adtree() {
-
- }
-
- /* =============================================================================
- * freeNode
- * =============================================================================
- */
- public void
- freeNode (AdtreeNode nodePtr)
- {
- nodePtr.varyVectorPtr.vector_free();
- nodePtr = null;
- }
-
-
-
- /* =============================================================================
- * freeVary
- * =============================================================================
- */
- public void
- freeVary (AdtreeVary varyPtr)
- {
- varyPtr = null;
- }
-
-
/* =============================================================================
* adtree_alloc
* =============================================================================
*/
- public static Adtree adtree_alloc ()
- {
- Adtree adtreePtr = new Adtree();
- if (adtreePtr != null) {
- adtreePtr.numVar = -1;
- adtreePtr.numRecord = -1;
- adtreePtr.rootNodePtr = null;
- }
-
- return adtreePtr;
- }
-
-
- /* =============================================================================
- * freeNodes
- * =============================================================================
- */
- public void
- freeNodes (AdtreeNode nodePtr)
- {
- if (nodePtr != null) {
- Vector_t varyVectorPtr = nodePtr.varyVectorPtr;
- int numVary = varyVectorPtr.vector_getSize();
- for (int v = 0; v < numVary; v++) {
- AdtreeVary varyPtr = (AdtreeVary)(varyVectorPtr.vector_at(v));
- freeNodes(varyPtr.zeroNodePtr);
- freeNodes(varyPtr.oneNodePtr);
- freeVary(varyPtr);
- }
- freeNode(nodePtr);
- }
- }
-
+ public Adtree() {
+ numVar = -1;
+ numRecord = -1;
+ rootNodePtr = null;
+ }
- /* =============================================================================
- * adtree_free
- * =============================================================================
- */
- public void
- adtree_free ()
- {
- freeNodes(rootNodePtr);
- }
/* =============================================================================
* makeVary
* =============================================================================
*/
- public AdtreeVary
- makeVary (int parentIndex,
- int index,
- int start,
- int numRecord,
- Data dataPtr)
- {
- AdtreeVary varyPtr = AdtreeVary.allocVary(index);
-
- if ((parentIndex + 1 != index) && (numRecord > 1)) {
- dataPtr.data_sort(start, numRecord, index);
- }
-
- int num0 = dataPtr.data_findSplit(start, numRecord, index);
- int num1 = numRecord - num0;
-
- int mostCommonValue = ((num0 >= num1) ? 0 : 1);
- varyPtr.mostCommonValue = mostCommonValue;
-
- if (num0 == 0 || mostCommonValue == 0) {
- varyPtr.zeroNodePtr = null;
- } else {
- varyPtr.zeroNodePtr =
- makeNode(index, index, start, num0, dataPtr);
- varyPtr.zeroNodePtr.value = 0;
- }
-
- if (num1 == 0 || mostCommonValue == 1) {
- varyPtr.oneNodePtr = null;
- } else {
- varyPtr.oneNodePtr =
- makeNode(index, index, (start + num0), num1, dataPtr);
- varyPtr.oneNodePtr.value = 1;
- }
+ public AdtreeVary makeVary (int parentIndex, int index, int start, int numRecord, Data dataPtr) {
+ AdtreeVary varyPtr = new AdtreeVary(index);
- return varyPtr;
+ if ((parentIndex + 1 != index) && (numRecord > 1)) {
+ dataPtr.data_sort(start, numRecord, index);
}
+
+ int num0 = dataPtr.data_findSplit(start, numRecord, index);
+ int num1 = numRecord - num0;
+
+ int mostCommonValue = ((num0 >= num1) ? 0 : 1);
+ varyPtr.mostCommonValue = mostCommonValue;
+
+ if (num0 == 0 || mostCommonValue == 0) {
+ } else {
+ varyPtr.zeroNodePtr = makeNode(index, index, start, num0, dataPtr);
+ varyPtr.zeroNodePtr.value = 0;
+ }
+
+ if (num1 == 0 || mostCommonValue == 1) {
+ } else {
+ varyPtr.oneNodePtr = makeNode(index, index, (start + num0), num1, dataPtr);
+ varyPtr.oneNodePtr.value = 1;
+ }
+
+ return varyPtr;
+ }
/* =============================================================================
* makeNode
* =============================================================================
*/
- public AdtreeNode
- makeNode (int parentIndex,
- int index,
- int start,
- int numRecord,
- Data dataPtr)
- {
- AdtreeNode nodePtr = AdtreeNode.allocNode(index);
-
- nodePtr.count = numRecord;
-
- Vector_t varyVectorPtr = nodePtr.varyVectorPtr;
-
- int numVar = dataPtr.numVar;
- for (int v = (index + 1); v < numVar; v++) {
- AdtreeVary varyPtr =
- makeVary(parentIndex, v, start, numRecord, dataPtr);
- boolean status;
- if((status = varyVectorPtr.vector_pushBack(varyPtr)) != true) {
- System.out.println("Assert failed: varyVectorPtr.vector_pushBack != true");
- System.exit(0);
- }
- }
-
- return nodePtr;
+ public AdtreeNode makeNode (int parentIndex, int index, int start, int numRecord, Data dataPtr) {
+ int numVar = dataPtr.numVar;
+ AdtreeNode nodePtr = new AdtreeNode(index, numVar-index-1);
+ nodePtr.count = numRecord;
+
+ AdtreeVary varyVectorPtr[] = nodePtr.varyVectorPtr;
+ int i=0;
+
+ for (int v = (index + 1); v < numVar; v++) {
+ AdtreeVary varyPtr = makeVary(parentIndex, v, start, numRecord, dataPtr);
+ varyVectorPtr[i++]=varyPtr;
}
+
+ return nodePtr;
+ }
/* =============================================================================
* -- Records in dataPtr will get rearranged
* =============================================================================
*/
- public void
- adtree_make (Data dataPtr)
- {
- numVar = dataPtr.numVar;
- numRecord = dataPtr.numRecord;
- dataPtr.data_sort(0, numRecord, 0);
- rootNodePtr = makeNode(-1, -1, 0, numRecord, dataPtr);
- }
+ public void adtree_make (Data dataPtr) {
+ numVar = dataPtr.numVar;
+ numRecord = dataPtr.numRecord;
+ dataPtr.data_sort(0, numRecord, 0);
+ rootNodePtr = makeNode(-1, -1, 0, numRecord, dataPtr);
+ }
/* =============================================================================
System.exit(0);
}
- Vector_t varyVectorPtr = nodePtr.varyVectorPtr;
- AdtreeVary varyPtr = (AdtreeVary)(varyVectorPtr.vector_at((queryIndex - nodeIndex - 1)));
+ AdtreeVary varyPtr = nodePtr.varyVectorPtr[queryIndex - nodeIndex - 1];
int queryValue = queryPtr.value;
* query with the current toggled (invertCount).
*/
int numQuery = queryVectorPtr.vector_getSize();
- Vector_t superQueryVectorPtr = Vector_t.vector_alloc(numQuery - 1);
+ Vector_t superQueryVectorPtr = new Vector_t(numQuery - 1);
for (int qq = 0; qq < numQuery; qq++) {
if (qq != q) {
- boolean status = superQueryVectorPtr.vector_pushBack(
- queryVectorPtr.vector_at(qq));
+ boolean status = superQueryVectorPtr.vector_pushBack(queryVectorPtr.vector_at(qq));
}
}
int superCount = adtree_getCount(superQueryVectorPtr);
-
- superQueryVectorPtr.vector_free();
+ superQueryVectorPtr.clear();
int invertCount;
if (queryValue == 0) {
int index;
int value;
int count;
- Vector_t varyVectorPtr;
+ AdtreeVary varyVectorPtr[];
public AdtreeNode() {
* allocNode
* =============================================================================
*/
- public static AdtreeNode
- allocNode (int index)
- {
- AdtreeNode nodePtr = new AdtreeNode();
-
- if (nodePtr != null) {
- nodePtr.varyVectorPtr = Vector_t.vector_alloc(1);
- if (nodePtr.varyVectorPtr == null) {
- nodePtr = null;
- return null;
- }
- nodePtr.index = index;
- nodePtr.value = -1;
- nodePtr.count = -1;
- }
-
- return nodePtr;
- }
+ public AdtreeNode(int index, int vecsize) {
+ this.varyVectorPtr = new AdtreeVary[vecsize];
+ this.index = index;
+ this.value = -1;
+ this.count = -1;
+ }
}
AdtreeNode oneNodePtr;
public AdtreeVary() {
-
}
/* =============================================================================
* allocVary
* =============================================================================
*/
- public AdtreeVary
- allocVary (int index)
- {
- AdtreeVary varyPtr= new AdtreeVary();
-
- if (varyPtr != null) {
- varyPtr.index = index;
- varyPtr.mostCommonValue = -1;
- varyPtr.zeroNodePtr = null;
- varyPtr.oneNodePtr = null;
- }
-
- return varyPtr;
- }
+ public AdtreeVary(int index) {
+ this.index = index;
+ mostCommonValue = -1;
+ }
+ public void free_vary() {
+ zeroNodePtr=null;
+ oneNodePtr=null;
+ }
}
* score
* =============================================================================
*/
- public float
- score (Net netPtr, Adtree adtreePtr)
- {
- /*
- * Create dummy data structures to conform to learner_score assumptions
- */
-
- Data dataPtr = Data.data_alloc(1, 1, null);
-
- Learner learnerPtr = Learner.learner_alloc(dataPtr, adtreePtr, 1, global_insertPenalty, global_maxNumEdgeLearned, global_operationQualityFactor);
-
- Net tmpNetPtr = learnerPtr.netPtr;
- learnerPtr.netPtr = netPtr;
+ public float score (Net netPtr, Adtree adtreePtr) {
+ /*
+ * Create dummy data structures to conform to learner_score assumptions
+ */
+
+ Data dataPtr = new Data(1, 1, null);
+
+ Learner learnerPtr = new Learner(dataPtr, adtreePtr, 1, global_insertPenalty, global_maxNumEdgeLearned, global_operationQualityFactor);
+
+ Net tmpNetPtr = learnerPtr.netPtr;
+ learnerPtr.netPtr = netPtr;
+
+ float score = learnerPtr.learner_score();
+ learnerPtr.netPtr = tmpNetPtr;
+ learnerPtr.learner_free();
+ dataPtr.data_free();
- float score = learnerPtr.learner_score();
- learnerPtr.netPtr = tmpNetPtr;
- learnerPtr.learner_free();
- dataPtr.data_free();
- return score;
- }
+ return score;
+ }
/**
randomPtr.random_alloc();
randomPtr.random_seed(randomSeed);
- Data dataPtr = Data.data_alloc(numVar, numRecord, randomPtr);
+ Data dataPtr = new Data(numVar, numRecord, randomPtr);
Net netPtr = dataPtr.data_generate(-1, maxNumParent, percentParent);
System.out.println("done.");
* Generate adtree
*/
- Adtree adtreePtr = Adtree.adtree_alloc();
+ Adtree adtreePtr = new Adtree();
System.out.print("Generating adtree... ");
adtreePtr.adtree_make(dataPtr);
+ dataPtr.data_free();
System.out.println("done.");
* Learn structure of Bayesian network
*/
- Learner learnerPtr = Learner.learner_alloc(dataPtr, adtreePtr, numThread, b.global_insertPenalty, b.global_maxNumEdgeLearned, b.global_operationQualityFactor);
-
- dataPtr.data_free(); /* save memory */
+ Learner learnerPtr = new Learner(dataPtr, adtreePtr, numThread, b.global_insertPenalty, b.global_maxNumEdgeLearned, b.global_operationQualityFactor);
System.out.print("Learning structure...");
/*
* Clean up
*/
-
-#ifndef SIMULATOR
- adtreePtr.adtree_free();
-# if 0
- learnerPtr.learner_free();
-# endif
-#endif
-
}
}
/* =============================================================================
int numRecord;
byte[] records; /* coordination of all records */
Random randomPtr;
-
- public Data() {
- }
+ Sort sort;
/* =============================================================================
* data_alloc
* =============================================================================
*/
- public static Data data_alloc (int numVar, int numRecord, Random randomPtr)
+ public Data(int numVar, int numRecord, Random randomPtr)
{
- Data dataPtr = new Data();
-
- if (dataPtr != null) {
- int numDatum = numVar * numRecord;
- dataPtr.records = new byte[numDatum];
- for(int i = 0; i<numDatum; i++)
- dataPtr.records[i] = (byte)DATA_INIT;
-
- dataPtr.numVar = numVar;
- dataPtr.numRecord = numRecord;
- dataPtr.randomPtr = randomPtr;
- }
-
- return dataPtr;
+ int numDatum = numVar * numRecord;
+ records = new byte[numDatum];
+ for(int i = 0; i<numDatum; i++)
+ this.records[i] = (byte)DATA_INIT;
+
+ this.numVar = numVar;
+ this.numRecord = numRecord;
+ this.randomPtr = randomPtr;
+ this.sort=new Sort();
}
-
- /* =============================================================================
- * data_free
- * =============================================================================
- */
- void
- data_free ()
- {
- records = null;
- }
+ public void data_free() {
+ records=null;
+ randomPtr=null;
+ }
/* =============================================================================
* data_generate
* Generate random Bayesian network
*/
- Net netPtr = Net.net_alloc(numVar);
+ Net netPtr = new Net(numVar);
netPtr.net_generateRandomEdges(maxNumParent, percentParent, randomPtr);
/*
}
}
- /*
- * Clean up
- */
-
- doneBitmapPtr.bitmap_free();
- orderedBitmapPtr.bitmap_free();
- dependencyVectorPtr.vector_free();
- workQueuePtr.queue_free();
- order = null;
-
- for (v = 0; v < numVar; v++) {
- thresholdsTable[v] = null;
- }
-
- thresholdsTable = null;
-
return netPtr;
}
int numDstDatum = dstPtr.numVar * dstPtr.numRecord;
int numSrcDatum = srcPtr.numVar * srcPtr.numRecord;
if (numDstDatum != numSrcDatum) {
- dstPtr.records = null;
dstPtr.records = new byte[numSrcDatum];
- if (dstPtr.records == null) {
- return false;
- }
}
dstPtr.numVar = srcPtr.numVar;
System.exit(0);
}
- Sort.sort(records,
+ sort.sort(records,
start * numVar,
num,
numVar,
* learner_alloc
* =============================================================================
*/
- public static Learner
- learner_alloc (Data dataPtr,
- Adtree adtreePtr,
- int numThread,
- int global_insertPenalty,
- int global_maxNumEdgeLearned,
- float global_operationQualityFactor)
- {
- Learner learnerPtr = new Learner();
-
- if (learnerPtr != null) {
- learnerPtr.adtreePtr = adtreePtr;
- learnerPtr.netPtr = Net.net_alloc(dataPtr.numVar);
- learnerPtr.localBaseLogLikelihoods = new float[dataPtr.numVar];
- learnerPtr.baseLogLikelihood = 0.0f;
- learnerPtr.tasks = new LearnerTask[dataPtr.numVar];
- learnerPtr.taskListPtr = List.list_alloc();
- learnerPtr.numTotalParent = 0;
+ public Learner(Data dataPtr,
+ Adtree adtreePtr,
+ int numThread,
+ int global_insertPenalty,
+ int global_maxNumEdgeLearned,
+ float global_operationQualityFactor) {
+ this.adtreePtr = adtreePtr;
+ this.netPtr = new Net(dataPtr.numVar);
+ this.localBaseLogLikelihoods = new float[dataPtr.numVar];
+ this.baseLogLikelihood = 0.0f;
+ this.tasks = new LearnerTask[dataPtr.numVar];
+ this.taskListPtr = List.list_alloc();
+ this.numTotalParent = 0;
#ifndef TEST_LEARNER
- learnerPtr.global_insertPenalty = global_insertPenalty;
- learnerPtr.global_maxNumEdgeLearned = global_maxNumEdgeLearned;
- learnerPtr.global_operationQualityFactor = global_operationQualityFactor;
+ this.global_insertPenalty = global_insertPenalty;
+ this.global_maxNumEdgeLearned = global_maxNumEdgeLearned;
+ this.global_operationQualityFactor = global_operationQualityFactor;
#endif
- }
-
- return learnerPtr;
- }
-
+ }
- /* =============================================================================
- * learner_free
- * =============================================================================
- */
- public void
- learner_free ()
- {
- taskListPtr.list_free();
- tasks = null;
- localBaseLogLikelihoods = null;
- netPtr.net_free();
- }
+ public void learner_free() {
+ adtreePtr=null;
+ netPtr=null;
+ localBaseLogLikelihoods=null;
+ tasks=null;
+ taskListPtr=null;
+ }
/* =============================================================================
queries[0] = new Query();
queries[1] = new Query();
- Vector_t queryVectorPtr = Vector_t.vector_alloc(2);
- if(queryVectorPtr == null) {
- System.out.println("Assert failed: cannot allocate vector");
- System.exit(0);
- }
+ Vector_t queryVectorPtr = new Vector_t(2);
if((status = queryVectorPtr.vector_pushBack(queries[0])) == false) {
System.out.println("Assert failed: status = "+ status + "vector_pushBack failed in createTaskList()");
}
Query parentQuery = new Query();
- Vector_t parentQueryVectorPtr = Vector_t.vector_alloc(1);
-
- if(parentQueryVectorPtr == null) {
- System.out.println("Assert failed: for vector_alloc at createTaskList()");
- System.exit(0);
- }
+ Vector_t parentQueryVectorPtr = new Vector_t(1);
int numVar = learnerPtr.adtreePtr.numVar;
int numRecord = learnerPtr.adtreePtr.numRecord;
} // for each variable
- queryVectorPtr.vector_free();
- parentQueryVectorPtr.vector_free();
+ queryVectorPtr.clear();
+ parentQueryVectorPtr.clear();
#ifdef TEST_LEARNER
ListNode it = learnerPtr.taskListPtr.head;
}
Queue workQueuePtr = Queue.queue_alloc(-1);
- if(workQueuePtr == null) {
- System.out.println("Assert failed: for vector alloc in learnStructure()");
- System.exit(0);
- }
int numVar = learnerPtr.adtreePtr.numVar;
Query[] queries = new Query[numVar];
- if(queries == null) {
- System.out.println("Assert failed: for queries alloc in learnStructure()");
- System.exit(0);
- }
-
for (int v = 0; v < numVar; v++) {
queries[v] = new Query();
queries[v].index = v;
float basePenalty = (float)(-0.5 * Math.log((double)numRecord));
- Vector_t queryVectorPtr = Vector_t.vector_alloc(1);
- if(queryVectorPtr == null) {
- System.out.println("Assert failed: for vector_alloc in learnStructure()");
- System.exit(0);
- }
-
- Vector_t parentQueryVectorPtr = Vector_t.vector_alloc(1);
- if(parentQueryVectorPtr == null) {
- System.out.println("Assert failed: for vector_alloc in learnStructure()");
- System.exit(0);
- }
-
- Vector_t aQueryVectorPtr = Vector_t.vector_alloc(1);
- if(aQueryVectorPtr == null) {
- System.out.println("Assert failed: for vector_alloc in learnStructure()");
- System.exit(0);
- }
-
- Vector_t bQueryVectorPtr = Vector_t.vector_alloc(1);
- if(bQueryVectorPtr == null) {
- System.out.println("Assert failed: for vector_alloc in learnStructure()");
- System.exit(0);
- }
-
+ Vector_t queryVectorPtr = new Vector_t(1);
+ Vector_t parentQueryVectorPtr = new Vector_t(1);
+ Vector_t aQueryVectorPtr = new Vector_t(1);
+ Vector_t bQueryVectorPtr = new Vector_t(1);
FindBestTaskArg arg = new FindBestTaskArg();
arg.learnerPtr = learnerPtr;
visitedBitmapPtr.bitmap_free();
workQueuePtr.queue_free();
- bQueryVectorPtr.vector_free();
- aQueryVectorPtr.vector_free();
- queryVectorPtr.vector_free();
- parentQueryVectorPtr.vector_free();
+ bQueryVectorPtr.clear();
+ aQueryVectorPtr.clear();
+ queryVectorPtr.clear();
+ parentQueryVectorPtr.clear();
queries = null;
}
learner_score ()
{
- Vector_t queryVectorPtr = Vector_t.vector_alloc(1);
- Vector_t parentQueryVectorPtr = Vector_t.vector_alloc(1);
+ Vector_t queryVectorPtr = new Vector_t(1);
+ Vector_t parentQueryVectorPtr = new Vector_t(1);
int numVar = adtreePtr.numVar;
Query[] queries = new Query[numVar];
logLikelihood += localLogLikelihood;
}
- queryVectorPtr.vector_free();
- parentQueryVectorPtr.vector_free();
+ queryVectorPtr.clear();
+ parentQueryVectorPtr.clear();
queries = null;
* allocNode
* =============================================================================
*/
- public static NetNode allocNode (int id)
- {
+
+ public static NetNode allocNode (int id) {
NetNode nodePtr = new NetNode();
if (nodePtr != null) {
* net_alloc
* =============================================================================
*/
- public static Net net_alloc (int numNode)
- {
- Net netPtr = new Net();
- if (netPtr != null) {
- Vector_t nodeVectorPtr = Vector_t.vector_alloc(numNode);
- if (nodeVectorPtr == null) {
- netPtr = null;
- return null;
- }
-
- for (int i = 0; i < numNode; i++) {
- NetNode nodePtr = allocNode(i);
- if (nodePtr == null) {
- for (int j = 0; j < i; j++) {
- nodePtr = (NetNode)(nodeVectorPtr.vector_at(j));
- nodePtr.freeNode();
- }
- nodeVectorPtr.vector_free();
- netPtr = null;
- return null;
- }
+ public Net(int numNode) {
+ Vector_t nodeVectorPtr = new Vector_t(numNode);
- boolean status = nodeVectorPtr.vector_pushBack(nodePtr);
- }
- netPtr.nodeVectorPtr = nodeVectorPtr;
+ for (int i = 0; i < numNode; i++) {
+ NetNode nodePtr = allocNode(i);
+ boolean status = nodeVectorPtr.vector_pushBack(nodePtr);
}
-
- return netPtr;
+ this.nodeVectorPtr = nodeVectorPtr;
}
* net_free
* =============================================================================
*/
- public void
- net_free ()
- {
- int numNode = nodeVectorPtr.vector_getSize();
- for (int i = 0; i < numNode; i++) {
- NetNode nodePtr = (NetNode)(nodeVectorPtr.vector_at(i));
- nodePtr.freeNode();
- }
- nodeVectorPtr.vector_free();
- }
-
+ public void net_free () {
+ nn=null;
+ nodeVectorPtr=null;
+ }
/* =============================================================================
* insertEdge
*
* @param a the array to sort
*/
- public void sort(Object[] a)
+ public static void sort(Object[] a)
{
qsort(a, 0, a.length);
}
- public void sort(Object[] a, int fromIndex, int toIndex)
+ public static void sort(Object[] a, int fromIndex, int toIndex)
{
qsort(a, fromIndex, toIndex);
}
- private int med3(int a, int b, int c, Object[]d)
+ private static int med3(int a, int b, int c, Object[]d)
{
if(less(d[a], d[b])) {
if(less(d[b], d[c])) {
}
}
- private void swap(int i, int j, Object[] a)
+ private static void swap(int i, int j, Object[] a)
{
Object c = a[i];
a[i] = a[j];
a[j] = c;
}
- private void qsort(Object[] a, int start, int n)
+ private static void qsort(Object[] a, int start, int n)
{
// use an insertion sort on small arrays
if (n <= 7)
qsort(a, pn - s, s);
}
- private void vecswap(int i, int j, int n, Object[] a)
+ private static void vecswap(int i, int j, int n, Object[] a)
{
for (; n > 0; i++, j++, n--)
swap(i, j, a);
* -- For vector_sort
* ===========================================
*/
- public boolean less(Object x, Object y) {
+ public static boolean less(Object x, Object y) {
Query aQueryPtr = (Query) x;
Query bQueryPtr = (Query) y;
if(aQueryPtr.index < bQueryPtr.index)
return false;
}
- public int diff(Object x, Object y) {
+ public static int diff(Object x, Object y) {
Query aQueryPtr = (Query) x;
Query bQueryPtr = (Query) y;
return (aQueryPtr.index - bQueryPtr.index);
public class Random {
long[] mt;
int mti;
- long RANDOM_DEFAULT_SEED;
+ int RANDOM_DEFAULT_SEED;
/* period parameter */
- int N;
- int M;
- long MATRIX_A;
- long UPPER_MASK;
- long LOWER_MASK;
+
public Random() {
- RANDOM_DEFAULT_SEED = 0L;
- N = 624;
- M = 397;
- mt = new long[N];
- mti = N;
- MATRIX_A = 0x9908b0dfL; /* constant vector a */
- UPPER_MASK = 0x80000000L; /* most significant w-r bits */
- LOWER_MASK = 0x7fffffffL; /* least significant r bits */
+ RANDOM_DEFAULT_SEED = 0;
+ mt = new long[624];
}
public void random_alloc() {
}
/* initializes mt[N] with a seed */
- public void init_genrand(long s) {
- int mti;
- mt[0]= s & 0xFFFFFFFFL;
- for (mti=1; mti<N; mti++) {
- mt[mti] = (1812433253L * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
+ public void init_genrand(int s) {
+ mt[0]= ((long)s) & 0xFFFFFFFFL;
+ for (int mti=1; mti<624; mti++) {
+ mt[mti] = (1812433253L * (mt[mti-1] ^ (mt[mti-1] >> 30)) + ((long)mti));
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
/* In the previous versions, MSBs of the seed affect */
/* only MSBs of the array mt[]. */
mt[mti] &= 0xFFFFFFFFL;
/* for >32 bit machines */
}
- this.mti=mti;
+ this.mti=624;
}
- public void random_seed(long seed) {
+ public void random_seed(int seed) {
init_genrand(seed);
}
public long random_generate() {
- return genrand_int32();
+ long x= genrand_int32()&0xFFFFFFFFL;
+ return x;
+ }
+
+ public long posrandom_generate() {
+ long r=genrand_int32();
+ if (r>0)
+ return r;
+ else
+ return -r;
}
- //public static long genrand_int32(long[] mt, long mtiPtr) {
public long genrand_int32() {
long y;
- long[] mag01= new long[2];
- mag01[0] = 0x0L;
- mag01[1] = MATRIX_A;
int mti = this.mti;
+ long[] mt = this.mt;
- /* mag01[x] = x * MATRIX_A for x=0,1 */
-
- if (mti >= N) { /* generate N words at one time */
+ if (mti >= 624) { /* generate N words at one time */
int kk;
- if (mti == N+1) /* if init_genrand() has not been called, */
- init_genrand(5489L); /* a default initial seed is used */
-
- for (kk=0;kk<N-M;kk++) {
- y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
- mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[(int)(y & 0x1L)];
+ if (mti == 624+1) { /* if init_genrand() has not been called, */
+ init_genrand(5489); /* a default initial seed is used */
+ mti=this.mti;
+ }
+ for (kk=0;kk<(624-397);kk++) {
+ y = (mt[kk]&0x80000000L)|(mt[kk+1]&0x7fffffffL);
+ mt[kk] = mt[kk+397] ^ (y >> 1) ^ ((y & 0x1)==0 ? 0L:0x9908b0dfL);
}
- for (;kk<N-1;kk++) {
- y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
- mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[(int)(y & 0x1L)];
+ for (;kk<(624-1);kk++) {
+ y = (mt[kk]&0x80000000L)|(mt[kk+1]&0x7fffffffL);
+ mt[kk] = mt[kk+(397-624)] ^ (y >> 1) ^ ((y & 0x1)==0 ? 0L:0x9908b0dfL);
}
- y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
- mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[(int)(y & 0x1L)];
+ y = (mt[624-1]&0x80000000L)|(mt[0]&0x7fffffffL);
+ mt[624-1] = mt[397-1] ^ (y >> 1) ^ ((y & 0x1)==0 ? 0L:0x9908b0dfL);
mti = 0;
}
#define CUTOFF 8
public class Sort {
+ int[] lostk;
+ int[] histk;
- public Sort() {
+ public Sort() {
+ lostk= new int[30];
+ histk= new int[30];
}
/* =============================================================================
* swap
* =============================================================================
*/
- public static void
- swap (byte[] base, int a, int b, int width)
- {
- if (a != b ) {
- while (width--) {
- byte tmp = base[a];
- base[a++] = base[b];
- base[b++] = tmp;
- }
+ public static void swap (byte[] base, int a, int b, int width) {
+ if (a != b ) {
+ while(width--) {
+ byte tmp = base[a];
+ base[a++] = base[b];
+ base[b++] = tmp;
}
}
+ }
/* =============================================================================
* sort
* =============================================================================
*/
- public static void
- sort (byte[] base,
- int start,
- int num,
- int width,
- int n,
- int offset)
- {
- if (num < 2 || width == 0) {
- return;
- }
-
- /**
- * Pointers that keep track of
- * where to start looking in
- * the base array
- **/
- int[] lostk= new int[30];
- int[] histk= new int[30];
-
- int stkptr = 0;
-
- int lo = start;
- int hi = start + (width * (num - 1));
-
- int size = 0;
-
- int pvlo = lo;
- int pvhi = hi;
- int pvwidth = width;
- int pvn = n;
- int pvmid;
- int pvloguy;
- int pvhiguy;
- int typeflag;
-
- while(true) {
-
- size = (pvhi - pvlo) / pvwidth + 1;
-
- if (size <= CUTOFF) {
-
- shortsort(base, pvlo, pvhi, pvwidth, pvn, offset);
-
- } else {
-
- pvmid = pvlo + (size / 2) * pvwidth;
- swap(base, pvmid, pvlo, pvwidth);
-
- pvloguy = pvlo;
- pvhiguy = pvhi + pvwidth;
-
- while(true) {
- do {
- pvloguy += pvwidth;
- } while (pvloguy <= pvhi && cmp(base, pvloguy, pvlo, pvn, offset) <= 0);
- do {
- pvhiguy -= pvwidth;
- } while (pvhiguy > pvlo && cmp(base, pvhiguy, pvlo, pvn, offset) >= 0);
- if (pvhiguy < pvloguy) {
- break;
- }
- swap(base, pvloguy, pvhiguy, pvwidth);
- }
-
- swap(base, pvlo, pvhiguy, pvwidth);
-
- if ((pvhiguy - 1 - pvlo) >= (pvhi - pvloguy)) {
- if (pvlo + pvwidth < pvhiguy) {
- lostk[stkptr] = pvlo;
- histk[stkptr] = pvhiguy - pvwidth;
- ++stkptr;
- }
-
- if (pvloguy < pvhi) {
- pvlo = pvloguy;
- continue;
- }
- } else {
- if (pvloguy < pvhi) {
- lostk[stkptr] = pvloguy;
- histk[stkptr] = pvhi;
- ++stkptr;
- }
- if (pvlo + pvwidth < pvhiguy) {
- pvhi = pvhiguy - pvwidth;
- continue;
+ public void sort (byte[] base,
+ int start,
+ int num,
+ int width,
+ int n,
+ int offset) {
+ if (num < 2 || width == 0) {
+ return;
+ }
+
+ /**
+ * Pointers that keep track of
+ * where to start looking in
+ * the base array
+ **/
+ int[] lostk=this.lostk;
+ int[] histk=this.histk;
+
+ int stkptr = 0;
+
+ int lo = start;
+ int hi = start + (width * (num - 1));
+
+ int size = 0;
+
+ int pvlo = lo;
+ int pvhi = hi;
+ int pvwidth = width;
+ int pvn = n;
+ int pvmid;
+ int pvloguy;
+ int pvhiguy;
+ int typeflag;
+
+ while(true) {
+
+ size = (pvhi - pvlo) / pvwidth + 1;
+
+ if (size <= CUTOFF) {
+
+ shortsort(base, pvlo, pvhi, pvwidth, pvn, offset);
+
+ } else {
+
+ pvmid = pvlo + (size / 2) * pvwidth;
+ swap(base, pvmid, pvlo, pvwidth);
+
+ pvloguy = pvlo;
+ pvhiguy = pvhi + pvwidth;
+
+ while(true) {
+ do {
+ pvloguy += pvwidth;
+ } while (pvloguy <= pvhi && cmp(base, pvloguy, pvlo, pvn, offset) <= 0);
+ do {
+ pvhiguy -= pvwidth;
+ } while (pvhiguy > pvlo && cmp(base, pvhiguy, pvlo, pvn, offset) >= 0);
+ if (pvhiguy < pvloguy) {
+ break;
+ }
+ swap(base, pvloguy, pvhiguy, pvwidth);
+ }
+
+ swap(base, pvlo, pvhiguy, pvwidth);
+
+ if ((pvhiguy - 1 - pvlo) >= (pvhi - pvloguy)) {
+ if (pvlo + pvwidth < pvhiguy) {
+ lostk[stkptr] = pvlo;
+ histk[stkptr] = pvhiguy - pvwidth;
+ ++stkptr;
+ }
+
+ if (pvloguy < pvhi) {
+ pvlo = pvloguy;
+ continue;
+ }
+ } else {
+ if (pvloguy < pvhi) {
+ lostk[stkptr] = pvloguy;
+ histk[stkptr] = pvhi;
+ ++stkptr;
+ }
+ if (pvlo + pvwidth < pvhiguy) {
+ pvhi = pvhiguy - pvwidth;
+ continue;
}
- }
- }
-
- --stkptr;
- if (stkptr >= 0) {
- pvlo = lostk[stkptr];
- pvhi = histk[stkptr];
- continue;
- }
- break;
+ }
}
+
+ --stkptr;
+ if (stkptr >= 0) {
+ pvlo = lostk[stkptr];
+ pvhi = histk[stkptr];
+ continue;
+ }
+ break;
}
+ }
/* =============================================================================
* compareRecord
--- /dev/null
+public class Vector_t {
+ int size;
+ int capacity;
+ Object[] elements;
+
+ public Vector_t() {
+ }
+
+ /* =============================================================================
+ * Vector_alloc
+ * -- Returns null if failed
+ * =============================================================================
+ */
+ public Vector_t(int initCapacity) {
+ int capacity = Math.imax(initCapacity, 1);
+ this.size = 0;
+ this.capacity = capacity;
+ this.elements = new Object[capacity];
+ }
+
+ /* =============================================================================
+ * Vector_free
+ * =============================================================================
+ */
+ public void clear() {
+ elements = null;
+ }
+
+ /* =============================================================================
+ * Vector_at
+ * -- Returns null if failed
+ * =============================================================================
+ */
+ public Object vector_at (int i) {
+ if ((i < 0) || (i >= size)) {
+ System.out.println("Illegal Vector.element\n");
+ return null;
+ }
+ return (elements[i]);
+ }
+
+
+ /* =============================================================================
+ * Vector_pushBack
+ * -- Returns false if fail, else true
+ * =============================================================================
+ */
+ public boolean vector_pushBack (Object dataPtr) {
+ if (size == capacity) {
+ int newCapacity = capacity * 2;
+ Object[] newElements = new Object[newCapacity];
+
+ if (newElements == null) {
+ return false;
+ }
+ capacity = newCapacity;
+ for (int i = 0; i < size; i++) {
+ newElements[i] = elements[i];
+ }
+ elements = null;
+ elements = newElements;
+ }
+
+ elements[size++] = dataPtr;
+
+ return true;
+ }
+
+ /* =============================================================================
+ * Vector_popBack
+ * -- Returns null if fail, else returns last element
+ * =============================================================================
+ */
+ public Object vector_popBack () {
+ if (size < 1) {
+ return null;
+ }
+ Object o=elements[--(size)];
+ elements[size]=null;
+ return o;
+ }
+
+ /* =============================================================================
+ * Vector_getSize
+ * =============================================================================
+ */
+ public int
+ vector_getSize ()
+ {
+ return (size);
+ }
+
+ /* =============================================================================
+ * Vector_clear
+ * =============================================================================
+ */
+ public void vector_clear () {
+ while(size>0)
+ elements[--size]=null;
+ }
+
+ /* =============================================================================
+ * Vector_sort
+ * =============================================================================
+ */
+ public void
+ vector_sort ()
+ {
+ QuickSort.sort(elements, 0, size);
+ }
+
+ /* =============================================================================
+ * Vector_copy
+ * =============================================================================
+ */
+ public static boolean
+ vector_copy (Vector_t dstVectorPtr, Vector_t srcVectorPtr)
+ {
+ int dstCapacity = dstVectorPtr.capacity;
+ int srcSize = srcVectorPtr.size;
+ if (dstCapacity < srcSize) {
+ int srcCapacity = srcVectorPtr.capacity;
+ Object[] elements = new Object[srcCapacity];
+
+ if (elements == null) {
+ return false;
+ }
+ dstVectorPtr.elements = null;
+ dstVectorPtr.elements = elements;
+ dstVectorPtr.capacity = srcCapacity;
+ }
+
+ for(int i = 0; i< srcSize; i++) {
+ dstVectorPtr.elements[i] = srcVectorPtr.elements[i];
+ }
+
+ dstVectorPtr.size = srcSize;
+
+ return true;
+ }
+}
IntListNode.java \
tmpQueue.java \
Random.java \
- ../common/Vector_t.java \
+ Vector_t.java \
ListNode.java \
tmpList.java \
QuickSort.java \
../../../ClassLibrary/JavaSTM/Barrier.java \
../common/LocalStartStop.java
-FLAGS=-mainclass ${MAINCLASS} -singleTM -optimize -debug -dcopts -stmstats -fastmemcpy -transstats -abcclose
+include ../common/Makefile.flags
-default:
+include ../common/Makefile.builds
+
+prep:
cpp Bayes.java > tmp1Bayes.java
cpp Data.java > tmp1Data.java
cpp Net.java > tmp1Net.java
cpp -DLIST_NO_DUPLICATES IntList.java > tmp1IntList.java
cpp -DLIST_NO_DUPLICATES List.java > tmp1List.java
./extractLines
- ../../../buildscript ${FLAGS} -o ${MAINCLASS} ${SRC}
clean:
rm tmp1Bayes.java