unsigned long graphAddGLI(graph *ctx, gateInputType type, bool nin, size_t ID, unsigned int delay) { // if hashmapGet does not return null then there is already a gate with the desired ID. if ((GLI*) hashmapGet(ctx->GIDMap, ID)) return -1; //Give up. GLI *gli = (GLI*) malloc(sizeof(GLI)); gli->ID = ID; // set ID. // TODO: Generate Better IDs gli->state = DTKNOW; // Lets be honest about this. // Just copy this across. gli->delay = delay; gli->inputMode = type; gli->inputNegate = nin; gli->seen = false; gli->lastUpdated = -1; gli->updatedBy = -1; // Push gli into the map hashmapInsert(ctx->GIDMap, (void*)gli, gli->ID); fastlistAdd(ctx->nodes, (void*)gli); // Create Connection Lists for SRC and DRN for this gate fastlist * srcList = fastlistCreate(CONN_LIST_SIZE); //TODO: safety this; hashmapInsert(ctx->srcMap, (void*)srcList, gli->ID); fastlist *drnList = fastlistCreate(CONN_LIST_SIZE); //TODO: safety this; hashmapInsert(ctx->drnMap, (void*)drnList, gli->ID); //Construction complete, cleanup; ctx->nodeCount++; return gli->ID; // gli goes out of scope here. (psst, don't tell anyone the ID is a pointer) };
unsigned long graphAddConnection(graph *ctx, size_t src, size_t drn) { // get src and drn GLI* srcGli = (GLI*) hashmapGet(ctx->GIDMap, src); GLI* drnGli = (GLI*) hashmapGet(ctx->GIDMap, drn); //if the drn of this connection is an input gate no dice. if (drnGli->inputMode == INPUT) return -1; //fail //if the src of this connection is an output no dice. if (srcGli->inputMode == OUTPUT) return -1; // if the drn of this connection is in UNITY or output mode it can only accept 1 input if ((drnGli->inputMode == UNITY) || (drnGli->inputMode == OUTPUT) ) { // get the list of inputs to the drn. fastlist *drnInputs = (fastlist*) hashmapGet(ctx->drnMap, drn); // if the list is not currently empty then fail. if (fastlistSize(drnInputs)) return -1; } // TODO: safety this connection *conn = (connection*) malloc(sizeof(connection)); conn->ID = (size_t) conn; // We're using the pointer as a UUID, as we don't have a generator. // TODO: Generate Better IDs conn->srcEp = srcGli; conn->drnEp = drnGli; conn->srcID = src; conn->drnID = drn; // insert into Connection ID map; hashmapInsert(ctx->CIDMap, (void*)conn, conn->ID); fastlistAdd(ctx->connections, (void*)conn); // add the Connection to the srcList; fastlist *srcList = (fastlist*) hashmapGet(ctx->srcMap, src); fastlistAdd(srcList, (void*)conn); // add the Connection to the drnList; fastlist *drnList = (fastlist*) hashmapGet(ctx->drnMap, drn); fastlistAdd(drnList, (void*)conn); // done, cleanup return conn->ID; };
pNode pNodeSearch(hashmap *phmap,unsigned long aid) { pNode p; p = (pNode)hashmapGet(phmap,aid); if(p == NULL) { //printf("#3");fflush(NULL); pAlistAdd(aid); //printf("#4");fflush(NULL); p = (pNode)malloc(sizeof(aNode)); //printf("#5");fflush(NULL); p->len = 1; //printf("#6");fflush(NULL); p->aid[0] = aid; //printf("#7");fflush(NULL); hashmapInsert(phmap,p,aid); //printf("#8");fflush(NULL); } return p; }
int hashmap_test(void) { printf("Starting hashmap tests...\n\n"); hashmap* hmap = hashmapCreate(20); if (hashmapCount(hmap) != 0){ printf("\nTest failed. Hashmap does not initialize to empty."); return -1; } hashmapInsert(hmap, 1, 1); if (hashmapCount(hmap) != 1){ printf("\nTest failed. Hashmap does not have proper count of elements."); return -1; } int test = 0; test = (int *) hashmapGet(hmap, 1); if (test != 1){ printf("\nTest failed. Value found inside was actually %d ", test); return -1; } hashmapInsert(hmap, "Word", 2); char* word; word = (char*) hashmapGet(hmap, 2); if (strcmp(word, "Word") != 0){ printf("\nTest failed. The word found inside was actually %s", word); return -1; } test = (int*) hashmapRemove(hmap, 1); printf("\nTest 5: "); if (test == -1){ printf("\nTest failed. HASHMAPERROR returned."); return -1; } if (hashmapCount(hmap) != 1){ printf("\nTest failed. Hashmap value not actually removed"); return -1; } printf("\nTest 6: "); hashmapInsert(hmap, 2, 1); test = (int *) hashmapGet(hmap, 1); if (test != 2){ printf("\nTest failed. Value was not inserted."); return -1; } hashmapInsert(hmap, 7, 1); test = (int *) hashmapGet(hmap, 1); if (test != 7){ printf("\nTest failed. Value was not replaced."); return -1; } hashmapInsert(hmap, 'd', 13); char c = (char) hashmapGet(hmap, 13); if (c != 'd'){ printf("\nTest failed. Char 'd' was not properly inserted"); return -1; } hashmapInsert(hmap, 9, 4294967295); test = (int *) hashmapGet(hmap, 4294967295); if (test != 9){ printf("\nTest failed. 9 was not inserted"); return -1; } test = hashmapRemove(hmap, 11); if (test != -1){ printf("\nTest failed. ERROR should be returned."); return -1; } test = hashmapGet(hmap, 11); if (test != -1){ printf("\nTest failed. ERROR should be returned."); return -1; } hashmapDelete(hmap); hashmap* hmap2 = hashmapCreate(10); int x = 0; while(x < 200){ hashmapInsert(hmap2, x, x); int test2 = (int*) hashmapGet(hmap, x); if (test2 != x){ printf("\nTest failed. Improper value retrieved."); return -1; } x++; } x = 0; while (x< 200){ hashmapGet(hmap, 1); int test2 = (int*) hashmapGet(hmap, 1); if (test2 != 1){ printf("\nTest failed. Constant retrieval does not work."); return -1; } x++; } x = 0; while (x < 200){ int test2 = (int*) hashmapRemove(hmap, x); if (test2 == -1){ printf("\nTest failed. ERROR was thrown, when it shouldn't have been."); return -1; } x++; } printf("\n\nAll hashmap tests passed!\n"); hashmapDelete(hmap2); return 0; }