|
| 1 | +#include <unordered_map> |
| 2 | +#include <iostream> |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +struct node { |
| 6 | + int key_; |
| 7 | + int value_; |
| 8 | + node* pre_; |
| 9 | + node* next_; |
| 10 | + node(int key, int val): key_(key), value_(val), pre_(NULL), next_(NULL) {} |
| 11 | +}; |
| 12 | + |
| 13 | +class node_list { |
| 14 | +public: |
| 15 | + node* head_; |
| 16 | + node* tail_; |
| 17 | + int length_; |
| 18 | + node_list(): head_(NULL), tail_(NULL), length_(0) {} |
| 19 | + |
| 20 | + void push(node* n){ |
| 21 | + if (head_==NULL && tail_==NULL) |
| 22 | + head_ = tail_ = n; |
| 23 | + else { |
| 24 | + n->next_ = head_; |
| 25 | + n->pre_ = NULL; |
| 26 | + head_->pre_ = n; |
| 27 | + head_ = n; |
| 28 | + } |
| 29 | + ++length_; |
| 30 | + } |
| 31 | + |
| 32 | + void pop(){ |
| 33 | + if (head_==NULL && tail_==NULL) return; |
| 34 | + if (head_ == tail_) { |
| 35 | + head_ = tail_ = NULL; |
| 36 | + return; |
| 37 | + } |
| 38 | + tail_ = tail_->pre_; |
| 39 | + tail_->next_ = NULL; |
| 40 | + --length_; |
| 41 | + } |
| 42 | + |
| 43 | + void update(node* n){ |
| 44 | + if (n==NULL || n==head_) return; |
| 45 | + if (n->pre_!=NULL) n->pre_->next_ = n->next_; |
| 46 | + if (n->next_!=NULL) n->next_->pre_ = n->pre_; |
| 47 | + if (n==tail_) tail_ = tail_->pre_; |
| 48 | + head_->pre_ = n; |
| 49 | + n->next_ = head_; |
| 50 | + n->pre_ = NULL; |
| 51 | + head_ = n; |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + void print() { |
| 56 | + node* p = head_; |
| 57 | + while (p!=NULL){ |
| 58 | + cout << "print\tkey\t" << p->key_ << "\tvalue\t" << p->value_ << endl; |
| 59 | + p = p->next_; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + int len(){ |
| 64 | + return length_; |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +class LRUCache{ |
| 69 | +public: |
| 70 | + int capacity_; |
| 71 | + unordered_map<int, node*> mapping; |
| 72 | + node_list lru_list; |
| 73 | +public: |
| 74 | + LRUCache(int capacity): capacity_(capacity) {} |
| 75 | + |
| 76 | + int get(int key) { |
| 77 | + if (mapping.find(key)!=mapping.end()) { |
| 78 | + lru_list.update(mapping[key]); |
| 79 | + return mapping[key]->value_; |
| 80 | + } |
| 81 | + else |
| 82 | + return -1; |
| 83 | + } |
| 84 | + |
| 85 | + void set(int key, int value) { |
| 86 | + if (mapping.find(key)!=mapping.end()){ |
| 87 | + mapping[key]->value_ = value; |
| 88 | + lru_list.update(mapping[key]); |
| 89 | + return; |
| 90 | + } |
| 91 | + if (lru_list.len()==capacity_){ |
| 92 | + node* tail = lru_list.tail_; |
| 93 | + lru_list.pop(); |
| 94 | + mapping.erase(mapping.find(tail->key_)); |
| 95 | + delete tail; |
| 96 | + } |
| 97 | + node* new_node = new node(key, value); |
| 98 | + lru_list.push(new_node); |
| 99 | + mapping[key] = new_node; |
| 100 | + } |
| 101 | +}; |
| 102 | + |
| 103 | +int main(){ |
| 104 | + LRUCache cache(2); |
| 105 | + cache.set(2, 1); |
| 106 | + cache.lru_list.print(); |
| 107 | + cache.set(1, 1); |
| 108 | + cache.lru_list.print(); |
| 109 | + cache.get(2); |
| 110 | + cache.lru_list.print(); |
| 111 | + cache.set(4,1); |
| 112 | + cache.lru_list.print(); |
| 113 | + cache.get(1); |
| 114 | + cache.lru_list.print(); |
| 115 | + cache.get(2); |
| 116 | + cache.lru_list.print(); |
| 117 | + return 0; |
| 118 | +} |
0 commit comments