Exemple #1
0
void
trie_clear (trie_t *t)
{
  if (t != NULL)
    {
      trie_clear (t->left);
      trie_clear (t->right);
      free (t);
    }
}
Exemple #2
0
void pathman_clear(pathman_t * pman)
{
  if (pman) {
    {
      pman->dfreelist = 0;
      for (uint32_t n = 0; n < pman->dbanks; n++) {
        free(pman->dirs[n]);
      }
      free(pman->dirs);
      pman->dirs = NULL;
      pman->dbanks = 0;
      pman->dabank = NULL;
    }

    {
      pman->ffreelist = 0;
      for (uint32_t n = 0; n < pman->fbanks; n++) {
        free(pman->files[n]);
      }
      free(pman->files);
      pman->files = NULL;
      pman->fbanks = 0;
      pman->fabank = NULL;
    }

    trie_clear(pman->trie);
  }
}
Exemple #3
0
int parse_and_print_to_stdout(const char * operation)
{
    if (NULL == operation)
    {
        skip_line(stdin);
        return -1;
    }
    if (0 == strcmp(OP_INSERT, operation))
    {
        if (0 != parse_insert(stdin, word_buffer, MAX_WORD_LEN))
            return -1;
        int word_num = trie_insert(word_buffer);
        if (word_num < 0)
            return -1;
        printf("word number: %d\n", word_num);
    }
    if (0 == strcmp(OP_PREV, operation))
    {
        int number, start, end;
        if (0 != parse_prev(stdin, &number, &start, &end))
            return -1;
        int word_num = trie_prev(number, start, end);
        if (word_num < 0)
            return -1;
        printf("word number: %d\n", word_num);
    }
    if (0 == strcmp(OP_DELETE, operation))
    {
        int number;
        if (0 != parse_delete(stdin, &number))
            return -1;
        int word_num = trie_delete(number);
        if (word_num < 0)
            return -1;
        printf("deleted: %d\n", number);
    }
    if (0 == strcmp(OP_FIND, operation))
    {
        if (0 != parse_find(stdin, word_buffer, MAX_WORD_LEN))
            return -1;
        int find_result = trie_find(word_buffer, strlen(word_buffer));
        if (find_result < 0)
            return -1;
        puts(find_result == 0 ? "YES" : "NO");
    }
    if (0 == strcmp(OP_CLEAR, operation))
    {
        if (0 != parse_clear(stdin))
            return -1;
        if (0 != trie_clear())
            return -1;
        puts("cleared");
    }
    return 0;
}
Exemple #4
0
void run(bool verbose)
{
    while (!end_of_input())
    {
        if (0 != run_line(verbose))
        {
            puts("ignored");
        }
    }
    trie_clear();
}
Exemple #5
0
void test() {
    struct trie t;
    trie_clear(&t);
    add_word(&t, "a");
    add_word(&t, "a");
    add_word(&t, "b1");
    add_word(&t, "b2");
    add_word(&t, "b13");
    printf("%s\n", (char *) get_value(&t, "b2"));
    printf("%s\n", (char *) get_value(&t, "b1"));
    printf("%s\n", (char *) get_value(&t, "b13"));
    printf("%s\n", (char *) get_value(&t, "b"));
    printf("%s\n", (char *) get_value(&t, ""));
}
Exemple #6
0
void add_word(
    struct trie *ptrie,
    char *word
) {
    int i = 0;
    char c;
    while (c = word[i]) {
        struct char_hash *pch = &(ptrie->ch);
        struct trie *pbranch = (ptrie->ch.get)(pch, c);
        if (!pbranch) {
            pbranch = malloc(sizeof(*pbranch));
            trie_clear(pbranch);
            (ptrie->ch.put)(&(ptrie->ch), c, pbranch);
        }
        ptrie = pbranch;
        ++i;
    }
    ptrie->value = word;
}