From e2c430b237da5c0229fd8e0832eedc3b85045ec0 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 12 Sep 2006 17:30:23 +0000 Subject: Added cache functionality, which is really nice, things go much faster now. --- src/cache.cpp | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 src/cache.cpp (limited to 'src/cache.cpp') diff --git a/src/cache.cpp b/src/cache.cpp new file mode 100644 index 0000000..10971b1 --- /dev/null +++ b/src/cache.cpp @@ -0,0 +1,149 @@ +#include "cache.h" +#include "serializer.h" +#include "staticstring.h" + +Cache::Cache() +{ +} + +Cache::~Cache() +{ + for( std::map::iterator i = mCache.begin(); + i != mCache.end(); i++ ) + { + delete (*i).second; + } +} + +void Cache::serialize( class Serializer &ar ) +{ + if( ar.isLoading() ) + { + int sCache, sData, sIndex; + + ar >> sIndex; + StaticString *Index = new StaticString[sIndex]; + for( int i = 0; i < sIndex; i++ ) + { + ar >> Index[i]; + } + + ar >> sCache; + int nTmp; + for( int i = 0; i < sCache; i++ ) + { + Entry *e = new Entry; + ar >> e->tCreated; + ar >> sData; + std::list &lData = e->lData; + for( int j = 0; j < sData; j++ ) + { + ar >> nTmp; + lData.push_back( Index[nTmp].getString() ); + } + ar >> nTmp; + mCache[Index[nTmp].getString()] = e; + } + /* + int sCache, sData; + ar >> sCache; + std::string sTmp; + + for( int i = 0; i < sCache; i++ ) + { + Entry *e = new Entry; + ar >> e->tCreated; + ar >> sData; + std::list &lData = e->lData; + for( int j = 0; j < sData; j++ ) + { + ar >> sTmp; + lData.push_back( sTmp ); + } + ar >> sTmp; + mCache[sTmp] = e; + } + */ + } + else + { + std::map mIndex; + for( std::map::iterator i = mCache.begin(); + i != mCache.end(); i++ ) + { + mIndex[(*i).first] = 0; + std::list &lData = (*i).second->lData; + for( std::list::iterator j = lData.begin(); + j != lData.end(); j++ ) + { + mIndex[(*j)] = 0; + } + } + + ar << mIndex.size(); + int cnt = 0; + for( std::map::iterator i = mIndex.begin(); + i != mIndex.end(); i++ ) + { + (*i).second = cnt; + cnt++; + std::string s = ((*i).first); + ar << s; + } + + ar << mCache.size(); + for( std::map::iterator i = mCache.begin(); + i != mCache.end(); i++ ) + { + ar << (*i).second->tCreated; + std::list &lData = (*i).second->lData; + ar << lData.size(); + for( std::list::iterator j = lData.begin(); + j != lData.end(); j++ ) + { + ar << mIndex[(*j)]; + } + + ar << mIndex[(*i).first]; + } + + + /* + ar << mCache.size(); + for( std::map::iterator i = mCache.begin(); + i != mCache.end(); i++ ) + { + ar << (*i).second->tCreated; + std::list &lData = (*i).second->lData; + ar << lData.size(); + for( std::list::iterator j = lData.begin(); + j != lData.end(); j++ ) + { + ar << (*j); + } + + std::string str = (*i).first; + ar << str; + } + */ + } +} + +Cache::Entry *Cache::get( const std::string &id ) +{ + std::map::iterator i = mCache.find( id ); + if( i != mCache.end() ) + return (*i).second; + + return NULL; +} + +void Cache::put( const std::string &id, Entry *data ) +{ + std::map::iterator i = mCache.find( id ); + if( i != mCache.end() ) + delete (*i).second; + + mCache[id] = data; +} + -- cgit v1.2.3