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

shz.core.st.triest.ConcurrentJTrieST Maven / Gradle / Ivy

There is a newer version: 2024.0.2
Show newest version
package shz.core.st.triest;

import shz.core.queue.a.JArrayQueue;
import shz.core.queue.a.LArrayQueue;
import shz.core.stack.l.LLinkedStack;

import java.util.function.LongPredicate;
import java.util.function.ToLongFunction;

/**
 * 值为long类型的基于单词查找树的符号表
 * 

* 8+24+2*r(r为chars数组长度)=chars * 8+(56+8*r)*n(n为元素个数)+8*r*n*w(w为键的平均长度) *

* B=56*(n+1)+8*r*n*(w+1)+(2*r+对齐填充) */ public class ConcurrentJTrieST extends ConcurrentTrieST { private static final long serialVersionUID = 2905502017351689723L; /** * 8+25+8*r(r为数组长度)+对齐填充=40+8*r *

* B=56+8*r */ protected static final class Node extends ConcurrentTrieST.Node { private static final long serialVersionUID = 6876618105900045091L; public long val; public Node(int r) { super(new Node[r]); } } public ConcurrentJTrieST(char[] chars) { super(chars); root = new Node(chars.length); } public static ConcurrentJTrieST of(char[] chars) { return new ConcurrentJTrieST(chars); } public final void put(char[] a, long val) { acceptWrite(() -> { Node x = root; for (char c : a) { int i = charIndex.idx(c); if (x.next[i] == null) x.next[i] = new Node(len); x = x.next[i]; } x.val = val; x.leaf = true; }); } public final Long get(char[] a, int d) { return applyRead(() -> { Node x = get(root, a, d); return x == null || !x.leaf ? null : x.val; }); } public final Long get(char[] a) { return get(a, a.length); } public final JArrayQueue getAll() { return applyRead(() -> get(root)); } protected final JArrayQueue get(Node x) { JArrayQueue queue = JArrayQueue.of(); if (x != null) { LLinkedStack stack = LLinkedStack.of(); push(stack, x); while (!stack.isEmpty()) { Node pop = stack.pop(); if (pop.leaf) queue.offer(pop.val); push(stack, pop); } } return queue; } private void push(LLinkedStack stack, Node x) { if (x.next == null) return; for (int i = 0; i < len; ++i) if (x.next[i] != null) stack.push(x.next[i]); } public final JArrayQueue getByPrefix(char[] prefix) { return applyRead(() -> get(get(root, prefix, prefix.length))); } public final LArrayQueue getChars(LongPredicate predicate, int limit) { return applyRead(() -> getChars0(x -> predicate == null || predicate.test(x.val), limit)); } public final long computeIfAbsent(char[] a, ToLongFunction func) { Long oldVal = get(a); if (oldVal != null) return oldVal; return applyWrite(() -> { Node x = get(root, a, a.length); if (x != null && x.leaf) return x.val; long val = func.applyAsLong(a); put(a, val); return val; }); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy