This commit is contained in:
Looly 2023-03-21 00:14:47 +08:00
parent 1247a59b19
commit 9d350acfb3
2 changed files with 166 additions and 165 deletions

View File

@ -3,11 +3,11 @@ package cn.hutool.core.lang.page;
/**
* 导航分页信息类<br>
* 根据提供的总页数每页记录数导航页码数等信息生成导航数组
* <pre>
* <pre>{@code
* [1] 2 3 4 5 >>
* << 1 [2] 3 4 5 >>
* << 1 2 3 4 [5]
* </pre>
* }</pre>
*
* @author 莫取网名
*/

View File

@ -44,7 +44,7 @@ public class NFA {
*
* @param words 添加的新词
*/
public NFA(String... words) {
public NFA(final String... words) {
this();
this.insert(words);
}
@ -54,16 +54,15 @@ public class NFA {
*
* @param word 添加的新词
*/
public void insert(String word) {
public void insert(final String word) {
synchronized (insertTreeLock) {
needBuildAc = true;
Node p = root;
for (char curr : word.toCharArray()) {
int ind = curr;
if (p.next.get(ind) == null) {
p.next.put(ind, new Node());
for (final char curr : word.toCharArray()) {
if (p.next.get((int) curr) == null) {
p.next.put((int) curr, new Node());
}
p = p.next.get(ind);
p = p.next.get((int) curr);
}
p.flag = true;
p.str = word;
@ -75,8 +74,8 @@ public class NFA {
*
* @param words 添加的新词
*/
public void insert(String... words) {
for (String word : words) {
public void insert(final String... words) {
for (final String word : words) {
this.insert(word);
}
}
@ -85,15 +84,15 @@ public class NFA {
* 构建基于NFA模型的 AC自动机
*/
private void buildAc() {
Queue<Node> queue = new LinkedList<>();
Node p = root;
for (Integer key : p.next.keySet()) {
final Queue<Node> queue = new LinkedList<>();
final Node p = root;
for (final Integer key : p.next.keySet()) {
p.next.get(key).fail = root;
queue.offer(p.next.get(key));
}
while (!queue.isEmpty()) {
Node curr = queue.poll();
for (Integer key : curr.next.keySet()) {
final Node curr = queue.poll();
for (final Integer key : curr.next.keySet()) {
Node fail = curr.fail;
// 查找当前节点匹配失败他对应等效匹配的节点是哪个
while (fail != null && fail.next.get(key) == null) {
@ -114,16 +113,18 @@ public class NFA {
/**
* @param text 查询的文本母串
* @return FoundWord列表查找到的所有关键词
*/
public List<FoundWord> find(String text) {
public List<FoundWord> find(final String text) {
return this.find(text, true);
}
/**
* @param text 查找的文本母串
* @param isDensityMatch 是否密集匹配
* @return FoundWord列表查找到的所有关键词
*/
public List<FoundWord> find(String text, boolean isDensityMatch) {
public List<FoundWord> find(final String text, final boolean isDensityMatch) {
// double check防止重复无用的 buildAC
if (needBuildAc) {
synchronized (buildAcLock) {
@ -132,10 +133,10 @@ public class NFA {
}
}
}
List<FoundWord> ans = new ArrayList<>();
final List<FoundWord> ans = new ArrayList<>();
Node p = root, k = null;
for (int i = 0, len = text.length(); i < len; i++) {
int ind = text.charAt(i);
final int ind = text.charAt(i);
// 状态转移(沿着fail指针链接的链表此处区别于DFA模型)
while (p != null && p.next.get(ind) == null) {
p = p.fail;