*
* Copyright (C) Stanford University, 2006. All Rights Reserved.
* Author: Chi Cao Minh
+ * Ported to Java June 2009 Alokika Dash
+ * University of California, Irvine
*
* =============================================================================
*
*
* =============================================================================
*/
- /*
-#include <assert.h>
-#include <stdlib.h>
-#include "adtree.h"
-#include "data.h"
-#include "query.h"
-#include "utility.h"
-#include "vector.h"
-*/
public class Adtree {
int numVar;
{
nodePtr.varyVectorPtr.vector_free();
nodePtr = null;
- //free(nodePtr);
}
freeVary (AdtreeVary varyPtr)
{
varyPtr = null;
- //free(varyPtr);
}
public static Adtree adtree_alloc ()
{
Adtree adtreePtr = new Adtree();
- //adtree_t* adtreePtr;
- //adtreePtr = (adtree_t*)malloc(sizeof(adtree_t));
if (adtreePtr != null) {
adtreePtr.numVar = -1;
adtreePtr.numRecord = -1;
{
if (nodePtr != null) {
Vector_t varyVectorPtr = nodePtr.varyVectorPtr;
- //vector_t* varyVectorPtr = nodePtr.varyVectorPtr;
- //int v;
int numVary = varyVectorPtr.vector_getSize();
for (int v = 0; v < numVary; v++) {
AdtreeVary varyPtr = (AdtreeVary)(varyVectorPtr.vector_at(v));
- //adtree_vary_t* varyPtr = (adtree_vary_t*)vector_at(varyVectorPtr, v);
freeNodes(varyPtr.zeroNodePtr);
freeNodes(varyPtr.oneNodePtr);
freeVary(varyPtr);
adtree_free ()
{
freeNodes(rootNodePtr);
- this = null;
- //free(adtreePtr);
}
-
- /*
- static adtree_vary_t*
- makeVary (int parentIndex,
- int index,
- int start,
- int numRecord,
- Data* dataPtr);
-
- static adtree_node_t*
- makeNode (int parentIndex,
- int index,
- int start,
- int numRecord,
- Data* dataPtr);
- */
-
-
/* =============================================================================
* makeVary
* =============================================================================
Data dataPtr)
{
AdtreeVary varyPtr = AdtreeVary.allocVary(index);
- //assert(varyPtr);
if ((parentIndex + 1 != index) && (numRecord > 1)) {
dataPtr.data_sort(start, numRecord, index);
Data dataPtr)
{
AdtreeNode nodePtr = AdtreeNode.allocNode(index);
- //adtree_node_t* nodePtr = allocNode(index);
- //assert(nodePtr);
nodePtr.count = numRecord;
Vector_t varyVectorPtr = nodePtr.varyVectorPtr;
- //vector_t* varyVectorPtr = nodePtr.varyVectorPtr;
-
- //int v;
int numVar = dataPtr.numVar;
for (int v = (index + 1); v < numVar; v++) {
AdtreeVary varyPtr =
- //adtree_vary_t* varyPtr =
makeVary(parentIndex, v, start, numRecord, dataPtr);
- //assert(varyPtr);
boolean status;
if((status = varyVectorPtr.vector_pushBack(varyPtr)) != true) {
System.out.println("varyVectorPtr.vector_pushBack != true");
System.exit(0);
}
- //assert(status);
}
return nodePtr;
Query queryPtr = (Query)(queryVectorPtr.vector_at(q));
- //query_t* queryPtr = (query_t*)vector_at(queryVectorPtr, q);
if (queryPtr != null) {
return nodePtr.count;
}
System.out.println("Assert failed");
System.exit(0);
}
- //assert(queryIndex <= lastQueryIndex);
+
Vector_t varyVectorPtr = nodePtr.varyVectorPtr;
- //vector_t* varyVectorPtr = nodePtr.varyVectorPtr;
AdtreeVary varyPtr = (AdtreeVary)(varyVectorPtr.vector_at((queryIndex - nodeIndex - 1)));
- //adtree_vary_t* varyPtr =
- // (adtree_vary_t*)vector_at(varyVectorPtr,
- // (queryIndex - nodeIndex - 1));
- //assert(varyPtr);
int queryValue = queryPtr.value;
*/
int numQuery = queryVectorPtr.vector_getSize();
Vector_t superQueryVectorPtr = Vector_t.vector_alloc(numQuery - 1);
- //vector_t* superQueryVectorPtr = PVECTOR_ALLOC(numQuery - 1); //MEMORY ALLOACATED FROM THREAD POOL
- //assert(superQueryVectorPtr);
- //int qq;
for (int qq = 0; qq < numQuery; qq++) {
if (qq != q) {
boolean status = superQueryVectorPtr.vector_pushBack(
queryVectorPtr.vector_at(qq));
- //assert(status);
}
}
int superCount = adtree_getCount(superQueryVectorPtr);
superQueryVectorPtr.vector_free();
- //PVECTOR_FREE(superQueryVectorPtr);
int invertCount;
if (queryValue == 0) {
queryVectorPtr,
lastQueryIndex);
} else { /* QUERY_VALUE_WILDCARD */
- /*
-#if 0
- count += getCount(varyPtr.zeroNodePtr,
- (i + 1),
- (q + 1),
- queryVectorPtr,
- lastQueryIndex);
- count += getCount(varyPtr.oneNodePtr,
- (i + 1),
- (q + 1),
- queryVectorPtr,
- lastQueryIndex,
- adtreePtr);
-#else
- assert(0); // catch bugs in learner
-#endif
- */
+ System.out.println("Program shouldn't get here"); // catch bugs in learner
+ System.exit(0);
}
}
public int
adtree_getCount (Vector_t queryVectorPtr)
{
- //AdtreeNode rootNodePtr = adtreePtr.rootNodePtr;
if (rootNodePtr == null) {
return 0;
}
int numQuery = queryVectorPtr.vector_getSize();
if (numQuery > 0) {
Query lastQueryPtr = (Query)(queryVectorPtr.vector_at(numQuery - 1));
- //query_t* lastQueryPtr = (query_t*)vector_at(queryVectorPtr, (numQuery - 1));
lastQueryIndex = lastQueryPtr.index;
}
queryVectorPtr,
lastQueryIndex);
}
-
-
- /* #############################################################################
- * TEST_ADTREE
- * #############################################################################
- */
- /*
-#ifdef TEST_ADTREE
-
-#include <stdio.h>
-#include "timer.h"
-
- static void printNode (adtree_node_t* nodePtr);
- static void printVary (adtree_vary_t* varyPtr);
-
- boolean global_doPrint = FALSE;
-
-
- static void
- printData (Data* dataPtr)
- {
- int numVar = dataPtr.numVar;
- int numRecord = dataPtr.numRecord;
-
- int r;
- for (r = 0; r < numRecord; r++) {
- printf("%4li: ", r);
- char* record = data_getRecord(dataPtr, r);
- assert(record);
- int v;
- for (v = 0; v < numVar; v++) {
- printf("%li", (int)record[v]);
- }
- puts("");
- }
- }
-
-
- static void
- printNode (adtree_node_t* nodePtr)
- {
- if (nodePtr) {
- printf("[node] index=%li value=%li count=%li\n",
- nodePtr.index, nodePtr.value, nodePtr.count);
- vector_t* varyVectorPtr = nodePtr.varyVectorPtr;
- int v;
- int numVary = vector_getSize(varyVectorPtr);
- for (v = 0; v < numVary; v++) {
- adtree_vary_t* varyPtr = (adtree_vary_t*)vector_at(varyVectorPtr, v);
- printVary(varyPtr);
- }
- }
- puts("[up]");
- }
-
-
- static void
- printVary (adtree_vary_t* varyPtr)
- {
- if (varyPtr) {
- printf("[vary] index=%li\n", varyPtr.index);
- printNode(varyPtr.zeroNodePtr);
- printNode(varyPtr.oneNodePtr);
- }
- puts("[up]");
- }
-
-
- static void
- printAdtree (adtree_t* adtreePtr)
- {
- printNode(adtreePtr.rootNodePtr);
- }
-
-
- static void
- printQuery (vector_t* queryVectorPtr)
- {
- printf("[");
- int q;
- int numQuery = vector_getSize(queryVectorPtr);
- for (q = 0; q < numQuery; q++) {
- query_t* queryPtr = (query_t*)vector_at(queryVectorPtr, q);
- printf("%li:%li ", queryPtr.index, queryPtr.value);
- }
- printf("]");
- }
-
-
- static int
- countData (Data* dataPtr, vector_t* queryVectorPtr)
- {
- int count = 0;
- int numQuery = vector_getSize(queryVectorPtr);
-
- int r;
- int numRecord = dataPtr.numRecord;
- for (r = 0; r < numRecord; r++) {
- char* record = data_getRecord(dataPtr, r);
- boolean isMatch = TRUE;
- int q;
- for (q = 0; q < numQuery; q++) {
- query_t* queryPtr = (query_t*)vector_at(queryVectorPtr, q);
- int queryValue = queryPtr.value;
- if ((queryValue != QUERY_VALUE_WILDCARD) &&
- ((char)queryValue) != record[queryPtr.index])
- {
- isMatch = FALSE;
- break;
- }
- }
- if (isMatch) {
- count++;
- }
- }
-
- return count;
- }
-
-
- static void
- testCount (adtree_t* adtreePtr,
- Data* dataPtr,
- vector_t* queryVectorPtr,
- int index,
- int numVar)
- {
- if (index >= numVar) {
- return;
- }
-
- int count1 = adtree_getCount(adtreePtr, queryVectorPtr);
- int count2 = countData(dataPtr, queryVectorPtr);
- if (global_doPrint) {
- printQuery(queryVectorPtr);
- printf(" count1=%li count2=%li\n", count1, count2);
- fflush(stdout);
- }
- assert(count1 == count2);
-
- query_t query;
-
- int i;
- for (i = 1; i < numVar; i++) {
- query.index = index + i;
- boolean status = vector_pushBack(queryVectorPtr, (void*)&query);
- assert(status);
-
- query.value = 0;
- testCount(adtreePtr, dataPtr, queryVectorPtr, query.index, numVar);
-
- query.value = 1;
- testCount(adtreePtr, dataPtr, queryVectorPtr, query.index, numVar);
-
- vector_popBack(queryVectorPtr);
- }
- }
-
-
- static void
- testCounts (adtree_t* adtreePtr, Data* dataPtr)
- {
- int numVar = dataPtr.numVar;
- vector_t* queryVectorPtr = vector_alloc(numVar);
- int v;
- for (v = -1; v < numVar; v++) {
- testCount(adtreePtr, dataPtr, queryVectorPtr, v, dataPtr.numVar);
- }
- vector_free(queryVectorPtr);
- }
-
-
- static void
- test (int numVar, int numRecord)
- {
- random_t* randomPtr = random_alloc();
- Data* dataPtr = data_alloc(numVar, numRecord, randomPtr);
- assert(dataPtr);
- data_generate(dataPtr, 0, 10, 10);
- if (global_doPrint) {
- printData(dataPtr);
- }
-
- Data* copyDataPtr = data_alloc(numVar, numRecord, randomPtr);
- assert(copyDataPtr);
- data_copy(copyDataPtr, dataPtr);
-
- adtree_t* adtreePtr = adtree_alloc();
- assert(adtreePtr);
-
- TIMER_T start;
- TIMER_READ(start);
-
- adtree_make(adtreePtr, copyDataPtr);
-
- TIMER_T stop;
- TIMER_READ(stop);
-
- printf("%lf\n", TIMER_DIFF_SECONDS(start, stop));
-
- if (global_doPrint) {
- printAdtree(adtreePtr);
- }
-
- testCounts(adtreePtr, dataPtr);
-
- adtree_free(adtreePtr);
- random_free(randomPtr);
- data_free(dataPtr);
- }
-
-
- int
- main ()
- {
- puts("Starting...");
-
- puts("Test 1:");
- test(3, 8);
-
- puts("Test 2:");
- test(4, 64);
-
- puts("Test 3:");
- test(8, 256);
-
- puts("Test 4:");
- test(12, 256);
-
- puts("Test 5:");
- test(48, 1024);
-
- puts("All tests passed.");
-
- return 0;
- }
-
-
-#endif // TEST_ADTREE
- */
-
}
/* =============================================================================
*
- * End of adtree.c
+ * End of adtree.java
*
* =============================================================================
*/
+/**
+ * Author: Alokika Dash
+ * University of California, Irvine
+ * adash@uci.edu
+ *
+ * - Helper class of Adtree.java
+ **/
public class AdtreeNode {
int index;
int value;
allocNode (int index)
{
AdtreeNode nodePtr = new AdtreeNode();
- //adtree_node_t* nodePtr;
- //nodePtr = (adtree_node_t*)malloc(sizeof(adtree_node_t));
if (nodePtr != null) {
- //nodePtr.varyVectorPtr = vector_alloc(1);
nodePtr.varyVectorPtr = Vector_t.vector_alloc(1);
if (nodePtr.varyVectorPtr == null) {
nodePtr = null;
- //free(nodePtr);
return null;
}
nodePtr.index = index;
+/**
+ * Author: Alokika Dash
+ * University of California, Irvine
+ * adash@uci.edu
+ *
+ * - Helper class of Adtree.java
+ **/
+
public class AdtreeVary {
int index;
int mostCommonValue;
allocVary (int index)
{
AdtreeVary varyPtr= new AdtreeVary();
- //adtree_vary_t* varyPtr;
- //varyPtr = (adtree_vary_t*)malloc(sizeof(adtree_vary_t));
if (varyPtr != null) {
varyPtr.index = index;
varyPtr.mostCommonValue = -1;
*
* Copyright (C) Stanford University, 2006. All Rights Reserved.
* Author: Chi Cao Minh
- * Ported to Java
- * Author: Alokika Dash
+ * Ported to Java June 2009 Alokika Dash
+ * University of California, Irvine
*
* =============================================================================
*
Learner learnerPtr = Learner.learner_alloc(dataPtr, adtreePtr, 1);
- //learner_t* learnerPtr = learner_alloc(dataPtr, adtreePtr, 1);
-
Net tmpNetPtr = learnerPtr.netPtr;
learnerPtr.netPtr = netPtr;
int percentParent = b.global_params[PARAM_PERCENT];
b.global_insertPenalty = b.global_params[PARAM_INSERT];
b.global_maxNumEdgeLearned = b.global_params[PARAM_EDGE];
- //SIM_GET_NUM_CPU(numThread);
- //TM_STARTUP(numThread);
- //P_MEMORY_STARTUP(numThread);
/* Initiate Barriers */
- //Barrier.setBarrier(numThread);
+ Barrier.setBarrier(numThread);
Bayes[] binit = new Bayes[numThread];
- System.out.println("Random seed \n" + randomSeed);
- System.out.println("Number of vars \n" + numVar);
- System.out.println("Number of records \n" + numRecord);
- System.out.println("Max num parents \n" + maxNumParent);
- System.out.println("%% chance of parent \n" + percentParent);
- System.out.println("Insert penalty \n" + b.global_insertPenalty);
- System.out.println("Max num edge learned / var \n" + b.global_maxNumEdgeLearned);
- System.out.println("Operation quality factor \n" + b.global_operationQualityFactor);
+ System.out.println("Random seed " + randomSeed);
+ System.out.println("Number of vars " + numVar);
+ System.out.println("Number of records " + numRecord);
+ System.out.println("Max num parents " + maxNumParent);
+ System.out.println("%% chance of parent " + percentParent);
+ System.out.println("Insert penalty " + b.global_insertPenalty);
+ System.out.println("Max num edge learned / var " + b.global_maxNumEdgeLearned);
+ System.out.println("Operation quality factor " + b.global_operationQualityFactor);
/*
* Generate data
*/
- System.out.println("Generating data... ");
+ System.out.print("Generating data... ");
Random randomPtr = new Random();
randomPtr.random_alloc();
randomPtr.random_seed(randomSeed);
- //random_t* randomPtr = random_alloc();
- //assert(randomPtr);
- //random_seed(randomPtr, randomSeed);
Data dataPtr = Data.data_alloc(numVar, numRecord, randomPtr);
- //Data* dataPtr = data_alloc(numVar, numRecord, randomPtr);
- //assert(dataPtr);
Net netPtr = dataPtr.data_generate(-1, maxNumParent, percentParent);
- //Net* netPtr = data_generate(dataPtr, -1, maxNumParent, percentParent);
System.out.println("done.");
- //puts("done.");
- //fflush(stdout);
/*
* Generate adtree
*/
Adtree adtreePtr = Adtree.adtree_alloc();
- //adtree_t* adtreePtr = adtree_alloc();
- //assert(adtreePtr);
-
- System.out.println("Generating adtree... ");
- //fflush(stdout);
- //TIMER_T adtreeStartTime;
- //TIMER_READ(adtreeStartTime);
+ System.out.print("Generating adtree... ");
adtreePtr.adtree_make(dataPtr);
- //TIMER_T adtreeStopTime;
- //TIMER_READ(adtreeStopTime);
-
System.out.println("done.");
- //fflush(stdout);
- //System.out.println("Adtree time = %f\n",
- // TIMER_DIFF_SECONDS(adtreeStartTime, adtreeStopTime));
- //fflush(stdout);
/*
* Score original network
* Learn structure of Bayesian network
*/
- //START FROM HERE
Learner learnerPtr = Learner.learner_alloc(dataPtr, adtreePtr, numThread);
- //learner_t* learnerPtr = learner_alloc(dataPtr, adtreePtr, numThread);
- //assert(learnerPtr);
+
dataPtr.data_free(); /* save memory */
- System.out.println("Learning structure...");
- //fflush(stdout);
+ System.out.print("Learning structure...");
/* Create and Start Threads */
for(int i = 1; i<numThread; i++) {
}
- //TIMER_T learnStartTime;
- //TIMER_READ(learnStartTime);
- //GOTO_SIM();
-
/*
Barrier.enterBarrier();
Learner.learner_run(0, numThread, learnerPtr);
Barrier.enterBarrier();
*/
- //GOTO_REAL();
- //TIMER_T learnStopTime;
- //TIMER_READ(learnStopTime);
-
System.out.println("done.");
- //fflush(stdout);
- //System.out.println("Learn time = %f\n",
- // TIMER_DIFF_SECONDS(learnStartTime, learnStopTime));
- //fflush(stdout);
/*
* Check solution
System.out.println("System has an incorrect result");
System.exit(0);
}
- //assert(!status);
#ifndef SIMULATOR
float learnScore = learnerPtr.learner_score();
* Clean up
*/
- //fflush(stdout);
#ifndef SIMULATOR
adtreePtr.adtree_free();
# if 0
# endif
#endif
- //TM_SHUTDOWN();
- //P_MEMORY_SHUTDOWN();
-
- //GOTO_SIM();
-
- //thread_shutdown();
-
- //MAIN_RETURN(0);
}
}
/* =============================================================================
*
* Copyright (C) Stanford University, 2006. All Rights Reserved.
* Author: Chi Cao Minh
+ * Ported to Java June 2009 Alokika Dash
+ * University of California, Irvine
*
* =============================================================================
*
*/
public static Data data_alloc (int numVar, int numRecord, Random randomPtr)
{
- //data_t* dataPtr;
Data dataPtr = new Data();
- //dataPtr = (data_t*)malloc(sizeof(data_t));
- //if (dataPtr) {
- int numDatum = numVar * numRecord;
- dataPtr.records = new char[numDatum];
- for(int i = 0; i<numDatum; i++)
- dataPtr.records[i] = (char)DATA_INIT;
+ if (dataPtr != null) {
+ int numDatum = numVar * numRecord;
+ dataPtr.records = new char[numDatum];
+ for(int i = 0; i<numDatum; i++)
+ dataPtr.records[i] = (char)DATA_INIT;
- dataPtr.numVar = numVar;
- dataPtr.numRecord = numRecord;
- dataPtr.randomPtr = randomPtr;
-
- //memset(dataPtr.records, DATA_INIT, (numDatum * sizeof(char)));
- //dataPtr.numVar = numVar;
- //dataPtr.numRecord = numRecord;
- //dataPtr.randomPtr = randomPtr;
- //}
+ dataPtr.numVar = numVar;
+ dataPtr.numRecord = numRecord;
+ dataPtr.randomPtr = randomPtr;
+ }
return dataPtr;
}
*/
public Net data_generate (int seed, int maxNumParent, int percentParent)
{
- //Random randomPtr = dataPtr.randomPtr;
if (seed >= 0) {
randomPtr.random_seed(seed);
}
* Generate random Bayesian network
*/
- //int numVar = dataPtr.numVar;
Net netPtr = Net.net_alloc(numVar);
- //assert(netPtr);
netPtr.net_generateRandomEdges(maxNumParent, percentParent, randomPtr);
/*
* value instances
*/
- //int** thresholdsTable = (int**)malloc(numVar * sizeof(int*));
- //int[][] thresholdsTable = new int[numVar][numThreshold];
- int[][] thresholdsTable = new int[numVar][];
- //assert(thresholdsTable);
+ int[][] thresholdsTable = new int[numVar][]; //TODO check if this is alright
int v;
for (v = 0; v < numVar; v++) {
- //list_t* parentIdListPtr = Net.net_getParentIdListPtr(netPtr, v);
IntList parentIdListPtr = netPtr.net_getParentIdListPtr(v);
int numThreshold = 1 << parentIdListPtr.list_getSize();
- //int numThreshold = 1 << list_getSize(parentIdListPtr);
int[] thresholds = new int[numThreshold];
- //int* thresholds = (int*)malloc(numThreshold * sizeof(int));
- //assert(thresholds);
- //int t;
for (int t = 0; t < numThreshold; t++) {
int threshold = randomPtr.random_generate() % (DATA_PRECISION + 1);
thresholds[t] = threshold;
*/
int[] order = new int[numVar];
- //int* order = (int*)malloc(numVar * sizeof(int));
- //assert(order);
int numOrder = 0;
Queue workQueuePtr = Queue.queue_alloc(-1);
- //queue_t* workQueuePtr = queue_alloc(-1);
- //assert(workQueuePtr);
IntVector dependencyVectorPtr = IntVector.vector_alloc(1);
- //vector_t* dependencyVectorPtr = vector_alloc(1);
- //assert(dependencyVectorPtr);
BitMap orderedBitmapPtr = BitMap.bitmap_alloc(numVar);
- //bitmap_t* orderedBitmapPtr = bitmap_alloc(numVar);
- //assert(orderedBitmapPtr);
orderedBitmapPtr.bitmap_clearAll();
BitMap doneBitmapPtr = BitMap.bitmap_alloc(numVar);
- //bitmap_t* doneBitmapPtr = bitmap_alloc(numVar);
- //assert(doneBitmapPtr);
doneBitmapPtr.bitmap_clearAll();
- //bitmap_clearAle(doneBitmapPtr);
+
v = -1;
- //while ((v = bitmap_findClear(doneBitmapPtr, (v + 1))) >= 0)
while ((v = doneBitmapPtr.bitmap_findClear(v + 1)) >= 0) {
IntList childIdListPtr = netPtr.net_getChildIdListPtr(v);
- //list_t* childIdListPtr = net_getChildIdListPtr(netPtr, v);
int numChild = childIdListPtr.list_getSize();
if (numChild == 0) {
System.out.println("status= "+ status + "should be true");
System.exit(0);
}
- //assert(status);
+
while (!(workQueuePtr.queue_isEmpty())) {
int id = workQueuePtr.queue_pop();
if((status = doneBitmapPtr.bitmap_set(id)) != true) {
System.out.println("status= "+ status + "should be true");
System.exit(0);
}
- //assert(status);
- //CHECK THIS
- if((status = dependencyVectorPtr.vector_pushBack(id)) != true) {
+
+ if((status = dependencyVectorPtr.vector_pushBack(id)) == false) {
System.out.println("status= "+ status + "should be true");
System.exit(0);
}
- //assert(status);
- //list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, id);
- //list_iter_t it;
- //list_iter_reset(&it, parentIdListPtr);
+
IntList parentIdListPtr = netPtr.net_getParentIdListPtr(id);
IntListNode it = parentIdListPtr.head;
parentIdListPtr.list_iter_reset(it);
- //while (list_iter_hasNext(&it, parentIdListPtr))
+
while (parentIdListPtr.list_iter_hasNext(it)) {
it = it.nextPtr;
- //int parentId = (int)list_iter_next(&it, parentIdListPtr);
int parentId = parentIdListPtr.list_iter_next(it);
- if((status = workQueuePtr.queue_push(parentId)) != true) {
+ if((status = workQueuePtr.queue_push(parentId)) == false) {
System.out.println("status= "+ status + "should be true");
System.exit(0);
}
- //assert(status);
}
}
* Create ordering
*/
- //int i;
int n = dependencyVectorPtr.vector_getSize();
for (int i = 0; i < n; i++) {
int id = dependencyVectorPtr.vector_popBack();
System.out.println("numVar should be equal to numOrder");
System.exit(0);
}
- //assert(numOrder == numVar);
/*
* Create records
*/
- char[] tmprecord = records;
int startindex = 0;
- //int r;
- //int numRecord = dataPtr.numRecord;
for (int r = 0; r < numRecord; r++) {
- //int o;
for (int o = 0; o < numOrder; o++) {
v = order[o];
IntList parentIdListPtr = netPtr.net_getParentIdListPtr(v);
- //list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, v);
int index = 0;
- //list_iter_t it;
- //list_iter_reset(&it, parentIdListPtr);
IntListNode it = parentIdListPtr.head;
parentIdListPtr.list_iter_reset(it);
while (parentIdListPtr.list_iter_hasNext(it)) {
it = it.nextPtr;
int parentId = parentIdListPtr.list_iter_next(it);
- int value = tmprecord[parentId];
+ int value = records[startindex + parentId];
if(value == DATA_INIT) {
System.out.println("value should be != DATA_INIT");
System.exit(0);
}
int rnd = randomPtr.random_generate() % DATA_PRECISION;
int threshold = thresholdsTable[v][index];
- tmprecord[v] = (char) ((rnd < threshold) ? 1 : 0);
+ records[startindex + v] = (char) ((rnd < threshold) ? 1 : 0);
}
- //record += numVar;
startindex += numVar;
if(startindex > numRecord * numVar) {
System.out.println("value should be != DATA_INIT in data_generate()");
System.exit(0);
}
- //assert(record <= (dataPtr.records + numRecord * numVar));
}
/*
dependencyVectorPtr.vector_free();
workQueuePtr.queue_free();
order = null;
- //bitmap_free(doneBitmapPtr);
- //bitmap_free(orderedBitmapPtr);
- //vector_free(dependencyVectorPtr);
- //queue_free(workQueuePtr);
- //free(order);
+
for (v = 0; v < numVar; v++) {
thresholdsTable[v] = null;
- //free(thresholdsTable[v]);
}
+
thresholdsTable = null;
- //free(thresholdsTable);
return netPtr;
}
- /* =============================================================================
- * data_getRecord
- * -- Returns null if invalid index
- * =============================================================================
- */
- /*
- char*
- data_getRecord (data_t* dataPtr, int index)
- {
- if (index < 0 || index >= (dataPtr.numRecord)) {
- return null;
- }
-
- return &dataPtr.records[index * dataPtr.numVar];
- }
- */
-
-
/* =============================================================================
* data_copy
* -- Returns false on failure
int numSrcDatum = srcPtr.numVar * srcPtr.numRecord;
if (numDstDatum != numSrcDatum) {
dstPtr.records = null;
- //free(dstPtr.records);
dstPtr.records = new char[numSrcDatum];
- //dstPtr.records = (char*)calloc(numSrcDatum, sizeof(char));
if (dstPtr.records == null) {
return false;
}
dstPtr.numRecord = srcPtr.numRecord;
for(int i=0; i<numSrcDatum; i++)
dstPtr.records[i] = srcPtr.records[i];
- //memcpy(dstPtr.records, srcPtr.records, (numSrcDatum * sizeof(char)));
return true;
}
-
- /* =============================================================================
- * compareRecord
- * =============================================================================
- */
- /*
- public static int
- compareRecord (const void* p1, const void* p2, int n, int offset)
- {
- int i = n - offset;
- const char* s1 = (const char*)p1 + offset;
- const char* s2 = (const char*)p2 + offset;
-
- while (i-- > 0) {
- unsigned char u1 = (unsigned char)*s1++;
- unsigned char u2 = (unsigned char)*s2++;
- if (u1 != u2) {
- return (u1 - u2);
- }
- }
-
- return 0;
- }
- */
-
-
/* =============================================================================
* data_sort
* -- In place
if((start + num < 0) || (start + num > numRecord))
System.out.println("start + num: Assert failed in data_sort");
- //assert(start >= 0 && start <= dataPtr.numRecord);
- //assert(num >= 0 && num <= dataPtr.numRecord);
- //assert(start + num >= 0 && start + num <= dataPtr.numRecord);
-
- //int numVar = dataPtr.numVar;
-
//FIXME
- //Sort.sort((dataPtr.records + (start * numVar)),
Sort.sort(records,
start * numVar,
num,
int low = start;
int high = start + num - 1;
- //int numVar = dataPtr.numVar;
- //char* records = dataPtr.records;
-
while (low <= high) {
int mid = (low + high) / 2;
if (records[numVar * mid + offset] == 0) {
+/**
+ * Author: Alokika Dash
+ * University of California, Irvine
+ * adash@uci.edu
+ *
+ * - Helper class of Learner.java
+ **/
+
public class FindBestTaskArg {
int toId;
Learner learnerPtr;
* Copyright (C) Stanford University, 2006. All Rights Reserved.
* Author: Chi Cao Minh
*
- * Ported to Java June 2006, Alokika Dash
+ * Ported to Java June 2009, Alokika Dash
* adash@uci.edu
* University of California, Irvine
*
public int
list_iter_next (IntListNode itPtr)
{
- //itPtr = itPtr.nextPtr;
return itPtr.dataPtr;
}
freeNode (IntListNode nodePtr)
{
nodePtr = null;
- //free(nodePtr);
}
+/**
+ * Author: Alokika Dash
+ * University of California, Irvine
+ * adash@uci.edu
+ *
+ * - Helper class for IntList.java
+ **/
+
public class IntListNode {
public int dataPtr;
public IntListNode nextPtr;
+/**
+ * Author: Alokika Dash
+ * University of California, Irvine
+ * adash@uci.edu
+ *
+ * - Vector class for "ints" elements
+ **/
+
public class IntVector {
int size;
int capacity;
int newCapacity = capacity * 2;
int[] newElements = new int[newCapacity];
- //void** newElements = (void**)malloc(newCapacity * sizeof(void*));
if (newElements == null) {
return false;
}
public void
vector_sort ()
{
- //qsort.sort(elements, 0, (elements.length - 1));
- //qsort(elements, size, 4, compare);
+ qsort.sort(elements, 0, (elements.length - 1));
}
/* =============================================================================
*
* Copyright (C) Stanford University, 2006. All Rights Reserved.
* Author: Chi Cao Minh
+ * Ported to Java June 2009 Alokika Dash
+ * University of California, Irvine
+ *
*
* =============================================================================
*
* =============================================================================
*/
public void
- //learner_free (learner_t* learnerPtr)
learner_free ()
{
taskListPtr.list_free();
tasks = null;
localBaseLogLikelihoods = null;
netPtr.net_free();
- //free(learnerPtr.tasks);
- //free(learnerPtr.localBaseLogLikelihoods);
- //net_free(learnerPtr.netPtr);
- this = null;
- //free(learnerPtr);
}
System.exit(0);
}
- //assert(parentCount >= count);
- //assert(parentCount > 0);
-
float fval = (float)(probability * (Math.log((double)count/ (double)parentCount)));
return fval;
}
boolean status;
status = Vector_t.vector_copy(queryVectorPtr, parentQueryVectorPtr);
- //TODO CHECK ASSERT
- //assert(status);
+ if(status == false ) {
+ System.out.println("Assert failed: whille vector copy in populateQueryVectors()");
+ System.exit(0);
+ }
+
status = queryVectorPtr.vector_pushBack(queries[id]);
- //TODO CHECK ASSERT
- //assert(status);
+ if(status == false ) {
+ System.out.println("Assert failed: whille vector pushBack in populateQueryVectors()");
+ System.exit(0);
+ }
+
queryVectorPtr.vector_sort();
}
float localLogLikelihood = 0.0f;
- //query_t* parentQueryPtr = vector_at(parentQueryVectorPtr, i);
Query parentQueryPtr = (Query) (parentQueryVectorPtr.vector_at(i));
int parentIndex = parentQueryPtr.index;
public float
learner_score ()
{
- //adtree_t* adtreePtr = learnerPtr.adtreePtr;
- //net_t* netPtr = learnerPtr.netPtr;
Vector_t queryVectorPtr = Vector_t.vector_alloc(1);
- //assert(queryVectorPtr);
Vector_t parentQueryVectorPtr = Vector_t.vector_alloc(1);
- //vector_t* parentQueryVectorPtr = vector_alloc(1);
- //assert(parentQueryVectorPtr);
int numVar = adtreePtr.numVar;
Query[] queries = new Query[numVar];
- //query_t* queries = (query_t*)malloc(numVar * sizeof(query_t));
- //assert(queries);
- //int v;
+
for (int v = 0; v < numVar; v++) {
queries[v] = new Query();
queries[v].index = v;
for (int v = 0; v < numVar; v++) {
IntList parentIdListPtr = netPtr.net_getParentIdListPtr(v);
- //list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, v);
numTotalParent += parentIdListPtr.list_getSize();
queryVectorPtr.vector_free();
parentQueryVectorPtr.vector_free();
queries = null;
- //vector_free(queryVectorPtr);
- //vector_free(parentQueryVectorPtr);
- //free(queries);
+
int numRecord = adtreePtr.numRecord;
float penalty = (float)(-0.5f * (double)numTotalParent * Math.log((double)numRecord));
return score;
}
-
-
- /* #############################################################################
- * TEST_LEARNER
- * #############################################################################
- */
- /*
-#ifdef TEST_LEARNER
-
-#include <stdio.h>
-
-
-static void
-testPartition (int min, int max, int n)
-{
-int start;
-int stop;
-
-printf("min=%li max=%li, n=%li\n", min, max, n);
-
-int i;
-for (i = 0; i < n; i++) {
-createPartition(min, max, i, n, &start, &stop);
-printf("%li: %li . %li\n", i, start, stop);
-}
-puts("");
-}
-
-
-int
-main (int argc, char* argv[])
-{
-thread_startup(1);
-
-puts("Starting...");
-
-testPartition(0, 4, 8);
-testPartition(0, 15, 8);
-testPartition(3, 103, 7);
-
-int numVar = 56;
-int numRecord = 256;
-
-random_t* randomPtr = random_alloc();
-data_t* dataPtr = data_alloc(numVar, numRecord, randomPtr);
-assert(dataPtr);
-data_generate(dataPtr, 0, 10, 10);
-
-adtree_t* adtreePtr = adtree_alloc();
-assert(adtreePtr);
-adtree_make(adtreePtr, dataPtr);
-
-
-learner_t* learnerPtr = learner_alloc(dataPtr, adtreePtr, 1);
-assert(learnerPtr);
-
-data_free(dataPtr);
-
-learner_run(learnerPtr);
-
-assert(!net_isCycle(learnerPtr.netPtr));
-
-float score = learner_score(learnerPtr);
-printf("score = %lf\n", score);
-
-learner_free(learnerPtr);
-
-puts("Done.");
-
-adtree_free(adtreePtr);
-random_free(randomPtr);
-
-thread_shutdown();
-
-return 0;
-}
-
-#endif // TEST_LEARNER
-*/
-
}
/* =============================================================================
+
+/**
+ * Author: Alokika Dash
+ * University of California, Irvine
+ * adash@uci.edu
+ *
+ * - Helper class for Learner.java
+ **/
public class LearnerTask {
Operation op;
int fromId;
* ============================================================================
*/
-
-/*
-#include <assert.h>
-#include <stdlib.h>
-#include "bitmap.h"
-#include "learner.h"
-#include "list.h"
-#include "net.h"
-#include "operation.h"
-#include "queue.h"
-#include "tm.h"
-#include "vector.h"
-*/
-
#define NET_NODE_MARK_INIT 0
#define NET_NODE_MARK_DONE 1
#define NET_NODE_MARK_TEST 2
public Net() {
}
- /*
- typedef enum net_node_mark {
- NET_NODE_MARK_INIT = 0,
- NET_NODE_MARK_DONE = 1,
- NET_NODE_MARK_TEST = 2,
- } net_node_mark_t;
-
- typedef struct net_node {
- int id;
- list_t* parentIdListPtr;
- list_t* childIdListPtr;
- net_node_mark_t mark;
- } net_node_t;
-
- struct net {
- vector_t* nodeVectorPtr;
- };
- */
-
-
- /* =============================================================================
- * compareId
- * =============================================================================
- */
- /*
- public static int
- compareId (const void* aPtr, const void* bPtr)
- {
- int a = (int)aPtr;
- int b = (int)bPtr;
-
- return (a - b);
- }
- */
-
-
/* =============================================================================
* allocNode
* =============================================================================
{
NetNode nodePtr = new NetNode();
- //nodePtr = (net_node_t*)malloc(sizeof(net_node_t));
- //if (nodePtr) {
- nodePtr.parentIdListPtr = IntList.list_alloc();
- if (nodePtr.parentIdListPtr == null) {
- nodePtr = null;
- return null;
- }
- nodePtr.childIdListPtr = IntList.list_alloc();
- if (nodePtr.childIdListPtr == null) {
- nodePtr.parentIdListPtr.list_free();
- nodePtr = null;
- return null;
+ if (nodePtr != null) {
+ nodePtr.parentIdListPtr = IntList.list_alloc();
+ if (nodePtr.parentIdListPtr == null) {
+ nodePtr = null;
+ return null;
+ }
+ nodePtr.childIdListPtr = IntList.list_alloc();
+ if (nodePtr.childIdListPtr == null) {
+ nodePtr.parentIdListPtr.list_free();
+ nodePtr = null;
+ return null;
+ }
+ nodePtr.id = id;
}
- nodePtr.id = id;
- //}
return nodePtr;
}
public static Net net_alloc (int numNode)
{
Net netPtr = new Net();
- Vector_t nodeVectorPtr = Vector_t.vector_alloc(numNode);
- if (nodeVectorPtr == null) {
- netPtr = null;
- return null;
- }
- //netPtr = (net_t*)malloc(sizeof(net_t));
- //if (netPtr) {
- // vector_t* nodeVectorPtr = vector_alloc(numNode);
- // if (nodeVectorPtr == null) {
- // free(netPtr);
- // return null;
- // }
- // int i;
- for (int i = 0; i < numNode; i++) {
- NetNode nodePtr = allocNode(i);
- //net_node_t* nodePtr = allocNode(i);
- if (nodePtr == null) {
- for (int j = 0; j < i; j++) {
- //nodePtr = (net_node_t*)vector_at(nodeVectorPtr, j);
- nodePtr = (NetNode)(nodeVectorPtr.vector_at(j));
- nodePtr.freeNode();
- }
- nodeVectorPtr.vector_free();
+ if (netPtr != null) {
+ Vector_t nodeVectorPtr = Vector_t.vector_alloc(numNode);
+ if (nodeVectorPtr == null) {
netPtr = null;
- //free(netPtr);
return null;
}
- boolean status = nodeVectorPtr.vector_pushBack(nodePtr);
- //boolean status = vector_pushBack(nodeVectorPtr, (void*)nodePtr);
- //assert(status);
+ 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;
+ }
+
+ boolean status = nodeVectorPtr.vector_pushBack(nodePtr);
+ }
+ netPtr.nodeVectorPtr = nodeVectorPtr;
}
- netPtr.nodeVectorPtr = nodeVectorPtr;
- //}
return netPtr;
}
public void
net_free ()
{
- //int i;
- //vector_t* nodeVectorPtr = netPtr.nodeVectorPtr;
int numNode = nodeVectorPtr.vector_getSize();
for (int i = 0; i < numNode; i++) {
NetNode nodePtr = (NetNode)(nodeVectorPtr.vector_at(i));
- //net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, i);
nodePtr.freeNode();
}
nodeVectorPtr.vector_free();
- //free(netPtr);
}
public void
insertEdge (int fromId, int toId)
{
- //vector_t* nodeVectorPtr = netPtr.nodeVectorPtr;
boolean status;
NetNode childNodePtr = (NetNode)(nodeVectorPtr.vector_at(toId));
IntList parentIdListPtr = childNodePtr.parentIdListPtr;
- //net_node_t* childNodePtr = (net_node_t*)vector_at(nodeVectorPtr, toId);
- //list_t* parentIdListPtr = childNodePtr.parentIdListPtr;
+
if((status = parentIdListPtr.list_insert(fromId)) != true) {
System.out.println("Assert failed for parentIdListPtr.list_insert in insertEdge()");
System.exit(0);
}
- //status = list_insert(parentIdListPtr, (void*)fromId);
- //assert(status);
NetNode parentNodePtr = (NetNode)(nodeVectorPtr.vector_at(fromId));
IntList childIdListPtr = parentNodePtr.childIdListPtr;
- //net_node_t* parentNodePtr = (net_node_t*)vector_at(nodeVectorPtr, fromId);
- //list_t* childIdListPtr = parentNodePtr.childIdListPtr;
+
if((status = childIdListPtr.list_insert(toId)) != true) {
System.out.println("Assert failed for childIdListPtr.list_insert in insertEdge()");
System.exit(0);
}
- //status = list_insert(childIdListPtr, (void*)toId);
- //assert(status);
}
boolean status;
NetNode childNodePtr = (NetNode)(nodeVectorPtr.vector_at(toId));
- //net_node_t* childNodePtr = (net_node_t*)vector_at(nodeVectorPtr, toId);
IntList parentIdListPtr = childNodePtr.parentIdListPtr;
- //list_t* parentIdListPtr = childNodePtr.parentIdListPtr;
status = parentIdListPtr.list_remove(fromId);
if(status == false) {
System.out.println("Assert failed: when removing from list");
System.exit(0);
}
- //assert(status);
NetNode parentNodePtr = (NetNode)(nodeVectorPtr.vector_at(fromId));
- //net_node_t* parentNodePtr = (net_node_t*)vector_at(nodeVectorPtr, fromId);
IntList childIdListPtr = parentNodePtr.childIdListPtr;
- //list_t* childIdListPtr = parentNodePtr.childIdListPtr;
status = childIdListPtr.list_remove(toId);
if(status == false) {
System.out.println("Assert failed: when removing from list");
* =============================================================================
*/
public void
- //reverseEdge (net_t* netPtr, int fromId, int toId)
reverseEdge (int fromId, int toId)
{
removeEdge(fromId, toId);
public boolean
net_hasEdge (int fromId, int toId)
{
- //vector_t* nodeVectorPtr = netPtr.nodeVectorPtr;
NetNode childNodePtr = (NetNode)(nodeVectorPtr.vector_at(toId));
- //net_node_t* childNodePtr = (net_node_t*)vector_at(nodeVectorPtr, toId);
IntList parentIdListPtr = childNodePtr.parentIdListPtr;
IntListNode it = parentIdListPtr.head; //intialize iterator
parentIdListPtr.list_iter_reset(it);
- //list_iter_t it;
- //list_iter_reset(&it, parentIdListPtr);
- //while (list_iter_hasNext(&it, parentIdListPtr)) {
while (parentIdListPtr.list_iter_hasNext(it)) {
it = it.nextPtr;
{
boolean status;
- //Vector_t nodeVectorPtr = netPtr.nodeVectorPtr;
if(visitedBitmapPtr.numBit != nodeVectorPtr.vector_getSize()) {
System.out.println("Assert failed for numbit == vector size in net_isPath()");
System.exit(0);
}
- //vector_t* nodeVectorPtr = netPtr.nodeVectorPtr;
- //assert(visitedBitmapPtr.numBit == vector_getSize(nodeVectorPtr));
visitedBitmapPtr.bitmap_clearAll();
workQueuePtr.queue_clear();
- //bitmap_clearAll(visitedBitmapPtr);
- //queue_clear(workQueuePtr);
if((status = workQueuePtr.queue_push(fromId)) != true) {
System.out.println("Assert failed while inserting into Queue in net_isPath()");
System.exit(0);
}
- //status = queue_push(workQueuePtr, (void*)fromId);
- //assert(status);
while (!workQueuePtr.queue_isEmpty()) {
int id = workQueuePtr.queue_pop();
workQueuePtr.queue_clear();
return true;
}
+
if((status = visitedBitmapPtr.bitmap_set(id)) != true) {
System.out.println("Assert failed while checking bitmap_set in net_isPath()");
System.exit(0);
}
- //status = bitmap_set(visitedBitmapPtr, id);
- //assert(status);
+
NetNode nodePtr = (NetNode) (nodeVectorPtr.vector_at(id));
IntList childIdListPtr = nodePtr.childIdListPtr;
IntListNode it = childIdListPtr.head;
childIdListPtr.list_iter_reset(it);
- //net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, id);
- //list_t* childIdListPtr = nodePtr.childIdListPtr;
- //list_iter_t it;
- //list_iter_reset(&it, childIdListPtr);
- //while (list_iter_hasNext(&it, childIdListPtr))
+
while (childIdListPtr.list_iter_hasNext(it)) {
it = it.nextPtr;
int childId = childIdListPtr.list_iter_next(it);
System.out.println("Assert failed: queue_push failed in net_isPath()");
System.exit(0);
}
- //assert(status);
}
}
}
IntList childIdListPtr = nodePtr.childIdListPtr;
IntListNode it = childIdListPtr.head;
childIdListPtr.list_iter_reset(it);
- //list_iter_t it;
- //list_iter_reset(&it, childIdListPtr);
- //while (list_iter_hasNext(&it, childIdListPtr))
+
while (childIdListPtr.list_iter_hasNext(it)) {
it = it.nextPtr;
int childId = childIdListPtr.list_iter_next(it);
NetNode childNodePtr = (NetNode)(nodeVectorPtr.vector_at(childId));
- //net_node_t* childNodePtr =
- // (net_node_t*)vector_at(nodeVectorPtr, childId);
if (isCycle(nodeVectorPtr, childNodePtr)) {
return true;
}
}
+
} else if(nodePtr.mark == NET_NODE_MARK_TEST) {
return true;
} else if(nodePtr.mark == NET_NODE_MARK_DONE) {
* =============================================================================
*/
public boolean
- //net_isCycle (net_t* netPtr)
net_isCycle ()
{
- //vector_t* nodeVectorPtr = netPtr.nodeVectorPtr;
int numNode = nodeVectorPtr.vector_getSize();
- //int n;
for (int n = 0; n < numNode; n++) {
NetNode nodePtr = (NetNode)(nodeVectorPtr.vector_at(n));
- //net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, n);
nodePtr.mark = NET_NODE_MARK_INIT;
}
for (int n = 0; n < numNode; n++) {
NetNode nodePtr = (NetNode)(nodeVectorPtr.vector_at(n));
- //net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, n);
if(nodePtr.mark == NET_NODE_MARK_INIT) {
if(isCycle(nodeVectorPtr, nodePtr))
return true;
/* do nothing */
;
} else if(nodePtr.mark == NET_NODE_MARK_TEST) {
- /* Assert 0 */
+ /* Assert 0 */
System.out.println("We should have never come here in net_isCycle()");
System.exit(0);
break;
} else {
- /* Assert 0 */
+ /* Assert 0 */
System.out.println("We should have never come here in net_isCycle()");
System.exit(0);
break;
{
boolean status;
- //vector_t* nodeVectorPtr = netPtr.nodeVectorPtr;
-
if(ancestorBitmapPtr.numBit != nodeVectorPtr.vector_getSize()) {
System.out.println("Assert failed for numbit == vector size in net_findAncestors()");
System.exit(0);
}
- //assert(ancestorBitmapPtr.numBit == vector_getSize(nodeVectorPtr));
ancestorBitmapPtr.bitmap_clearAll();
workQueuePtr.queue_clear();
{
NetNode nodePtr = (NetNode)(nodeVectorPtr.vector_at(id));
- //net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, id);
IntList parentIdListPtr = nodePtr.parentIdListPtr;
IntListNode it = parentIdListPtr.head;
parentIdListPtr.list_iter_reset(it);
- //list_t* parentIdListPtr = nodePtr.parentIdListPtr;
- //list_iter_t it;
- //list_iter_reset(&it, parentIdListPtr);
- //while (list_iter_hasNext(&it, parentIdListPtr))
+
while (parentIdListPtr.list_iter_hasNext(it)) {
it = it.nextPtr;
int parentId = parentIdListPtr.list_iter_next(it);
System.out.println("Assert failed: for bitmap_set in net_findAncestors()");
System.exit(0);
}
- //assert(status);
if((status = workQueuePtr.queue_push(parentId)) == false) {
System.out.println("Assert failed: for workQueuePtr.queue_push in net_findAncestors()");
System.exit(0);
}
- //assert(status);
}
+
}
while (!workQueuePtr.queue_isEmpty()) {
return false;
}
NetNode nodePtr = (NetNode)(nodeVectorPtr.vector_at(parentId));
- //net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, parentId);
IntList grandParentIdListPtr = nodePtr.parentIdListPtr;
IntListNode it = grandParentIdListPtr.head;
grandParentIdListPtr.list_iter_reset(it);
- //list_t* grandParentIdListPtr = nodePtr.parentIdListPtr;
- //list_iter_t it;
- //list_iter_reset(&it, grandParentIdListPtr);
+
while (grandParentIdListPtr.list_iter_hasNext(it)) {
it = it.nextPtr;
int grandParentId = grandParentIdListPtr.list_iter_next(it);
System.out.println("Assert failed: for ancestorBitmapPtr bitmap_set in net_findAncestors()");
System.exit(0);
}
- //assert(status);
+
if((status = workQueuePtr.queue_push(grandParentId)) == false) {
System.out.println("Assert failed: for workQueuePtr.queue_push in net_findAncestors()");
System.exit(0);
}
- //assert(status);
}
}
}
IntListNode it = childIdListPtr.head;
childIdListPtr.list_iter_reset(it);
- //net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, id);
- //list_t* childIdListPtr = nodePtr.childIdListPtr;
- //list_iter_t it;
- //list_iter_reset(&it, childIdListPtr);
while (childIdListPtr.list_iter_hasNext(it)) {
it = it.nextPtr;
int childId = childIdListPtr.list_iter_next(it);
System.out.println("Assert failed: for descendantBitmapPtr.bitmap_set in net_findDescendants()");
System.exit(0);
}
- //assert(status);
+
if((status = workQueuePtr.queue_push(childId)) == false) {
System.out.println("Assert failed: for workQueuePtr.queue_push in net_findDescendants()");
System.exit(0);
}
- //assert(status);
+
}
}
IntList grandChildIdListPtr = nodePtr.childIdListPtr;
IntListNode it = grandChildIdListPtr.head;
grandChildIdListPtr.list_iter_reset(it);
- //net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, childId);
- //list_t* grandChildIdListPtr = nodePtr.childIdListPtr;
- //list_iter_t it;
- //list_iter_reset(&it, grandChildIdListPtr);
- //while (list_iter_hasNext(&it, grandChildIdListPtr))
+
while (grandChildIdListPtr.list_iter_hasNext(it)) {
it = it.nextPtr;
int grandChildId = grandChildIdListPtr.list_iter_next(it);
System.out.println("Assert failed: for descendantBitmapPtr.bitmap_set in net_findDescendants()");
System.exit(0);
}
- //assert(status);
+
if((status = workQueuePtr.queue_push(grandChildId)) == false) {
System.out.println("Assert failed: for workQueuePtr.queue_push in net_findDescendants()");
System.exit(0);
}
- //assert(status);
}
}
}
int percentParent,
Random randomPtr)
{
- //Vector_t nodeVectorPtr = netPtr.nodeVectorPtr;
-
int numNode = nodeVectorPtr.vector_getSize();
BitMap visitedBitmapPtr = BitMap.bitmap_alloc(numNode);
if(visitedBitmapPtr == null) {
System.out.println("Assert failed: during bitmap_alloc in net_generateRandomEdges()");
System.exit(0);
}
- //assert(visitedBitmapPtr);
- Queue workQueuePtr = Queue.queue_alloc(-1);
- //Queue* workQueuePtr = queue_alloc(-1);
- //int n;
+ Queue workQueuePtr = Queue.queue_alloc(-1);
for (int n = 0; n < numNode; n++) {
- //int p;
for (int p = 0; p < maxNumParent; p++) {
int value = randomPtr.random_generate() % 100;
if (value < percentParent) {
!net_hasEdge(parent, n) &&
!net_isPath(n, parent, visitedBitmapPtr, workQueuePtr))
{
-#ifdef TEST_NET
- System.out.println("node=" + n + " parent= " + parent);
-#endif
insertEdge(parent, n);
}
}
System.out.println("Assert failed: Cycle detected in net_generateRandomEdges()");
System.exit(0);
}
- //assert(!net_isCycle(netPtr));
visitedBitmapPtr.bitmap_free();
workQueuePtr.queue_free();
}
-
-
- /* #############################################################################
- * TEST_NET
- * #############################################################################
- */
- /*
-#ifdef TEST_NET
-
-#include <stdio.h>
-
-
-int
-main ()
-{
-int numNode = 100;
-
-puts("Starting tests...");
-
-boolean status;
-
-net_t* netPtr = net_alloc(numNode);
-assert(netPtr);
-Bitmap* visitedBitmapPtr = bitmap_alloc(numNode);
-assert(visitedBitmapPtr);
-Queue* workQueuePtr = queue_alloc(-1);
-assert(workQueuePtr);
-
-assert(!net_isCycle(netPtr));
-
-int aId = 31;
-int bId = 14;
-int cId = 5;
-int dId = 92;
-
-net_applyOperation(netPtr, OPERATION_INSERT, aId, bId);
-assert(net_isPath(netPtr, aId, bId, visitedBitmapPtr, workQueuePtr));
-assert(!net_isPath(netPtr, bId, aId, visitedBitmapPtr, workQueuePtr));
-assert(!net_isPath(netPtr, aId, cId, visitedBitmapPtr, workQueuePtr));
-assert(!net_isPath(netPtr, aId, dId, visitedBitmapPtr, workQueuePtr));
-assert(!net_isCycle(netPtr));
-
-net_applyOperation(netPtr, OPERATION_INSERT, bId, cId);
-net_applyOperation(netPtr, OPERATION_INSERT, aId, cId);
-net_applyOperation(netPtr, OPERATION_INSERT, dId, aId);
-assert(!net_isCycle(netPtr));
-net_applyOperation(netPtr, OPERATION_INSERT, cId, dId);
-assert(net_isCycle(netPtr));
-net_applyOperation(netPtr, OPERATION_REVERSE, cId, dId);
-assert(!net_isCycle(netPtr));
-net_applyOperation(netPtr, OPERATION_REVERSE, dId, cId);
-assert(net_isCycle(netPtr));
-assert(net_isPath(netPtr, aId, dId, visitedBitmapPtr, workQueuePtr));
-net_applyOperation(netPtr, OPERATION_REMOVE, cId, dId);
-assert(!net_isPath(netPtr, aId, dId, visitedBitmapPtr, workQueuePtr));
-
-Bitmap* ancestorBitmapPtr = bitmap_alloc(numNode);
-assert(ancestorBitmapPtr);
-status = net_findAncestors(netPtr, cId, ancestorBitmapPtr, workQueuePtr);
-assert(status);
-assert(bitmap_isSet(ancestorBitmapPtr, aId));
-assert(bitmap_isSet(ancestorBitmapPtr, bId));
-assert(bitmap_isSet(ancestorBitmapPtr, dId));
-assert(bitmap_getNumSet(ancestorBitmapPtr) == 3);
-
-Bitmap* descendantBitmapPtr = bitmap_alloc(numNode);
-assert(descendantBitmapPtr);
-status = net_findDescendants(netPtr, aId, descendantBitmapPtr, workQueuePtr);
-assert(status);
-assert(bitmap_isSet(descendantBitmapPtr, bId));
-assert(bitmap_isSet(descendantBitmapPtr, cId));
-assert(bitmap_getNumSet(descendantBitmapPtr) == 2);
-
-bitmap_free(visitedBitmapPtr);
-queue_free(workQueuePtr);
-bitmap_free(ancestorBitmapPtr);
-bitmap_free(descendantBitmapPtr);
- net_free(netPtr);
-
- random_t* randomPtr = random_alloc();
- assert(randomPtr);
- netPtr = net_alloc(numNode);
- assert(netPtr);
- net_generateRandomEdges(netPtr, 10, 10, randomPtr);
- net_free(netPtr);
-
- puts("All tests passed.");
-
- return 0;
}
-
-#endif // TEST_NET
-*/
-
-}
-
-
/* =============================================================================
*
* End of net.java
+/**
+ * Author: Alokika Dash
+ * University of California, Irvine
+ * adash@uci.edu
+ *
+ * - Helper class for Net.java
+ **/
+
public class NetNode {
int id;
int mark;
*
* ------------------------------------------------------------------------
*
- * For the license of kmeans, please see kmeans/LICENSE.kmeans
- *
- * ------------------------------------------------------------------------
- *
- * For the license of ssca2, please see ssca2/COPYRIGHT
- *
- * ------------------------------------------------------------------------
- *
- * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
- * header of the files.
- *
- * ------------------------------------------------------------------------
- *
- * For the license of lib/rbtree.h and lib/rbtree.c, please see
- * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
- *
- * ------------------------------------------------------------------------
- *
* Unless otherwise noted, the following license applies to STAMP files:
*
* Copyright (c) 2007, Stanford University
* =============================================================================
*/
-//#include "sort.h"
-
#define CUTOFF 8
public class Sort {
return;
}
+ /**
+ * Pointers that keep track of
+ * where to start looking in
+ * the base array
+ **/
+ char[] lostk= new char[30];
+ char[] histk= new char[30];
+
+ int stkptr = 0;
+
int lo = start;
int hi = start + (width * (num - 1));
+ int size = 0;
+
+ /**
+ * debug
+ **/
//System.out.println("start= " + start + " base.length= " + base.length + " hi= " + hi);
- recurse(base, lo, hi, width, n, offset);
+ recurse(base, lo, hi, width, n, offset, lostk, histk, stkptr, size);
}
- public void recurse(char[] base, int lo, int hi, int width, int n, int offset)
+ public void recurse(char[] base,
+ int lo,
+ int hi,
+ int width,
+ int n,
+ int offset,
+ char[] lostk,
+ char[] histk,
+ int stkptr,
+ int size)
{
- char[] lostk= new char[30];
- char[] histk= new char[30];
-
- int stkptr = 0;
- int size;
- //recurse:
size = (hi - lo) / width + 1;
if (loguy < hi) {
lo = loguy;
- recurse(base, lo, hi, width, n, offset);
+ recurse(base, lo, hi, width, n, offset, lostk, histk, stkptr, size);
}
} else {
if (loguy < hi) {
}
if (lo + width < higuy) {
hi = higuy - width;
- recurse(base, lo, hi, width, n, offset);
+ recurse(base, lo, hi, width, n, offset, lostk, histk, stkptr, size);
}
}
}
if (stkptr >= 0) {
base[lo] = lostk[stkptr];
base[hi] = histk[stkptr];
- recurse(base, lo, hi, width, n, offset);
+ recurse(base, lo, hi, width, n, offset, lostk, histk, stkptr, size);
}
}
int s1 = p1 + offset;
int s2 = p2 + offset;
- //const char* s1 = (const char*)p1 + offset;
- //const char* s2 = (const char*)p2 + offset;
-
while (i-- > 0) {
char u1 = base[s1];
char u2 = base[s2];
- //unsigned char u1 = (unsigned char)*s1++;
- //unsigned char u2 = (unsigned char)*s2++;
if (u1 != u2) {
- return (u1 - u2); //CAN YOU DO THIS
+ return (u1 - u2); //TODO check this in java
}
}
return 0;
}
}
+
/* =============================================================================
*
* End of sort.java
../common/ListNode.java \
../common/List.java \
../common/QuickSort.java \
- IntVector.java
+ IntVector.java \
+ ../../../ClassLibrary/JavaSTM/Barrier.java
FLAGS=-mainclass ${MAINCLASS} -thread -nooptimize -debug