Exemple #1
0
/**
 * Function: TestHashTable
 * -----------------------
 * Runs a test of the hashset using a frequency structure as the element
 * type.  It will open a file, read each char, and count the number of
 * times each char occurs.  Tests enter, lookup, and mapping for the hashset.
 * Prints contents of table to stdout.  Then it dumps all the table elements
 * into a vector and sorts them by frequency of occurrences and
 * prints the array out.  Note that this particular stress test passes
 * 0 as the initialAllocation, which the vector is required to handle
 * gracefully - be careful!
 */
static void TestHashTable(void)
{
  hashset counts;
  vector sortedCounts;

  HashSetNew(&counts, sizeof(struct frequency), kNumBuckets, HashFrequency, CompareLetter, NULL);

  fprintf(stdout, "\n\n ------------------------- Starting the HashTable test\n");
  BuildTableOfLetterCounts(&counts);

  fprintf(stdout, "Here is the unordered contents of the table:\n");
  HashSetMap(&counts, PrintFrequency, stdout);  // print contents of table

  VectorNew(&sortedCounts, sizeof(struct frequency), NULL, 0);
  HashSetMap(&counts, AddFrequency, &sortedCounts);   // add all freq to array
  VectorSort(&sortedCounts, CompareLetter);      // sort by char
  fprintf(stdout, "\nHere are the trials sorted by char: \n");
  VectorMap(&sortedCounts, PrintFrequency, stdout);

  VectorSort(&sortedCounts, CompareOccurrences); //sort by occurrences
  fprintf(stdout, "\nHere are the trials sorted by occurrence & char: \n");
  VectorMap(&sortedCounts, PrintFrequency, stdout);	// print out array

  VectorDispose(&sortedCounts);				// free all storage
  HashSetDispose(&counts);
}
Exemple #2
0
static void ListTopArticles(rssIndexEntry *matchingEntry, vector *previouslySeenArticles)
{
  int i, numArticles, articleIndex, count;
  rssRelevantArticleEntry *relevantArticleEntry;
  rssNewsArticle *relevantArticle;
  
  numArticles = VectorLength(&matchingEntry->relevantArticles);
  printf("Nice! We found %d article%s that include%s the word \"%s\". ", 
	 numArticles, (numArticles == 1) ? "" : "s", (numArticles != 1) ? "" : "s", matchingEntry->meaningfulWord);
  if (numArticles > 10) { printf("[We'll just list 10 of them, though.]"); numArticles = 10; }
  printf("\n\n");
  
  VectorSort(&matchingEntry->relevantArticles, ArticleFrequencyCompare);
  for (i = 0; i < numArticles; i++) {
    relevantArticleEntry = VectorNth(&matchingEntry->relevantArticles, i);
    articleIndex = relevantArticleEntry->articleIndex;
    count = relevantArticleEntry->freq;
    relevantArticle = VectorNth(previouslySeenArticles, articleIndex);
    printf("\t%2d.) \"%s\" [search term occurs %d time%s]\n", i + 1, 
	   relevantArticle->title, count, (count == 1) ? "" : "s");
    printf("\t%2s   \"%s\"\n", "", relevantArticle->fullURL);
  }
  
  printf("\n");
}
static void ProcessValidResponse(const char *word, hashset *stopWords,
				 hashset *wordHash, hashset *articlesSeen)
{
  currWord curr;    
  char* test = strdup(word);      
  curr.thisWord = test;
  currWord* elemAddr = (currWord*)HashSetLookup(wordHash,&curr);

  if(elemAddr != NULL){
    int numArts = VectorLength(&elemAddr->articles);

    if(numArts > 10){
      printf("\nNice! We found %i articles that include the word\"%s\". [We'll just list 10 of them,though.]\n\n",numArts, curr.thisWord);
      numArts = 10;
    } else {
      printf("\nNice! We found %i articles that include the word \"%s\".\n\n",numArts, curr.thisWord);
    }

    VectorSort(&elemAddr->articles, OccurrenceCompare);
    MapFoundArticles(&elemAddr->articles, numArts);

  } else {
    printf("None of today's news articles contain the word \"%s\".\n",word);
  }

  free(test);
}
Exemple #4
0
static void TestSortSearch(vector *alphabet)
{
  VectorSort(alphabet, CompareChar);	 // Sort into order again
  fprintf(stdout, "\nAfter sorting: ");
  VectorMap(alphabet, PrintChar, stdout);
  TestSearch(alphabet, 'J');	// Test searching capabilities
  TestSearch(alphabet, '$');
}
/** 
 * Function: ProcessResponse
 * -------------------------
 * Placeholder implementation for what will become the search of a set of indices
 * for a list of web documents containing the specified word.
 */
