struct WordEntryTree *addToEntryTree(struct WordEntryTree *tree, struct WordEntry *entry) { struct WordEntryTree *newtree; int cmp; if (tree == NULL) { /*if there is no tree, make one*/ newtree = (struct WordEntryTree *)testalloc(sizeof(struct WordEntryTree)); newtree->rhymes = NULL; pushWordEntry(&newtree->rhymes, entry); newtree->rhymekey = entry->rhymekey; newtree->greater = NULL; newtree->lesser = NULL; return newtree; } else { /*if there is a tree*/ cmp = cmpStringParts(tree->rhymekey, entry->rhymekey); if (cmp == 0) { /*if the keys match*/ pushWordEntry(&tree->rhymes, entry); /*push the word on the stack*/ } else if (cmp > 0) { /*add to the lesser tree if the word is below the current syllables*/ tree->lesser = addToEntryTree(tree->lesser, entry); } else { /*otherwise, add to the greater tree*/ tree->greater = addToEntryTree(tree->greater, entry); } /*and, finally, return the existing tree*/ return tree; } }
int main() { void **rings; /* yay! */ void **ring_alloc, **ring_free; /* yay! */ int i, n; srandom(time(NULL)); rings = malloc(NALLOC * NRING * sizeof(void *)); /* so we can free() immediately without stress */ memset(rings, 0, NALLOC * NRING * sizeof(void *)); for (n = 0;; n = (n+1) % NRING) { if (n == 0) mstats(""); ring_alloc = &rings[n * NALLOC]; ring_free = &rings[((n + NRING/2) % NRING) * NALLOC]; for (i = 0; i < NALLOC; i++) { ring_alloc[i] = testalloc(); bmk_memfree(ring_free[i]); } } }
void pushWordEntry(struct WordEntryStack **stack, struct WordEntry *entry) { struct WordEntryStack *head; head = (struct WordEntryStack *)testalloc(sizeof(struct WordEntryStack)); head->entry = entry; head->next = *stack; *stack = head; }
main() { char *s1,*s2,*s3,*s4,*s5,*s6; unsigned size; s1 = testalloc(1); s2 = testalloc(2); s3 = testalloc(0x10); s4 = testalloc(0x100); s5 = testalloc(0x1000); s6 = testalloc(0xfff0); testfree(s3); s3 = testalloc(6); testfree(s2); testalloc(8); #ifdef __GNUC__ for(size = 0xe0000000; size;) #else for(size = 0xe000; size;) #endif { if (testalloc(size)) ; else size >>= 1; } testfree(s1); testfree(s5); testfree(s4); testfree(s6); return 1; }
struct WordEntry *stringToWordEntry(char *string) { struct WordEntry *toreturn; struct StringPart *parts; struct StringPart *current; parts = getStringParts(string, 0, strlen(string)); if (countStringParts(parts) < 2) return NULL; toreturn = (struct WordEntry *)testalloc(sizeof(struct WordEntry)); toreturn->word = parts; toreturn->phonemes = parts->next; toreturn->rhymekey = NULL; toreturn->syllables = 0; for (current = parts->next ; current != NULL ; current = current->next) { if (isSyllable(current)) { toreturn->syllables++; if (isPrimaryStress(current)) toreturn->rhymekey = current; } } /*words without primary keys are dropped entirely if (toreturn->rhymekey == NULL) { free(toreturn); return NULL; }*/ toreturn->alternate = NULL; toreturn->string = string; return toreturn; }