Exemplo n.º 1
0
Arquivo: vector.c Projeto: takayuki/al
static void
removeInt (vector_t* vectorPtr)
{
    printf("Removing: %li\n", *((long*)vector_popBack(vectorPtr)));
    printVector(vectorPtr);

}
Exemplo n.º 2
0
/* =============================================================================
 * maze_free
 * =============================================================================
 */
void
maze_free (maze_t* mazePtr)
{
    coordinate_t* coordPtr;
    if (mazePtr->gridPtr != NULL) {
        grid_free(mazePtr->gridPtr);
    }
    queue_free(mazePtr->workQueuePtr);
    vector_free(mazePtr->wallVectorPtr);
    while ((coordPtr = (coordinate_t *)vector_popBack (mazePtr->srcVectorPtr)) != NULL) {
        coordinate_free(coordPtr);
    }
    vector_free(mazePtr->srcVectorPtr);
    while ((coordPtr = (coordinate_t *)vector_popBack (mazePtr->dstVectorPtr)) != NULL) {
        coordinate_free(coordPtr);
    }
    vector_free(mazePtr->dstVectorPtr);
    free(mazePtr);
}
Exemplo n.º 3
0
static void
testCount (adtree_t* adtreePtr,
           data_t* dataPtr,
           vector_t* queryVectorPtr,
           long index,
           long numVar)
{
    if (index >= numVar) {
        return;
    }

    long count1 = adtree_getCount(adtreePtr, queryVectorPtr);
    long 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;

    long i;
    for (i = 1; i < numVar; i++) {
        query.index = index + i;
        bool_t 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);
    }
}
Exemplo n.º 4
0
/* =============================================================================
 * data_generate
 * -- Binary variables of random PDFs
 * -- If seed is <0, do not reseed
 * -- Returns random network
 * =============================================================================
 */
net_t*
data_generate (data_t* dataPtr, long seed, long maxNumParent, long percentParent)
{
    random_t* randomPtr = dataPtr->randomPtr;
    if (seed >= 0) {
        random_seed(randomPtr, seed);
    }

    /*
     * Generate random Bayesian network
     */

    long numVar = dataPtr->numVar;
    net_t* netPtr = net_alloc(numVar);
    assert(netPtr);
    net_generateRandomEdges(netPtr, maxNumParent, percentParent, randomPtr);

    /*
     * Create a threshold for each of the possible permutation of variable
     * value instances
     */

    long** thresholdsTable = (long**)SEQ_MALLOC(numVar * sizeof(long*));
    assert(thresholdsTable);
    long v;
    for (v = 0; v < numVar; v++) {
        list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, v);
        long numThreshold = 1 << list_getSize(parentIdListPtr);
        long* thresholds = (long*)SEQ_MALLOC(numThreshold * sizeof(long));
        assert(thresholds);
        long t;
        for (t = 0; t < numThreshold; t++) {
            long threshold = random_generate(randomPtr) % (DATA_PRECISION + 1);
            thresholds[t] = threshold;
        }
        thresholdsTable[v] = thresholds;
    }

    /*
     * Create variable dependency ordering for record generation
     */

    long* order = (long*)SEQ_MALLOC(numVar * sizeof(long));
    assert(order);
    long numOrder = 0;

    queue_t* workQueuePtr = queue_alloc(-1);
    assert(workQueuePtr);

    vector_t* dependencyVectorPtr = vector_alloc(1);
    assert(dependencyVectorPtr);

    bitmap_t* orderedBitmapPtr = bitmap_alloc(numVar);
    assert(orderedBitmapPtr);
    bitmap_clearAll(orderedBitmapPtr);

    bitmap_t* doneBitmapPtr = bitmap_alloc(numVar);
    assert(doneBitmapPtr);
    bitmap_clearAll(doneBitmapPtr);
    v = -1;
    while ((v = bitmap_findClear(doneBitmapPtr, (v + 1))) >= 0) {
        list_t* childIdListPtr = net_getChildIdListPtr(netPtr, v);
        long numChild = list_getSize(childIdListPtr);
        if (numChild == 0) {

            bool status;

            /*
             * Use breadth-first search to find net connected to this leaf
             */

            queue_clear(workQueuePtr);
            status = queue_push(workQueuePtr, (void*)v);
            assert(status);
            while (!queue_isEmpty(workQueuePtr)) {
                long id = (long)queue_pop(workQueuePtr);
                status = bitmap_set(doneBitmapPtr, id);
                assert(status);
                status = vector_pushBack(dependencyVectorPtr, (void*)id);
                assert(status);
                list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, id);
                list_iter_t it;
                list_iter_reset(&it, parentIdListPtr);
                while (list_iter_hasNext(&it, parentIdListPtr)) {
                    long parentId = (long)list_iter_next(&it, parentIdListPtr);
                    status = queue_push(workQueuePtr, (void*)parentId);
                    assert(status);
                }
            }

            /*
             * Create ordering
             */

            long i;
            long n = vector_getSize(dependencyVectorPtr);
            for (i = 0; i < n; i++) {
                long id = (long)vector_popBack(dependencyVectorPtr);
                if (!bitmap_isSet(orderedBitmapPtr, id)) {
                    bitmap_set(orderedBitmapPtr, id);
                    order[numOrder++] = id;
                }
            }

        }
    }
    assert(numOrder == numVar);

    /*
     * Create records
     */

    char* record = dataPtr->records;
    long r;
    long numRecord = dataPtr->numRecord;
    for (r = 0; r < numRecord; r++) {
        long o;
        for (o = 0; o < numOrder; o++) {
            long v = order[o];
            list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, v);
            long index = 0;
            list_iter_t it;
            list_iter_reset(&it, parentIdListPtr);
            while (list_iter_hasNext(&it, parentIdListPtr)) {
                long parentId = (long)list_iter_next(&it, parentIdListPtr);
                long value = record[parentId];
                assert(value != DATA_INIT);
                index = (index << 1) + value;
            }
            long rnd = random_generate(randomPtr) % DATA_PRECISION;
            long threshold = thresholdsTable[v][index];
            record[v] = ((rnd < threshold) ? 1 : 0);
        }
        record += numVar;
        assert(record <= (dataPtr->records + numRecord * numVar));
    }

    /*
     * Clean up
     */

    bitmap_free(doneBitmapPtr);
    bitmap_free(orderedBitmapPtr);
    vector_free(dependencyVectorPtr);
    queue_free(workQueuePtr);
    SEQ_FREE(order);
    for (v = 0; v < numVar; v++) {
        SEQ_FREE(thresholdsTable[v]);
    }
    SEQ_FREE(thresholdsTable);

    return netPtr;
}