/* Add the item to the back of the linked list. */ void l_add_back(linked_list list, int item) { //Similar to l_add_front. link new_link = (link) malloc(sizeof(link_t)); assert(new_link != NULL); // Store the item at this new node new_link->item = item; //Since the new link will be the back of the list, //it points next to NULL. new_link->next = NULL; if (l_length(list) == 0) { //If the list was empty, this link is both the back //and the front link. new_link->prev = NULL; list->front = new_link; } else { //Otherwise, have the former front point back to this new //link and this new link point forward to the former front new_link->prev = list->back; list->back->next = new_link; } //In either case, the new link is the new front of the list. list->back = new_link; list->length += 1; }
void S_render(Screen * screen) { for(size_t i = 0;i < l_length(&screen->mRenderQueue);i++) { imgKey key = l_get(&screen->mRenderQueue,i)->mKey; S_apply(screen,&key.mImage); } }
/* Removes the first item held by the list. */ void l_remove_front(linked_list list) { assert(l_length(list) > 0); //If l_remove() is implemented propertly below, just call //it with the pointer to the front link. l_remove(list, list->front); }
Array a_of_l(List list) { assert_argument_not_null(list); int n = l_length(list); int i = 0; Array result = a_create(n, list->s); for (ListNode *node = list->first; node != NULL; node = node->next, i++) { Any element = node + 1; a_set(result, i, element); } return result; }
double dl_foldr(List list, DoubleDoubleIntToDouble f, double init) { assert_function_not_null(f); assert_argument_not_null(list); dl_assert_element_size(list); List rev = l_reverse(list); int i = l_length(list) - 1; for (DoubleListNode *node = rev->first; node != NULL; node = node->next, i--) { init = f(node->value, init, i); } l_free(rev); return init; }
Any pl_foldr(List list, AnyAnyIntToAny f, Any init) { assert_function_not_null(f); assert_argument_not_null(list); pl_assert_element_size(list); List rev = l_reverse(list); int i = l_length(list) - 1; for (PointerListNode *node = rev->first; node != NULL; node = node->next, i--) { init = f(node->value, init, i); } l_free(rev); return init; }
/* unparsetxt: generate C rule table for tpo */ static void unparsetxt (List r, FILE *fp) { List *v, *p; int i; l_ltov(v, List, r, ARENA0); for (p = v, i = 0; *p; p++, i++) { fprintf(fp, "char *tr%dins[] = {\n", i); l_mapc(*p, char *, unparsetxtlin, fp); fprintf(fp, "\t0\n};\n"); } fprintf(fp, "trule_t tpoR[] = {\n"); for (p = v, i = 0; *p; p++, i++) fprintf(fp, "\t{ %d, tr%dins },\n", l_length(*p), i); fprintf(fp, "\t{ 0 }\n};\n"); }
/* Destroy all links in the list and the list itself, freeing up all memory used by the list. */ void l_destroy(linked_list list) { /* Handy shortcut: If you implement, say, l_remove_front() then just call it a bunch of times until the list is empty. Remember to also free the memory used by "list" itself when this is done. */ while (l_length(list) > 0) { l_remove_front(list); } free(list); }
static void dl_repeat_test(void) { printsln((String)__func__); List list; list = dl_repeat(3, 0); check_within_d(dl_get(list, 0), 0, EPSILON); check_within_d(dl_get(list, 1), 0, EPSILON); check_within_d(dl_get(list, 2), 0, EPSILON); l_free(list); list = dl_repeat(2, -1); check_within_d(dl_get(list, 0), -1, EPSILON); check_within_d(dl_get(list, 1), -1, EPSILON); l_free(list); list = dl_repeat(0, 2); check_expect_i(l_length(list), 0); l_free(list); }
/* scan: parse the contents of fp, expecting the input side (phase) of a rule if ph=='i', or the output side if ph=='o' */ List scan (FILE *fp, int ph) { int c = 0; /* Current char on fp */ int nb = 0, nw = 0; /* Position in rule (bit/word) */ Symbol csym = 0L; /* Current variable being scanned */ T f_msk=(T)0, f_val=(T)0; /* Current constant mask and value */ List pats = 0L; /* All the patterns (words) for this phase */ List vars = 0L; /* All the vars for a given pattern */ while ((c=getc(fp)) != EOF) { if (debugp) fprintf(stdout, "/* bit: %c msk: 0x%x val: 0x%x */\n", c, (unsigned)f_msk, (unsigned)f_val); /* Inputs end with '=', outputs with '+' */ if ((c == '=' && ph == 'i') || (c == '+' && ph == 'o')) break; switch(c) { case ' ': case '\f': case '\n': case '\r': case '\t': case '\v': continue; case '#': /* Comment character */ while ((c=getc(fp)) != EOF && c != '\n'); if (c == EOF && (nb > 0 || (ph == 'i' && nw > 0))) error(stringf("Unexpected end of file reading patterns at " "rule %d, word %c%d, bit %d", nr,ph,nw,nb)); continue; case '-': /* Denotes body of a variable */ if (!csym) /* We must be defining a variable */ error(stringf("'-' appears with no leading symbol " "at rule %d, word %c%d, bit %d", nr, ph, nw, nb)); else { /* Extend the variable's right boundary */ ++csym->r; assert(csym->r == nb); } break; case '0': case '1': /* Constant characters */ if (csym) { /* End any variable definition */ vars = mkvar(csym, vars); csym = 0L; } f_msk |= 1; f_val |= (c-'0'); break; default: c = tolower(c); /* Variables named by a-z, case insensitive */ if (c >= 'a' && c <= 'z') { char *s = stringf("%c", c); if (csym) vars = mkvar(csym, vars); if (!lookup(s, stab) && ph == 'o') error(stringf("Symbol '%c' used with no prior " "definition at rule %d, word o%d", c, nr, nw)); csym = install(s, stab); csym->l = csym->r = nb; } else error(stringf("Illegal character in rule file at rule %d, " "word %c%d, bit %d", nr, ph, nw, nb)); } if (nb == SZ-1) { /* End of a pattern (word): append this info */ pat_t *pat; /* to list of patterns, and reset current */ if (csym) { /* pattern state (masks, vars, bit count) */ vars = mkvar(csym, vars); csym = 0L; } NEW(pat, 1, ARENA0); pat->f_msk = f_msk; f_msk = (T)0; pat->f_val = f_val; f_val = (T)0; pat->nv = l_length(vars); if (debugp) fprintf(stdout, "/* msk: 0x%x, val: 0x%x, nv: 0x%x */\n", (unsigned)pat->f_msk, (unsigned)pat->f_val, pat->nv); l_ltov(pat->v, var_t *, vars, ARENA0); pats = l_append(pat, pats, ARENA0); ++nw; nb = 0; vars = 0L; } else { /* Still more to go: move on to next bit */ ++nb; f_msk <<= 1; f_val <<= 1;
/* Test the length function. */ void length_test(linked_list list, int len) { int val = l_length(list); if (val != len) { printf("Length test failed: expected %d got %d.\n", len, val); } }
/* Removes the last item held by the list. */ void l_remove_back(linked_list list) { assert(l_length(list) > 0); l_remove(list, list->back); }
/* Return the last item in the list. */ int l_back(linked_list list) { assert(l_length(list) > 0); return list->back->item; }
/* Return the first item in the list. */ int l_front(linked_list list) { assert(l_length(list) > 0); return list->front->item; }
static void unparsepat (List p, FILE *fp) { fprintf(fp, "unsigned int tpoNP = %d;\n", l_length(p)); fprintf(fp, "char *tpoP[] = {\n"); l_mapc(p, char *, unparsetxtlin, fp); fprintf(fp, "\t0\n};\n"); }