【LRU Cache】(Least recently used | template ADT| list + double hash)

RESULT

   TEST DEMO

int main(void){
	constexpr auto ADDRESS_MAX { 1024U };
	srand((unsigned)time(nullptr));

	using Address_t = int32_t;
	using Data_t    = char;
	LRUCache<Address_t, Data_t> cache;

	std::mt19937 gen(std::random_device{}());
	std::uniform_int_distribution<> distrib(0, 100);

	int rounds { 1024 };
	std::size_t hits { 0U };
	std::size_t misses { 0U };
	std::size_t loads { 0U };
	std::size_t fetches { 0U };

	while( rounds-- ){
		if(distrib(gen) <= 90){
			++loads;
			int address { rand() % ADDRESS_MAX };
			char data { (char)(rand() % 26 + 'a') };
			std::printf("load [%x, %c]...\n",address, data);
			cache.put({address, data});
		}else{
			++fetches;
			int address { rand() % ADDRESS_MAX };
			std::puts("--------------------------------------------");
			std::printf("try to fetch data at address [%x] from cache...\n", address);
			auto result { cache.get(address) };
			if( result.has_value() ){
				++hits;
				std::printf("hit the data [%c] from cache...\n", *result);
			}else{
				++misses;
				puts("miss the data from cache...\n");
			}
			std::puts("--------------------------------------------");
		}
	}

	puts("---------LRUCache result-------------");
	std::cout << "load times:\t" << loads << '\n'
			  << "fetch times:\t" << fetches << '\n'
			  << "hit times:\t"   << hits << '\n'
			  << "miss times:\t"  << misses << '\n'
			  << "hit rate:\t"
			  << std::setprecision(4)
			  << (double)hits/fetches;
   std::endl(std::cout);
	return 0;
}

DETAIL IMPLEMENTATION

#include <list>
#include <unordered_map>
#include <concepts>
#include <type_traits>
#include <utility>
#include <optional>
#include <random>
#include <exception>
#include <iostream>
#include <iomanip>

template <class Key,
		  class Value,
		  class Hash = std::hash<Key>,
		  class = std::common_type<
		  typename std::result_of<typename
		  std::decay<Hash>::type(Key&&)>::type,
		  std::size_t>>
class LRUCache{
public:
	typedef std::pair<Key, Value> store_type;
	typedef std::optional<Value> get_return_type;
	static constexpr std::size_t default_capacity { 64U };
protected:
	std::unordered_map<Key, Value, Hash> hash;
	std::list<Key> cache;
	std::unordered_map<Key,
		typename decltype(cache)::iterator>
	assotiator;
private:
	std::size_t capacity { default_capacity };
	template <class K, class V, class H, class>
	friend class LRUCache;
public:
	constexpr LRUCache(void) noexcept = default;
	LRUCache(std::size_t __capacity) noexcept
	: capacity(__capacity) {}
	LRUCache(const LRUCache&) = default;
	explicit LRUCache(LRUCache&&) noexcept = default;
	
	[[discard]] get_return_type
	get(const Key& key) try{
		if(!hash.count(key))
			return static_cast
			<get_return_type>(std::nullopt);
		const auto& it = assotiator[key];
		cache.splice(cache.begin(), cache, it);
		return std::make_optional<Value>(*it);
	}catch(const std::exception& e){
		std::cerr << "located LRU::get():\t"
				  << e.what() << '\n';
	}
	void put(const store_type& p)
		requires std::assignable_from<Key&, Key>
		&&       std::assignable_from<Value&, Value>
	try{
		const auto& [__key, __value] = p;
		if(hash.count(__key)) cache.erase(assotiator[__key]);
		cache.push_front(__key);
		hash.insert(p);
		assotiator[__key] = cache.begin();
		
		if(cache.size() > capacity){
			auto& __fkey = cache.back();
			hash.erase(__fkey);
			assotiator.erase(__fkey);
			cache.pop_back();
		}
	}catch(const std::exception& e){
		std::cout << "locate LRUCache::put():\t"
				  << e.what() << '\n';
	}
};

 

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XNB's Not a Beginner

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值