Пример #1
0
void trie(FILE *pool,FILE *check,FILE *result) {
    TRIE *head = trie_create();
    char line[BUFFERSIZE];
    int exitflag=0;
    int i;
    while(fgets(line,BUFFERSIZE,pool)) {
        /*delete the useless character '\r'*/
        exitflag = trimString(line);
        if(!exitflag) {
            reverseString(line);
            trie_add(&head,line);
        } else {
            /*printf("Error email %s",line);*/
            continue;
        }
    }
    while(fgets(line,BUFFERSIZE,check)) {

        i = 0;
        while(line[i]!='\r' && line[i]!='\n') i++;
        line[i] = '\0';
        exitflag = trimString(line);
        if(!exitflag) {
            reverseString(line);
            if(trie_check(&head,line)) {
                fprintf(result,"yes\n");
            } else {
                fprintf(result,"no\n");
            }
        }
    }
    trie_destroy(&head);
}
Пример #2
0
int Trie_init(Trie *self, PyObject *args, PyObject *kwds)
{
    unsigned int order = 5;
    static char *kwlist[] = {"order", NULL};
    
    self->__safe_for_unpickling__ = 1;
    if (! PyArg_ParseTupleAndKeywords(args, kwds, "|I", kwlist, &order)) {
        PyErr_Print();
        return -1;
    }
    if (self == NULL) {
        return -1;
    } else {
        self->trie = trie_create(order);
        return 0;
    }
}
Пример #3
0
void trie(FILE *pool,FILE *check,FILE *result) {
	clock_t start,end,start1;
	start = clock();
	TRIE *head = trie_create();
	char line[BUFFERSIZE];
	int count=0;
	int i=0;
	int exitflag=0;
	while(fgets(line,BUFFERSIZE,pool)) {
		/*delete the useless character '\r'*/
		exitflag = trimString(line);
		if(!exitflag){
			reverseString(line);
			trie_add(&head,line);	
		//	if(!(++count%100000)){ 
		//		end = clock();
		//		printf("%d,%f \n",count++,(double)(end -start)/CLOCKS_PER_SEC);
		//	} 
		}else{
			/*printf("Error email %s",line);*/
			continue;
		}
	}
	//end = clock();
	//printf("Creating tree using %f\n",(double)(end -start)/CLOCKS_PER_SEC);
	start1 = clock();
	while(fgets(line,BUFFERSIZE,check)) {

		i = 0;
		while(line[i]!='\r' && line[i]!='\n') i++;
		line[i] = '\0';
		exitflag = trimString(line);
		if(!exitflag){
			reverseString(line);
			if(trie_check(&head,line)) {
				fprintf(result,"yes\n");
			}else {
				fprintf(result,"no\n");
			}
		}
	}
	trie_destroy(&head);
	end = clock();
	printf("%f\n",(double)(end -start)/CLOCKS_PER_SEC);

}
Пример #4
0
void trie_add(TRIE *head, char *str, void *object) {
	TRIE *trie = head;
	char *p = str;
	
	if(*str == '\0' || str == 0) return;
	
	while(1) {
		if(trie->nodes[(int)*p] == 0) {
			trie->nodes[(int)*p] = trie_create();
		}
		trie = trie->nodes[(int)*p];
		if(*(p+1) == '\0')
			break;
		p++;
	}
	trie->object = object;
}
Пример #5
0
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;
}
Пример #6
0
/* Gets node which might involve its creation.  Sets *return to negative value
 * on error, to zero on successful insertion and to positive number if element
 * was already in the trie. */
static void
get_or_create(trie_t trie, const char str[], void *data, int *result)
{
	trie_t *link = ≜
	while(1)
	{
		/* Create inexistent node. */
		if(trie == NULL_TRIE)
		{
			trie = trie_create();
			if(trie == NULL_TRIE)
			{
				*result = -1;
				break;
			}
			trie->value = *str;
			*link = trie;
		}

		if(trie->value == *str)
		{
			if(*str == '\0')
			{
				/* Found full match. */
				*result = (trie->exists != 0);
				trie->exists = 1;
				trie->data = data;
				break;
			}

			link = &trie->children;
			++str;
		}
		else
		{
			link = (*str < trie->value) ? &trie->left : &trie->right;
		}
		trie = *link;
	}
}
Пример #7
0
int main()
{
	trie_t t;
	trie_create(&t, sizeof(int));

	char* nome = "yudi";
	int i = 50;
	trie_add_element(&t, nome, strlen(nome), &i);

	nome = "";
	i = -1;
	trie_add_element(&t, nome, strlen(nome), &i);

	nome = "barbara";
	i = 100;
	trie_add_element(&t, nome, strlen(nome), &i);

	nome = "raul";
	i = 10;
	trie_add_element(&t, nome, strlen(nome), &i);

	nome = "tutu";
	i = 1;
	trie_add_element(&t, nome, strlen(nome), &i);

	nome = "boris";
	i = 100000;
	trie_add_element(&t, nome, strlen(nome), &i);

	print_lexicography(&t);
	printf("remove: %s\n", nome);
	trie_remove_element(&t, nome, strlen(nome));
	print_lexicography(&t);

	trie_destroy(&t);

	return 0;
}
Пример #8
0
int intern_init(struct intern *pool)
{
    pool->trie = trie_create();
    return pool->trie == NULL ? -1 : 0;
}
Пример #9
0
 *  documentation are those of the authors and should not be interpreted
 *  as representing official policies, either expressed.
 */

