// Add word to the tableand position. Position is added to the corresponding linked list.
void wtable_add(WordTable * wtable, char * word, int position)
{
	// Find first word if it exists
	for (int i = 0; i < wtable->nWords; i++) {
		if ( strcmp(wtable->wordArray[i].word, word)== 0 ) {
			// Found word. Add position in the list of positions
			llist_insert_last(&wtable->wordArray[i].positions, position);
			return;
		}
	}

	if (wtable-> nWords == wtable->maxWords) {
	    wtable->maxWords = wtable->maxWords*2;
	    int newsize = wtable->maxWords*sizeof(WordInfo);
	    wtable->wordArray = (WordInfo*) realloc(wtable->wordArray, newsize);
	    for (int i = wtable->nWords; i < wtable->maxWords; i++) {
	        llist_init(&wtable->wordArray[i].positions);
	    }
	}
	
	// Word not found.

	// Make sure that the array has space.
	// Expand the wordArray here.

	// Add new word and position
	
	wtable->wordArray[wtable->nWords].word = strdup(word);
	llist_insert_last(&wtable->wordArray[wtable->nWords].positions, position);
	wtable->nWords++;
}
Пример #2
0
// Add word to the tableand position. Position is added to the corresponding linked list.
void wtable_add(WordTable * wtable, char * word, int position)
{
	// Find first word if it exists
	for (int i = 0; i < wtable->nWords; i++) {
		if ( strcmp(wtable->wordArray[i].word, word)== 0 ) {
			// Found word. Add position in the list of positions
			llist_insert_last(&wtable->wordArray[i].positions, position);
			return;
		}
	}

	// Word not found.

	// Make sure that the array has space.
	// Expand the wordArray here.
    if (wtable->nWords >= wtable->maxWords) {
        //wtable->maxWords *= 2;
        //wtable->wordArray = (WordInfo *) realloc(wtable->wordArray, (wtable->maxWords) * sizeof(WordInfo));
        
        //for (int i = wtable->maxWords; i < wtable->maxWords * 2; i++) {
        //    llist_init(&wtable->wordArray[i].positions);
        //}
        //int temp = wtable->nWords;
        //int tempMax = wtable->maxWords;
        wtable_reinit(wtable);
        //wtable->nWords = temp;
        //wtable->maxWords += tempMax;
        //for (int i = 0; i < wtable->maxWords * 2; i++) {
        //    llist_init(&wtable->wordArray[i].positions);
        //}
    }
    //wtable->maxWords = wtable->maxWords * 2;
    //for (int i = wtable->nWords; i < wtable->maxWords; i++) {
    //    llist_init(&wtable->wordArray[i].positions);
    //}
    //wtable_init(wtable);

	// Add new word and position
	wtable->wordArray[wtable->nWords].word = strdup(word);
	llist_insert_last(&wtable->wordArray[wtable->nWords].positions, position);
	wtable->nWords++;
    //count++;
    //printf("nWords %d\n", wtable->nWords);

}
Пример #3
0
extern void symtab_add(t_symtab *symtab, const char *s)
{
   t_symrec *symrec = calloc(sizeof(t_symrec), 1);
   if (symrec == NULL) {
      error_message("Out of memory when creating a symbol table record");
      exit(-1);
   }
   symrec->symtab = symtab;
   symrec->s = dstrcpy(s);
   llist_insert_last(symtab->list, symrec);
   if (symtab->lookup_table) {
      hash_insert(symtab->lookup_table, symrec->s, symrec);
   }
}