//-----------------------------------------------------------------------------------------// int main() { hash_table_t *t = CreateHashTable(); { Set( t, u("abc"), 3, u("ABC"), 3 ); uint8_t tmp[4]; size_t rs = 0; Get( t, u("abc"), 3, tmp, sizeof( tmp ), &rs ); tmp[rs] = '\0'; assert( rs == 3 ); assert( memcmp( tmp, "ABC", rs ) == 0 ); } { for( size_t i = 0 ; i < 0xffff; ++i ) { assert( Set( t, u( &i ), sizeof( i ), u( &i ), sizeof( i ) ) ); } for( size_t i = 0 ; i < 0xffff; ++i ) { size_t n = 0; size_t s = 0; assert( Get( t, u( &i ), sizeof( i ), u( &n ), sizeof( n ), &s ) == true ); assert( s == sizeof( i ) ); assert( memcmp( &i, &n, s ) == 0 ); } } { for( size_t i = 0 ; i < 0xffff; ++i ) { Set( t, u( &i ), sizeof( i ), u( &i ), sizeof( i ) ); } for( size_t i = 0 ; i < 0xffff; ++i ) { assert( Remove( t, u( &i ), sizeof( i ) ) ); } for( size_t i = 0 ; i < 0xffff; ++i ) { size_t n = 0; assert( Get( t, u( &i ), sizeof( i ), u( &n ), sizeof( n ), NULL ) == false ); } } { assert( Set( t, u( "abc" ), 3, u( "ABC" ), 3 ) ); assert( Append( t, u( "abc" ), 3, u( "DEF" ), 3 ) ); uint8_t tmp[6]; size_t n = 0; assert( Get( t, u( "abc" ), 3, tmp, 6, &n ) == true ); assert( n == 6 ); assert( memcmp( "ABCDEF", tmp, 6 ) == 0 ); } DeleteHashTable( t ); return 0; }
static inline void s_clean(mccp_hashmap_t hm, bool free_values) { if (free_values == true) { s_freeup_all_values(hm); } DeleteHashTable(&(hm->m_hashtable)); (void)memset(&(hm->m_hashtable), 0, sizeof(HashTable)); hm->m_n_entries = 0; }
void DeleteMap(Map* mp) { if (!mp) return; unsigned int i; for (i=0; i<mp->tb->size; i++) { void* ls = mp->tb->entries[i]; if (ls&&ls!=mp->tb->dummy) DeleteList(ls); } DeleteHashTable(mp->tb); free(mp); }
void main (int argc, char *argv[]) { if (argc < 2) { puts ("\nMapTool v1.0 by Erick Jap\n"); puts ("Usage: MapTool mapfile -d width height -w width height [-t tileno] [-s setno]"); puts ("where: mapfile is map file data to be used"); puts (" -d width height indicate map dimension"); puts (" -w width height indicate sliding window dimension"); puts (" -t tileno --> return area with >= than tileno tiles"); puts (" -s setno --> return area with >= than setno sets\n"); puts ("Note: - Sliding window dimension must be less than the data dimension"); puts (" - if tileno is 0, calculate coordinate of area with max number of tiles"); puts (" - if setno is 0, calculate coordinate of area with max number of sets\n"); exit (1); } int i, j, flag; int maxtileno, maxsetno; flag = 0; for (i=2;i < argc;i++) { if (argv[i][0] == '-') { if (argv[i][1] == 'd' || argv[i][1] == 'D') { flag |= HAS_DATA_DIMENSION; datawidth = atoi(argv[++i]); dataheight = atoi(argv[++i]); } else if (argv[i][1] == 'w' || argv[i][1] == 'W') { flag |= HAS_WINDOW_DIMENSION; windowwidth = atoi(argv[++i]); windowheight = atoi(argv[++i]); } else if (argv[i][1] == 't' || argv[i][1] == 'T') { maxtileno = atoi(argv[++i]); if (maxtileno) flag |= HAS_MAX_TILE; else flag |= CALCULATE_COORD_TILE; } else if (argv[i][1] == 's' || argv[i][1] == 'S') { maxsetno = atoi(argv[++i]); if (maxsetno) flag |= HAS_MAX_SET; else flag |= CALCULATE_COORD_SET; } } } if (!(flag & HAS_DATA_DIMENSION)) { puts ("You forget to enter the map dimension!"); exit (1); } if (!(flag & HAS_WINDOW_DIMENSION)) { puts ("You forget to enter the sliding window dimension!"); exit (1); } if (!(flag & HAS_QUERY)) { puts ("What do you want?"); exit (1); } if (windowwidth > datawidth) { puts ("Sliding window width must be less than the width of the map!"); exit (1); } if (windowheight > dataheight) { puts ("Sliding window height must be less than the height of the map!"); exit (1); } i = datawidth * dataheight * 2; databuffer = (unsigned short *) malloc (i); if (!databuffer) { printf ("Can not allocate memory to load map data!\n"); exit (1); } int infile = open (argv[1], O_BINARY | O_RDONLY); if (!infile) { printf ("Can not open map file %s\n", argv[1]); free (databuffer); exit (1); } read (infile, (char *) databuffer, i); close (infile); unsigned short *buff; int numwidth = datawidth - windowwidth + 1; int numheight = dataheight - windowheight + 1; if ((flag & HAS_MAX_TILE) || (flag & CALCULATE_COORD_TILE)) { int savetrow, savetcol, totaltile, curtile, maxtile; memset (HashRow, 0, HASH_MAX_SIZE * sizeof (ElementList *)); totaltile = CreateTileListElement (HashRow); maxtile = 0; buff = databuffer; for (i=0; i< numheight; i++) { DuplicateHashTable (HashRow, HashCol); curtile = totaltile; for (j=0; j< numwidth; j++) { if (flag & HAS_MAX_TILE) { if (curtile >= maxtileno) { printf ("TILE(%d) - %d %d\n", curtile, i, j); } } if (flag & CALCULATE_COORD_TILE) { if (curtile > maxtile) { maxtile = curtile; savetrow = i; savetcol = j; } } if (j < (numwidth - 1)) { curtile = InsertTileColElement (HashCol, curtile, &(buff[j+windowwidth])); curtile = DeleteTileColElement (HashCol, curtile, &(buff[j])); } } if (i < (numheight - 1)) { totaltile = InsertTileRowElement (HashRow, totaltile, buff + datawidth*windowheight); totaltile = DeleteTileRowElement (HashRow, totaltile, buff); buff += datawidth; } DeleteHashTable (HashCol); } if (flag & CALCULATE_COORD_TILE) printf ("MaxTile (%d) Coordinate %d %d\n", maxtile, savetrow, savetcol); DeleteHashTable (HashRow); } if ((flag & HAS_MAX_SET) || (flag & CALCULATE_COORD_SET)) { int savesrow, savescol, totalset, curset, maxset; memset (HashRow, 0, HASH_MAX_SIZE * sizeof (ElementList *)); totalset = CreateSetListElement (HashRow); maxset = 0; buff = databuffer; for (i=0; i< numheight; i++) { DuplicateHashTable (HashRow, HashCol); curset = totalset; for (j=0; j< numwidth; j++) { if (flag & HAS_MAX_SET) { if (curset >= maxsetno) { printf ("SET(%d) - %d %d\n", curset, i, j); } } if (flag & CALCULATE_COORD_SET) { if (curset > maxset) { maxset = curset; savesrow = i; savescol = j; } } if (j < (numwidth - 1)) { curset = InsertSetColElement (HashCol, curset, &(buff[j+windowwidth])); curset = DeleteSetColElement (HashCol, curset, &(buff[j])); } } if (i < (numheight - 1)) { totalset = InsertSetRowElement (HashRow, totalset, buff + datawidth*windowheight); totalset = DeleteSetRowElement (HashRow, totalset, buff); buff += datawidth; } DeleteHashTable (HashCol); } if (flag & CALCULATE_COORD_SET) printf ("MaxSet (%d) Coordinate %d %d\n", maxset, savesrow, savescol); DeleteHashTable (HashRow); } free (databuffer); }
MapWStrToInt::~MapWStrToInt() { DeleteHashTable(h); delete allocator; }
int main(int argc, char* argv[]){ int success; // contains 1 if removing from SinLL was successful int funcSuccess; int orNext; // contains > 0 if the next word in query should be ORed int firstAdd; // contains > 0 if the addition to SinLL is the first addition int tempChar; // used to flush the stdin for too long inputs char query[MAX_QUERY_LEN]; // contains string of query char *getsSuccess; // determines if EOF is met. int status = 1; SinLL *wordList; if(argc != 3){ // invalid number of arguments fprintf(stderr, ANSI_COLOR_RED "Usage: query [INDEXER OUTPUT FILE] [CRAWLER OUTPUT FILE DIRECTORY]" ANSI_COLOR_RESET "\n"); return 0; } if(!(access(argv[1], F_OK) != -1)){ // invalid file fprintf(stderr, ANSI_COLOR_RED "First argument is not a valid file." ANSI_COLOR_RESET "\n"); return 0; } if(!IsDir(argv[2])){ // invalid "directory" fprintf(stderr, ANSI_COLOR_RED "Second argument is not a directory." ANSI_COLOR_RESET "\n"); return 0; } HashTable *invertedIndex; invertedIndex = calloc(1, sizeof(HashTable)); if(!invertedIndex){ status = 0; goto cleanup; } funcSuccess = readFile(argv[1], invertedIndex); // recreate inverted index if(!funcSuccess){ status = 0; goto cleanup; } while(1){ // get the query from user fputs("QUERY> ", stdout); fflush(stdout); getsSuccess = fgets(query, sizeof(char)*MAX_QUERY_LEN, stdin); if(!getsSuccess) break; // EOF means exiting program // this means the user input more than MAX_QUERY_LEN characters to query if(getsSuccess[strlen(getsSuccess)-1] != '\n'){ fprintf(stderr, ANSI_COLOR_RED "Query length is over the maximum 1000 characters!" ANSI_COLOR_RESET "\n"); while((tempChar = getchar()) != '\n' && tempChar != EOF){ /*do nothing*/ } continue; } // at this stage, the next add is the first add, and we have not seen a // OR yet. orNext = 0; firstAdd = 1; wordList = CreateSinLL(); if(!wordList) break; char *wordP; wordP = strtok(query," "); // get all the words from the query while(wordP){ // last word in query will have a \n attached to it, so if // there is a \n at the end of a word, take that out if(wordP[strlen(wordP)-1] == '\n'){ wordP[strlen(wordP)-1] = 0; } // ignore ANDs. if(strcmp(AND, wordP) == 0){ wordP = strtok (NULL, " "); continue; } // ignore ORs but make sure you OR the next coming word. if(strcmp(OR, wordP) == 0){ orNext = 1; wordP = strtok (NULL, " "); continue; } // make word lowercase. If this word is the first one, or // the previous word was OR, make a new node in the SinLL // of WordChainList NormalizeWord(wordP); if(firstAdd){ funcSuccess = appendNewWordChain(wordP, wordList); if(!funcSuccess) break; firstAdd = 0; } else if(orNext){ funcSuccess = appendNewWordChain(wordP, wordList); if(!funcSuccess) break; orNext = 0; } // if not the previous two cases, just append the word to // current node. else{ appendWord(wordP, wordList); } wordP = strtok (NULL, " "); } // first process will AND all the words contained in each WordChainNodes // of the list. WordChainNode *curWordChain = wordList->head; while(curWordChain){ // while there are more nodes firstAdd = 1; DocNode *tempProcessDocNode; // contains original DocNodes to AND from index DocNode *processDocNode; // contains copied version of above. WordsLL *wordsProc = curWordChain->words; // gettng first set of words. while(wordsProc){ // while there are more words // get DocNodes associated with that word from the inverted index and // copy it as to not mess up the inverted index. tempProcessDocNode = DocsFromWordNode(wordsProc->word, invertedIndex); processDocNode = CopyDocs(tempProcessDocNode); // merge the above DocNodes with the DocNodes saved at the current // WordChainNode. DocMergedID(&processDocNode, &(curWordChain->docs)); // if it is the first add, we want to skip this step. If it isnt the // first add, and the above DocNodes with the ocNodes saved at the current // WordChainNode. if(!firstAdd){ ProcessAND(&processDocNode); } // Add the processed (ANDed) DocNode chain at the current // WordChainNode. AddDocNodeChain(curWordChain, processDocNode); // iterate through to the next word at the current node. wordsProc = wordsProc->nextWord; firstAdd = 0; } // move on to the next node. curWordChain = curWordChain->nextWords; } // now we OR each individual WordChainNodes' DocNode lists. curWordChain = wordList->head; DocNode *curDocs; DocNode *nextDocs; success = removeTopDoc(wordList, &curDocs); // gets the DocNode list from the first node // if there you fail here, it means that the list is empty/ if(success){ success = removeTopDoc(wordList, &nextDocs); // gets the next DocNode list from // the next WordChainNode while(success){ // if you fail here, there was only one WordChainNode in the list // process the DocNodes together by ORing them DocMergedID(&curDocs, &nextDocs); ProcessOR(&curDocs); // move on to the next DocNodes from the next WordChainNode. success = removeTopDoc(wordList, &nextDocs); } } // the list was empty, so found nothing. else{ printf("Found 0 pages\n"); continue; } // sort by the rank and print the results. SortByRank(&curDocs); PrintQueryResult(curDocs, argv[2]); free(wordList); // clean up for next query } cleanup: if(invertedIndex) DeleteHashTable(invertedIndex); // final clean up if(!status){ fprintf(stderr, ANSI_COLOR_RED "Failed inverted index building." ANSI_COLOR_RESET "\n"); return 0; } return 1; }