DynamicVector<string> AnagramController::anagramsForWord(string word) { //add the word as the key and the sorted word as value in the dictionary for (int i = 0; i<repo->getAll().getSize(); i++){ string key = repo->getAll().elementAtIndex(i); map.addValueAndKey(key, sortWord(key)); } word = sortWord(word); DynamicVector<string> elements = DynamicVector<string>(); Node<string, string> *first = map.getNodeForValue(word); //find the first node that has as value, the desired sorted word and //add the key to the elements while (first != NULL && first->getData()->getValue() == word) { elements.add(first->getData()->getKey()); first = first->getNext(); } return elements; }
void Anagramer::printAnagrams(std::string &word){ std::pair< std::multimap<std::string, std::string>::iterator, std::multimap<std::string, std::string>::iterator> wordRange; wordRange = baseWords.equal_range(sortWord(word)); std::multimap<std::string, std::string>::iterator rangeIter; for( rangeIter = wordRange.first; rangeIter != wordRange.second; rangeIter++){ std::cout << (*rangeIter).second << std::endl; } }
void Anagramer::insertIntoBase(std::string &word){ // TODO time using a hint baseWords.insert(std::pair<std::string,std::string> (sortWord(word),word)); }