void test() { // LRUCache * one = new LRUCache(1); // one->set(2,1); // cout << one->get(2) << endl; // one->set(3,2); // cout << one->get(2) << endl; // cout << one->get(3) << endl; // // one = new LRUCache(2); // one->set(2,1); // one->set(1,1); // cout << one->get(2) << endl; // one->set(4,1); // cout << one->get(1) << endl; // cout << one->get(2) << endl; LRUCache *one = new LRUCache(2); one->set(2,1); one->set(3,2); cout << one->get(3) << endl; cout << one->get(2) << endl; one->set(4,3); cout << one->get(2) << endl; cout << one->get(3) << endl; cout << one->get(4) << endl; }
int main(int argc, char const *argv[]) { LRUCache *cache = new LRUCache(1); cache->set(2, 1); cout<<cache->get(2)<<endl; cache->set(3, 2); cout<<cache->get(2)<<endl; cout<<cache->get(3)<<endl; return 0; }
int main(){ LRUCache* lru = new LRUCache(2); lru->set(2,1); lru->set(1,1); cout << lru->get(2); lru->set(4,1); cout << lru->get(1); cout << lru->get(2); }
int main(){ LRUCache *lru = new LRUCache(1); lru->set(1,1); lru->set(2,2); cout << lru->get(1)<< endl; return 1; }
void payload() { LCG rng; m_dummy = 0; for (size_t i = 0; i < 1000; ++i) m_dummy += m_cache.get(rand_int1(rng, 0, 99)); }
void Storage::CheckMemory(LRUCache &rLRUCache) { uint64 xToDelete; RecordIndexMap::accessor rWriteAccessor; while(m_memoryUsed > g_cfg.MemoryLimit) { if(!rLRUCache.get(&xToDelete)) break; //get record index if(m_dataIndexes.find(rWriteAccessor, xToDelete)) { //check if record does not wait in disk thread if(rWriteAccessor->second.m_flags & eRIF_InDiskWriteQueue) return; //can be NULL if(rWriteAccessor->second.m_pBlockManager) { //log Log.Debug(__FUNCTION__, "Memory usage: " I64FMTD ", memory limit: " I64FMTD ", removing x: " I64FMTD " from memory.", m_memoryUsed.load(), g_cfg.MemoryLimit, xToDelete); //update memory usage uint64 memoryUsage = blman_get_memory_usage(rWriteAccessor->second.m_pBlockManager); m_memoryUsed -= memoryUsage; //clean memory blman_destroy(rWriteAccessor->second.m_pBlockManager); rWriteAccessor->second.m_pBlockManager = NULL; } } //delete from LRU if(rLRUCache.remove(xToDelete) == false) { Log.Warning(__FUNCTION__, "m_pLRUCache->remove(" I64FMTD ") == false", xToDelete); } } }
void test(LRUCache<int>& lruCache){ lruCache.insert("one",1); lruCache.insert("two",2); lruCache.insert("three",3); lruCache.insert("four",4); lruCache.insert("five",5); lruCache.insert("six",6); lruCache.insert("seven",7); lruCache.insert("eight",8); lruCache.insert("nine",9); lruCache.insert("ten",10); std::cout << "check " << 1 << ":" << lruCache.get("one") << std::endl; std::cout << "check " << 11 << ":" << lruCache.get("eleven")<< std::endl; lruCache.insert("eleven",11); std::cout << "check " << 2 << ":" << lruCache.get("two")<< std::endl; std::cout << "check " << 11 << ":" << lruCache.get("eleven")<< std::endl; lruCache.insert("twelve",12); std::cout << "check " << 12 << ":" << lruCache.get("twelve")<< std::endl; std::cout << "check " << 3 << ":" << lruCache.get("three")<< std::endl; std::cout << "check " << 13 << ":" << lruCache.get("thirteen")<< std::endl; return; }
void test2(LRUCache<int>& lruCache){ lruCache.insert("1one",101); lruCache.insert("1two",102); lruCache.insert("1three",103); lruCache.insert("1four",104); lruCache.insert("1five",105); lruCache.insert("1six",106); lruCache.insert("1seven",107); lruCache.insert("1eight",108); lruCache.insert("1nine",109); lruCache.insert("1ten",110); std::cout << "check " << 101 << ":" << lruCache.get("1one") << std::endl; std::cout << "check " << 111 << ":" << lruCache.get("1eleven")<< std::endl; lruCache.insert("1eleven",111); std::cout << "check " << 102 << ":" << lruCache.get("1two")<< std::endl; std::cout << "check " << 111 << ":" << lruCache.get("1eleven")<< std::endl; lruCache.insert("1twelve",112); std::cout << "check " << 112 << ":" << lruCache.get("1twelve")<< std::endl; std::cout << "check " << 103 << ":" << lruCache.get("1three")<< std::endl; std::cout << "check " << 113 << ":" << lruCache.get("1thirteen")<< std::endl; return; }
} // 插入新节点到链表头部,并且在map中增加该节点。 m_cachelist.push_front(CacheNode(key, value)); m_cachemap[key] = m_cachelist.begin(); }else { // 更新节点的值,把当前访问的节点移到链表头部,并且更新map中该节点的地址。 m_cachemap[key]->value = value; m_cachelist.splice(m_cachelist.begin(), m_cachelist, m_cachemap[key]); m_cachemap[key] = m_cachelist.begin(); } } private: list<CacheNode> m_cachelist; unordered_map<int, list<CacheNode>::iterator> m_cachemap; int m_capacity; }; //-------------------------------------------------------------------------------------------------------------- TEST_CASE("LRU_Cache", "[Linked Lists]"){ LRUCache cache(5); cache.set(5, 50); REQUIRE(cache.get(5) == 50); cache.set(2, 20); REQUIRE(cache.get(2) == 20); cache.set(1, 10); cache.set(3, 30); cache.set(4, 40); cache.set(0, 100); REQUIRE(cache.get(5) == -1); }
//コメントアウト部をonにすればキャッシュにない画像にアクセスすることになるためエラーを吐くようになっている void imageTest(LRUCache<cv::Mat>& lruCache){ std::string filepath1 = "Sample Pictures\\angkor-wat.jpg"; std::string filepath2 = "Sample Pictures\\castle.jpg"; std::string filepath3 = "Sample Pictures\\Chrysanthemum.jpg"; std::string filepath4 = "Sample Pictures\\Desert.jpg"; std::string filepath5 = "Sample Pictures\\Hydrangeas.jpg"; std::string filepath6 = "Sample Pictures\\Jellyfish.jpg"; std::string filepath7 = "Sample Pictures\\Koala.jpg"; std::string filepath8 = "Sample Pictures\\Lake.jpg"; std::string filepath9 = "Sample Pictures\\Lighthouse.jpg"; std::string filepath10 = "Sample Pictures\\Penguins.jpg"; std::string filepath11 = "Sample Pictures\\Statue.jpg"; std::string filepath12 = "Sample Pictures\\Tulips.jpg"; cv::Mat img1 = cv::imread(filepath1,1); cv::Mat img2 = cv::imread(filepath2,1); cv::Mat img3 = cv::imread(filepath3,1); cv::Mat img4 = cv::imread(filepath4,1); cv::Mat img5 = cv::imread(filepath5,1); cv::Mat img6 = cv::imread(filepath6,1); cv::Mat img7 = cv::imread(filepath7,1); cv::Mat img8 = cv::imread(filepath8,1); cv::Mat img9 = cv::imread(filepath9,1); cv::Mat img10 = cv::imread(filepath10,1); cv::Mat img11 = cv::imread(filepath11,1); cv::Mat img12 = cv::imread(filepath12,1); lruCache.insert(filepath1,img1); lruCache.insert(filepath2,img2); lruCache.insert(filepath3,img3); lruCache.insert(filepath4,img4); lruCache.insert(filepath5,img5); lruCache.insert(filepath6,img6); lruCache.insert(filepath7,img7); lruCache.insert(filepath8,img8); lruCache.insert(filepath9,img9); lruCache.insert(filepath10,img10); cv::namedWindow("image1", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO); cv::imshow("image1",lruCache.get(filepath1)); cv::waitKey(0); //cv::namedWindow("image2", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO); //cv::imshow("image2",lruCache.get(filepath11)); //cv::waitKey(0); lruCache.insert(filepath11,img11); cv::namedWindow("image3", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO); cv::imshow("image3",lruCache.get(filepath11)); cv::waitKey(0); lruCache.insert(filepath12,img12); //cv::namedWindow("image4", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO); // cv::imshow("image4",lruCache.get(filepath2)); //cv::waitKey(0); cv::namedWindow("image5", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO); cv::imshow("image5",lruCache.get(filepath12)); cv::waitKey(0); return; }