コード例 #1
0
        int get(int key) {
            // The accessed node must be up-to-time -- take to the front 
            if (cacheMap.find(key) != cacheMap.end() ){
                cacheList.TakeToBegin(cacheMap[key]);
                return cacheMap[key]->value;
            }
            return -1;

        }
コード例 #2
0
 void set(int key, int value) {
     // key found, update the data, and take to the front 
     if (cacheMap.find(key) != cacheMap.end() ){
         Node *p = cacheMap[key];
         p->value = value;
         cacheList.TakeToBegin(cacheMap[key]);
     }else{
         // key not found, new a node to store data
         cacheMap[key] = cacheList.NewAtBegin(key, value);
         // if the capacity exceed, remove the last one.
         if( cacheList.Size() > capacity) {
             int key = cacheList.GetTailNode()->key; 
             cacheMap.erase(key);
             cacheList.DeleteLast();
         }
     }
 }