Ejemplo n.º 1
0
 //キャッシュへの挿入
 void insert(std::string key, DATA data){
     mutex.lock();
     //std::cout << "insert " << data << std::endl; 
     //新データの場合(挿入しサイズをオーバーする場合は一番古いものを削除)
     if (mCacheMap.count(key) == 0)
     {
         //listの先頭にクエリ追加
         mCacheList.push_front(std::make_pair(key, data));
         //ポインタの位置を保持
         mCacheMap[key] = mCacheList.begin();
         entries++;
         if (entries > capacity)
         {
             //keyを基にlistの最後尾をさすポインタを消去
             mCacheMap.erase(mCacheList.back().first);
             //キャッシュのLRU要素を削除
             mCacheList.pop_back();
             entries--;
         }
     }
     //既にあるデータの場合(位置を一番前にもってくるだけ)
     else
     {
         mCacheList.erase(mCacheMap[key]);
         mCacheList.push_front(std::make_pair(key, data));
         //ポインタの位置を先頭に更新
         mCacheMap[key] = mCacheList.begin();
     }
     mutex.unlock();
 }
Ejemplo n.º 2
0
char* rx_compile(const gcstring& s)
	{
	static CacheMap<10, gcstring, char*> cache;
	if (char** p = cache.get(s))
		return *p;
	return cache.put(s, RxCompile(s.ptr(), s.size()).compile());
	}
Ejemplo n.º 3
0
 //keyを基に値の取得
 DATA get(std::string key){
     if (mCacheMap.count(key) == 1){
         mutex.lock();
         EntryPair value = *mCacheMap[key];
         //現在の位置から削除し、最初の位置に変更
         mCacheList.erase(mCacheMap[key]);
         mCacheList.push_front(value);
         //ポインタの位置を先頭に更新
         mCacheMap[key] = mCacheList.begin();
         mutex.unlock();
         return value.second;
     }
     //キャッシュに求める値が入っていない場合 (エラーを投げる)
     else
     {
         throw "NOT IN YOUR CACHE";
         //値を返すようにする場合はこちら
         //if (typeid(DATA) == typeid(cv::Mat))
         //{
         //  cv::Mat mtx(cv::Size(100,100), CV_16U,cv::Scalar(0,0,0));
         //  return mtx;
         //}
         //if (typeid(DATA) == typeid(int))
         //{
         //  return -1;
         //}
     }
 }
Ejemplo n.º 4
0
void LinkedHashMapTest::testRemoveEldest() {

    int i;
    int size = 10;
    CacheMap map;
    for (i = 0; i < size; i++) {
        map.put(i, i * 2);
    }

    Collection<int>& values = map.values();
    Pointer< Iterator<int> > iter(values.iterator());

    CPPUNIT_ASSERT_MESSAGE("Returned set of incorrect size 1", map.size() == values.size());
    for (i = 5; iter->hasNext(); i++) {
        int current = iter->next();
        CPPUNIT_ASSERT_MESSAGE("Returned incorrect entry set 1", current == i * 2);
    }
    CPPUNIT_ASSERT_MESSAGE("Entries left in map", !iter->hasNext());

    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect number of removals", 5, map.removals);
}
Ejemplo n.º 5
0
bool Compare(const CacheMap<int,int>& map1, const CacheMap<int,int>& map2 )
{
    if(map1.GetMaxSize() != map2.GetMaxSize()) {
        return false;
    }

    if(map1.GetSize() != map2.GetSize()) {
        return false;
    }

    const CacheMap<int,int>::list_t& items1 = map1.GetItemList();
    for(CacheMap<int,int>::list_cit it = items1.begin(); it != items1.end(); ++it) {
        if(!map2.HasKey(it->key)) {
            return false;
        }
        int val = 0;
        if(!map2.Get(it->key, val)) {
            return false;
        }
        if(it->value != val) {
            return false;
        }
    }

    const CacheMap<int,int>::list_t& items2 = map2.GetItemList();
    for(CacheMap<int,int>::list_cit it = items2.begin(); it != items2.end(); ++it) {
        if(!map1.HasKey(it->key)) {
            return false;
        }
        int val = 0;
        if(!map1.Get(it->key, val)) {
            return false;
        }
        if(it->value != val) {
            return false;
        }
    }

    return true;
}
Ejemplo n.º 6
0
long progress(int x, int y)
{
    // if this route has been solved before then lookup that answer in the cache
    CacheMap::iterator itr = cache.find(hash(x, y));
    if (itr != cache.end())
        return (*itr).second;

    long i = 0;

    // solve it recursively
    if (x < gridSize)
        i += progress(x + 1, y);
    if (y < gridSize)
        i += progress(x, y + 1);

    if (x == gridSize && y == gridSize)
        return 1;

    // store this answer in the cache so that we don't have to do it again
    cache.insert(CacheMap::value_type(hash(x, y), i));

    return i;
}
Ejemplo n.º 7
0
 ~LRUCache(){
     mCacheList.clear();
     mCacheMap.clear();
 }