Example #1
0
/*************************************************************************
 * Destroy ReadBuffer: close file and free memory.
 */
void ReadBuffer_destroy(ReadBuffer *rb) {
    Array_destroy(&(rb->buffer));
    free(rb->inFile);
    if(rb->keepOpen) {
        fclose(rb->inFileDesc);
    }
}
Example #2
0
void testArray() {
    int n = 100;
    // make
    Array a = Array_make(sizeof(int), n);
    
    // set and get
    int i;
    for(i=0; i<n; i++) {
        Array_set(&a, i, &i);
    }
    for(i=0; i<n; i++) {
        int* val = (int*)Array_get(&a, i);
        assert(*val == i);
    }

    // save and load
    char* fname = "/tmp/test.array";
    Array_fileSave(&a, fname);
    Array b = Array_make(sizeof(int), n);
    Array_fileLoad(&b, fname);
    for(i=0; i<n; i++) {
        int* val = (int*)Array_get(&b, i);
        assert(*val == i);
    }

    // make init
    int buf[n];
    for(i=0; i<n; i++) {
        buf[i] = i;
    }
    Array c = Array_makeInit(sizeof(int), n, &buf);
    for(i=0; i<n; i++) {
        int* val = (int*)Array_get(&c, i);
        assert(*val == i);
    }

    Array_destroy(&a);
    Array_destroy(&b);
    Array_destroy(&c);

    printf("ARRAY PASSED TESTS\n");
}
Example #3
0
// Same as the map function above, but also updates the values of the
// input array.
void RoomyList_mapAndModify(RoomyList *rl,
                     void (*mapFunc)(void* oldVal, void* newValOut)) {
    // if list is locally empty, return without mapping
    if(rl->lSize == 0) return;

    char listFile[RGLO_STR_SIZE];
    RoomyList_getListFileName(rl, RGLO_MY_RANK, listFile);
    uint64 bytesLeft = numBytesInFile(listFile);
    uint64 chunkSize = RGLO_BUFFER_SIZE;
    chunkSize = chunkSize - (chunkSize % rl->bytesPer);
    uint64 arraySize = chunkSize / rl->bytesPer;
    Array chunk = Array_make(rl->bytesPer, arraySize);
    FILE* f = fopen(listFile, "a+");
    uint64 filePos = 0;
    void* newVal = malloc(rl->bytesPer);
    while(bytesLeft) {
        // load chunk
        uint64 curChunkSize = chunkSize;
        if(curChunkSize > bytesLeft) curChunkSize = bytesLeft;
        uint64 curArraySize = curChunkSize / rl->bytesPer;
        fseek(f, filePos, SEEK_SET);
        Array_fileDescLoad(&chunk, f, curArraySize);

        // apply map function, with modifications
        int i;
        for(i=0; i<curArraySize; i++) {
            mapFunc(Array_get(&chunk, i), newVal);
            
            // keep track of predicate changes
            int p;
            for(p=0; p<rl->numPredicates; p++) {
                rl->predicateVals[p] += rl->predFuncs[p](newVal) -
                                        rl->predFuncs[p](Array_get(&chunk, i));
            }

            Array_set(&chunk, i, newVal);
        }

        // save chunk
        fseek(f, filePos, SEEK_SET);
        Array_fileDescSave(&chunk, f, curArraySize);
        
        filePos += curChunkSize;
        bytesLeft -= curChunkSize;
    }
    
    // clean up
    Array_destroy(&chunk);
    free(newVal);
    fclose(f);
}
char *test_destroy()
{
  Array_destroy(array);

  return NULL;
}