You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current implementation of the Trie class supports insertion and deletion of values (.insert(key, value) and .delete(key)), but it lacks a straightforward method for retrieving a value by its key. Adding a .get(key) function would improve the usability of the library by providing an intuitive way to query the trie.
Proposed Implementation:
Implement a .get(key) method in the Trie class that:
Takes a key as input (either a Buffer or string).
Navigates through the trie structure to locate the key.
Returns the corresponding value if the key exists in the trie, or undefined if the key is not found.
/** * Retrieves the value at the given key from the Trie. * * @param {Buffer|string} key * The key to search for. Strings are treated as UTF-8 byte buffers. * @returns {Promise<Buffer|undefined>} * The value at the specified key, or `undefined` if the key is not found. */asyncget(key){// Convert the key into a path of nibblesconstpath=intoPath(key);// Use childAt to find the node corresponding to the pathconstnode=awaitthis.childAt(path);// If the node is a Leaf and the key matches, return the valueif(nodeinstanceofLeaf&&node.key.equals(Buffer.from(key))){returnnode.value;}// Return undefined if no matching node is foundreturnundefined;}
This addition will align with the existing .insert(key, value) and .delete(key) functions, making the API more complete and user-friendly.
Use Case Example:
consttrie=awaitTrie.fromList([{key: 'foo',value: 'bar'},{key: 'baz',value: 'qux'}]);// Retrieve the value at key 'foo'constvalue=awaittrie.get('foo');console.log(value);// Expected output: 'bar'
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Description:
The current implementation of the
Trie
class supports insertion and deletion of values (.insert(key, value)
and.delete(key)
), but it lacks a straightforward method for retrieving a value by its key. Adding a.get(key)
function would improve the usability of the library by providing an intuitive way to query the trie.Proposed Implementation:
.get(key)
method in theTrie
class that:key
as input (either aBuffer
orstring
).undefined
if the key is not found.This addition will align with the existing
.insert(key, value)
and.delete(key)
functions, making the API more complete and user-friendly.Use Case Example:
Beta Was this translation helpful? Give feedback.
All reactions