09-25-2008
STL MRU cache source from win
Help me please with STL source code that works on Windows
I've found on Inet STL MRU Cache (it compiles fine with Studio 2008), but when trying to build it with Kdevelop (g++ is the compiler) I've got a series of error. One of them I've placed in the source code. If it's important I can post here log with full error list, but fighting this error is major.
If you know the solution please post.
Thanks beforehands, Alex
//Source code
#include <list>
#include <map>
/**
* MRU Cache
*
* contains up to a fixed size of "Most Recently Used" items, where items are assiciated with keys.
* when asked to fetch a value that is not in the cache, HandleNonExistingKeyFetch is called.
* when an item is removed from the cache, HandleItemRelease is called.
* implementor of this cache must provide those methods.
*
*/
template <typename key_type, typename value_type>
class MruCache
{
public:
const int maxLength;
MruCache(int iMaxLength) : maxLength(iMaxLength) { }
inline value_type FetchItem(key_type key) { return __fetch_item(key); }
virtual ~MruCache() { Clear(); }
/**
* clear the cache.
* for every item contained, HandleItemRelease is called.
*/
virtual void Clear() { __clear(); }
protected:
virtual void HandleItemRelease(key_type key, value_type value) { };
virtual value_type HandleNonExistingKeyFetch(key_type key) = 0;
private:
typedef struct _Entry
{
key_type key;
value_type value;
} Entry;
typedef std::list<Entry> EntryList;
EntryList listOfEntries;
/**
* for fast search in the cache. this map contains pointers to iterators in EntryList.
*/
typedef std::map<key_type, void*> ItrPtrMap;
ItrPtrMap mapOfListIteratorPtr;
value_type __fetch_item(key_type key)
{
Entry entry;
//Error bangs on the line below, "ptrItr not found in the scope"
EntryList::iterator* ptrItr = (EntryList::iterator*) mapOfListIteratorPtr[key];
if (!ptrItr)
{
if ( (int)listOfEntries.size() >= maxLength)
{
Entry lruEntry = listOfEntries.back();
HandleItemRelease(lruEntry.key, lruEntry.value);
listOfEntries.pop_back();
delete mapOfListIteratorPtr[lruEntry.key];
mapOfListIteratorPtr.erase(lruEntry.key);
}
entry.value = HandleNonExistingKeyFetch(key);
entry.key = key;
listOfEntries.push_front(entry);
EntryList::iterator* ptrItr = new EntryList::iterator();
*ptrItr = listOfEntries.begin();
mapOfListIteratorPtr[key] = ptrItr;
}
else
{
entry = *(*ptrItr);
listOfEntries.erase(*ptrItr);
listOfEntries.push_front(entry);
*ptrItr = listOfEntries.begin();
}
return entry.value;
}
virtual void __clear()
{
for (ItrPtrMap::iterator i=mapOfListIteratorPtr.begin(); i!=mapOfListIteratorPtr.end(); i++)
{
void* ptrItr = i->second;
EntryList::iterator* pItr = (EntryList::iterator*) ptrItr;
HandleItemRelease( (*pItr)->key, (*pItr)->value );
delete ptrItr;
}
listOfEntries.clear();
mapOfListIteratorPtr.clear();
}
};
Last edited by Orlando_ua; 09-25-2008 at 05:19 AM..
6 More Discussions You Might Find Interesting
1. Programming
Hi there,
I am using the STL map and I print the map using:
map <string, float> ngram_token_index ;
map <string, float>::iterator map_iter ;
//read the map
...
// print the map
for ( map_iter = ngram_token_index.begin() ; map_iter != ngram_token_index.end() ; map_iter++ )
cout << ... (2 Replies)
Discussion started by: superuser84
2 Replies
2. UNIX for Dummies Questions & Answers
Hello,
I'm using RHEL 5.3, I need to compile C++ code and I'd like to know which rpm contains the STL library.
Thanks:) (3 Replies)
Discussion started by: pppswing
3 Replies
3. Programming
class tst
{
public:
tst() {
cout<<"ctor tst()\n";
}
tst(const tst& ob) {
cout<<"cp ctor tst()\n";
}
~tst()
{
cout<<"dtor tst()\n";
}
};
map<string,tst> mp;
int main(void)
{
mp;
//mp=tst();
} (1 Reply)
Discussion started by: johnbach
1 Replies
4. Programming
hi all
i have a tree structure:
root --- node 1 to node 10
in each node --- sub node 1 to sub node 10
in each sub node --- leaf node 1 to leaf node 10
i have to print this in following fashion:
root ---- n1 ---- sn1 ---- l1, l2, l3, ....., l10
n1 ---- sn2 ---- l1, l2, l3,... (2 Replies)
Discussion started by: vidyaj
2 Replies
5. Programming
Hi, guys.
I am working on a project right now. But when I debug my code, I can't see the values in stl container, e.g vector, map. Can anyone help me, please. I am really frustrated:wall:. I am using code::blocks IDE, by the way. Thanks in advance...... (0 Replies)
Discussion started by: tefino
0 Replies
6. Programming
Hi all,
I pass to the transform algorithm two vectors, and the suma function.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
class Duo{
public:
int one;
int two;
};
Duo suma(Duo first, Duo last){
Duo ret;
... (1 Reply)
Discussion started by: santiagorf
1 Replies