Exemple #1
0
int main(int argc, char** argv)
{
    int score, round;
    d = DictCreate();

    /*
    DictInsert(d, "foo", "hello world");
    puts(DictSearch(d, "foo"));
    DictInsert(d, "foo", "hello world2");
    puts(DictSearch(d, "foo"));
    DictDelete(d, "foo");
    puts(DictSearch(d, "foo"));
    DictDelete(d, "foo");
    assert(DictSearch(d, "foo") == 0);
    DictDelete(d, "foo");

    for(i = 0; i < 10000; i++) {
      sprintf(buf, "%d", i);
      DictInsert(d, buf, buf);
    }
    */

    round = atoi(argv[1]);
    score = atoi(argv[2]);

    printf("%f\n", scoreProb(round, score));

    DictDestroy(d);

    return 0;
}
Exemple #2
0
static void grow(Dict d)
{
    Dict d2;            /* new dictionary we'll create */
    struct dict swap;   /* temporary structure for brain transplant */
    int i;
    struct elt *e;

    d2 = internalDictCreate(d->size * GROWTH_FACTOR);

    for(i = 0; i < d->size; i++) {
        for(e = d->table[i]; e != 0; e = e->next) {
            /* note: this recopies everything */
            /* a more efficient implementation would
             * patch out the strdups inside DictInsert
             * to avoid this problem */
            DictInsert(d2, e->key, e->value, e->dist);
        }
    }

    /* the hideous part */
    /* We'll swap the guts of d and d2 */
    /* then call DictDestroy on d2 */
    swap = *d;
    *d = *d2;
    *d2 = swap;

    DictDestroy(d2);
}
Exemple #3
0
int
main(int argc, char* argv[])
{
    Dict d;
    int i;
    char a = 96;
    //int code = 0;
    int MAXBITS = 12;
    int pused = 2;
    int pref = 0;
    if (argc == 2){
        MAXBITS = atoi(argv[1]);
    }
    d = internalDictCreate(1 << MAXBITS);

    printf("%d", d->size);

    for(i = 0; i < 8; i++) {
        a++;
        pref =(int) a + i;
        if(d->n >= d->size) {
            printf("%s\n", "Table filled, grow");
            //exit(1);
            Dictgrow(d);
        }       
        DictInsert(d, pref, a, i);
        DictUse(d, pref, a, i);
        if (i % 2 == 0){
            DictUse(d, pref, a, i);
        }
        printf("Found (%d, %d) at code %d\n", pref, a, i);

    }
    DictPrune(d, pused);
    DictInsert(d, 105, 'e', 4);
    FILE *fp = fopen("table", "w");
    DicttoFile(d, fp);

    /*printf("%d", d->size);

    for(i = 0; i < 26; i++) {
        a++;
        code = (int) a + i;
        if(d->n >= d->size) {
            printf("%s\n", "Table filled, grow by x2");
            Dictgrow(d);
        }
        DictInsert(d, i, (int) a, code);
        printf("Found (%d, %d) with code %d\n", i, a, DictSearch(d, i, a));
    }
    FILE *fp = fopen("table", "w");
    DicttoFile(d, fp);
    DictDelete(d, 7, 104);
    assert(DictSearch(d, 7, 104) == -1);
    a=96;
    for(i = 0; i < 26; i++) {
        a++;
        if (DictSearch(d, i, (int)a) != -1){
            printf("Found (%d, %d) with code %d\n", i, a, DictSearch(d, i, a));
        }
        else{
            printf("Did not find (%d, %d)\n", i, a);
        }
    }*/

    DictDestroy(d);
    printf("%s\n", "Dictionary is empty");

    return 0;
}
Exemple #4
0
int search(struct position source, struct position target, int (*blocked)(struct position))
{
    struct position current;
    current = source;

    Dict d;
    d = DictCreate();

    PQ* q;
    q = initQueue();

    heapNode hn;
    hn.distFromSource = 0;
    hn.value = abs(current.x - target.x) + abs(current.y - target.y);
    hn.pos = current;
    enqueue(hn,q);
    DictInsert(d,current.x,current.y,hn.distFromSource);

    while(q->size)	//stops when priority queue is empty
    {
        int i;
        hn = dequeue(q);
        current = hn.pos;
        if(foundTarget(current,target))	//hooray! target found!!
        {
            DictDestroy(d);
            destroyQueue(q);
            return hn.distFromSource;
        }
        for(i = 0; i < 4; i++) //for loop explores all the four neighbors defined as 0...3
        {
            struct position neighbor = createNeighbor(current,i);
            int dictSearchDist = DictSearch(d,neighbor.x,neighbor.y);
            if(!blocked(neighbor) && dictSearchDist < 0)	//add the neighbor to PQ
            {
                DictInsert(d,neighbor.x,neighbor.y,hn.distFromSource + 1);
                heapNode node;
                int distToTarget = abs(neighbor.x - target.x) + abs(neighbor.y - target.y);	//manhattan distance
                node.value = (hn.distFromSource + 1) + distToTarget;
                node.pos = neighbor;
                node.distFromSource = hn.distFromSource + 1;
                enqueue(node,q);
            }
            else if(dictSearchDist >= 0)
            {
                if(dictSearchDist > hn.distFromSource + 1)
                {
                    DictInsert(d,neighbor.x,neighbor.y,hn.distFromSource + 1);
                    heapNode node;
                    int distToTarget = abs(neighbor.x - target.x) + abs(neighbor.y - target.y);	//manhattan distance
                    node.value = (hn.distFromSource + 1) + distToTarget;
                    node.pos = neighbor;
                    node.distFromSource = hn.distFromSource + 1;
                    enqueue(node,q);
                }
            }
        }
    }
    DictDestroy(d);
    destroyQueue(q);
    return NO_PATH;
}