|
| 1 | +packagehard; |
| 2 | + |
| 3 | +importjava.util.HashMap; |
| 4 | +importjava.util.Map; |
| 5 | + |
| 6 | +publicclassLRUCache_use_DoublyLinkedList_plus_HashMap { |
| 7 | +privateclassNode { |
| 8 | +intkey,value; |
| 9 | +Nodeprev,next; |
| 10 | + |
| 11 | +Node(intk,intv) { |
| 12 | +this.key =k; |
| 13 | +this.value =v; |
| 14 | + } |
| 15 | + |
| 16 | +Node() { |
| 17 | +this.key =0; |
| 18 | +this.value =0; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | +privateintcapacity; |
| 23 | +privateintcount; |
| 24 | +privateNodehead,tail; |
| 25 | +privateMap<Integer,Node>map;// ATTN: the value should be Node type! This is the whole point |
| 26 | +// of having a class called Node! |
| 27 | + |
| 28 | +publicLRUCache_use_DoublyLinkedList_plus_HashMap(intcapacity) { |
| 29 | +this.capacity =capacity; |
| 30 | +this.count =0;// we need a count to keep track of the number of elements in the cache so |
| 31 | +// that we know when to evict the LRU one from the cache |
| 32 | +this.map =newHashMap(); |
| 33 | +head =newNode(); |
| 34 | +tail =newNode(); |
| 35 | +head.next =tail; |
| 36 | +tail.prev =head; |
| 37 | + } |
| 38 | + |
| 39 | +publicintget(intkey) { |
| 40 | +Nodenode =map.get(key);// HashMap allows value to be null, this is superior than |
| 41 | +// HashTable! |
| 42 | +if (node ==null) { |
| 43 | +return -1; |
| 44 | + }else { |
| 45 | +update(node); |
| 46 | +returnnode.value; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | +publicvoidset(intkey,intvalue) { |
| 51 | +Nodenode =map.get(key); |
| 52 | +if (node ==null) { |
| 53 | +node =newNode(key,value); |
| 54 | +map.put(key,node); |
| 55 | +add(node); |
| 56 | +count++; |
| 57 | + |
| 58 | +if (count >capacity) { |
| 59 | +// ATTN: It's tail.prev, not tail, because tail is always an invalid node, it |
| 60 | +// doesn't contain anything, it's always the tail.prev that is the last node in the |
| 61 | +// cache |
| 62 | +NodetoDelete =tail.prev; |
| 63 | +map.remove(toDelete.key); |
| 64 | +remove(toDelete); |
| 65 | +count--; |
| 66 | + } |
| 67 | + }else { |
| 68 | +remove(node); |
| 69 | +node.value =value; |
| 70 | +add(node); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | +privatevoidupdate(Nodenode) { |
| 75 | +// this simplifies the process, just do two operations, remove the old node first, and then |
| 76 | +// just add the node again |
| 77 | +// this will guarantee that this node will be at the latest position: the most recently used |
| 78 | +// position. |
| 79 | +remove(node); |
| 80 | +add(node); |
| 81 | + } |
| 82 | + |
| 83 | +privatevoidremove(Nodenode) { |
| 84 | +Nodenext =node.next,prev =node.prev; |
| 85 | +prev.next =next; |
| 86 | +next.prev =prev; |
| 87 | + } |
| 88 | + |
| 89 | +privatevoidadd(Nodenode) {// ATTN: we'll always add the node into the first position: |
| 90 | +// head.next!!!! |
| 91 | +Nodenext =head.next; |
| 92 | +head.next =node; |
| 93 | +node.next =next; |
| 94 | +node.prev =head; |
| 95 | +next.prev =node; |
| 96 | + } |
| 97 | + |
| 98 | +} |