コード例 #1
0
ファイル: test-trie.c プロジェクト: ColinCui/c-algorithms
void test_trie_lookup(void)
{
	Trie *trie;
	char buf[10];
	int *val;
	int i;

	trie = generate_trie();

	/* Test lookup for non-existent values */

	assert(trie_lookup(trie, "000000000000000") == TRIE_NULL);
	assert(trie_lookup(trie, "") == TRIE_NULL);

	/* Look up all values */

	for (i=0; i<NUM_TEST_VALUES; ++i) {

		sprintf(buf, "%i", i);

		val = (int *) trie_lookup(trie, buf);
		
		assert(*val == i);
	}

	trie_free(trie);
}
コード例 #2
0
ファイル: trie.c プロジェクト: Fruneau/pfixtools
static void check_trie_with_file(const trie_t *db, const char *file)
{
    file_map_t map;
    const char *p, *end;
    char line[BUFSIZ];

    if (!file_map_open(&map, file, false)) {
        return;
    }
    p   = map.map;
    end = map.end;
    while (end > p && end[-1] != '\n') {
        --end;
    }
    if (end != map.end) {
        warn("file %s miss a final \\n, ignoring last line", file);
    }

    while (p < end && p != NULL) {
        const char *eol = (char *)memchr(p, '\n', end - p);
        if (eol == NULL) {
            eol = end;
        }
        if (eol - p > BUFSIZ) {
            p = eol - BUFSIZ;
        }
        int i = 0;
        if (*p != '#' && p != eol) {
#if 1
          for (const char *s = eol - 1 ; s >= p ; --s) {
              line[i++] = ascii_tolower(*s);
          }
#else
          memcpy(line, p, eol - p);
          i = eol - p;
#endif
          line[i] = '\0';
          if (!trie_lookup(db, line)) {
            warn("'%s' not found in the trie", line);
          }
          strcat(line, "coucou");
          if (trie_lookup(db, line)) {
            warn("'%s' found in trie", line);
          }
          if (!trie_prefix(db, line)) {
            warn("'%s' has no prefix in trie", line);
          }
        }
        p = eol + 1;
    }
    file_map_close(&map);
}
コード例 #3
0
int main()
{
	trie* a;

	char* str=(char*)malloc(10);
	char* key=(char*)malloc(10);
        int ch,res;
        a=init();
       	do
	{
		printf("\n1.Insert into trie\n2.Search for a word in trie\n3.Exit\nEnter choice:\n");
		scanf("%d",&ch);
		switch(ch)
		{
			case 1:
				printf("Enter the word to be inserted\n");
				scanf("%s",str);
				trie_add(a,str);
				break;
			case 2:
				printf("Enter the word to be searched\n");
				scanf("%s",key);
				res=trie_lookup(a,key);
				if(res!=0)
					printf("Word present!\n");
				else
					printf("Word not in trie!\n");
				break;
			case 3:
				printf("Exitting.....\n");
				break;
		}
	}while(ch!=3);
	return 0;
}
コード例 #4
0
ファイル: trie_run.c プロジェクト: NachtZ/trie
int main(int argc, char* argv[])
{

	int *a, *b, *c, *d;
	a = malloc(sizeof(int));
	*a = 1;
	b = malloc(sizeof(int));
	*b = 2;
	c = malloc(sizeof(int));
	*c = 3;
	d = malloc(sizeof(int));
	*d = 4;

	struct trie* test_trie = trie_create();
	printf("created new trie at %p\n", test_trie);
	
	void* inserted = trie_insert(test_trie, "aa", a);
	printf("inserted %d at %p\n", *a, inserted);

	int *trie_value = trie_lookup(test_trie, "aa");
	printf("lookup got %d\n", *trie_value);

	inserted = trie_insert(test_trie, "ab", b);
	printf("inserted %d at %p\n", *a, inserted);

	trie_value = trie_lookup(test_trie, "ab");
	printf("lookup got %d\n", *trie_value);

	inserted = trie_insert(test_trie, "ccz", c);
	printf("inserted %d at %p\n", *a, inserted);

	trie_value = trie_lookup(test_trie, "ccz");
	printf("lookup got %d\n", *trie_value);

	trie_value = trie_lookup_prefix(test_trie, "c", NULL);
	printf("prefix lookup got %d\n", *trie_value);

	trie_value = trie_lookup_prefix(test_trie, "a", d);
	printf("ambiguous prefix lookup got %d\n", *trie_value);

	trie_destroy(test_trie);
	printf("freed trie\n");
	
	return 0;
}
コード例 #5
0
ファイル: trie.c プロジェクト: 5432935/crossbridge
/* Looks up a string, s, in a trie, t, and returns the number of occ. */
int   trie_lookup(trie t, string s)
{
  if(t == NULL)
    return 0;				/* Didn't find it */
  
  if(s[0] == '\0')			/* At the end */
    return (t->next[char2index(s[0])])->number;
  else
    return (trie_lookup(t->next[char2index(s[0])], s+1));
}
コード例 #6
0
ファイル: erlxslt_adapter.c プロジェクト: chinnurtb/erlxslt-2
/**
    \fn memo_xsl
**/
inline xsp_t memo_xsl(Trie* trie, unsigned char *xslfile){
    debug("xsp_t memo_xsl(Trie* trie, unsigned char *xslfile)");
    xsp_t xsl = (xsp_t)trie_lookup(trie, (char*)xslfile);
    if(NULL == xsl)
    {
        xsl = parse_xslt((const xc_t*) xslfile);
        trie_insert(trie, (char*)xslfile, xsl);
    }

    return xsl;
}
コード例 #7
0
ファイル: test-trie.c プロジェクト: ColinCui/c-algorithms
static void test_trie_negative_keys(void)
{
	char my_key[] = { 'a', 'b', 'c', -50, -20, '\0' };
	Trie *trie;
	void *value;

	trie = trie_new();

	assert(trie_insert(trie, my_key, "hello world") != 0);

	value = trie_lookup(trie, my_key);

	assert(!strcmp(value, "hello world"));

	assert(trie_remove(trie, my_key) != 0);
	assert(trie_remove(trie, my_key) == 0);
	assert(trie_lookup(trie, my_key) == NULL);

	trie_free(trie);
}
コード例 #8
0
ファイル: test.c プロジェクト: nelseric/router
void test(trie_t *top, char * testfile){
    FILE * tests = fopen(testfile, "r");

    char buf[128];

    int passed = 0;
    int failed = 0;

    while(!feof(tests)){
        if(fgets(buf, 128, tests) == NULL){
            if(ferror(tests)){
                perror("file");
                exit(1);
            } else {
                break;
            }
        }

        if(strlen(buf) < 7){
            break;
        }

        char *  input_temp;
        char *  dest_temp;
        input_temp = strtok(buf, "\t");
        dest_temp = strtok(NULL, "\n");        

        uint32_t dest, input;
        inet_pton(AF_INET, input_temp, &input);
        inet_pton(AF_INET, dest_temp, &dest);

        input = ntohl(input);
        dest = ntohl(dest);

        uint32_t res;
        res = trie_lookup(top, input);  
        if(dest != res){
            char ipbuf[INET_ADDRSTRLEN];
            res = htonl(res);
            inet_ntop(AF_INET, &res, ipbuf, INET_ADDRSTRLEN);

            printf("lookup(%s)=\"%s\", expected \"%s\"\n", input_temp, ipbuf, dest_temp );
            failed++;
        } else {
            passed++;
        }
    }

    fclose(tests);
    printf("%d of %d tests passed, %d tests failed.\n", passed, passed+failed, failed);
}
コード例 #9
0
ファイル: trie.c プロジェクト: JiweiLiu/scrabble
/* Inserts S into TRIE. Returns the new node marking the end of S, or NULL on
 * memory failure. */
Node *trie_insert(Trie *trie, char *s) {
        Node *np;
        char *si;

        np = trie_lookup(trie, s, &si);
        for (s = si; *s != '\0'; s++) {
                np = node_add_child(np, *s);
                if (np == NULL)
                        return NULL;
                trie->size++;
        }
        np->end_of_word = 1;
        return np;
}
コード例 #10
0
ファイル: bloom.c プロジェクト: hagemt/bloom
void
archive(struct file_info_t *info, struct file_entry_t *entry)
{
	Set *hash_set = trie_lookup(info->hash_trie, entry->hash);
	if (hash_set == TRIE_NULL) {
		/* Otherwise, the value needs a new list */
		hash_set = set_new(&pointer_hash, &pointer_equal);
		slist_prepend(&info->duplicates, hash_set);
		trie_insert(info->hash_trie, entry->hash, hash_set);
	}
	if (!set_insert(hash_set, entry)) {
		#ifndef NDEBUG
		fprintf(stderr, "[DEBUG] '%s' (extra file)\n", entry->path);
		#endif
	}
}
コード例 #11
0
ファイル: test-trie.c プロジェクト: ColinCui/c-algorithms
void test_trie_insert_empty(void)
{
	Trie *trie;
	char buf[10];

	trie = trie_new();

	/* Test insert on empty string */

	assert(trie_insert(trie, "", buf) != 0);
	assert(trie_num_entries(trie) != 0);
	assert(trie_lookup(trie, "") == buf);
	assert(trie_remove(trie, "") != 0);

	assert(trie_num_entries(trie) == 0);

	trie_free(trie);
}
コード例 #12
0
ファイル: test-trie.c プロジェクト: ColinCui/c-algorithms
void test_trie_replace(void)
{
	Trie *trie;
	int *val;

	trie = generate_trie();

	/* Test replacing values */

	val = malloc(sizeof(int));
	*val = 999;
	assert(trie_insert(trie, "999", val) != 0);
	assert(trie_num_entries(trie) == NUM_TEST_VALUES);

	assert(trie_lookup(trie, "999") == val);
	free(val);
	trie_free(trie);
}
コード例 #13
0
ファイル: robocompiler.c プロジェクト: stevely/New-Robowar
static int convert_ident_token( token_list *tok ) {
    token_trie *t = trie_lookup(tok->ident);
    if( t ) {
        if( t->pick == 1 ) {
            tok->t = t->type.tok;
        }
        else if( t->pick == 2 ) {
            tok->t = token_reg;
            tok->value = (int)t->type.reg;
        }
        else {
            return 0;
        }
        free(tok->ident);
        tok->ident = NULL;
        return 1;
    }
    else {
        return 0;
    }
}
コード例 #14
0
ファイル: merge_common_prefix.c プロジェクト: sangyf/klcs
void lookup_trie(Trie * trie){

	int i;
	int *val;
	char buf[10];
	
	if (!trie) {
		printf("Error: trie is null!\n");
		return;
	}


	for(i = 0; i < NUM_TEST_VALUES; ++i) {
		sprintf(buf, "%i", i);
		val = (int *) trie_lookup(trie, buf);
		assert(*val == i);
		printf("%i \t", *val);
	}

	printf("\n");
	return ;

}
コード例 #15
0
ファイル: phtoelm.c プロジェクト: 10sun/DRL-AI
trie_ptr
enter_trans(char *trans, int verbose)
{
    trie_ptr table = NULL;
    FILE *f = fopen(trans, "r");
    if (f) {
	char buf[1024];
	char *s;
	if (verbose)
	    fprintf(stderr, "Reading %s\n", trans);
	while ((s = fgets(buf, sizeof(buf), f))) {
	    char *p;
	    char *x;
	    while (isspace((unsigned) *s))
		s++;
	    p = s;
	    while (*s && !isspace((unsigned) *s))
		s++;
	    while (isspace((unsigned) *s))
		*s++ = '\0';
	    x = (char *) trie_lookup(&phtoelm, &s);
	    while (isspace(*s))
		s++;
	    if (*s) {
		fprintf(stderr, "%s does not map (leaves %s)\n", p, s);
	    }
	    else {
		trie_insert(&table, p, x);
	    }
	}
	fclose(f);
    }
    else {
	perror(trans);
    }
    return table;
}
コード例 #16
0
ファイル: depgraph.c プロジェクト: hitchiker42/my-code
struct node *get_node(struct trie *nodes, const char *str,
		      unsigned int *nnodesp)
{
    struct node *n = trie_lookup(nodes, str);

    if (!n) {
	int err;

	n = create_node(str, *nnodesp);
	if (!n)
	    return NULL;
	*nnodesp += 1;

	err = trie_insert(nodes, str, n);
	if (err) {
	    free_node(n);
	    return NULL;
	}

	debug_printf(DEBUG_LOTS, "new node for %s\n", str);
    }

    return n;
}
コード例 #17
0
ファイル: trie.c プロジェクト: Fruneau/pfixtools
int main(int argc, char *argv[])
{
    /* test_lookup(); */

    /* Trivial tests
     */
    trie_t *trie = trie_new();
    trie_insert(trie, "abcde123456789");
    trie_insert(trie, "abcde123654789");
    trie_insert(trie, "abcde123654789");
    trie_insert(trie, "abcdefghi");
    trie_insert(trie, "coucou");
    trie_insert(trie, "coucou chez vous");
    trie_insert(trie, "debout !");
    trie_compile(trie, false);
    trie_inspect(trie, true);

#define ASSERT_TRUE(str)                            \
    if (!trie_lookup(trie, str)) {                  \
        printf("\"%s\" not found in trie\n", str);  \
        return 1;                                   \
    }
#define ASSERT_FALSE(str)                           \
    if (trie_lookup(trie, str)) {                   \
        printf("\"%s\" found in trie\n", str);      \
        return 1;                                   \
    }
    ASSERT_FALSE("");
    ASSERT_FALSE("coucou ");
    ASSERT_FALSE("abcde123");
    ASSERT_FALSE("abcde");
    ASSERT_FALSE("coucou chez vous tous");
    ASSERT_TRUE("abcde123456789");
    ASSERT_TRUE("abcde123456789");
    ASSERT_TRUE("abcde123654789");
    ASSERT_TRUE("abcdefghi");
    ASSERT_TRUE("coucou");
    ASSERT_TRUE("coucou chez vous");
    ASSERT_TRUE("debout !");

    trie_delete(&trie);

    /* Perf test
     */
    if (argc > 1) {
        trie = create_trie_from_file(argv[1]);
        trie_inspect(trie, true);
        check_trie_with_file(trie, argv[1]);
        if (argc > 2) {
            const uint32_t how_many = 8 * 1000 * 1000;
            struct timeval start, end;
            double diff;

            gettimeofday(&start, NULL);
            for (uint32_t i = 0 ; i < how_many ; ++i) {
                trie_lookup(trie, argv[2]);
            }
            gettimeofday(&end, NULL);
            diff = (end.tv_sec - start.tv_sec) + (double)(end.tv_usec - start.tv_usec) / 10e6;
            printf("%u lookups per second\n", (int)(how_many / diff));

            trie_match_t match;
            gettimeofday(&start, NULL);
            for (uint32_t i = 0 ; i < how_many ; ++i) {
                trie_lookup_match(trie, argv[2], &match);
            }
            gettimeofday(&end, NULL);
            diff = (end.tv_sec - start.tv_sec) + (double)(end.tv_usec - start.tv_usec) / 10e6;
            printf("%u lookups per second\n", (int)(how_many / diff));

        }
        trie_delete(&trie);
    }
    return 0;
}
コード例 #18
0
ファイル: trie_test.c プロジェクト: Tibo-lg/HomePort
TEST_START("trie.c")

TEST(create_destroy)
   struct trie *root = trie_create();
   trie_destroy(root, dealloc);
TSET()

TEST(simple_insert)
   char *key = "cat";
   struct trie *trie = trie_create();

   struct trie_iter *iter = trie_insert(trie, key, key);
   ASSERT_STR_EQUAL(trie_key(iter), key);
   ASSERT_STR_EQUAL(trie_value(iter), key);

   iter = trie_lookup(trie, key);
   ASSERT_STR_EQUAL(trie_key(iter), key);
   ASSERT_STR_EQUAL(trie_value(iter), key);

   trie_destroy(trie, NULL);
TSET()

TEST(simple_remove)
   char *key = "cat";
   struct trie *trie = trie_create();

   struct trie_iter *iter = trie_insert(trie, key, key);
   ASSERT_STR_EQUAL(trie_key(iter), key);
   ASSERT_STR_EQUAL(trie_value(iter), key);

   iter = trie_lookup(trie, key);
コード例 #19
0
ファイル: phtoelm.c プロジェクト: 10sun/DRL-AI
static unsigned
phone_to_elm(rsynth_t * rsynth, int n, char *phone, darray_ptr elm,
	     darray_ptr f0)
{
    float F0Hz = rsynth->speaker->F0Hz;
    float speed = rsynth->speed;
    int stress = 0;
    int seen_vowel = 0;
    int islong = 0;
    char *s = phone;
    unsigned t = 0;
    unsigned f0t = 0;
    char *limit = s + n;
    float f = darray_float(f0, F0Hz * 1.1F);
    if (!phtoelm)
	enter_phonemes();
    while (s < limit && *s) {
	char *e = (char *) trie_lookup(&phtoelm, &s);
	if (e) {
	    int n = *e++;
	    while (n-- > 0) {
		int x = *e++;
		Elm_ptr p = &Elements[x];
		darray_append(elm, x);
		/* StressDur works because only vowels have ud != du,
		   and we set stress just before a vowel
		 */
		t += darray_append(elm,
				   (int) (StressDur(p, stress, islong)));
		if (p->feat & vwl) {
		    seen_vowel = 1;
		}
		else if (seen_vowel) {
		    stress = 0;
		}
	    }
	}
	else {
	    char ch = *s++;
	    switch (ch) {
	    case '\'':		/* Primary stress */
		stress++;
	    case ',':		/* Secondary stress */
		stress++;
	    case '+':		/* Tertiary stress */
		stress++;
		if (stress > 3)
		    stress = 3;
		seen_vowel = 0;
		/* f0 has been declining since f0t */
		f = decline_f0(F0Hz, f0, f, t - f0t);
		f0t = t;
		/* Now stress pulse pushes f0 up "instantly" */
		darray_float(f0, 0);
		darray_float(f0, f + F0Hz * stress * 0.02);
		break;
	    case '-':		/* hyphen in input */
		break;
	    case ':':		/* Length mark */
		islong = 1;
		break;
	    default:
		fprintf(stderr, "Ignoring %c in '%.*s'\n", ch, n, phone);
		break;
	    }
	}
    }
    /* Add final decline to f0 contour */
    decline_f0(F0Hz, f0, f, t - f0t);
    return t;
}
コード例 #20
0
ファイル: dictionary.c プロジェクト: manhtai/some-c
/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char* word)
{
    return trie_lookup(&trie, word);
}
コード例 #21
0
ファイル: phtoelm.c プロジェクト: 10sun/DRL-AI
void
rsynth_pho(rsynth_t * rsynth, const char *path, int dodur, char *trans)
{
    int verbose = rsynth_verbose(rsynth);
    FILE *f = fopen(path, "r");
    trie_ptr table;
    if (!phtoelm)
	enter_phonemes();
    if (trans && *trans && strcmp(trans, "sampa"))
	table = enter_trans(trans, verbose);
    else
	table = phtoelm;
    if (f) {
	char buffer[1024];
	char *s;
	darray_t elm;
	unsigned t = 0;
	float f0a[1024] = { rsynth->speaker->F0Hz };
	unsigned nf0 = 0;	/* index of last f0 value */
	unsigned f0t = 0;	/* time of last f0 value */
	darray_init(&elm, sizeof(char), 1024);
	if (verbose) {
	    fprintf(stderr, "Frame is %.3gms\n",
		    rsynth->samples_frame * 1000.0 / rsynth->sr);
	}
	while ((s = fgets(buffer, sizeof(buffer), f))) {
	    /* skip leading space - should not be any but ... */
	    while (isspace((unsigned) *s))
		s++;
	    if (*s && *s != ';') {
		/* Not a comment */
		char *ps = s;
		char *e = (char *) trie_lookup(&table, &s);
		if (*s == ':')
		    s++;
		if (e && isspace((unsigned) *s)) {
		    char *pe = s;
		    unsigned pt = 0;
		    int n = *e++;
		    int i;
		    double ms = strtod(s, &s);
		    float frames =
			ms * (rsynth->sr / rsynth->samples_frame) / 1000;
		    float edur = 0;
		    float estp = 0;
		    int nstp = 0;
		    float delta = 0;
		    for (i = 0; i < n; i++) {
			int x = e[i];
			Elm_ptr p = &Elements[x];
			if (!p->du || p->feat & stp)
			    estp += p->ud;
			else {
			    edur += p->ud;
			    nstp++;
			}
		    }
		    /* Stops don't change length */
		    frames -= estp;
		    delta = frames - edur;
#if 0
		    /* FIXME - revisit the rounding process */
		    if (verbose)
			fprintf(stderr,
				"'%.*s' %gms %d elem %g frames vs %g nat d=%g) %d stops\n",
				(pe - ps), ps, ms, n, frames, edur, delta,
				n - nstp);
#endif
		    for (i = 0; i < n; i++) {
			int x = e[i];
			Elm_ptr p = &Elements[x];
			darray_append(&elm, x);
			if (!p->du || p->feat & stp)
			    pt += darray_append(&elm, p->ud);
			else {
			    if (dodur) {
				float share =
				    (nstp >
				     1) ? rint(delta * (p->ud -
							1) / (edur -
							      nstp)) :
				    delta;
#if 0
				fprintf(stderr,
					"%s d=%d vs nstp=%g delta=%g take=%g\n",
					p->name, p->ud, edur, delta,
					share);
#endif
				edur -= p->ud;
				delta -= share;
				nstp--;
				pt +=
				    darray_append(&elm,
						  (int) (p->ud + share));
			    }
			    else
				pt += darray_append(&elm, p->du);
			}
		    }
		    /* Now have elements entered and duration of phone computed */
		    if (verbose && dodur) {
			float got =
			    1.0 * pt * rsynth->samples_frame / rsynth->sr *
			    1000;
			if (fabs(got - ms) > 0.5) {
			    fprintf(stderr,
				    "'%.*s' want=%gms got=%.3g (%+3.0f%%)\n",
				    (pe - ps), ps, ms, got,
				    100 * (got - ms) / ms);
			}
		    }
		    while (isspace((unsigned) *s))
			s++;
		    while (*s) {
			float percent = strtod(s, &s);
			float f0 = strtod(s, &s);
			unsigned nt =
			    (unsigned) (t + (percent * pt / 100));
			if (nt > f0t) {
			    /* time has advanced */
			    f0a[++nf0] = (nt - f0t);
			    f0a[++nf0] = f0;
			    f0t = nt;
			}
			else {
			    /* same time - change target inplace */
			    f0a[nf0] = f0;
			}
			while (isspace((unsigned) *s))
			    s++;
		    }
		    t += pt;
		}
		else {
		    fputs(buffer, stderr);
		    fprintf(stderr, "Unknown phone:%s", ps);
		}
	    }
	}
	fclose(f);
	if (t) {
	    float f0 = f0a[nf0++];
	    if (f0t < t) {
		f0a[nf0++] = t - f0t;
		f0a[nf0++] = f0;
	    }
	    rsynth_flush(rsynth,
	        rsynth_interpolate(rsynth,
			    (unsigned char *) darray_find(&elm,	0),
    	    	    	    elm.items, f0a, nf0));
	}
    }
    else {
	perror(path);
    }
}
コード例 #22
0
ファイル: bloom.c プロジェクト: hagemt/bloom
int
main(int argc, char *argv[])
{
	size_t path_len, total_files;
	off_t bytes_wasted, total_wasted;
	char path_buffer[PATH_MAX_LEN], *hash_value;
	struct file_entry_t *file_entry, *trie_entry;

	SListIterator slist_iterator;
	SetIterator set_iterator;

	/* Step 0: Session data */
	struct file_info_t file_info;
	clear_info(&file_info);

	/* Step 1: Parse arguments */
	while (--argc) {
		/* Being unable to record implies insufficient resources */
		if (!record(argv[argc], &file_info)){
			fprintf(stderr, "[FATAL] out of memory\n");
			destroy_info(&file_info);
			return (EXIT_FAILURE);
		}
	}

	/* Step 2: Fully explore any directories specified */
	#ifndef NDEBUG
	printf("[DEBUG] Creating file list...\n");
	#endif
	while (slist_length(file_info.file_stack) > 0) {
		/* Pick off the top of the file stack */
		file_entry = (struct file_entry_t *)(slist_data(file_info.file_stack));
		slist_remove_entry(&file_info.file_stack, file_info.file_stack);
		assert(file_entry->type == DIRECTORY);
		/* Copy the basename to a buffer */
		memset(path_buffer, '\0', PATH_MAX_LEN);
		path_len = strnlen(file_entry->path, PATH_MAX_LEN);
		memcpy(path_buffer, file_entry->path, path_len);
		/* Ignore cases that would cause overflow */
		if (path_len < PATH_MAX_LEN) {
			/* Append a trailing slash */
			path_buffer[path_len] = '/';
			/* Record all contents (may push onto file stack or one of the lists) */
			DIR *directory = opendir(file_entry->path);
			if (traverse(&file_info, directory, path_buffer, ++path_len)) {
				fprintf(stderr, "[FATAL] out of memory\n");
				destroy_info(&file_info);
				return (EXIT_FAILURE);
			} else if (closedir(directory)) {
				fprintf(stderr, "[WARNING] '%s' (close failed)\n", file_entry->path);
			}
		}
		/* Discard this entry */
		destroy_entry(file_entry);
	}

	/* Step 3: Warn about any ignored files */
	if (slist_length(file_info.bad_files) > 0) {
		slist_iterate(&file_info.bad_files, &slist_iterator);
		while (slist_iter_has_more(&slist_iterator)) {
			file_entry = slist_iter_next(&slist_iterator);
			fprintf(stderr, "[WARNING] '%s' ", file_entry->path);
			switch (file_entry->type) {
			case INVALID:
				++file_info.invalid_files;
				fprintf(stderr, "(invalid file)\n");
				break;
			case INACCESSIBLE:
				++file_info.protected_files;
				fprintf(stderr, "(protected file)\n");
				break;
			default:
				++file_info.irregular_files;
				fprintf(stderr, "(irregular file)\n");
				break;
			}
		}
		fprintf(stderr, "[WARNING] %lu file(s) ignored\n",
			(long unsigned)(num_errors(&file_info)));
	}
	#ifndef NDEBUG
	if (num_errors(&file_info) > 0) {
		fprintf(stderr, "[FATAL] cannot parse entire file tree\n");
		destroy_info(&file_info);
		return (EXIT_FAILURE);
	}
	printf("[DEBUG] Found %lu / %lu valid files\n",
		(unsigned long)(num_files(&file_info)),
		(unsigned long)(file_info.total_files));
	#endif

	/* Step 4: Begin the filtering process */
	#ifndef NDEBUG
	printf("[DEBUG] Creating file table...\n");
	#endif
	if (slist_length(file_info.good_files) > 0) {
		file_info.hash_trie = trie_new();
		file_info.shash_trie = trie_new();
		optimize_filter(&file_info);
		/* Extract each file from the list (they should all be regular) */
		slist_iterate(&file_info.good_files, &slist_iterator);
		while (slist_iter_has_more(&slist_iterator)) {
			file_entry = slist_iter_next(&slist_iterator);
			assert(file_entry->type == REGULAR);
			/* Perform a "shallow" hash of the file */
			hash_value = hash_entry(file_entry, SHALLOW);
			#ifndef NDEBUG
			printf("[SHASH] %s\t*%s\n", file_entry->path, hash_value);
			#endif
			/* Check to see if we might have seen this file before */
			if (bloom_filter_query(file_info.shash_filter, hash_value)) {
				/* Get the full hash of the new file */
				hash_value = hash_entry(file_entry, FULL);
				#ifndef NDEBUG
				printf("[+HASH] %s\t*%s\n", file_entry->path, hash_value);
				#endif
				archive(&file_info, file_entry);
				/* Check to see if bloom failed us */
				trie_entry = trie_lookup(file_info.shash_trie, file_entry->shash);
				if (trie_entry == TRIE_NULL) {
					#ifndef NDEBUG
					printf("[DEBUG] '%s' (false positive)\n", file_entry->path);
					#endif
					trie_insert(file_info.shash_trie, file_entry->shash, file_entry);
				} else {
					/* Get the full hash of the old file */
					hash_value = hash_entry(trie_entry, FULL);
					#ifndef NDEBUG
					if (hash_value) {
						printf("[-HASH] %s\t*%s\n", trie_entry->path, hash_value);
					}
					#endif
					archive(&file_info, trie_entry);
				}
			} else {
				/* Add a record of this shash to the filter */
				bloom_filter_insert(file_info.shash_filter, hash_value);
				trie_insert(file_info.shash_trie, hash_value, file_entry);
			}
		}
		persist("bloom_store", &file_info);
	}

	/* Step 5: Output results and cleanup before exit */
	printf("[EXTRA] Found %lu sets of duplicates...\n",
		(unsigned long)(slist_length(file_info.duplicates)));
	slist_iterate(&file_info.duplicates, &slist_iterator);
	for (total_files = total_wasted = bytes_wasted = 0;
		slist_iter_has_more(&slist_iterator);
		total_wasted += bytes_wasted)
	{
		Set *set = slist_iter_next(&slist_iterator);
		int size = set_num_entries(set);
		if (size < 2) { continue; }
		printf("[EXTRA] %lu files (w/ same hash):\n", (unsigned long)(size));
		set_iterate(set, &set_iterator);
		for (bytes_wasted = 0;
			set_iter_has_more(&set_iterator);
			bytes_wasted += file_entry->size,
			++total_files)
		{
			file_entry = set_iter_next(&set_iterator);
			printf("\t%s (%lu bytes)\n",
				file_entry->path,
				(unsigned long)(file_entry->size));
		}
	}
	printf("[EXTRA] %lu bytes in %lu files (wasted)\n",
		(unsigned long)(total_wasted),
		(unsigned long)(total_files));
	destroy_info(&file_info);
	return (EXIT_SUCCESS);
}
コード例 #23
0
ファイル: writer.c プロジェクト: djoshea/matudp
void addEventGroupFields(mxArray* mxTrial, mxArray* mxGroupMeta,
    const GroupInfo* pg, unsigned trialIdx, timestamp_t timeTrialStart,
    bool useGroupPrefix, unsigned groupMetaIndex)
{
    // field names will be groupName_<eventName>
    // but the signal always comes in as .eventName and the contents are the name
    // of the event
    //
    // so: build up a trie where the eventName is the key and a TimestampBuffer is the value
    Trie* eventTrie = trie_create();
    Trie* trieNode;

    // get timestamp buffer from group buffer
    const TimestampBuffer* groupTimestamps = pg->tsBuffers + trialIdx;
    const char* groupName = pg->name;

    // for now check that the event group has only 1 signal and it's type is EventName
    bool printError = false;
    if(pg->nSignals != 1)
    	printError = true;
    else if(pg->signals[0]->type != SIGNAL_TYPE_EVENTNAME)
    	printError = true;
    if(printError) {
    	logError("Event groups must have 1 signal of type event name");
    	return;
    }

    const SignalDataBuffer*psdb = pg->signals[0];
    const SampleBuffer* ptb = psdb->buffers + trialIdx;
    char eventName[MAX_SIGNAL_NAME];

    char* dataPtr = (char*)ptb->data;
    for(unsigned iSample = 0; iSample < ptb->nSamples; iSample++) {
        // first copy string into buffer, then zero terminate it
        unsigned bytesThisSample = ptb->bytesEachSample[iSample];

        // TODO add overflow detection
        memcpy(eventName, dataPtr, bytesThisSample);
        dataPtr += bytesThisSample;
        eventName[bytesThisSample] = '\0';

        //logError("Event %s\n", eventName);

        // search for this eventName in the trie
        EventTrieInfo* info = (EventTrieInfo*)trie_lookup(eventTrie, eventName);
        if(info == NULL) {
            // doesn't exist, give it a TimestampBuffer
            info = (EventTrieInfo*)CALLOC(sizeof(EventTrieInfo), 1);
            strncpy(info->eventName, eventName, MAX_SIGNAL_NAME);
            trie_add(eventTrie, eventName, info);
        }

        // push this timestamp to the buffer
        bool success = pushTimestampToTimestampBuffer(&info->tsBuffer, groupTimestamps->timestamps[iSample]);
        if(!success) {
            logError("Issue building event fields\n");
            return;
        }
    }

    // now iterate over the eventName trie and add each field
    unsigned nEventNames = trie_count(eventTrie);
    mxArray* mxSignalNames = mxCreateCellMatrix(nEventNames, 1);

    unsigned iEvent = 0;
    unsigned fieldNum = 0;
    trieNode = trie_get_first(eventTrie);
    char fieldName[MAX_SIGNAL_NAME];
    while(trieNode != NULL) {
        EventTrieInfo* info = (EventTrieInfo*)trieNode->value;

        // build the groupName_eventName field name
        if(useGroupPrefix)
            snprintf(fieldName, MAX_SIGNAL_NAME, "%s_%s", groupName, info->eventName);
        else
            strncpy(fieldName, info->eventName, MAX_SIGNAL_NAME);

        // store the name of the field in the cell array
        mxSetCell(mxSignalNames, iEvent, mxCreateString(fieldName));

        // copy timestamps from buffer to double vector
        mxArray* mxTimestamps = mxCreateNumericMatrix(info->tsBuffer.nSamples, 1, mxDOUBLE_CLASS, mxREAL);

        // subtract off trial start time and convert to ms, rounding at ms
        double_t* buffer = (double_t*)mxGetData(mxTimestamps);
        for(unsigned i = 0; i < info->tsBuffer.nSamples; i++)
            buffer[i] = round((info->tsBuffer.timestamps[i] - timeTrialStart));

        // add event time list field to trial struct
        fieldNum = mxAddField(mxTrial, fieldName);
        mxSetFieldByNumber(mxTrial, 0, fieldNum, mxTimestamps);

        // get the next event in the trie
        trieNode = trie_get_next(trieNode);
        iEvent++;
    }

    // free the event Trie resources
    trie_flush(eventTrie, FREE);

    // add signal names to the meta array
    fieldNum = mxGetFieldNumber(mxGroupMeta, "signalNames");
    if(fieldNum == -1)
        fieldNum = mxAddField(mxGroupMeta, "signalNames");
    mxSetFieldByNumber(mxGroupMeta, groupMetaIndex, fieldNum, mxSignalNames);

}
コード例 #24
0
ファイル: mime.c プロジェクト: ajrisi/libwebshare
char *mime_lookup(char *extension)
{
  return (char *)trie_lookup(mime_trie, extension);
}
コード例 #25
0
ファイル: phtoelm.c プロジェクト: megrimm/pd-chips
/// Append Holmes-elements for phones in (phonestr) to (eltq).
/// Returns ps->t.
unsigned phone_to_elm(phtoelm_state_t *ps, char *phonestr, dsqueue_t *eltq) {

#ifdef PHOLMES_DEBUG
  post("phone_to_elm(): called with phonestr='%s'", phonestr);
#endif

  ps->s = phonestr;
  while (ps->s && *ps->s) {
    pte_eltseq_str es = trie_lookup(&phtoelm, &(ps->s));
    if (es) {
     int n = *es++;
     while (n-- > 0) {
       int eid = *es++;             // -- index of sequence-element in Elements[]
       holmes_qelt_t he;            // -- encoded Holmes-triple for output-queue
       Elm_ptr ep = &Elements[eid]; // -- pointer to actual current element
       int dur;                     // -- placeholder for element duration

       //
       // This works because only vowels have ud != du,
       //  and we set stress just before a vowel
       //
       if (!(ep->feat & vwl)) ps->stress = 0;
       dur = StressDur(ep,ps->stress);
       he = hqeNew(eid,dur,ps->stress);

       // append the encoded element to the output queue
       dsqueue_append(eltq, (void *)he);

#ifdef PHOLMES_DEBUG
       post("phone_to_elm(): enqueued Holmes-triple %s,%d,%d", ep->name,dur,ps->stress);
#endif
     }
    }
    else {
     char ch = *(ps->s++);
     switch (ch) {
     case '\'':                // Primary stress
       ps->stress = 3;
       break;
     case ',':                 // Secondary stress
       ps->stress = 2;
       break;
     case '+':                 // Tertiary stress
       ps->stress = 1;
       break;
     case '-':                 // hyphen in input
       break;
     case '.':                 // literal dot indicates end-of-utterance
       dsqueue_append(eltq, (void *)hqeNew(0,0,0));
       break;
     default:
       {
	 fprintf(stderr,
		 "phone_to_elm(): ignoring unknown character '%c'\n",
		 ch);
	 break;
       }
     }
    }
  }
  return ps->t;
}