Skip to content

Commit be5a896

Browse files
committed
lru-cache
1 parent 44ae9ee commit be5a896

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

src/lru-cache/bench.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "solution.hpp"
2+
3+
#include <benchmark/benchmark.h>
4+
5+
6+
static void BM_LRU_Cache(benchmark::State& state) {
7+
for (auto _ : state) {
8+
auto s = std::make_shared<LRUCache>(2);
9+
s->put(2, 1);
10+
s->put(1, 1);
11+
s->put(2, 3);
12+
s->put(4, 1);
13+
s->get(1);
14+
s->get(2);
15+
}
16+
}
17+
18+
BENCHMARK(BM_LRU_Cache);
19+
BENCHMARK_MAIN();

src/lru-cache/solution.hpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <leetcode.hpp>
2+
#include <unordered_map>
3+
#include <list>
4+
5+
using namespace leetcode;
6+
7+
class LRUCache
8+
{
9+
public:
10+
using kv = std::pair<int, int>;
11+
using list_iter = std::list<kv>::iterator;
12+
13+
LRUCache(int capacity)
14+
{
15+
this->capacity_ = capacity;
16+
}
17+
18+
int get(int key)
19+
{
20+
auto it = cache_item_map_.find(key);
21+
if (it == cache_item_map_.end())
22+
{
23+
return -1;
24+
}
25+
else
26+
{
27+
cache_item_list_.splice(cache_item_list_.begin(), cache_item_list_, it->second);
28+
return it->second->second;
29+
}
30+
}
31+
32+
void put(int key, int value)
33+
{
34+
auto it = cache_item_map_.find(key);
35+
if (it == cache_item_map_.end())
36+
{
37+
cache_item_list_.push_front({key, value});
38+
cache_item_map_[key] = cache_item_list_.begin();
39+
40+
if (cache_item_map_.size() > capacity_)
41+
{
42+
auto last = cache_item_list_.end();
43+
--last;
44+
cache_item_map_.erase(last->first);
45+
cache_item_list_.pop_back();
46+
}
47+
48+
}
49+
else
50+
{
51+
it->second->second = value;
52+
cache_item_list_.splice(cache_item_list_.begin(), cache_item_list_, it->second);
53+
}
54+
}
55+
56+
private:
57+
std::list<kv> cache_item_list_;
58+
std::unordered_map<int, list_iter> cache_item_map_;
59+
int capacity_;
60+
};

src/lru-cache/test.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "solution.hpp"
2+
#include <memory>
3+
4+
#define CATCH_CONFIG_MAIN
5+
#include <catch2/catch.hpp>
6+
7+
TEST_CASE(__FILE__){
8+
SECTION("example1"){
9+
auto s = std::make_shared<LRUCache>(2);
10+
s->put(1, 1);
11+
s->put(2, 2);
12+
REQUIRE(s->get(1) == 1);
13+
s->put(3, 3);
14+
REQUIRE(s->get(2) == -1);
15+
s->put(4, 4);
16+
REQUIRE(s->get(1) == -1);
17+
REQUIRE(s->get(3) == 3);
18+
REQUIRE(s->get(4) == 4);
19+
}
20+
21+
SECTION("example2"){
22+
auto s = std::make_shared<LRUCache>(2);
23+
s->put(2, 1);
24+
s->put(1, 1);
25+
s->put(2, 3);
26+
s->put(4, 1);
27+
REQUIRE(s->get(1) == -1);
28+
REQUIRE(s->get(2) == 3);
29+
}
30+
31+
32+
}

xmake.lua

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ define_target("reverse-linked-list")
3535
define_target("binary-tree-maximum-path-sum")
3636
define_target("lowest-common-ancestor-of-a-binary-tree")
3737
define_target("delete-node-in-a-bst")
38+
define_target("lru-cache")
3839

3940
task("problem")
4041

0 commit comments

Comments
 (0)