int Array::searchIndex(int left, int right, int src) { //идентичная функция для поиска индекса if (left > right) exit(EXIT_FAILURE); int median = (left + right) / 2; if (arr[median] == src) return median; if (src < arr[median]) return searchIndex(left, median - 1, src); else return searchIndex(median + 1, right, src); }
/* Funcao: searchIndex (local) Procura matricula na arvore binaria de busca. Parametros: tree: ponteiro pra a raiz da arvore mat: matricula a ser pesquisada Retorno: NULL se a matricula não for encontrada ou ponteiro para o nó onde foi encontrado. */ tArvoreBB* searchIndex(tArvoreBB *tree, int mat) { if(tree == NULL) return NULL; else { if(tree->mat == mat) return tree; else if(tree->mat > mat) return searchIndex(tree->esq, mat); else return searchIndex(tree->dir, mat); } }
int Locals::getIndex (const std::string& name) const { int index = searchIndex ('s', name); if (index!=-1) return index; index = searchIndex ('l', name); if (index!=-1) return index; return searchIndex ('f', name); }
void ExtendibleHash::insert(const int &object) { ExtendibleLeaf *ptr = searchIndex(object,bits); if (ptr->count < ptr->leafsize && ptr->search(object) == -1) { ptr->binaryNode[ptr->count] = object; ptr->count = ptr->count + 1; }//if leaf isnt full, insert /* for(int iter = 0; iter< ptr->count; iter++) { cout<< ptr->binaryNode[iter]<< endl; } cout<<ptr->count<<endl<<endl; *///TO TEST IF INSERTING CORRECTLY else if (ptr->count == ptr->leafsize) { if(splitOrExtend(ptr,object) == false) { extend(object); insert(object); } }//else if leaf is full, split or index extends } // insert()
int main() { FILE * in; in = fopen("/home/paopao/Data/soft/php-5.5.1/ext/huamanshu/UTFWry.dat", "rb"); char local[255] = {0}; unsigned int indexHead = 0; unsigned int indexTail = 0; fread(&indexHead, sizeof(indexHead), 1, in); fread(&indexTail, sizeof(indexTail), 1, in); //IpStruct xip = {143,24,199,121}; IpStruct xip = {211,157,79,117}; unsigned int iplong = 1968152019; IpStruct tmp; int pos = searchIndex(iplong, in, indexHead, indexTail); fseek(in, indexHead+pos*ipsize, SEEK_SET); printf("====\n"); printf("xip %d\n", BYTE4INT(xip.ip)); fread(&tmp, ipsize, 1, in); printf("xip %d\n", BYTE4INT(tmp.ip)); searchLocal(tmp, iplong, in, local); //printf("xip %d\n", BYTE4INT(xip.ip)); //printf("zip %d\n", BYTE4INT(zip.ip)); return 0; }
bool DLList::get(int target) const { if (searchIndex(target) == NULL) return false; else return true; }
void PhoneBook::setRecord(const long index, const Record & rec) { Iter i; if ((i=searchIndex(index, _records.begin())) != _records.end()) *i = rec; }
void PhoneBook::delRecord(const long index) { Iter i; if ((i=searchIndex(index, _records.begin())) != _records.end()) _records.erase(i); }
const Record & PhoneBook::searchByIndex(const long index) { Iter i; if ((i=searchIndex(index, _records.begin())) != _records.end()) return *i; else return empty_record; }
/* Funcao: listAll() Lista atributos de todos os funcionarios Parametros: arq: ponteiro para arquivo func.bin tree: ponteiro para raiz da arvore de indexacao */ void listAll(FILE *arq, tArvoreBB *tree) { tfunc func; fseek(arq, 0, SEEK_SET); while(fread(&func, sizeof(tfunc), 1, arq) != 0) { tArvoreBB *index = searchIndex(tree, func.mat); if(index != NULL && ftell(arq) - sizeof(tfunc) == index->index) printf("Matricula: %03d\nNome: %s\nSalario: %.02f \n ----------\n", func.mat, func.nome, func.sal); } }
/* Funcao: alterSalary() Altera salario de funcionario escolhido. Parametros: arq: ponteiro para ponteiro para arquivo func.bin (precisa ser ponteiro de ponteiro pois precisará ser alterado) tree: ponteiro para raiz da arvore mat: matricula do funcionario que tera salario alterado newsal: novo salario a ser atribuido para funcionario */ void alterSalary(FILE **arq, tArvoreBB *tree, int mat, float newsal) { if(searchIndex(tree,mat) != NULL) { int index; tfunc func; FILE* newfile = fopen("tempfunc.bin","wb"); index = (searchIndex(tree, mat))->index; fseek(*arq, 0, SEEK_SET); while(fread(&func, sizeof(tfunc), 1, *arq) != 0) { if(ftell(*arq) - sizeof(tfunc) == index) func.sal = newsal; fwrite(&func, sizeof(func), 1, newfile); } fclose(*arq); fclose(newfile); if(remove("func.bin") == 0) { if(rename("tempfunc.bin","func.bin") == 0) { *arq = fopen("func.bin", "a+b"); if(*arq != NULL) printf("Salario alterado com sucesso.\n"); else printf("Erro ao abrir arquivo novamente"); } else printf("Erro ao renomear arquivo"); } else printf("Erro ao remover arquivo"); } else { printf("Funcionario nao encontrado"); } }
/* Funcao: listFunc() Lista atributos de um unico funcionario Parametros: arq: ponteiro para arquivo func.bin mat: matricula do funcionario a ser exibido */ void listFunc(FILE *arq, tArvoreBB *tree, int mat) { tfunc func; tArvoreBB* index = searchIndex(tree, mat); if(index != NULL) { fseek(arq, index->index, SEEK_SET); fread(&func, sizeof(tfunc), 1, arq); printf("Nome: %s \nSalario: %.02f", func.nome, func.sal); } else { printf("Funcionario nao encontrado."); } }
bool DLList::removeFirst(int target) { bool tFound = false; if (head == NULL) return tFound; else if (head -> getContents() == target) { popFront(); tFound = true; } else tFound = true; DLNode* targetFound = searchIndex(target); targetFound ->getPrevious() -> setNext(targetFound ->getNext()); targetFound ->getNext() ->setPrevious (targetFound ->getPrevious()); targetFound = NULL; delete targetFound; size--; return tFound ; }
/* Funcao: insertFunc() Insere funcionario no arquivo e na arvore de indexacao Parametros: arq: ponteiro para o arquivo func.bin tree: ponteiro para ponteiro para raiz da arvore func: funcionario a ser inserido no registro */ void insertFunc(FILE *arq, tArvoreBB **tree, tfunc func) { if(searchIndex(*tree, func.mat) != NULL) { printf("Matricula ja existe!"); } else { if(fwrite(&func, sizeof(tfunc), 1, arq) > 0) { fseek(arq, 0, SEEK_END); insertIndex(tree, ftell(arq) - sizeof(func), func.mat); printf("Funcionario inserido com sucesso!"); } else{ printf("Erro ao inserir funcionario!"); } } }
/* Funcao: reindex() Reindexa arvore e arquivo, removendo funcionarios que foram excluidos, do arquivo, e remontando arvore de indexacao Parametros: arq: ponteiro para ponteiro para func.bin tree: ponteiro para ponteiro para raiz da arvore */ void reindex(FILE** arq, tArvoreBB **tree) { tfunc func; FILE* temp; temp = fopen("temp.bin","w+b"); fseek(*arq, 0, SEEK_SET); while(fread(&func, sizeof(tfunc), 1, (*arq)) != 0) { if(searchIndex(*tree, func.mat) != NULL) fwrite(&func, sizeof(tfunc), 1, temp); } if(fclose((*arq)) == 0) { if(fclose(temp) == 0) { if(remove("func.bin") == 0) { if(rename("temp.bin", "func.bin") == 0) { *arq = fopen("func.bin", "a+b"); if(*arq != NULL){ deleteTree(tree); loadTree(*arq, tree); printf("Reindexado com sucesso!\n"); } else printf("Erro ao reabrir arquivo!\n"); } else printf("Erro ao renomear arquivo!\n"); } else printf("Erro ao remover arquivo!\n"); } else printf("Erro ao fechar arquivo temporario!\n"); } else printf("Erro ao fechar arquivo principal!\n"); }
int main(int argc, char* arg[]){ int flag = 1; char index_file[MAX_WORD_LEN]; char target_dir[MAX_WORD_LEN]; struct stat sb; // used for stat call INVERTED_INDEX *index; printf("starting the program\n"); // initialize the qholder for first time initializeQHOLDER(); // initialize index index = (INVERTED_INDEX*)malloc(sizeof(INVERTED_INDEX)+1); if(index == NULL){ printf("No enough memory at %s:line%d ", __FILE__, __LINE__); exit(-1); } bzero(index, sizeof(INVERTED_INDEX)+1); index->start = index->end = NULL; // input arg check // check to see if the user inputs too many or too few args if(argc != 3){ printf("You did not enter the correct number of arguments.\n"); exit(-1); } // copy in filename of the printed index strcpy(index_file, arg[1]); // set target directory and check if it's available strcpy(target_dir, arg[2]); stat(target_dir, &sb); if(!S_ISDIR(sb.st_mode)){ printf("The directory you chose is not available.\n"); return 1; } // read file into new index -- now index is loaded from file index = readFile(index_file); // initialize user input variable char *input; input = malloc(sizeof(char)*(MAX_WORD_LEN+1)); if(index == NULL){ printf("No enough memory at %s:line%d ", __FILE__, __LINE__); exit(-1); } bzero(input, MAX_WORD_LEN+1); // Main while loop used in query - see psuedocode above // Used to create a node for every word containing a list of the // documents the word is in. Each node will contain the // list of DOCUMENTNODES which contain the document id and // the frequency of the word in that document. int keepInWhile = 1; while(keepInWhile){ // initialize the QHOLDER so it is empty for every search // only want words in the qholder from each different search flag = 0; // prompt user for a query search printf("\nTinySearch > "); fgets(input, MAX_WORD_LEN, stdin); if(strcmp(input, "c\n") == 0){ printf("You are now exitting the program.\n"); break; } printf("The word you are searching for is %s\n", input); // load input words into list of words using getNextWord... // create and zero word and list inputs will be written to int currentPosition =0; int i = 0; char **wordList; wordList = malloc(sizeof(char)*(MAX_WORD_LEN+1)); if(wordList == NULL){ printf("No enough memory at %s:line%d ", __FILE__, __LINE__); \ exit(-1); } bzero(wordList, MAX_WORD_LEN); // Get a list of all the user input words to be searched. // The list is called wordList and after getNextWord is called, // the wordList will be loaded getNextWord(input, currentPosition, wordList, i); // initialize new word node to be set at the correct // position in the index WORDNODE *wn; i = 0; while(wordList[i] != NULL){ // if user wrote AND or OR, keep it in list, but don't add node if(strcmp(wordList[i], "OR") == 0){ printf("continuing...\n"); i++; continue; } else if(strcmp(wordList[i], "AND") == 0){ printf("continuing...\n"); i++; continue; } else{ // find the wordnode form index for the current query word makeLower(wordList[i]); wn = searchIndex(&flag, wordList[i], index); // this flag is for if the word was found or not. 1 is yes. if(flag == 0){ printf("Word not found. Try again.\n"); break; } // add WORDNODE to the qholder to be used later addWORDNODE(wn); i++; } } if(flag == 0) continue; if(rankWords(wordList, i) == 0){ continue; } printDOCs(target_dir); bzero(input, MAX_WORD_LEN+1); initializeQHOLDER(); } return 0; }
bool Locals::search (char type, const std::string& name) const { return searchIndex (type, name)!=-1; }