Adding several const, assert, and small optimizations

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@785 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
clitte_bbt 2006-11-21 20:31:27 +00:00
parent 59bc51b565
commit 7dc499a4e1

View file

@ -332,8 +332,9 @@ namespace Loki
// This function sorts the map according to the score // This function sorts the map according to the score
// and returns the lower bound of the sorted container // and returns the lower bound of the sorted container
DT& getLowerBound(){ DT& getLowerBound(){
assert(!m_mHitCount.empty());
SwappedHitMap copyMap; SwappedHitMap copyMap;
for(HitMapItr itr = m_mHitCount.begin(); itr != m_mHitCount.end(); itr++) for(HitMapItr itr = m_mHitCount.begin(); itr != m_mHitCount.end(); ++itr)
copyMap.insert(SwappedPair(itr->second, itr->first)); copyMap.insert(SwappedPair(itr->second, itr->first));
if(copyMap.rbegin()->first == 0) // the higher score is 0 ... if(copyMap.rbegin()->first == 0) // the higher score is 0 ...
throw EvictionException(); // there is no key evict throw EvictionException(); // there is no key evict
@ -395,8 +396,7 @@ namespace Loki
// LRU Eviction policy // LRU Eviction policy
void evict() void evict()
{ {
DT& evictKey = EH::getLowerBound(); remove(EH::getLowerBound());
remove(evictKey);
} }
const char* name(){return "LRU";} const char* name(){return "LRU";}
}; };
@ -440,10 +440,10 @@ namespace Loki
// If the key is the key of the fetched object : // If the key is the key of the fetched object :
// the counter is shifted to the right and it's MSB is set to 1 // the counter is shifted to the right and it's MSB is set to 1
// else // else
// the counter is shifted to the right // the counter is shifted to the left
void onRelease(const DT& key) void onRelease(const DT& key)
{ {
for( HitMapItr itr = EH::m_mHitCount.begin(); itr != EH::m_mHitCount.end(); itr++){ for( HitMapItr itr = EH::m_mHitCount.begin(); itr != EH::m_mHitCount.end(); ++itr){
itr->second = (itr->first == key ? (itr->second >> 1) | ( 1 << ((sizeof(ST)-1)*8) ) : itr->second >> 1); itr->second = (itr->first == key ? (itr->second >> 1) | ( 1 << ((sizeof(ST)-1)*8) ) : itr->second >> 1);
D( cout << itr->second << endl; ) D( cout << itr->second << endl; )
} }
@ -461,8 +461,7 @@ namespace Loki
// LRU with Aging Eviction policy // LRU with Aging Eviction policy
void evict() void evict()
{ {
DT& evictKey = EH::getLowerBound(); remove(EH::getLowerBound());
remove(evictKey);
} }
const char* name(){return "LRU with aging";} const char* name(){return "LRU with aging";}
}; };
@ -511,12 +510,10 @@ namespace Loki
// Random Eviction policy // Random Eviction policy
void evict() void evict()
{ {
if(m_vKeys.size()==0) if(m_vKeys.empty())
throw EvictionException(); throw EvictionException();
size_type random = static_cast<size_type>((m_vKeys.size()*rand())/int(RAND_MAX + 1)); size_type random = static_cast<size_type>((m_vKeys.size()*rand())/int(RAND_MAX + 1));
iterator pos = m_vKeys.begin()+random; remove(*(m_vKeys.begin()+random));
DT& evictKey = *pos;
remove(evictKey);
} }
const char* name(){return "random";} const char* name(){return "random";}
}; };
@ -702,8 +699,6 @@ namespace Loki
AbstractProduct* const getPointerToObjectInContainer(ObjVector &entry) AbstractProduct* const getPointerToObjectInContainer(ObjVector &entry)
{ {
AbstractProduct* pObject = NULL;
(void) pObject;
if(entry.empty()) // No object available if(entry.empty()) // No object available
{ // the object will be created in the calling function. { // the object will be created in the calling function.
// It has to be created in the calling function because of // It has to be created in the calling function because of
@ -719,7 +714,7 @@ namespace Loki
} }
} }
bool shouldCreateObject(AbstractProduct* pProduct){ bool shouldCreateObject(AbstractProduct * const pProduct){
if(pProduct!=NULL) // object already exists if(pProduct!=NULL) // object already exists
return false; return false;
if(CP::canCreate()==false) // Are we allowed to Create ? if(CP::canCreate()==false) // Are we allowed to Create ?
@ -732,28 +727,28 @@ namespace Loki
entry.push_back(object); entry.push_back(object);
} }
void onFetch(AbstractProduct* pProduct) void onFetch(AbstractProduct * const pProduct)
{ {
SP::onFetch(); SP::onFetch();
EP::onFetch(pProduct); EP::onFetch(pProduct);
++outObjects; ++outObjects;
} }
void onRelease(AbstractProduct* pProduct) void onRelease(AbstractProduct * const pProduct)
{ {
SP::onRelease(); SP::onRelease();
EP::onRelease(pProduct); EP::onRelease(pProduct);
--outObjects; --outObjects;
} }
void onCreate(AbstractProduct* pProduct) void onCreate(AbstractProduct * const pProduct)
{ {
CP::onCreate(); CP::onCreate();
SP::onCreate(); SP::onCreate();
EP::onCreate(pProduct); EP::onCreate(pProduct);
} }
void onDestroy(AbstractProduct* pProduct) void onDestroy(AbstractProduct * const pProduct)
{ {
CP::onDestroy(); CP::onDestroy();
SP::onDestroy(); SP::onDestroy();
@ -761,7 +756,7 @@ namespace Loki
} }
protected: protected:
virtual void remove(AbstractProduct* pProduct) virtual void remove(AbstractProduct * const pProduct)
{ {
typename FetchedObjToKeyMap::iterator fetchedItr = providedObjects.find(pProduct); typename FetchedObjToKeyMap::iterator fetchedItr = providedObjects.find(pProduct);
if(fetchedItr!=providedObjects.end()) // object is unreleased. if(fetchedItr!=providedObjects.end()) // object is unreleased.
@ -769,7 +764,7 @@ namespace Loki
bool productRemoved = false; bool productRemoved = false;
typename KeyToObjVectorMap::iterator objVectorItr; typename KeyToObjVectorMap::iterator objVectorItr;
typename ObjVector::iterator objItr; typename ObjVector::iterator objItr;
for(objVectorItr=fromKeyToObjVector.begin();objVectorItr!=fromKeyToObjVector.end();objVectorItr++) for(objVectorItr=fromKeyToObjVector.begin();objVectorItr!=fromKeyToObjVector.end();++objVectorItr)
{ {
ObjVector &v(objVectorItr->second); ObjVector &v(objVectorItr->second);
objItr = remove_if(v.begin(), v.end(), bind2nd(equal_to<AbstractProduct*>(), pProduct)); objItr = remove_if(v.begin(), v.end(), bind2nd(equal_to<AbstractProduct*>(), pProduct));
@ -803,8 +798,9 @@ namespace Loki
D( cout << "====>> Cache destructor : deleting "<< providedObjects.size()<<" objects <<====" << endl << endl; ) D( cout << "====>> Cache destructor : deleting "<< providedObjects.size()<<" objects <<====" << endl << endl; )
} }
// cleaning the Cache // cleaning the Cache
typename FetchedObjToKeyMap::iterator itr; typename FetchedObjToKeyMap::iterator itr;
for(itr=providedObjects.begin(); itr!=providedObjects.end();itr++) for(itr=providedObjects.begin(); itr!=providedObjects.end();++itr)
delete itr->first; delete itr->first;
} }
@ -829,9 +825,8 @@ namespace Loki
return factory.Unregister(id); return factory.Unregister(id);
} }
// TODO : prevent the Vector to be returned by copy.
/// Return the registered ID in this Factory /// Return the registered ID in this Factory
std::vector<IdentifierType> RegisteredIds() std::vector<IdentifierType>& RegisteredIds()
{ {
return factory.RegisteredIds(); return factory.RegisteredIds();
} }