Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MutableTrieMap.computeIfAbsent() #147

Open
rovarga opened this issue Apr 13, 2023 · 2 comments
Open

Implement MutableTrieMap.computeIfAbsent() #147

rovarga opened this issue Apr 13, 2023 · 2 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@rovarga
Copy link
Collaborator

rovarga commented Apr 13, 2023

We currently rely on default implementation from ConcurrentMap. That implementation defers to get() and then to putIfAbsent() -- which means that we end up traversing the tree twice, whereas given the key we know the insertion point.

We should be doing better than that, provided we can plug into our lookup machinery that is :)

The idea is that the get() operation is inevitably finding an existing entry or at least something close to the insertion point. Once we have established that point and the mapping does not exist, we should consult the mapping function. If it produces a non-null value, we should perform an insert operation based on the context.

Implementation-wise this is mostly about INode and its methods: it feels like an extension of recInsertIf().

@rovarga rovarga added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Apr 13, 2023
@rovarga
Copy link
Collaborator Author

rovarga commented Apr 13, 2023

Note this has some overlap with #148, hence we need to think about those methods when modifying INode. Both issues seem to have an underlying mechanic in mind, which we need to analyze in terms of meta-meaning and provide a reasonable set of INode methods so we do not end up copy&pasting a ton of code.

@007Harshvardhan
Copy link

public V computeIfAbsent(String key, Function<? super String, ? extends V> mappingFunction) {

Objects.requireNonNull(mappingFunction);

TrieNode<V> node = root;

// Traverse the trie to find the node for the key
for (char c : key.toCharArray()) {
    node = node.getChildren().get(c);
    if (node == null) {
        break;
    }
}

V value = node != null ? node.getValue() : null;

if (value == null) {
    // Compute the value using the mapping function
    value = mappingFunction.apply(key);

    if (value == null) {
        return null;
    }

    // Add the key-value pair to the trie
    node = root;
    for (char c : key.toCharArray()) {
        node = node.getChildren().computeIfAbsent(c, k -> new TrieNode<>());
    }
    node.setValue(value);
}

return value;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants