opusdev/vector-similarity-api
1
1'use strict';2 3var inspect = require('object-inspect');4 5var $TypeError = require('es-errors/type');6 7/*8* This function traverses the list returning the node corresponding to the given key.9*10* That node is also moved to the head of the list, so that if it's accessed again we don't need to traverse the whole list.11* By doing so, all the recently used nodes can be accessed relatively quickly.12*/13/** @type {import('./list.d.ts').listGetNode} */14// eslint-disable-next-line consistent-return15var listGetNode = function (list, key, isDelete) {16 /** @type {typeof list | NonNullable<(typeof list)['next']>} */17 var prev = list;18 /** @type {(typeof list)['next']} */19 var curr;20 // eslint-disable-next-line eqeqeq21 for (; (curr = prev.next) != null; prev = curr) {22 if (curr.key === key) {23 prev.next = curr.next;24 if (!isDelete) {25 // eslint-disable-next-line no-extra-parens26 curr.next = /** @type {NonNullable<typeof list.next>} */ (list.next);27 list.next = curr; // eslint-disable-line no-param-reassign28 }29 return curr;30 }31 }32};33 34/** @type {import('./list.d.ts').listGet} */35var listGet = function (objects, key) {36 if (!objects) {37 return void undefined;38 }39 var node = listGetNode(objects, key);40 return node && node.value;41};42/** @type {import('./list.d.ts').listSet} */43var listSet = function (objects, key, value) {44 var node = listGetNode(objects, key);45 if (node) {46 node.value = value;47 } else {48 // Prepend the new node to the beginning of the list49 objects.next = /** @type {import('./list.d.ts').ListNode<typeof value, typeof key>} */ ({ // eslint-disable-line no-param-reassign, no-extra-parens50 key: key,51 next: objects.next,52 value: value53 });54 }55};56/** @type {import('./list.d.ts').listHas} */57var listHas = function (objects, key) {58 if (!objects) {59 return false;60 }61 return !!listGetNode(objects, key);62};63/** @type {import('./list.d.ts').listDelete} */64// eslint-disable-next-line consistent-return65var listDelete = function (objects, key) {66 if (objects) {67 return listGetNode(objects, key, true);68 }69};70 71/** @type {import('.')} */72module.exports = function getSideChannelList() {73 /** @typedef {ReturnType<typeof getSideChannelList>} Channel */74 /** @typedef {Parameters<Channel['get']>[0]} K */75 /** @typedef {Parameters<Channel['set']>[1]} V */76 77 /** @type {import('./list.d.ts').RootNode<V, K> | undefined} */ var $o;78 79 /** @type {Channel} */80 var channel = {81 assert: function (key) {82 if (!channel.has(key)) {83 throw new $TypeError('Side channel does not contain ' + inspect(key));84 }85 },86 'delete': function (key) {87 var root = $o && $o.next;88 var deletedNode = listDelete($o, key);89 if (deletedNode && root && root === deletedNode) {90 $o = void undefined;91 }92 return !!deletedNode;93 },94 get: function (key) {95 return listGet($o, key);96 },97 has: function (key) {98 return listHas($o, key);99 },100 set: function (key, value) {101 if (!$o) {102 // Initialize the linked list as an empty node, so that we don't have to special-case handling of the first node: we can always refer to it as (previous node).next, instead of something like (list).head103 $o = {104 next: void undefined105 };106 }107 // eslint-disable-next-line no-extra-parens108 listSet(/** @type {NonNullable<typeof $o>} */ ($o), key, value);109 }110 };111 // @ts-expect-error TODO: figure out why this is erroring112 return channel;113};114 