The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 09-25-2008
Orlando_ua Orlando_ua is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 1
Exclamation 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 04:19 AM..
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:46 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0