#include "pstringfactory.h" PstringFactory * PstringFactory::ourFactory = 0; PstringFactory::PstringFactory() { // nothing to do here } void PstringFactory::reportStorage() // post: report on all storage used { long storage=0; IT it; for(it=myStorage.begin(); it != myStorage.end(); it++) { storage += sizeof(*it) + string(*it).length()*sizeof(char); } cout << "total entries = " << myStorage.size() << endl; cout << "total storage = " << storage << endl; } PstringFactory * PstringFactory::getInstance() // create one factory for entire program // post: return pointer to a factory { if (ourFactory == 0) { ourFactory = new PstringFactory(); } return ourFactory; } Pstring PstringFactory::getString(const string& s) // post: return Pstring equivalent to s // internally stores s once in a Pstring and looks // s up to see if it's there already { IT it; Pstring lookup(&s); // need this to search for s it = myStorage.find(lookup); // already seen? if (it == myStorage.end()) // no, store and return { Pstring ps = Pstring(new string(s)); myStorage.insert(ps); return ps; } else // already seen string, return { return *it; } }