{"version":3,"file":"CacheList.mjs","sources":["../../../../src/Cache/utils/CacheList.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { CacheErrorCode, assert } from './errorHelpers';\nclass DoubleLinkedNode {\n    constructor(keyVal) {\n        this.key = keyVal || '';\n        this.prevNode = null;\n        this.nextNode = null;\n    }\n}\n/**\n * double linked list plus a hash table inside\n * each key in the cache stored as a node in the list\n * recently visited node will be rotated to the head\n * so the Last Recently Visited node will be at the tail\n *\n * @member head - dummy head of the linked list\n * @member tail - dummy tail of the linked list\n * @member hashtable - the hashtable which maps cache key to list node\n * @member length - length of the list\n */\nexport class CacheList {\n    /**\n     * initialization\n     */\n    constructor() {\n        this.head = new DoubleLinkedNode();\n        this.tail = new DoubleLinkedNode();\n        this.hashtable = {};\n        this.length = 0;\n        this.head.nextNode = this.tail;\n        this.tail.prevNode = this.head;\n    }\n    /**\n     * insert node to the head of the list\n     *\n     * @param node\n     */\n    insertNodeToHead(node) {\n        const tmp = this.head.nextNode;\n        this.head.nextNode = node;\n        node.nextNode = tmp;\n        node.prevNode = this.head;\n        assert(tmp !== null, CacheErrorCode.NullPreviousNode);\n        tmp.prevNode = node;\n        this.length = this.length + 1;\n    }\n    /**\n     * remove node\n     *\n     * @param node\n     */\n    removeNode(node) {\n        assert(node.prevNode !== null, CacheErrorCode.NullPreviousNode);\n        assert(node.nextNode !== null, CacheErrorCode.NullNextNode);\n        node.prevNode.nextNode = node.nextNode;\n        node.nextNode.prevNode = node.prevNode;\n        node.prevNode = null;\n        node.nextNode = null;\n        this.length = this.length - 1;\n    }\n    /**\n     * @return true if list is empty\n     */\n    isEmpty() {\n        return this.length === 0;\n    }\n    /**\n     * refresh node so it is rotated to the head\n     *\n     * @param key - key of the node\n     */\n    refresh(key) {\n        const node = this.hashtable[key];\n        this.removeNode(node);\n        this.insertNodeToHead(node);\n    }\n    /**\n     * insert new node to the head and add it in the hashtable\n     *\n     * @param key - the key of the node\n     */\n    insertItem(key) {\n        const node = new DoubleLinkedNode(key);\n        this.hashtable[key] = node;\n        this.insertNodeToHead(node);\n    }\n    /**\n     * @return the LAST Recently Visited key\n     */\n    getLastItem() {\n        assert(this.tail.prevNode !== null, CacheErrorCode.NullPreviousNode);\n        return this.tail.prevNode.key;\n    }\n    /**\n     * remove the cache key from the list and hashtable\n     * @param key - the key of the node\n     */\n    removeItem(key) {\n        const removedItem = this.hashtable[key];\n        this.removeNode(removedItem);\n        delete this.hashtable[key];\n    }\n    /**\n     * @return length of the list\n     */\n    getSize() {\n        return this.length;\n    }\n    /**\n     * @return true if the key is in the hashtable\n     * @param key\n     */\n    containsKey(key) {\n        return key in this.hashtable;\n    }\n    /**\n     * clean up the list and hashtable\n     */\n    clearList() {\n        for (const key of Object.keys(this.hashtable)) {\n            if (Object.prototype.hasOwnProperty.call(this.hashtable, key)) {\n                delete this.hashtable[key];\n            }\n        }\n        this.head.nextNode = this.tail;\n        this.tail.prevNode = this.head;\n        this.length = 0;\n    }\n    /**\n     * @return all keys in the hashtable\n     */\n    getKeys() {\n        return Object.keys(this.hashtable);\n    }\n    /**\n     * mainly for test\n     *\n     * @param key\n     * @return true if key is the head node\n     */\n    isHeadNode(key) {\n        const node = this.hashtable[key];\n        return node.prevNode === this.head;\n    }\n    /**\n     * mainly for test\n     *\n     * @param key\n     * @return true if key is the tail node\n     */\n    isTailNode(key) {\n        const node = this.hashtable[key];\n        return node.nextNode === this.tail;\n    }\n}\n"],"names":[],"mappings":";;AAAA;AACA;AAEA,MAAM,gBAAgB,CAAC;AACvB,IAAI,WAAW,CAAC,MAAM,EAAE;AACxB,QAAQ,IAAI,CAAC,GAAG,GAAG,MAAM,IAAI,EAAE,CAAC;AAChC,QAAQ,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;AAC7B,QAAQ,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;AAC7B,KAAK;AACL,CAAC;AACD;AACA;AACA;AACA;AACA;AAC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