Ejemplo n.º 1
0
Archivo: main.c Proyecto: shtef12/CS107
/**
 * 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);
}
Ejemplo n.º 2
0
//INDEX HASHSET HELPER FUNCTIONS
static void IndexMap(void*elemAddr,void*auxData){
  indexData *data = elemAddr;  
  printf("\n word:::::::::::::::::::%s:::::::::::::::::::::::::::::::: ",  data->word);
  fflush(stdout);
  int i=0;  
  VectorMap(&data->data,PrintResultsMapFn,&i);
}
Ejemplo n.º 3
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, '$');
}
Ejemplo n.º 4
0
static void TestAppend(vector *alphabet)
{
  char ch;
  int i;
  
  for (ch = 'A'; ch <= 'Z'; ch++)   //  Start with letters of alphabet
    VectorAppend(alphabet, &ch);
  fprintf(stdout, "First, here is the alphabet: ");
  VectorMap(alphabet, PrintChar, stdout);
  
  for (i = 0; i < 10; i++) {	    // Append digit characters
    ch = '0' + i;                   // convert int to ASCII digit character
    VectorAppend(alphabet, &ch);
  }
  fprintf(stdout, "\nAfter append digits: ");
  VectorMap(alphabet, PrintChar, stdout);
}
Ejemplo n.º 5
0
void HashSetMap(hashset *h, HashSetMapFunction mapfn, void *auxData)
{
  assert(mapfn!=NULL);
  for(int i=0; i<h->numBuckets; i++)
    {
      VectorMap((h->buckets+i), mapfn, auxData);
    }
}
Ejemplo n.º 6
0
void HashSetMap(hashset *h, HashSetMapFunction mapfn, void *auxData){
	assert(mapfn != NULL && "mapping routine is NULL");
	int i;
	vector *vAddress;
	for(int i = 0; i < h->numBuckets; i++){
		vAddress = h->buckets + i;
		VectorMap(vAddress, mapfn, auxData);
	}
}
Ejemplo n.º 7
0
static void TestAt(vector *alphabet)
{
  int i;
  
  for (i = 0; i < VectorLength(alphabet); i += 2) { // Lowercase every other
    char *elem = (char *) VectorNth(alphabet, i);
    *elem = tolower(*elem);
  }
  fprintf(stdout, "\nAfter lowercase every other letter: ");
  VectorMap(alphabet, PrintChar, stdout);
}
Ejemplo n.º 8
0
/** 
 * 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);
}
Ejemplo n.º 9
0
static void TestInsertDelete(vector *alphabet)
{
  char ch = '-';
  int i;
  
  for (i = 3; i < VectorLength(alphabet); i += 4) // Insert dash every 4th char 
    VectorInsert(alphabet, &ch, i);
  fprintf(stdout, "\nAfter insert dashes: ");
  VectorMap(alphabet, PrintChar, stdout);
  
  for (i = 3; i < VectorLength(alphabet); i += 3) // Delete every 4th char 
    VectorDelete(alphabet, i);
  fprintf(stdout, "\nAfter deleting dashes: ");
  VectorMap(alphabet, PrintChar, stdout);
    
  ch = '!';
  VectorInsert(alphabet, &ch, VectorLength(alphabet));
  VectorDelete(alphabet, VectorLength(alphabet) - 1);
  fprintf(stdout, "\nAfter adding and deleting to very end: ");
  VectorMap(alphabet, PrintChar, stdout);
}
Ejemplo n.º 10
0
static void TestReplace(vector *alphabet)
{
  int found = 0;
  char toFind = 's', toReplace = '*';
  
  while (found < VectorLength(alphabet)) {
    found = VectorSearch(alphabet, &toFind, CompareChar, found, false);
    if (found == -1) break;
    VectorReplace(alphabet, &toReplace, found);
  }
  
  fprintf(stdout, "\nAfter changing all %c to %c: ", toFind, toReplace);
  VectorMap(alphabet, PrintChar, stdout);
}
Ejemplo n.º 11
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);
  }
  
}  
void VectorLocalization2D::loadAtlas()
{
  static const bool debug = true;
  string atlasFile = mapsFolder + "/atlas.txt";
  FILE* fid = fopen(atlasFile.c_str(),"r");
  if(fid==NULL){
    TerminalWarning("Unable to load Atlas!");
    return;
  }
  char mapName[4096];
  int mapNum;
  if(debug) printf("Loading Atlas...\n");
  maps.clear();
  while(fscanf(fid,"%d %s\n",&mapNum,mapName)==2){
    if(debug) printf("Loading map %s\n",mapName);
    maps.push_back(VectorMap(mapName,mapsFolder.c_str(),true));
  }
  if(maps.size()>0)
    currentMap = &maps[0];
  if(debug) printf("Done Loading Atlas.\n");
}
Ejemplo n.º 13
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);
  }
}
Ejemplo n.º 14
0
static void MemoryTest()
{
  int i;
  const char * const kQuestionWords[] = {"who", "what", "where", "how", "why"};
  const int kNumQuestionWords = sizeof(kQuestionWords) / sizeof(kQuestionWords[0]);
  vector questionWords;
  char *questionWord;
  
  fprintf(stdout, "\n\n------------------------- Starting the memory tests...\n");
  fprintf(stdout, "Creating a vector designed to store dynamically allocated C-strings.\n");
  VectorNew(&questionWords, sizeof(char *), FreeString, kNumQuestionWords);
  fprintf(stdout, "Populating the char * vector with the question words.\n");
  for (i = 0; i < kNumQuestionWords; i++) {
    questionWord = malloc(strlen(kQuestionWords[i]) + 1);
    strcpy(questionWord, kQuestionWords[i]);
    VectorInsert(&questionWords, &questionWord, 0);  // why the ampersand? isn't questionWord already a pointer?
  }
  
  fprintf(stdout, "Mapping over the char * vector (ask yourself: why are char **'s passed to PrintString?!!)\n");
  VectorMap(&questionWords, PrintString, stdout);
  fprintf(stdout, "Finally, destroying the char * vector.\n");
  VectorDispose(&questionWords);
}