UNPKG

5.82 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
6 * the License. A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0/
9 *
10 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
12 * and limitations under the License.
13 */
14var __values = (this && this.__values) || function(o) {
15 var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
16 if (m) return m.call(o);
17 if (o && typeof o.length === "number") return {
18 next: function () {
19 if (o && i >= o.length) o = void 0;
20 return { value: o && o[i++], done: !o };
21 }
22 };
23 throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
24};
25Object.defineProperty(exports, "__esModule", { value: true });
26var DoubleLinkedNode = /** @class */ (function () {
27 function DoubleLinkedNode(keyVal) {
28 this.key = keyVal ? keyVal : '';
29 this.prevNode = null;
30 this.nextNode = null;
31 }
32 return DoubleLinkedNode;
33}());
34/**
35 * double linked list plus a hash table inside
36 * each key in the cache stored as a node in the list
37 * recently visited node will be rotated to the head
38 * so the Last Recently Visited node will be at the tail
39 *
40 * @member head - dummy head of the linked list
41 * @member tail - dummy tail of the linked list
42 * @member hashtable - the hashtable which maps cache key to list node
43 * @member length - length of the list
44 */
45var CacheList = /** @class */ (function () {
46 /**
47 * initialization
48 */
49 function CacheList() {
50 this.head = new DoubleLinkedNode();
51 this.tail = new DoubleLinkedNode();
52 this.hashtable = {};
53 this.length = 0;
54 this.head.nextNode = this.tail;
55 this.tail.prevNode = this.head;
56 }
57 /**
58 * insert node to the head of the list
59 *
60 * @param node
61 */
62 CacheList.prototype.insertNodeToHead = function (node) {
63 var tmp = this.head.nextNode;
64 this.head.nextNode = node;
65 node.nextNode = tmp;
66 node.prevNode = this.head;
67 tmp.prevNode = node;
68 this.length = this.length + 1;
69 };
70 /**
71 * remove node
72 *
73 * @param node
74 */
75 CacheList.prototype.removeNode = function (node) {
76 node.prevNode.nextNode = node.nextNode;
77 node.nextNode.prevNode = node.prevNode;
78 node.prevNode = null;
79 node.nextNode = null;
80 this.length = this.length - 1;
81 };
82 /**
83 * @return true if list is empty
84 */
85 CacheList.prototype.isEmpty = function () {
86 return this.length === 0;
87 };
88 /**
89 * refresh node so it is rotated to the head
90 *
91 * @param key - key of the node
92 */
93 CacheList.prototype.refresh = function (key) {
94 var node = this.hashtable[key];
95 this.removeNode(node);
96 this.insertNodeToHead(node);
97 };
98 /**
99 * insert new node to the head and add it in the hashtable
100 *
101 * @param key - the key of the node
102 */
103 CacheList.prototype.insertItem = function (key) {
104 var node = new DoubleLinkedNode(key);
105 this.hashtable[key] = node;
106 this.insertNodeToHead(node);
107 };
108 /**
109 * @return the LAST Recently Visited key
110 */
111 CacheList.prototype.getLastItem = function () {
112 return this.tail.prevNode.key;
113 };
114 /**
115 * remove the cache key from the list and hashtable
116 * @param key - the key of the node
117 */
118 CacheList.prototype.removeItem = function (key) {
119 var removedItem = this.hashtable[key];
120 this.removeNode(removedItem);
121 delete this.hashtable[key];
122 };
123 /**
124 * @return length of the list
125 */
126 CacheList.prototype.getSize = function () {
127 return this.length;
128 };
129 /**
130 * @return true if the key is in the hashtable
131 * @param key
132 */
133 CacheList.prototype.containsKey = function (key) {
134 return key in this.hashtable;
135 };
136 /**
137 * clean up the list and hashtable
138 */
139 CacheList.prototype.clearList = function () {
140 var e_1, _a;
141 try {
142 for (var _b = __values(Object.keys(this.hashtable)), _c = _b.next(); !_c.done; _c = _b.next()) {
143 var key = _c.value;
144 if (this.hashtable.hasOwnProperty(key)) {
145 delete this.hashtable[key];
146 }
147 }
148 }
149 catch (e_1_1) { e_1 = { error: e_1_1 }; }
150 finally {
151 try {
152 if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
153 }
154 finally { if (e_1) throw e_1.error; }
155 }
156 this.head.nextNode = this.tail;
157 this.tail.prevNode = this.head;
158 this.length = 0;
159 };
160 /**
161 * @return all keys in the hashtable
162 */
163 CacheList.prototype.getKeys = function () {
164 return Object.keys(this.hashtable);
165 };
166 /**
167 * mainly for test
168 *
169 * @param key
170 * @return true if key is the head node
171 */
172 CacheList.prototype.isHeadNode = function (key) {
173 var node = this.hashtable[key];
174 return node.prevNode === this.head;
175 };
176 /**
177 * mainly for test
178 *
179 * @param key
180 * @return true if key is the tail node
181 */
182 CacheList.prototype.isTailNode = function (key) {
183 var node = this.hashtable[key];
184 return node.nextNode === this.tail;
185 };
186 return CacheList;
187}());
188exports.default = CacheList;
189//# sourceMappingURL=CacheList.js.map
\No newline at end of file