/** * Parses a file and returns all unique words according * to some parser rules and all hyperlinks based * on the format the parser is designed to implement */ void MDPageParser::parse(std::string filename, MySet<std::string>& allWords, MySet<std::string>& allLinks){ std::ifstream input_file(filename.c_str());// ifstream constructor takes char* NOT strings std::string link; std::string word; std::string contents; char ch; //copy over enter file into a string while(!input_file.eof()){ input_file.get(ch); if(!input_file.fail()){ contents += ch; } } for(unsigned int i = 0; i<contents.size(); i++){ if( isalpha(contents[i]) || isdigit(contents[i]) ){ //a letter or number while( isalpha(contents[i]) || isdigit(contents[i]) ){ //add char to word as long as it is a letter/num word += contents[i]; i++; } i--; //we went 1 to far in the above while loop allWords.insert(word); word.clear(); } else if( contents[i] == '[' ){ i++; // skip '[' while( (contents[i] != ']') ){ //add to word as long as we are in the brackets while( isalpha(contents[i]) || isdigit(contents[i]) ){ word += contents[i]; i++; } //at ']' char allWords.insert(word); word.clear(); if(contents[i] != ']'){ i++; } } i++; if(word != "") { allWords.insert(word); } word.clear(); if( contents[i] == '(' ){ // [] is followed by '(' i++; // skip '(' while( contents[i] != ')' ){ link += contents[i]; i++; } allLinks.insert(link); link.clear(); } } } }
MySet<Type>* MySet<Type>::Union(MySet<Type> *B) { typename MySet<Type>::iterator i; std::set<Type> setB = B->GetSet(); MySet<Type> *tempSet = new MySet<Type>; for(i = set1.begin(); i != set1.end(); i++) tempSet->AddElement(*i); for(i = setB.begin(); i != setB.end(); i++) tempSet->AddElement(*i); return tempSet; }
MySet<Type>* MySet<Type>::Intersect(MySet<Type> *B) { typename MySet<Type>::iterator i; typename MySet<Type>::iterator j; std::set<Type> setB = B->GetSet(); MySet<Type> *tempSet = new MySet<Type>; for(i = set1.begin(); i != set1.end(); i++) for(j = setB.begin(); j != setB.end(); j++) if(*i == *j) tempSet->AddElement(*i); return tempSet; }
int main(int argc, char** argv) { std::string s = "transformers"; printf("Old string is: %s \n", s.c_str()); std::transform(s.begin(), s.end(), s.begin(), ::toupper); printf("New string is: %s \n", s.c_str()); typedef std::map<int, double> MyMap; std::map<int, double> M; typedef std::set<int> MySet; MySet S; typedef std::set<double> MySet2; MySet2 S2; M[1] = 452.334; M[3] = 15.35; M[178] = 16.28; M[32] = 478.6789; // The bind method is the equivalent of select1st // (which is only avail via SGI STL) std::transform(M.begin(), M.end(), std::inserter(S, S.begin()), std::bind(&MyMap::value_type::first, _1)); printf("======== dump keys ======= \n"); std::for_each(S.begin(), S.end(), printVal); std::transform(M.begin(), M.end(), std::inserter(S2, S2.begin()), std::bind(&MyMap::value_type::second, _1)); printf("======== dump values with for_each== \n"); std::for_each(S2.begin(), S2.end(), printVal2); MySet2::iterator it = S2.begin(); printf("======== dump values with loop=== \n"); while (it != S2.end()) { printf("%f \n", *it); it++; } printf("========\n"); return 0; }
void MD::parse(std::string filename, MySet<std::string>& allWords, MySet<std::string>& allLinks) { std::ifstream file ( filename.c_str() ); std::string line; std::vector <std::string> storage; //import lines while ( getline (file,line) ) { std::string m = line; int i = 0; //char c; while (m[i]) { m[i] = tolower(m[i]); i++; } storage.push_back(m); } file.close(); //main parser for (unsigned int i=0; i < storage.size(); i++) { std::string liner = storage[i]; size_t start_ = 0; for (size_t g = 0; g < liner.length(); g++) { if (isalpha(liner[g])) { ; } else if (liner[g-1] == ']' && liner[g] == '(') { int m = g; while (liner[m]!= ')') { m++; } //std::cout << "links" << liner.substr(g+1, m-g-1) << std::endl; allLinks.insert( liner.substr(g+1, m-g-1) ); start_ = m; g= m-1; } else { int word_length = g - start_; if(word_length > 0) { //std::cout << liner.substr(start_, word_length) << std::endl; // show the word allWords.insert( liner.substr(start_, word_length) ); } start_ = g + 1; } } } }
iterator start() { clang_analyzer_checkInlined(true); #if INLINE // expected-warning@-2 {{TRUE}} #endif return iterator{impl.begin().impl}; }
wrapped_iterator begin() { clang_analyzer_checkInlined(true); #if INLINE // expected-warning@-2 {{TRUE}} #endif return IterImpl(impl.begin()); }
void foo2() { string arr[3]; string check = "321"; arr[0] = "123"; arr[1] = "456"; arr[2] = "789"; MySet<string> S; S.add(arr[0]); S.add(arr[1]); S.add(arr[2]); S.add(arr[2]); S.show(); cout << endl << S.size() << endl; cout << boolalpha << S.contains(arr[1]) << endl; cout << boolalpha << S.contains(check) << endl; S.remove(arr[1]); S.show(); cout << endl << S.size() << endl << endl; };
void foo1() { int arr[3]; int check = 321; arr[0] = 123; arr[1] = 456; arr[2] = 789; MySet<int> S; S.add(arr[0]); S.add(arr[1]); S.add(arr[2]); S.add(arr[2]); S.show(); cout << endl << S.size() << endl; cout << boolalpha << S.contains(arr[1]) << endl; cout << boolalpha << S.contains(check) << endl; S.remove(arr[1]); S.show(); cout << endl << S.size() << endl << endl; };
MySetResult mySetRemove(MySet set, MySetElement element){ if (set==NULL || element == NULL){ return MY_SET_NULL_ARGUMENT; } if (!mySetIsIn(set, element)){ return MY_SET_ITEM_DOES_NOT_EXIST; } MySetElement elementFound = mySetExtract(set, element); set->freeElement(elementFound); return MY_SET_SUCCESS; }
bool mySetIsIn(MySet set, MySetElement element) { if (set == NULL || element == NULL) { return false; } for (MySetNode iterator = set->head; iterator != NULL; iterator = iterator->next) { if (set->compareElements(iterator->element, element) == 0) { return true; } } return false; }
MySet<WebPage*> SearchEng::OR_command(std::vector<std::string> words_to_search){ MySet<WebPage*> cand_search_result; MySet<WebPage*> match_search; for(unsigned int i = 0; i< words_to_search.size(); i++){ if(word_to_setofwps.count(words_to_search[i] ) ){// word in search exists in our map match_search = match_search.set_union( word_to_setofwps[words_to_search[i] ] ); } } //go through all the wps that match and add their outgoing and incoming links into the new set MySet<WebPage*>::iterator it; for(it = match_search.begin(); it != match_search.end(); ++it){ cand_search_result.insert(*it); MySet<WebPage*> outgoingSet = (*it)->outgoing_links(); MySet<WebPage*>::iterator itOut; for(itOut = outgoingSet.begin(); itOut != outgoingSet.end(); ++itOut){ cand_search_result.insert(*itOut); } MySet<WebPage*> incomingSet = (*it)->incoming_links(); MySet<WebPage*>::iterator itIn; for(itIn = incomingSet.begin(); itIn != incomingSet.end(); ++itIn){ cand_search_result.insert(*itIn); } } return cand_search_result; }
MySet<WebPage*> SearchEng::one_word_search(std::string word){//word is lowercase MySet<WebPage*> cand_search_result; MySet<WebPage*> match_search = word_to_setofwps[word]; //go through all the wps that match and add their outgoing and incoming links into the new set MySet<WebPage*>::iterator it; for(it = match_search.begin(); it != match_search.end(); ++it){ cand_search_result.insert(*it); MySet<WebPage*> outgoingSet = (*it)->outgoing_links(); MySet<WebPage*>::iterator itOut; for(itOut = outgoingSet.begin(); itOut != outgoingSet.end(); ++itOut){ cand_search_result.insert(*itOut); } MySet<WebPage*> incomingSet = (*it)->incoming_links(); MySet<WebPage*>::iterator itIn; for(itIn = incomingSet.begin(); itIn != incomingSet.end(); ++itIn){ cand_search_result.insert(*itIn); } } return cand_search_result; }
void mySetDestroy(MySet set) { if(set == NULL) { return; } while (set->head != NULL) { MySetNode current = set->head; set->head = set->head->next; set->freeElement(current->element); free(current); } free(set); }
MySetElement mySetExtract(MySet set, MySetElement element) { if (set == NULL || !mySetIsIn(set, element)) { return NULL; } assert(set->head != NULL); MySetElement result; MySetNode node; // node we want to deallocate if (set->compareElements(set->head->element, element) == 0) { // pop front result = set->head->element; node = set->head; // extracting set->head = set->head->next; } else { MySetNode position; for (position = set->head; position->next != NULL && set->compareElements(position->next->element, element) != 0; position = position->next); assert(position->next != NULL && set->compareElements(position->next->element, element) == 0); result = position->next->element; if (position->next->next == NULL) { node = position->next; // extracting position->next = NULL; } else { position = position->next; node = position->next; // extracting assert(position->next != NULL); position->element = position->next->element; position->next = position->next->next; } } free(node); return result; }
MySetResult mySetAdd(MySet set, MySetElement element) { if (set == NULL || element == NULL) { return MY_SET_NULL_ARGUMENT; } if (mySetIsIn(set, element)) { return MY_SET_ITEM_ALREADY_EXISTS; } // Create new node MySetNode node; MY_SET_ALLOCATION(MySetNode_t, node, MY_SET_OUT_OF_MEMORY); if (NULL == (node->element = set->copyElement(element))) { free(node); return MY_SET_OUT_OF_MEMORY; } if (set->head == NULL || set->compareElements(set->head->element, element) > 0) { //need to push front node->next = set->head; set->head = node; } else { assert(set->head != NULL); assert(set->compareElements(set->head->element, element) < 0); // find last position where element is lower then ours or last element MySetNode position; for (position = set->head; position->next != NULL && set->compareElements(position->next->element, element) < 0; position = position->next); assert(position != NULL); assert(position->next == NULL || set->compareElements(position->next->element, element) > 0); // insert after position node->next = position->next; position->next = node; } return MY_SET_SUCCESS; }
MySet<WebPage*> SearchEng::AND_command(std::vector<std::string> words_to_search){ MySet<WebPage*> cand_search_result; MySet<WebPage*> match_search; if(word_to_setofwps.count(words_to_search[0] ) ){ match_search = word_to_setofwps[words_to_search[0] ]; } if(words_to_search.size() > 1){//search has more than one word for(unsigned int i = 1; i< words_to_search.size(); ++i){ if(word_to_setofwps.count(words_to_search[i] ) ){// word in search exists in our map match_search = match_search.set_intersection( word_to_setofwps[words_to_search[i] ] ); }else{//word is not in our map, return empty set match_search.clear(); return match_search; } } } //go through all the wps that match and add their outgoing and incoming links into the new set MySet<WebPage*>::iterator it; for(it = match_search.begin(); it != match_search.end(); ++it){ cand_search_result.insert(*it); MySet<WebPage*> outgoingSet = (*it)->outgoing_links(); MySet<WebPage*>::iterator itOut; for(itOut = outgoingSet.begin(); itOut != outgoingSet.end(); ++itOut){ cand_search_result.insert(*itOut); } MySet<WebPage*> incomingSet = (*it)->incoming_links(); MySet<WebPage*>::iterator itIn; for(itIn = incomingSet.begin(); itIn != incomingSet.end(); ++itIn){ cand_search_result.insert(*itIn); } } return cand_search_result; }
int main(int argc, char* argv[], char* env[]) { char c; MySet b; MySet a; string s; while (true) { a.clear(); b.clear(); getline(cin, s); std::stringstream ss(s); while (ss >> c) { if (c == '[') { break; } } string str; int num; while (ss >> c) { if (c == ' ' || c == ',') { continue; } if (c == ']') { break; } str += c; if (ss.peek() == ' ' || ss.peek() == ',' || ss.peek() == ']') { num = std::stoi(str); a += num; str = ""; } if (ss.peek() == ']') { ss.ignore(); break; } } char op = '\0'; while (ss >> c) { if (c == ' ') { continue; } op = c; break; } while (ss >> c) { if (c == '[') { break; } } while (ss >> c) { if (c == ' ' || c == ',') { continue; } if (c == ']') { break; } str += c; if (ss.peek() == ' ' || ss.peek() == ',' || ss.peek() == ']') { num = std::stoi(str); b += num; str = ""; } if (ss.peek() == ']') { ss.ignore(); break; } } cout << "a = ["; a.foreach([](const int number)->void {cout << number << ", ";}); cout << "]" << endl; cout << "op = " << op << endl; cout << "b = ["; b.foreach([](const int number)->void {cout << number << ", ";}); cout << "]" << endl; switch (op) { case '+': a += b; break; case '-': a -= b; break; case '*': a *= b; break; default: throw "wtf"; } cout << "result = ["; a.foreach([](const int number)->void {cout << number << ", ";}); cout << "]" << endl; } }