UNPKG

4.84 kBJavaScriptView Raw
1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3import { __values } from "tslib";
4var DoubleLinkedNode = /** @class */ (function () {
5 function DoubleLinkedNode(keyVal) {
6 this.key = keyVal ? keyVal : '';
7 this.prevNode = null;
8 this.nextNode = null;
9 }
10 return DoubleLinkedNode;
11}());
12/**
13 * double linked list plus a hash table inside
14 * each key in the cache stored as a node in the list
15 * recently visited node will be rotated to the head
16 * so the Last Recently Visited node will be at the tail
17 *
18 * @member head - dummy head of the linked list
19 * @member tail - dummy tail of the linked list
20 * @member hashtable - the hashtable which maps cache key to list node
21 * @member length - length of the list
22 */
23var CacheList = /** @class */ (function () {
24 /**
25 * initialization
26 */
27 function CacheList() {
28 this.head = new DoubleLinkedNode();
29 this.tail = new DoubleLinkedNode();
30 this.hashtable = {};
31 this.length = 0;
32 this.head.nextNode = this.tail;
33 this.tail.prevNode = this.head;
34 }
35 /**
36 * insert node to the head of the list
37 *
38 * @param node
39 */
40 CacheList.prototype.insertNodeToHead = function (node) {
41 var tmp = this.head.nextNode;
42 this.head.nextNode = node;
43 node.nextNode = tmp;
44 node.prevNode = this.head;
45 tmp.prevNode = node;
46 this.length = this.length + 1;
47 };
48 /**
49 * remove node
50 *
51 * @param node
52 */
53 CacheList.prototype.removeNode = function (node) {
54 node.prevNode.nextNode = node.nextNode;
55 node.nextNode.prevNode = node.prevNode;
56 node.prevNode = null;
57 node.nextNode = null;
58 this.length = this.length - 1;
59 };
60 /**
61 * @return true if list is empty
62 */
63 CacheList.prototype.isEmpty = function () {
64 return this.length === 0;
65 };
66 /**
67 * refresh node so it is rotated to the head
68 *
69 * @param key - key of the node
70 */
71 CacheList.prototype.refresh = function (key) {
72 var node = this.hashtable[key];
73 this.removeNode(node);
74 this.insertNodeToHead(node);
75 };
76 /**
77 * insert new node to the head and add it in the hashtable
78 *
79 * @param key - the key of the node
80 */
81 CacheList.prototype.insertItem = function (key) {
82 var node = new DoubleLinkedNode(key);
83 this.hashtable[key] = node;
84 this.insertNodeToHead(node);
85 };
86 /**
87 * @return the LAST Recently Visited key
88 */
89 CacheList.prototype.getLastItem = function () {
90 return this.tail.prevNode.key;
91 };
92 /**
93 * remove the cache key from the list and hashtable
94 * @param key - the key of the node
95 */
96 CacheList.prototype.removeItem = function (key) {
97 var removedItem = this.hashtable[key];
98 this.removeNode(removedItem);
99 delete this.hashtable[key];
100 };
101 /**
102 * @return length of the list
103 */
104 CacheList.prototype.getSize = function () {
105 return this.length;
106 };
107 /**
108 * @return true if the key is in the hashtable
109 * @param key
110 */
111 CacheList.prototype.containsKey = function (key) {
112 return key in this.hashtable;
113 };
114 /**
115 * clean up the list and hashtable
116 */
117 CacheList.prototype.clearList = function () {
118 var e_1, _a;
119 try {
120 for (var _b = __values(Object.keys(this.hashtable)), _c = _b.next(); !_c.done; _c = _b.next()) {
121 var key = _c.value;
122 if (this.hashtable.hasOwnProperty(key)) {
123 delete this.hashtable[key];
124 }
125 }
126 }
127 catch (e_1_1) { e_1 = { error: e_1_1 }; }
128 finally {
129 try {
130 if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
131 }
132 finally { if (e_1) throw e_1.error; }
133 }
134 this.head.nextNode = this.tail;
135 this.tail.prevNode = this.head;
136 this.length = 0;
137 };
138 /**
139 * @return all keys in the hashtable
140 */
141 CacheList.prototype.getKeys = function () {
142 return Object.keys(this.hashtable);
143 };
144 /**
145 * mainly for test
146 *
147 * @param key
148 * @return true if key is the head node
149 */
150 CacheList.prototype.isHeadNode = function (key) {
151 var node = this.hashtable[key];
152 return node.prevNode === this.head;
153 };
154 /**
155 * mainly for test
156 *
157 * @param key
158 * @return true if key is the tail node
159 */
160 CacheList.prototype.isTailNode = function (key) {
161 var node = this.hashtable[key];
162 return node.nextNode === this.tail;
163 };
164 return CacheList;
165}());
166export default CacheList;
167//# sourceMappingURL=CacheList.js.map
\No newline at end of file