static void printResult(indexData* resultData, const char *askedWord){
  if(resultData != NULL){	
    vector resultVector = resultData->data;
    printf("there are %d records of this word",VectorLength(&resultVector));
    VectorSort(&resultVector, SortVectorCmpFn);
    int i=1;
    VectorMap(&resultVector,PrintResultsMapFn,&i);
  
    printf("\n");}
  else printf("\tWe dont't have records about \"%s\" into our set of indices.\n", askedWord);
}
Exemple #6
0
static void ProcessResponse(const char *word, rssData *allData)
{

  if (WordIsWellFormed(word)) {
        void *found = HashSetLookup(&allData->indices, &word); 
	if (found != NULL) {
	  indexEntry *entry = (indexEntry*)found;
	  VectorSort(&entry->articles, ReverseWordcountCmp); 
	  VectorMap(&entry->articles, PrintArticle, &allData->explored);
	  	  
	} else {
	  printf("\tWord not found in our indices\n");
	} 
  } else {
	printf("\tWe won't be allowing words like \"%s\" into our set of indices.\n", word);
  }
  
}  
Exemple #7
0
static void SortPermutation(vector *vectorToSort)
{
  long residue, embeddedLong;
  vector *sortedVector;
  fprintf(stdout, "Sorting all of those numbers. ");
  fflush(stdout);
  VectorSort(vectorToSort, LongCompare);
  fprintf(stdout, "[Done]\n");
  fflush(stdout);
  fprintf(stdout, "Confirming everything was properly sorted. ");
  fflush(stdout);
  sortedVector = vectorToSort; // need better name now that it's sorted... 
  for (residue = 0; residue < VectorLength(sortedVector); residue++) {
    embeddedLong = *(const long *) VectorNth(sortedVector, residue);
    assert(embeddedLong == residue);
  }
  fprintf(stdout, "[Yep, it's sorted]\n");
  fflush(stdout);
}
Exemple #8
0
static void ProcessResponse(const char *word, void *userData)
{
  if (WordIsWellFormed(word)) {
	rssFeedData *data = userData;
	if(HashSetLookup(&data->stopWords, &word)==NULL) {
		indexData *resultData = HashSetLookup(&data->indices, &word);
		if(resultData!=NULL) {
			vector resultVector = resultData->counters;
			printf("there are %d records of this word", VectorLength(&resultVector));
			VectorSort(&resultVector, SortVectorCmpFn);
			int i=1;
			VectorMap(&resultVector, PrintResultMapFn, &i);
			printf("\n");
		} else {
			printf("\tWe don't have records about %s into our set of indices.\n", word);
		}
	}
    
  } else {
    printf("\tWe won't be allowing words like \"%s\" into our set of indices.\n", word);
  }
}
Exemple #9
0
void ManipulateNumerics()
{
    /* We should initialize the container before any operations. */
    Vector* vector = VectorInit(DEFAULT_CAPACITY);

    /* Push the integer elements. */
    VectorPushBack(vector, (void*)(intptr_t)3);
    VectorPushBack(vector, (void*)(intptr_t)4);

    /* Insert the elements at the specified indexes. */
    VectorInsert(vector, 0, (void*)(intptr_t)1);
    VectorInsert(vector, 1, (void*)(intptr_t)2);

    /*---------------------------------------------------------------*
     * Now the vector should be: [1] | [2] | [3] | [4]               *
     *---------------------------------------------------------------*/

    /* Iterate through the vector. */
    int num = 1;
    void* elem;
    VectorFirst(vector, false);
    while (VectorNext(vector, &elem)) {
        assert((intptr_t)(void*)elem == num);
        ++num;
    }

    /* Reversely iterate through the vector. */
    num = 4;
    VectorFirst(vector, true);
    while (VectorReverseNext(vector, &elem)) {
        assert((intptr_t)(void*)elem == num);
        --num;
    }

    /* Get the elements from the specified indexes. */
    VectorGet(vector, 0, &elem);
    assert((intptr_t)(void*)elem == 1);
    VectorGet(vector, 3, &elem);
    assert((intptr_t)(void*)elem == 4);

    /* Replace the elements at the specified indexes. */
    VectorSet(vector, 0, (void*)(intptr_t)10);
    VectorSet(vector, 3, (void*)(intptr_t)40);

    /*---------------------------------------------------------------*
     * Now the vector should be: [10] | [2] | [3] | [40]             *
     *---------------------------------------------------------------*/

    /* Get the number of stored elements. */
    unsigned size = VectorSize(vector);
    assert(size == 4);

    /* Sort the integer elements. */
    VectorSort(vector, CompareNumber);

    /*---------------------------------------------------------------*
     * Now the vector should be: [2] | [3] | [10] | [40]             *
     *---------------------------------------------------------------*/

    /* Remove the elements at the specified indexes. */
    VectorRemove(vector, 3);
    VectorRemove(vector, 0);

    /*---------------------------------------------------------------*
     * Now the vector should be: [3] | [10]                          *
     *---------------------------------------------------------------*/

    VectorGet(vector, 0, &elem);
    assert((int)(intptr_t)elem == 3);
    VectorGet(vector, 1, &elem);
    assert((int)(intptr_t)elem == 10);

    /* Pop the elements. */
    VectorPopBack(vector);
    VectorPopBack(vector);
    assert(VectorSize(vector) == 0);

    VectorDeinit(vector);
}
Exemple #10
0
void ManipulateObjects()
{
    /* We should initialize the container before any operations. */
    Vector* vector = VectorInit(DEFAULT_CAPACITY);
    VectorSetClean(vector, CleanObject);

    /* Push the object elements. */
    Tuple* tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 3;
    tuple->second = -3;
    VectorPushBack(vector, tuple);
    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 4;
    tuple->second = -4;
    VectorPushBack(vector, tuple);

    /* Insert the elements at the specified indexes. */
    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 1;
    tuple->second = -1;
    VectorInsert(vector, 0, tuple);
    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 2;
    tuple->second = -2;
    VectorInsert(vector, 1, tuple);

    /*---------------------------------------------------------------*
     * Now the vector should be: [1] | [2] | [3] | [4]               *
     *---------------------------------------------------------------*/

    /* Iterate through the vector. */
    int num = 1;
    void* elem;
    VectorFirst(vector, false);
    while (VectorNext(vector, &elem)) {
        assert(((Tuple*)elem)->first == num);
        ++num;
    }

    /* Reversely iterate through the vector. */
    num = 4;
    VectorFirst(vector, true);
    while (VectorReverseNext(vector, &elem)) {
        assert(((Tuple*)elem)->first == num);
        --num;
    }

    /* Get the elements from the specified indexes. */
    VectorGet(vector, 0, &elem);
    assert(((Tuple*)elem)->first == 1);
    VectorGet(vector, 3, &elem);
    assert(((Tuple*)elem)->first == 4);

    /* Replace the elements at the specified indexes. */
    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 10;
    tuple->second = -10;
    VectorSet(vector, 0, tuple);
    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 40;
    tuple->second = -40;
    VectorSet(vector, 3, tuple);

    /*---------------------------------------------------------------*
     * Now the vector should be: [10] | [2] | [3] | [40]             *
     *---------------------------------------------------------------*/

    /* Get the number of stored elements. */
    unsigned size = VectorSize(vector);
    assert(size == 4);

    /* Sort the integer elements. */
    VectorSort(vector, CompareObject);

    /*---------------------------------------------------------------*
     * Now the vector should be: [2] | [3] | [10] | [40]             *
     *---------------------------------------------------------------*/

    /* Remove the elements at the specified indexes. */
    VectorRemove(vector, 3);
    VectorRemove(vector, 0);

    /*---------------------------------------------------------------*
     * Now the vector should be: [3] | [10]                          *
     *---------------------------------------------------------------*/

    VectorGet(vector, 0, &elem);
    assert(((Tuple*)elem)->first == 3);
    VectorGet(vector, 1, &elem);
    assert(((Tuple*)elem)->first == 10);

    /* Pop the elements. */
    VectorPopBack(vector);
    VectorPopBack(vector);
    assert(VectorSize(vector) == 0);

    VectorDeinit(vector);
}
void WordCountSortMapFn(void *elemAddr, void *auxData)
{
    wordSet *ws = (wordSet *) elemAddr;
    VectorSort(&ws->occ, WordCountSortCompareFn);
}