BUG-7464: Optimize LNode operations
[yangtools.git] / third-party / triemap / src / main / java / org / opendaylight / yangtools / triemap / LNode.java
index 0fec28954ab440fe46308cbfe7f9e85288b03538..0bc4c925258a065001353aaa09bcc9c3380cf165 100644 (file)
@@ -20,35 +20,40 @@ import java.util.Map.Entry;
 import java.util.Optional;
 
 final class LNode<K, V> extends MainNode<K, V> {
-    private final ListMap<K, V> listmap;
+    private final LNodeEntries<K, V> listmap;
 
-    private LNode(final ListMap<K, V> listmap) {
+    private LNode(final LNodeEntries<K, V> listmap) {
         this.listmap = listmap;
     }
 
     LNode(final K k1, final V v1, final K k2, final V v2) {
-        this(ListMap.map(k1, v1, k2, v2));
+        this(LNodeEntries.map(k1, v1, k2, v2));
     }
 
-    LNode<K, V> inserted(final K k, final V v) {
-        return new LNode<>(listmap.add(k, v));
+    LNode<K, V> insertChild( final K k, final V v) {
+        return new LNode<>(listmap.insert(k, v));
     }
 
-    MainNode<K, V> removed(final K k, final TrieMap<K, V> ct) {
+    MainNode<K, V> removeChild(final LNodeEntry<K, V> entry, final int hc) {
         // We only ever create ListMaps with two or more entries,  and remove them as soon as they reach one element
         // (below), so we cannot observe a null return here.
-        final ListMap<K, V> updmap = listmap.remove(k);
-        if (updmap.size() > 1) {
-            return new LNode<>(updmap);
+        final LNodeEntries<K, V> map = listmap.remove(entry);
+        final Optional<Entry<K, V>> maybeKv = map.maybeSingleton();
+        if (maybeKv.isPresent()) {
+            final Entry<K, V> kv = maybeKv.get();
+            // create it tombed so that it gets compressed on subsequent accesses
+            return new TNode<>(kv.getKey(), kv.getValue(), hc);
         }
 
-        final Entry<K, V> kv = updmap.iterator().next();
-        // create it tombed so that it gets compressed on subsequent accesses
-        return new TNode<>(kv.getKey(), kv.getValue(), ct.computeHash(kv.getKey()));
+        return new LNode<>(map);
     }
 
-    Optional<V> get(final K k) {
-        return listmap.get(k);
+    MainNode<K, V> replaceChild(final LNodeEntry<K, V> entry, final V v) {
+        return new LNode<>(listmap.replace(entry, v));
+    }
+
+    LNodeEntry<K, V> get(final Equivalence<? super K> equiv, final K k) {
+        return listmap.findEntry(equiv, k);
     }
 
     @Override
@@ -65,4 +70,6 @@ final class LNode<K, V> extends MainNode<K, V> {
     Iterator<Entry<K, V>> iterator() {
         return listmap.iterator();
     }
+
+
 }
\ No newline at end of file