#include "trie.h"
#include "unit_test.h"

void dealloc(void *val)
{
   free(val);
}

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);
Пример #10
0
int
compare_two_panes(CompareType ct, ListType lt, int group_paths, int skip_empty)
{
	int next_id = 1;
	entries_t curr, other;

	trie_t *const trie = trie_create();
	ui_cancellation_reset();
	ui_cancellation_enable();

	curr = make_diff_list(trie, curr_view, &next_id, ct, skip_empty, 0);
	other = make_diff_list(trie, other_view, &next_id, ct, skip_empty,
			lt == LT_DUPS);

	ui_cancellation_disable();
	trie_free_with_data(trie, &free_compare_records);

	/* Clear progress message displayed by make_diff_list(). */
	ui_sb_quick_msg_clear();

	if(ui_cancellation_requested())
	{
		free_dir_entries(curr_view, &curr.entries, &curr.nentries);
		free_dir_entries(other_view, &other.entries, &other.nentries);
		status_bar_message("Comparison has been cancelled");
		return 1;
	}

	if(!group_paths || lt != LT_ALL)
	{
		/* Sort both lists according to unique file numbers to group identical files
		 * (sorting is stable, tags are set in make_diff_list()). */
		qsort(curr.entries, curr.nentries, sizeof(*curr.entries), &id_sorter);
		qsort(other.entries, other.nentries, sizeof(*other.entries), &id_sorter);
	}

	if(lt == LT_UNIQUE)
	{
		make_unique_lists(curr, other);
		return 0;
	}

	if(lt == LT_DUPS)
	{
		leave_only_dups(&curr, &other);
	}

	flist_custom_start(curr_view, lt == LT_ALL ? "diff" : "dups diff");
	flist_custom_start(other_view, lt == LT_ALL ? "diff" : "dups diff");

	fill_side_by_side(curr, other, group_paths);

	if(flist_custom_finish(curr_view, CV_DIFF, 0) != 0)
	{
		show_error_msg("Comparison", "No results to display");
		return 0;
	}
	if(flist_custom_finish(other_view, CV_DIFF, 0) != 0)
	{
		assert(0 && "The error shouldn't be happening here.");
	}

	curr_view->list_pos = 0;
	other_view->list_pos = 0;
	curr_view->custom.diff_cmp_type = ct;
	other_view->custom.diff_cmp_type = ct;
	curr_view->custom.diff_path_group = group_paths;
	other_view->custom.diff_path_group = group_paths;

	assert(curr_view->list_rows == other_view->list_rows &&
			"Diff views must be in sync!");

	ui_view_schedule_redraw(curr_view);
	ui_view_schedule_redraw(other_view);
	return 0;
}
Пример #11
0
int
compare_one_pane(FileView *view, CompareType ct, ListType lt, int skip_empty)
{
	int i, dup_id;
	FileView *other = (view == curr_view) ? other_view : curr_view;
	const char *const title = (lt == LT_ALL)  ? "compare"
	                        : (lt == LT_DUPS) ? "dups" : "nondups";

	int next_id = 1;
	entries_t curr;

	trie_t *trie = trie_create();
	ui_cancellation_reset();
	ui_cancellation_enable();

	curr = make_diff_list(trie, view, &next_id, ct, skip_empty, 0);

	ui_cancellation_disable();
	trie_free_with_data(trie, &free_compare_records);

	/* Clear progress message displayed by make_diff_list(). */
	ui_sb_quick_msg_clear();

	if(ui_cancellation_requested())
	{
		free_dir_entries(view, &curr.entries, &curr.nentries);
		status_bar_message("Comparison has been cancelled");
		return 1;
	}

	qsort(curr.entries, curr.nentries, sizeof(*curr.entries), &id_sorter);

	flist_custom_start(view, title);

	dup_id = -1;
	next_id = 0;
	for(i = 0; i < curr.nentries; ++i)
	{
		dir_entry_t *entry = &curr.entries[i];

		if(lt == LT_ALL)
		{
			flist_custom_put(view, entry);
			continue;
		}

		if(entry->id == dup_id)
		{
			put_or_free(view, entry, next_id, lt == LT_DUPS);
			continue;
		}

		dup_id = (i < curr.nentries - 1 && entry[0].id == entry[1].id)
		       ? entry->id
		       : -1;

		if(entry->id == dup_id)
		{
			put_or_free(view, entry, ++next_id, lt == LT_DUPS);
			continue;
		}

		put_or_free(view, entry, next_id, lt == LT_UNIQUE);
	}

	/* Entries' data has been moved out of them or freed, so need to free only the
	 * list. */
	dynarray_free(curr.entries);

	if(flist_custom_finish(view, lt == LT_UNIQUE ? CV_REGULAR : CV_COMPARE,
				0) != 0)
	{
		show_error_msg("Comparison", "No results to display");
		return 0;
	}

	/* Leave the other pane, if it's in the CV_DIFF mode, two panes are needed for
	 * this. */
	if(other->custom.type == CV_DIFF)
	{
		cd_updir(other, 1);
	}

	view->list_pos = 0;
	ui_view_schedule_redraw(view);
	return 0;
}
Пример #12
0
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);

}