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

kotlin.reflect.jvm.internal.pcollections.HashPMap Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
/*
 * Copyright 2010-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package kotlin.reflect.jvm.internal.pcollections;

import org.jetbrains.annotations.NotNull;

/**
 * A persistent map from non-null keys to non-null values.
 * @suppress
 */
public final class HashPMap {
    private static final HashPMap EMPTY = new HashPMap(IntTreePMap.>>empty(), 0);

    @SuppressWarnings("unchecked")
    @NotNull
    public static  HashPMap empty() {
        return (HashPMap) HashPMap.EMPTY;
    }

    private final IntTreePMap>> intMap;
    private final int size;

    private HashPMap(IntTreePMap>> intMap, int size) {
        this.intMap = intMap;
        this.size = size;
    }

    public int size() {
        return size;
    }

    public boolean containsKey(Object key) {
        return keyIndexIn(getEntries(key.hashCode()), key) != -1;
    }

    public V get(Object key) {
        ConsPStack> entries = getEntries(key.hashCode());
        for (MapEntry entry : entries)
            if (entry.key.equals(key))
                return entry.value;
        return null;
    }

    @NotNull
    public HashPMap plus(K key, V value) {
        ConsPStack> entries = getEntries(key.hashCode());
        int size0 = entries.size();
        int i = keyIndexIn(entries, key);
        if (i != -1) entries = entries.minus(i);
        entries = entries.plus(new MapEntry(key, value));
        return new HashPMap(intMap.plus(key.hashCode(), entries), size - size0 + entries.size());
    }

    @NotNull
    public HashPMap minus(Object key) {
        ConsPStack> entries = getEntries(key.hashCode());
        int i = keyIndexIn(entries, key);
        if (i == -1) // key not in this
            return this;
        entries = entries.minus(i);
        if (entries.size() == 0) // get rid of the entire hash entry
            return new HashPMap(intMap.minus(key.hashCode()), size - 1);
        // otherwise replace hash entry with new smaller one:
        return new HashPMap(intMap.plus(key.hashCode(), entries), size - 1);
    }

    private ConsPStack> getEntries(int hash) {
        ConsPStack> entries = intMap.get(hash);
        if (entries == null) return ConsPStack.empty();
        return entries;
    }

    private static  int keyIndexIn(ConsPStack> entries, Object key) {
        int i = 0;
        for (MapEntry entry : entries) {
            if (entry.key.equals(key))
                return i;
            i++;
        }
        return -1;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy