All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.oracle.truffle.object.UnorderedTrieMap Maven / Gradle / Ivy

Go to download

Truffle is a multi-language framework for executing dynamic languages that achieves high performance when combined with Graal.

There is a newer version: 24.2.0
Show newest version
/*
 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * The Universal Permissive License (UPL), Version 1.0
 *
 * Subject to the condition set forth below, permission is hereby granted to any
 * person obtaining a copy of this software, associated documentation and/or
 * data (collectively the "Software"), free of charge and under any and all
 * copyright rights in the Software, and any and all patent rights owned or
 * freely licensable by each licensor hereunder covering either (i) the
 * unmodified Software as contributed to or provided by such licensor, or (ii)
 * the Larger Works (as defined below), to deal in both
 *
 * (a) the Software, and
 *
 * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
 * one is included with the Software each a "Larger Work" to which the Software
 * is contributed by such licensors),
 *
 * without restriction, including without limitation the rights to copy, create
 * derivative works of, display, perform, and distribute the Software and make,
 * use, sell, offer for sale, import, export, have made, and have sold the
 * Software and the Larger Work(s), and to sublicense the foregoing rights on
 * either these or other terms.
 *
 * This license is subject to the following condition:
 *
 * The above copyright notice and either this complete permission notice or at a
 * minimum a reference to the UPL must be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.oracle.truffle.object;

import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;

/**
 * An unordered immutable hash map based on an array mapped trie.
 *
 * Keys and values must not be null. Entries are stored as {@link Map.Entry}.
 */
final class UnorderedTrieMap implements ImmutableMap {

    private static final UnorderedTrieMap EMPTY = new UnorderedTrieMap<>(0, TrieNode.empty());

    /* Enables stricter assertions for debugging. */
    private static final boolean VERIFY = false;

    private final int size;
    private final TrieNode> root;

    static int hash(Object key) {
        return key.hashCode();
    }

    private UnorderedTrieMap(int size, TrieNode> root) {
        this.size = size;
        this.root = root;
        assert verify();
    }

    @SuppressWarnings("unchecked")
    public static  UnorderedTrieMap empty() {
        return (UnorderedTrieMap) EMPTY;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size() == 0;
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean containsKey(Object key) {
        return getEntry((K) key) != null;
    }

    @Override
    public boolean containsValue(Object value) {
        for (var entry : entrySet()) {
            if (Objects.equals(value, entry.getValue())) {
                return true;
            }
        }
        return false;
    }

    @SuppressWarnings("unchecked")
    @Override
    public V get(Object key) {
        var entry = getEntry((K) key);
        return entry == null ? null : entry.getValue();
    }

    Map.Entry getEntry(K key) {
        var entry = root.find(key, hash(key));
        assert entry == null || entry.getKey().equals(key) : Arrays.asList(entry, key);
        return entry;
    }

    @Override
    public UnorderedTrieMap copyAndPut(K key, V value) {
        return copyAndPutImpl(key, value, Map::entry, false);
    }

    UnorderedTrieMap copyAndPut(K key, V value, BiFunction> entryFactory) {
        return copyAndPutImpl(key, value, entryFactory, false);
    }

    UnorderedTrieMap copyAndPutIfAbsent(K key, V value, BiFunction> entryFactory) {
        return copyAndPutImpl(key, value, entryFactory, true);
    }

    private UnorderedTrieMap copyAndPutImpl(K key, V value, BiFunction> entryFactory, boolean ifAbsent) {
        int hash = hash(key);
        Map.Entry existing = root.find(key, hash);
        var newRoot = root;
        final int newSize;
        final Map.Entry newEntry;
        if (existing == null) {
            newSize = size + 1;
            // inserting a new entry
            newEntry = newEntry(key, value, entryFactory);
        } else {
            V existingValue = existing.getValue();
            // null values are treated as absent (handles weakly referenced values)
            if (existingValue != null && (ifAbsent || existingValue.equals(value))) {
                return this;
            } else {
                // replacing an existing entry
                newSize = size;
                newEntry = newEntry(key, value, entryFactory);
                assert !newEntry.equals(existing) : Arrays.asList(newEntry, existing);
            }
        }
        newRoot = newRoot.put(key, hash, newEntry);
        return new UnorderedTrieMap<>(newSize, newRoot);
    }

    private Map.Entry newEntry(K key, V newValue, BiFunction> entryFactory) {
        return entryFactory.apply(key, newValue);
    }

    @Override
    public UnorderedTrieMap copyAndRemove(K key) {
        int hash = hash(key);
        var existing = root.find(key, hash);
        if (existing == null) {
            return this;
        } else {
            if (size == 1) {
                return empty();
            }
            var newRoot = root;
            newRoot = newRoot.remove(key, hash);
            return new UnorderedTrieMap<>(size - 1, newRoot);
        }
    }

    UnorderedTrieMap copyAndRemoveEntry(Map.Entry entry) {
        K key = entry.getKey();
        int hash = hash(key);
        var existing = root.find(key, hash);
        if (existing != entry) {
            return this;
        } else {
            if (size == 1) {
                return empty();
            }
            var newRoot = root;
            newRoot = newRoot.remove(key, hash);
            return new UnorderedTrieMap<>(size - 1, newRoot);
        }
    }

    @Override
    public void forEach(BiConsumer action) {
        forEachEntry(new Consumer>() {
            @Override
            public void accept(Entry e) {
                action.accept(e.getKey(), e.getValue());
            }
        });
    }

    void forEachEntry(Consumer> consumer) {
        root.forEachEntry(consumer);
    }

    Iterator> entryIterator() {
        return root.entryIterator();
    }

    Iterator keyIterator() {
        return new Iterator<>() {
            private final Iterator> entryIterator = entryIterator();

            @Override
            public boolean hasNext() {
                return entryIterator.hasNext();
            }

            @Override
            public K next() {
                return entryIterator.next().getKey();
            }
        };
    }

    Iterator valueIterator() {
        return new Iterator<>() {
            private final Iterator> entryIterator = entryIterator();

            @Override
            public boolean hasNext() {
                return entryIterator.hasNext();
            }

            @Override
            public V next() {
                return entryIterator.next().getValue();
            }
        };
    }

    @Override
    public Set> entrySet() {
        return new AbstractSet<>() {
            @Override
            public Iterator> iterator() {
                return UnorderedTrieMap.this.entryIterator();
            }

            @Override
            public int size() {
                return UnorderedTrieMap.this.size();
            }
        };
    }

    @Override
    public Set keySet() {
        return new AbstractSet<>() {
            @Override
            public Iterator iterator() {
                return UnorderedTrieMap.this.keyIterator();
            }

            @Override
            public int size() {
                return UnorderedTrieMap.this.size();
            }
        };
    }

    @Override
    public Collection values() {
        return new AbstractSet<>() {
            @Override
            public Iterator iterator() {
                return UnorderedTrieMap.this.valueIterator();
            }

            @Override
            public int size() {
                return UnorderedTrieMap.this.size();
            }
        };
    }

    @Override
    public String toString() {
        return entrySet().toString();
    }

    private boolean verify() {
        assert size >= 0;
        assert root != null;
        if (VERIFY) {
            assert root.count() == size : root.count() + " != " + size;
            assert root.verify(0);
            int count = 0;
            for (var iterator = entrySet().iterator(); iterator.hasNext();) {
                var e = iterator.next();
                assert e == getEntry(e.getKey());
                count++;
            }
            assert count == size : count + " != " + size;
        }
        return true;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy