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

vlsi.utils.CompactHashMapClassEmptyDefaults Maven / Gradle / Ivy

There is a newer version: 0.40.13
Show newest version
/*
 * Copyright (c) 2011
 *
 * This file is part of CompactMap
 *
 * CompactMap is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CompactMap is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with CompactMap.  If not, see .
 */

package vlsi.utils;

import com.github.andrewoma.dexx.collection.Pair;

import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;

/**
 * This map represents CompactHashMapClass that has no default values (it can have nonempty key2slot).
 * It is used to determine the right CompactHashMapClass given the desired defaultValues map.
 *
 * @author Vladimir Sitnikov
 * @param  the type of keys maintained by this map
 * @param  the type of mapped values
 */
class CompactHashMapClassEmptyDefaults extends CompactHashMapClass {
    private Map> key2newKlass;
    private Map, CompactHashMapClass> defValues2Klass;

    public CompactHashMapClassEmptyDefaults(com.github.andrewoma.dexx.collection.Map key2Slot) {
        super(key2Slot);
    }

    @Override
    protected CompactHashMapClassEmptyDefaults getMapWithEmptyDefaults() {
        return this;
    }

    protected CompactHashMapClass getNewDefaultClass(Map newDef) {
        CompactHashMapClass newClass;
        if (newDef == null || newDef.isEmpty())
            return this;
        synchronized (this) {
            Map, CompactHashMapClass> defValues2Klass = this.defValues2Klass;
            if (defValues2Klass == null)
                this.defValues2Klass = defValues2Klass =
                        new IdentityHashMap, CompactHashMapClass>();

            newClass = defValues2Klass.get(newDef);
            if (newClass == null) {
                newClass = new CompactHashMapClassWithDefaults(key2slot, newDef, this);
                defValues2Klass.put(newDef, newClass);
            }
        }
        return newClass;
    }

    protected CompactHashMapClass getNextKlass(K key, Map defaultValues) {
        CompactHashMapClassEmptyDefaults newKlass = null;
        synchronized (this) {
            Map> key2newKlass = this.key2newKlass;
            if (key2newKlass != null)
                newKlass = key2newKlass.get(key);
        }

        if (defaultValues.containsKey(key))
            defaultValues = CompactHashMapDefaultValues.getNewDefaultValues(defaultValues, key, REMOVED_OBJECT);

        if (newKlass != null)
            return newKlass.getNewDefaultClass(defaultValues);

        int size = key2slot.size();
        com.github.andrewoma.dexx.collection.Map newKey2slot = key2slot;

        if (size < 3) size -= 3;
        else if (size == 3) {
            size = 1;
            for (Pair entry : key2slot)
                if (entry.component2() == -1) {
                    newKey2slot = newKey2slot.put(entry.component1(), 0);
                    break;
                }
        } else size -= 2;
        newKey2slot = newKey2slot.put(key, size);

        newKlass = new CompactHashMapClassEmptyDefaults(newKey2slot);
        synchronized (this) {
            if (key2newKlass == null) {
                key2newKlass = Collections.singletonMap(key, newKlass);
            } else {
                final CompactHashMapClassEmptyDefaults anotherNewKlass = key2newKlass.get(key);

                if (anotherNewKlass != null)
                    newKlass = anotherNewKlass;
                else {
                    if (key2newKlass.size() == 1) {
                        key2newKlass = new HashMap>(key2newKlass);
                    }
                    key2newKlass.put(key, newKlass);
                }
            }
        }

        return newKlass.getNewDefaultClass(defaultValues);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy