/* remove a word from the tree */ int atree_remove(atree_t *tree, const char *word) { unsigned int i; atree_node_t *node, **pnode; freqtab_t ftab; ftab_init(&ftab, word); pnode = _atree_walk(tree, &ftab, 0); if (pnode == NULL) return 0; node = *pnode; for (i = 0; i < node->degree; i++) if (strcmp(word, node->words[i]) == 0) break; if (i < node->degree) { if (i < node->degree - 1) memmove(&node->words[i], &node->words[i + 1], sizeof(const char *) * (node->degree - i - 1)); node->degree--; tree->count--; return 1; } return 0; }
void ftab_copy(struct array *oldtab, struct array **newtab){ struct fildes *fd; if(oldtab == NULL){ return; } *newtab = ftab_init(); for(unsigned int i = 2; (fd = ftab_get(oldtab, i)) != NULL; i++){ // v = array_get(oldtab, i); ftab_set(*newtab, (void *)fd, i); VOP_INCREF(fd->vn); } }
/* add a word to the tree */ int atree_add(atree_t *tree, const char *word) { unsigned int i; int cmp; atree_node_t *node, **pnode; freqtab_t ftab; ftab_init(&ftab, word); pnode = _atree_walk(tree, &ftab, 1); if (pnode == NULL) return 0; node = *pnode; // make sure that word is not already in the list i = _atree_find_word(node, word); if (i == INVALID) { i = 0; } else { cmp = strcmp(node->words[i], word); if (cmp == 0) return 0; else if (cmp < 0) i++; } if (node->degree == UINT16_MAX) return 0; node->degree++; if (node->degree > node->max_degree) { unsigned int new_length = node->degree + 8; node = realloc(node, sizeof(atree_node_t) + sizeof(const char *) * new_length); if (node == NULL) { #ifdef PATCHED (*pnode)->degree--; #endif return 0; // XXX BUG node->degree was already incremented } // set node and *pnode, and max_degree appropriately if (node->next_leaf) node->next_leaf->prev_leaf = node; if (node->prev_leaf) node->prev_leaf->next_leaf = node; else tree->leaf = node; *pnode = node; node->max_degree = new_length; } if (i < node->degree - 1) memmove(&node->words[i+1], &node->words[i], sizeof(const char *) * (node->degree - 1 - i)); node->words[i] = word; tree->count++; return 1; }