int insert( char str[], int val ) { /* get the correct linked list for this letter */ head = table[get_char_index(str[0])]; struct link *linkp = head.next; /* change this to current_link in the global namespace to implement caching */ while ( linkp != NULL ) { /* if the next word in the list is greater than the insert string, then insert after current string */ if (linkp->next == NULL || strcmp( linkp->next->word, str ) > 0 ) { break; } linkp++; } char *newstr ; struct link *newlink; newstr = malloc( 1 + strlen( str ) ); /* get memory for str */ if ( newstr == NULL ) /* or die */ return NO; strcpy( newstr, str ); /* copy to mem */ newlink = malloc(sizeof(struct link)); /* get mem for link */ if ( newlink == NULL ) /* or die */ return NO; newlink->word = newstr; /* put str in struct */ newlink->value = val ; /* put val in struct */ newlink->next = linkp->next; /* attach list to link */ linkp->next = newlink; /* make head pt to link */ return YES; }
int update( char str[], int val ) { LINK *linkp; for( linkp = table[get_char_index(str[0])].next ; linkp; linkp = linkp->next ) { if (DEBUG) printf("update: scanning for %s, linkp is %s\n", str, (linkp) ? "not null" : "null"); if ( strcmp( linkp->word, str) == 0 ){ if (DEBUG) printf("update: found %s updating value to %d \n", str, val ); linkp->value = val; if (DEBUG) show_table(); return YES; } else { if (DEBUG) show_table(); } } if (DEBUG) show_table(); return 0; }
/** draw a single character */ static int draw_char(char ch, uint16_t xpos, uint16_t ypos, uint32_t color, struct fgui_rect *clip) { int i; int x; int y; int pixel_is_set; i = get_char_index(ch); if (i < 0) { return -1; } // draw character for (y = 0; y < cHeight[i]; y++) { for (x = 0; x < cWidth[i]; x++) { pixel_is_set = (cData[cOff0[i] + y] & (1<<(cWidth[i]-x))); if (pixel_is_set) { fgui_draw_point(xpos+x, ypos+y, color, clip); } } } return 0; }
/* * lookup (char *) * input: a string that exists inside the word filed of a struct in the table * output: the value associated with that word, or 0 if not found * error: needs table defined in the class scope */ int lookup( char str[] ) { LINK *linkp; for( linkp = table[get_char_index(str[0])].next ; linkp ; linkp = linkp->next ) { if ( strcmp( linkp->word, str) == 0 ) { return linkp->value; } } return 0; }
int in_table( char str[] ) { int char_index = get_char_index(str[0]); /* this should = 0 for 'a' */ struct link *linkp = table[char_index].next; /* get the first link in the chain for that letter */ while( linkp != NULL && strcmp( linkp->word, str ) < 0 ) { if ( strcmp( linkp->word, str ) == 0 ) return YES ; else linkp = linkp->next ; } return NO; }
int in_table( char str[] ) { int char_index = get_char_index(str[0]); /* this should = 0 for 'a' */ LINK *linkp = table[char_index].next; /* get the first link in the chain for that letter */ if (DEBUG) printf("in_table: inside str[0] is %c str is %s linkp is %s \n", str[0], str, (linkp == NULL) ? "null" : "not null"); if (DEBUG && linkp) printf("in_table: strcmp (linkp->word, str) = %d \n", strcmp( linkp->word, str )); while( linkp != NULL) { if ( strcmp( linkp->word, str ) == 0 ) { if (DEBUG) printf("in_table: %s was found! \n", str); return YES ; } else if (strcmp( linkp->word, str ) < 0) { if (DEBUG) printf("in_table: looking for %s \n", str); linkp = linkp->next; } else { // fall to return below break; } } return NO; }
/* * word_delete(char[]) * input: a string that exists somewhere in the table * output: none * error: fails silently if the string is not found in the table */ void word_delete(char word[]) { int char_index = get_char_index(word[0]); /* this should = 0 for 'a' */ LINK *prev = &table[char_index]; LINK *linkp = table[char_index].next; /* get the first link in the chain for that letter */ while (linkp != NULL) { if ( strcmp( linkp->word, word ) == 0 ) { prev->next = linkp->next; free(linkp->word); free(linkp); //show_table(); return; } else { if (linkp->next == NULL) { return; } prev = prev->next; linkp = linkp->next; continue; } } return; }
static void render_char(int ch, int x) { int char_index = get_char_index(ch); SDL_Rect clip = { char_index * DIGIT_WIDTH, 0, DIGIT_WIDTH, DIGIT_HEIGHT }; texture_render(&digits_texture, PANEL_X + x, PANEL_Y, &clip); }
int insert( char str[], int val ) { /* get the correct linked list for this letter */ LINK *linkp = table[get_char_index(str[0])].next; char *new_string; LINK *new_link; new_string = malloc(strlen(str) + 1); /* create new space in memory for string, leave room for null char */ if (new_string == NULL) { /* this happens if there's no memory free */ return NO; } strcpy( new_string , str ); /* copy into new space */ new_link = malloc(sizeof(LINK)); /* create memory for new link */ if ( new_link == NULL ) { /* again check for memory overflow */ free(new_string); /* don't leak this memory */ return NO; } // new_link->word = new_string; new_link->value = val; new_link->next = NULL; // first case: empty list if (linkp == NULL) { table[get_char_index(str[0])].next = new_link; if (DEBUG) show_table(); return YES; // if new_link->word preceeds the word at the front // of the list, make new_link the front of the list. } else if ( strcmp(linkp->word, new_link->word) > 0 ) { new_link->next = table[get_char_index(str[0])].next; table[get_char_index(str[0])].next = new_link; if (DEBUG) show_table(); return YES; } else { /* seek to the link before where we want to insert */ while ( linkp != NULL ) { /* if the next word in the list is null then insert after current link */ if (linkp->next == NULL) { if (DEBUG) printf("adding %s at the end of the list \n", linkp->word); break; /* if the next word in the link after this is greater than the insert string, insert after current */ } else if ( strcmp( linkp->next->word, str ) > 0 ) { if (DEBUG) printf("adding %s after %s and before %s \n", new_link->word, linkp->word, linkp->next->word); break; } else { linkp = linkp->next; continue; } printf("UNDEFINED BEHAVIOR: Infinite looping will occur\n"); } new_link->next = linkp->next; linkp->next = new_link; } if (DEBUG) show_table(); return YES; }