Map *idtCreate() { Map *idt = mapCreate(sizeof(Instr), IDT_NUM_BUCKETS, (unsigned (*)(void *))djb2, // Hash function (int (*)(void *a, void *b))strcmp); // Comparison function // Real instructions Instr real_ins; real_ins.type = ins_t_real; for (int i = 0; i < NUM_REAL_INSTR; i++) { real_ins.data.real = real_md[i]; mapInsert(idt, real_symbols[i], strlen(real_symbols[i]) + 1, &real_ins); } // Pseudo instructions Instr pseudo_ins; pseudo_ins.type = ins_t_pseudo; for (int i = 0; i < NUM_PSEUDO_INSTR; i++) { pseudo_ins.data.pseudo = pseudo_instr[i]; mapInsert(idt, pseudo_symbols[i], strlen(pseudo_symbols[i]) + 1, &pseudo_ins); } return idt; }
/** * Performs the first pass of the assembly, reading the code and creating a sym- * bol table, containing each label in the code. * @param in Pointer to the file containing the code. It should already have * been opened in "r" mode and should point to the beginning of the * file. * @param idt Instruction Data Table created by idtCreate (idt.h) * @return Address to the newly created symbol table. */ Map *asmBuildSymTable(FILE *in, Map *idt){ Map *sym_table = mapCreate(sizeof(int), NUM_BUCKETS, (unsigned (*)(void *))djb2, // Hash function (int (*)(void *a, void *b))strcmp // Comparison function ); Line l; Instr ins; int ilc = 0; char buf[BUF_LEN + 1]; while (fgets(buf, BUF_LEN, in)){ parseLine(buf, &l); if (l.label){ mapInsert(sym_table, l.label, strlen(l.label) + 1, &ilc); } if (l.instr){ int get_status = mapGet(idt, l.instr, &ins); if (get_status == 1){ fprintf(stderr, "Invalid instruction: %s " "(ILC: %d)\n", l.instr, ilc); exit(1); } if (ins.type == ins_t_real){ ilc += 1 + ins.data.real.num_ops; } else { ilc += ins.data.pseudo.ilc_inc; } } } return sym_table; }
int main() { FILE *fin = fopen("gift1.in", "r"); FILE *fout = fopen("gift1.out", "w"); int number; char *name[10]; fscanf(fin, "%d", &number); int i; for(i = 0; i<number; i++){ name[i] = (char *)malloc(15 * sizeof(char)); fscanf(fin, "%s", name[i]); mapInsert(name[i]); } char giverName[15]; int j = 0; while(fscanf(fin, "%s", giverName) > 0){ //printf("---%s---\n", giverName); //printf("Number %d round: ", ++j); char *receiveName[10]; int moneyToGive; int person; fscanf(fin, "%d %d", &moneyToGive, &person); if(person == 0) continue; //printf("%s gives %d to %d person\n", giverName, moneyToGive, person); int i; for(i = 0; i < person; i++){ receiveName[i] = (char *)malloc(14 * sizeof(char)); fscanf(fin, "%s", receiveName[i]); struct map *cur; cur = mapSearch(receiveName[i]); add(cur, moneyToGive/person); ///printf("%s receive %d\n", receiveName[i], moneyToGive/person); free(receiveName[i]); } //printf("%s gives %d to\n", giverName, moneyToGive); minus(mapSearch(giverName), moneyToGive); moneyToGive = moneyToGive - moneyToGive / person * person; add(mapSearch(giverName), moneyToGive); } struct map *temp = head; while(temp){ fprintf(fout, "%s %d\n", temp->name, temp->money); temp = temp->next; } fclose(fin); fclose(fout); }
static char *map_test(){ int map_len = 8; // Initialization test Map *m = mapCreate(sizeof(int), map_len, (unsigned int (*)(void *))djb2, (int(*)(void *a, void *b))strcmp); mu_assert("m == NULL", m != NULL); mu_assert("m->len != 8", m->len == 8); // Insertion tests char *key = "sprite"; int val = 33; mapInsert(m, key, strlen(key) + 1, &val); unsigned int bucket_id = djb2((unsigned char *)key) % map_len; Bucket *b = m->bs[bucket_id]; mu_assert("bucket is empty", b->tail != b->head); mu_assert("bucket->last->data != val", *(int *)b->tail->data == val); // Retrieval tests int x = 12, get_status; get_status = mapGet(m, key, &x); mu_assert("x has the wrong value", x == val); mu_assert("get_status != 0", get_status == 0); // Removal tests int y = 44, pop_status; pop_status = mapPop(m, key, &y, free); mu_assert("c has the wrong value", y == val); mu_assert("pop_status != 0", pop_status == 0); mu_assert("bucket is not empty", b->tail == b->head); mapDestroy(m, free); return 0; }
int main() { map *p = NULL; size_t cap, tSize; int objType,keyType; int objTypeSize, keyTypeSize; void *data = malloc(sizeof(double)*10); void *data2 = malloc(sizeof(double)*10); double double_neg_inf = DOUBLE_NEG_INF; short short_neg_inf = SHORT_NEG_INF; mapit it = NULL, rit=NULL; while(1){ char cmd[10240] = {0}; int ret; if(p == NULL){ printf("The map needs to be initialized.\n"); printf("Init step 1. Key Type: d for double, others for short.\n"); scanf("%s", cmd); keyType = (cmd[0] == 'd')?__DS__DOUBLE__:__DS__SHORT__; printf("Init step 2. Value Type: d for double, others for short.\n"); scanf("%s", cmd); objType = (cmd[0] == 'd')?__DS__DOUBLE__:__DS__SHORT__; printf("Init step 3. Capacity: \n"); scanf("%zu", &cap); p = mapAlloc(); if(p == NULL) ret = __DS__MAP__OUT_OF_MEM__; else{ objTypeSize = (objType==__DS__DOUBLE__)? sizeof(double): sizeof(short); if(keyType == __DS__DOUBLE__) ret = mapInit(p, sizeof(double), objTypeSize, cap, doubleGT); else ret = mapInit(p, sizeof(short), objTypeSize, cap, shortGT); } if(ret != __DS__MAP__NORMAL__){ printf("Not enough memory.\n"); if(p != NULL) mapFree(p); p = NULL; } else{ printf("Initialization done.\n"); } }else{ int choice = 0; printf("size/capacity: %zu/%zu\n", mapSize(p), mapCap(p)); printf("Valid operation: 1)insert, 2)delete, 3)get iterator, 4)set\n"); printf(" 5)update by it, 6)get order, 7)next, 8)pre, 9)get by iterator\n"); printf(" 10)empty, 11)free, 12)quit\n"); while(choice <= 0 || choice > 12){ scanf("%s", cmd); sscanf(cmd, "%d", &choice); } if (choice == 1){//insert printf("input a %s-type key:", (keyType == __DS__DOUBLE__)?"double":"short"); getData(keyType, data); printf("input a %s-type value:", (objType == __DS__DOUBLE__)?"double":"short"); getData(objType, data2); ret = mapInsert(p, data, data2); if(ret==__DS__MAP__FULL__)printf("Map is full!\n"); else if(ret==__DS__MAP__OBJ_EXIST__)printf("Element already exists!\n"); else if(ret==__DS__MAP__OUT_OF_MEM__)printf("Run out of memory\n"); else if(ret==__DS__MAP__NORMAL__)printf("Insert OK\n"); } else if(choice == 2){//delete printf("input a %s value:", (keyType == __DS__DOUBLE__)?"double":"short"); getData(keyType, data); ret = mapDelete(p, data); if(ret==__DS__MAP__OBJ_NOT_EXIST__){ printf("Element is not in the set.\n"); } else printf("Delete OK\n"); } else if (choice == 3){//get it printf("input a %s value:", (keyType == __DS__DOUBLE__)?"double":"short"); getData(keyType, data); ret = mapGetIt(p, data, &it); if(ret == __DS__MAP__OBJ_EXIST__){ printf("Get iterator OK!"); printf(".\n"); } else{ printf("The key "); printData(keyType, data); printf(" is not in the map.\n"); } } else if(choice == 4){//set by it printf("input the value to set:"); getData( objType, data2); ret = mapSetByIt(it, data2); if(ret == __DS__MAP__NORMAL__){ printf("Set OK!\n"); } else printf("Invalid It\n"); } else if(choice == 5){//update by it printf("input the key to set:"); getData( objType, data); printf("input the value to set:"); getData( objType, data2); ret = mapUpdateByIt(it, data, data2); if(ret == __DS__MAP__NORMAL__) { printf("Update OK!\n"); }else { printf("Invalid It\n"); } } else if (choice == 6){//get order int order; printf("input the order:"); scanf("%zu", &order); ret = mapGetOrderIt(p, order, data, data2, &it); if(ret == __DS__MAP__NORMAL__){ printf("The result key is "); printData( keyType, data); printf(".\n"); printf("The result value is "); printData( objType, data2); printf(".\n"); } else printf("No element found\n"); } else if(choice == 7){//next rit = mapNextIt(it); if(rit!=NULL) printf("Get Next OK!\n"); else printf("Cannot get next\n"); mapFreeIt(it); it = rit; } else if(choice == 8){//pre rit = mapPrevIt(it); if(rit!=NULL) printf("Get Priv OK!\n"); else printf("Cannot get priv\n"); mapFreeIt(it); it = rit; } else if(choice == 9){//get element by it ret = mapGetByIt(it,data,data2); if(ret==__DS__MAP__NORMAL__) { printf("The key of curremt it :"); printData( keyType, data); printf("\nThe value of curremt it :"); printData( objType, data2); printf("\n"); }else { printf("Invalid Iterator\n"); } } else if(choice == 10){//empty if(mapEmpty(p)==__DS__MAP__EMPTY__) printf("The map is empty.\n"); else printf("The map is not empty.\n"); } else if(choice == 11){ mapFree(p); p = NULL; } else if(choice == 12) break; } } }
static void como_execute(ComoFrame *frame, ComoFrame *callingframe) { size_t i; for(i = 0; i < O_AVAL(frame->code)->size; i++) { ComoOpCode *opcode = ((ComoOpCode *)(O_PTVAL(O_AVAL(frame->code)->table[i]))); switch(opcode->op_code) { default: { como_error_noreturn("Invalid OpCode got %d", opcode->op_code); } case POSTFIX_INC: { Object *value = NULL; value = mapSearchEx(frame->cf_symtab, O_SVAL(opcode->operand)->value); if(value == NULL) { como_error_noreturn("undefined variable '%s'", O_SVAL(opcode->operand)->value); } else { if(O_TYPE(value) != IS_LONG) { como_error_noreturn("unsupported value for POSTFIX_INC"); } else { long oldvalue = O_LVAL(value); O_LVAL(value) = oldvalue + 1; push(frame, newLong(oldvalue)); } } break; } case POSTFIX_DEC: { Object *value = NULL; value = mapSearchEx(frame->cf_symtab, O_SVAL(opcode->operand)->value); if(value == NULL) { como_error_noreturn("undefined variable '%s'", O_SVAL(opcode->operand)->value); } else { if(O_TYPE(value) != IS_LONG) { como_error_noreturn("unsupported value for POSTFIX_DEC"); } else { long oldvalue = O_LVAL(value); O_LVAL(value) = oldvalue - 1; push(frame, newLong(oldvalue)); } } break; } case IS_LESS_THAN: { Object *right = pop(frame); Object *left = pop(frame); assert(right); assert(left); if(objectValueIsLessThan(left, right)) { push(frame, newLong(1L)); } else { push(frame, newLong(0L)); } break; } case IADD: { Object *right = pop(frame); Object *left = pop(frame); assert(right); assert(left); if(O_TYPE(left) == IS_LONG && O_TYPE(right) == IS_LONG) { long value = O_LVAL(left) + O_LVAL(right); push(frame, newLong(value)); } else { char *left_str = objectToString(left); char *right_str = objectToString(right); Object *s1 = newString(left_str); Object *s2 = newString(right_str); Object *value = stringCat(s1, s2); push(frame, value); objectDestroy(s1); objectDestroy(s2); free(left_str); free(right_str); } break; } case IMINUS: { Object *right = pop(frame); Object *left = pop(frame); assert(right); assert(left); if(O_TYPE(left) != IS_LONG && O_TYPE(right) != IS_LONG) { como_error_noreturn("unsupported value for IMINUS"); } else { push(frame, newLong(O_LVAL(left) - O_LVAL(right))); } break; } case IS_LESS_THAN_OR_EQUAL: { Object *right = pop(frame); Object *left = pop(frame); assert(right); assert(left); if(objectValueCompare(left, right) || objectValueIsLessThan(left, right)) { push(frame, newLong(1L)); } else { push(frame, newLong(0L)); } break; } case JZ: { Object *cond = pop(frame); if(O_TYPE(cond) == IS_LONG && O_LVAL(cond) == 0) { i = (size_t)O_LVAL(opcode->operand); continue; } break; } case JMP: { i = O_LVAL(opcode->operand); continue; } case LABEL: { break; } case HALT: { break; } case IS_NOT_EQUAL: { Object *right = pop(frame); Object *left = pop(frame); if(!objectValueCompare(left, right)) { push(frame, newLong(1L)); } else { push(frame, newLong(0L)); } break; } case LOAD_CONST: { como_debug("LOAD_CONST"); push(frame, opcode->operand); break; } case STORE_NAME: { Object *value = pop(frame); mapInsertEx(frame->cf_symtab, O_SVAL(opcode->operand)->value, value); break; } /* This is where recursion was broken, don't do *ex */ case LOAD_NAME: { Object *value = NULL; value = mapSearch(frame->cf_symtab, O_SVAL(opcode->operand)->value); if(value) { goto load_name_leave; } else { value = mapSearch(global_frame->cf_symtab, O_SVAL(opcode->operand)->value); } if(value == NULL) { como_error_noreturn("undefined variable '%s'", O_SVAL(opcode->operand)->value); } load_name_leave: push(frame, value); break; } case CALL_FUNCTION: { Object *fn = pop(frame); Object *argcount = pop(frame); long i = O_LVAL(argcount); ComoFrame *fnframe; if(O_TYPE(fn) != IS_POINTER) { como_error_noreturn("name '%s' is not callable", O_SVAL(opcode->operand)->value); } fnframe = (ComoFrame *)O_PTVAL(fn); if(O_LVAL(argcount) != (long)(O_AVAL(fnframe->namedparameters)->size)) { como_error_noreturn("callable '%s' expects %ld arguments, but %ld were given", O_SVAL(opcode->operand)->value, (long)(O_AVAL(fnframe->namedparameters)->size), O_LVAL(argcount)); } //como_debug("calling '%s'", O_SVAL(opcode->operand)->value); // DOING THIS ACTUALLY DEFINES THE NAME AT RUNTIME // which could not be equal to that actual function body // declared // name = my_function // name() // that call will have "name" for value __FUNCTION__ // even though the real function is my_function // must define it at COMPILE time // mapInsertEx(fnframe->cf_symtab, "__FUNCTION__", // newString(O_SVAL(opcode->operand)->value)); while(i--) { como_debug("getting %ldth argument for function call '%s'", i, O_SVAL(opcode->operand)->value); Object *argname = O_AVAL(fnframe->namedparameters)->table[i]; Object *argvalue = pop(frame); mapInsert(fnframe->cf_symtab, O_SVAL(argname)->value, argvalue); char *argvaluestr = objectToString(argvalue); como_debug("%ldth argument: '%s' has value: %s", i, O_SVAL(argname)->value, argvaluestr); free(argvaluestr); } //ComoFrame *prev = frame; //fnframe->next = prev; como_execute(fnframe, NULL); push(frame, pop(fnframe)); //fnframe->next = NULL; break; } case IS_EQUAL: { Object *right = pop(frame); Object *left = pop(frame); push(frame, newLong((long)objectValueCompare(left, right))); break; } case ITIMES: { Object *right = pop(frame); Object *left = pop(frame); assert(right); assert(left); if(O_TYPE(right) != IS_LONG && O_TYPE(left) != IS_LONG) { como_error_noreturn("invalid operands for ITIMES"); } como_debug("ITIMES: %d, %d", O_TYPE(left), O_TYPE(right)); long value = O_LVAL(left) * O_LVAL(right); push(frame, newLong(value)); break; } case IRETURN: { /* If there wasn't a return statement found in func body* * The compiler will insert a 1 as the operand if * the AST had an expression for the return statement, * otherwise, it will be 0 * The actual value to be returned is popped from the stack */ if(! (O_LVAL(opcode->operand))) { push(frame, newLong(0L)); } return; } case IPRINT: { Object *value = pop(frame); size_t len = 0; char *sval = objectToStringLength(value, &len); fprintf(stdout, "%s\n", sval); fflush(stdout); free(sval); break; } } } }
int main(int argc, char **argv){ //check usage if(argc != 4){ fprintf(stderr, "usage: \"./pagerank\" damping_factor diffPR maxIterations"); return EXIT_FAILURE; } //initialise i/o FILE *fin = fopen("collection.txt","r"); FILE *fout = fopen("pagerankList.txt","w"); double d, diffPR; int maxIterations; slist urls = newList((void*)strdup, free); d = atof(argv[1]); diffPR = atof(argv[2]); maxIterations = atoi(argv[3]); //read collection.txt char buffer[BUFF_SIZE]; while(fscanf(fin, "%s", buffer) != EOF){ assert(buffer[0]); listEnter(urls, buffer); } int len = listLength(urls); Url *aurls = malloc(len * sizeof(url)); Graph g = newGraph(len); Hashmap m = newHashmap((len*3)/2); //convert llist to array for faster reading int i; for(i = 0; i < len; i++){ aurls[i] = malloc(sizeof(url)); aurls[i]->name = (char*)readList(urls); aurls[i]->pRank = (1/(double)len); aurls[i]->notActuallyZero = 1; mapInsert(m, aurls[i]->name, i); listNext(urls); } listReset(urls); //build graph for(i = 0; i < len; i++){ strcpy(buffer, aurls[i]->name); strcat(buffer, ".txt"); FILE *webpage = fopen(buffer, "r"); char *seen = calloc(len, sizeof(char)); int nOutgoingLinks = 0; fscanf(webpage, "#start Section-1"); while (fscanf(webpage, "%s", buffer) != EOF){ if(!strcmp(buffer, "#end")){ break; } int j = mapSearch(m, buffer); if(j != -1 && j != i && !seen[j]){ insertEdge(g,j,i); seen[j] = 1; nOutgoingLinks++; } } if(!nOutgoingLinks){ int j; for(j = 0; j < len; j++){ if(j != i) { insertEdge(g, j, i); } } nOutgoingLinks = len - 1; aurls[i]->notActuallyZero = 0; } aurls[i]->outdeg = nOutgoingLinks; fclose(webpage); free(seen); } dropMap(m); //calculate pagerank double diff = diffPR, *newPRanks = malloc(len * sizeof(double)); for(i = 0; i < maxIterations && diff > diffPR - EPS; i++){ diff = 0; int j; for(j = 0; j < len; j++){ double sum = 0; slist inUrls; for(inUrls = GetAdjacencies(g, j); hasNext(inUrls); listNext(inUrls)){ int val = *(int*)readList(inUrls); sum += aurls[val]->pRank / aurls[val]->outdeg; } listReset(inUrls); sum *= d; sum += (1 - d)/len; diff += fabs(aurls[j]->pRank - sum); newPRanks[j] = sum; } for(j = 0; j < len; j++) { aurls[j]->pRank = newPRanks[j]; } } free(newPRanks); mergesort((void**)aurls, len, urlComp, 1); print(aurls, fout, len); for(i = 0; i < len; i++){ free(aurls[i]); } free(aurls); fclose(fin); fclose(fout); freeList(urls); dropGraph(g); return EXIT_SUCCESS; }