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

shz.core.st.tst.ConcurrentSTST Maven / Gradle / Ivy

There is a newer version: 10.3.1
Show newest version
package shz.core.st.tst;

import shz.core.queue.a.SArrayQueue;
import shz.core.stack.l.LLinkedStack;

import java.util.Collections;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * 值为short类型的TST
 * 

* 8+48*n(n为元素个数) *

* B=24+48*n */ public class ConcurrentSTST extends ConcurrentTST { /** * 2+27+对齐填充=32 *

* B=48 */ protected static final class Node extends ConcurrentTST.Node { public short val; public Node(char c) { super(c); } } protected ConcurrentSTST() { } public static ConcurrentSTST of() { return new ConcurrentSTST(); } public final void put(char[] a, short val) { acceptWrite(() -> root = put(root, a, val, 0)); } protected final Node put(Node x, char[] a, short val, int d) { if (x == null) x = new Node(a[d]); if (a[d] < x.c) x.left = put(x.left, a, val, d); else if (a[d] > x.c) x.right = put(x.right, a, val, d); else if (d < a.length - 1) x.mid = put(x.mid, a, val, d + 1); else { x.val = val; x.leaf = true; } return x; } public final Short get(char[] a) { return applyRead(() -> { Node x = get(root, a, 0); return x == null || !x.leaf ? null : x.val; }); } public final Iterable getAll() { return applyRead(() -> get(root, false)); } protected final Iterable get(Node x, boolean prefix) { if (x == null) return Collections.emptyList(); SArrayQueue queue = SArrayQueue.of(); LLinkedStack stack = LLinkedStack.of(); if (x.mid != null) stack.push(x.mid); if (!prefix) { if (x.left != null) stack.push(x.left); if (x.right != null) stack.push(x.right); } while (stack.size() > 0) { Node pop = stack.pop(); if (pop.leaf) queue.offer(pop.val); push(stack, pop); } return queue.isEmpty() ? Collections.emptyList() : queue; } private void push(LLinkedStack stack, Node x) { if (x.left != null) stack.push(x.left); if (x.mid != null) stack.push(x.mid); if (x.right != null) stack.push(x.right); } public final Iterable getByPrefix(char[] prefix) { return applyRead(() -> get(get(root, prefix, 0), true)); } public final Iterable getChars(Predicate predicate, int limit) { return applyRead(() -> getChars0(x -> predicate == null || predicate.test(x.val), limit)); } public final Short computeIfAbsent(char[] a, Function func) { Short oldVal = get(a); if (oldVal != null) return oldVal; return applyWrite(() -> { Node x = get(root, a, 0); if (x != null && x.leaf) return x.val; Short val = func.apply(a); root = put(root, a, val, 0); return val; }); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy