示例#1
0
文件: wf.c 项目: dulton/tinybus
int main(int argc, char *argv[])
{
	FILE *in = stdin;
	char buf[0x4000]; /* limited to 16K character word length */
	cp_avltree *wordlist;
	cp_avltree *freqlist;
	int count = 0;
	int one = 1;
	int *wc;

	if (argc > 1) 
	{
		in = fopen(argv[1], "r");
		if (in == NULL)
		{
			fprintf(stderr, "can\'t open %s\n", argv[1]);
			exit(1);
		}
	}
	
	/* wordlist keys are distinct */
	wordlist = 
		cp_avltree_create_by_option(COLLECTION_MODE_NOSYNC | 
									COLLECTION_MODE_COPY | 
									COLLECTION_MODE_DEEP, 
									(cp_compare_fn) strcmp, 
									(cp_copy_fn) strdup, free, 
									(cp_copy_fn) intdup, free);
	
	while ((fscanf(in, "%s", buf)) != EOF)
	{
		filter_string(buf, PUNCT);
		to_lowercase(buf);
		wc = cp_avltree_get(wordlist, buf);
		if (wc) 
			(*wc)++;
		else
			cp_avltree_insert(wordlist, buf, &one);
		count++;
	}

	fclose(in);

	/* frequency list entry keys are not distinct */
	freqlist = 	
		cp_avltree_create_by_option(COLLECTION_MODE_NOSYNC | 
									COLLECTION_MODE_MULTIPLE_VALUES,
									(cp_compare_fn) freqcmp, 
									NULL, NULL, NULL, NULL);
	
	cp_avltree_callback(wordlist, add_freq, freqlist);
	cp_avltree_callback(freqlist, print_word_freq, &one);
	printf("%d words, %d distinct entries\n", count, cp_avltree_count(wordlist));

	cp_avltree_destroy(freqlist);
	cp_avltree_destroy(wordlist);

	return 0;
}
示例#2
0
void *cp_avltree_find(cp_avltree *tree, void *key, cp_op op)
{
	void *res = NULL;
	if (op == CP_OP_EQ) return cp_avltree_get(tree, key);

	/* lock unless COLLECTION_MODE_NOSYNC is set or this thread owns the tx */
	if (cp_avltree_txlock(tree, COLLECTION_LOCK_READ)) return NULL;

	res = avltree_find_internal(tree, tree->root, key, op);

	/* unlock unless COLLECTION_MODE_NOSYNC set or this thread owns the tx */
	cp_avltree_txunlock(tree);

	return res;
}
示例#3
0
allocate_splice_elements_t* allocate_new_splice(unsigned int chromosome, unsigned char strand, 
						size_t end, size_t start, 
						size_t splice_start, size_t splice_end, 
						allocate_splice_elements_t* chromosome_avls_p){
  node_element_splice_t *node;

  node = (node_element_splice_t *)cp_avltree_get(chromosome_avls_p[chromosome].avl_splice, (void *)start);

  if(node == NULL) {
    node = cp_avltree_insert(chromosome_avls_p[chromosome].avl_splice, (void *)start, (void *)start);
    node->splice_start_extend = splice_start;
  }
  
  node = search_and_insert_end_splice(chromosome, strand, end, splice_start, splice_end, node);

  return chromosome_avls_p;
}
示例#4
0
int cp_avltree_contains(cp_avltree *tree, void *key)
{
	return (cp_avltree_get(tree, key) != NULL);
}