/* * Copyright (c) 2002 by Cliff Green. All rights reserved. Individual files * may be covered by other copyrights (as noted in the file itself). * * Redistribution and use in source and binary forms are permitted * provided that this entire copyright notice is duplicated in all such * copies. * * This software is provided "as is" and without any expressed or implied * warranties, including, without limitation, the implied warranties of * merchantibility and fitness for any particular purpose. */ //---------------------------------------------------------------------- // Source file: dict.cpp // Written by: Cliff Green, 2002 // Compiler: Metrowerks CodeWarrior Pro 6, g++ 3.0.4 // History: // Modified: Mar. 5, 2002 // By: Cliff Green // Comments: Dictionary class implementation file. //---------------------------------------------------------------------- // This commentheader supports documentation tools such as Doc++ and // Doxygen: http://www.doxygen.org/ //---------------------------------------------------------------------- /// Dictionary class implementation. See header for more comments. /** * The main logic is to sort when needed (when a search is performed, * and the vector is unsorted). This design assumes lots of pre-processing - * many inserts at the beginning, then only searches from that point on. * Otherwise it would be very inefficient (since sorting is an expensive * operation). */ #include "dict.h" #include #include #include // The std lib 'set' container is even better for this bool Dictionary::insert (Word const& w) { if (w.getLength() == 0) { return false; } mWords.push_back (w); mSortState = UNSORTED; mSdxMap.insert(std::pair(w.getSoundexValue(), w)); return true; } bool Dictionary::search (Word const& w) { if (mSortState == UNSORTED) { std::sort( mWords.begin(), mWords.end() ); mSortState = SORTED; } return std::binary_search (mWords.begin(), mWords.end(), w); } // This is the naive / slow implementation of the getSameSdx function - note // that it doesn't use the mSdxMap container ... but it's simpler syntax and // declarations than the version using the multimap /* WordVec const Dictionary::getSameSdx(Word const& w) const { WordVec wds; for (WordVecSizeType i (0); i != mWords.size(); ++i) { if (std::strcmp(mWords[i].getSoundexValue(), w.getSoundexValue()) == 0) { wds.push_back(mWords[i]); } } return wds; } */ // This is the more efficient implementation, using a std lib multimap container // which allows multiple entries with the same key, but different values WordVec const Dictionary::getSameSdx(Word const& w) const { WordVec wds; std::string sdxVal(w.getSoundexValue()); SdxMap::const_iterator lower (mSdxMap.lower_bound(sdxVal)); SdxMap::const_iterator upper (mSdxMap.upper_bound(sdxVal)); for ( ; lower != upper; ++lower) { // get all sound alike words wds.push_back((*lower).second); } return wds; }