tech.pantheon.triemap.LNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of triemap Show documentation
Show all versions of triemap Show documentation
Java implementation of a concurrent trie hash map from Scala collections library
/*
* (C) Copyright 2016 Pantheon Technologies, s.r.o. and others.
*
* 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 tech.pantheon.triemap;
final class LNode extends MainNode {
// Internally-linked single list of of entries
private final LNodeEntries entries;
private final int size;
private LNode(final LNodeEntries entries, final int size) {
this.entries = entries;
this.size = size;
}
LNode(final K k1, final V v1, final K k2, final V v2) {
this(LNodeEntries.map(k1, v1, k2, v2), 2);
}
LNode insertChild(final K key, final V value) {
return new LNode<>(entries.insert(key, value), size + 1);
}
MainNode removeChild(final LNodeEntry entry, final int hc) {
// While remove() can return null, that case will never happen here, as we are starting off with two entries
// so we cannot observe a null return here.
final LNodeEntries map = VerifyException.throwIfNull(entries.remove(entry));
// If the returned LNode would have only one element, we turn it into a TNode, hence above null return from
// remove() can never happen.
if (size == 2) {
// create it tombed so that it gets compressed on subsequent accesses
return new TNode<>(map.getKey(), map.getValue(), hc);
}
return new LNode<>(map, size - 1);
}
MainNode replaceChild(final LNodeEntry entry, final V value) {
return new LNode<>(entries.replace(entry, value), size);
}
LNodeEntry get(final Equivalence super K> equiv, final K key) {
return entries.findEntry(equiv, key);
}
LNodeEntries entries() {
return entries;
}
@Override
int trySize() {
return size;
}
@Override
int size(final ImmutableTrieMap, ?> ct) {
return size;
}
}