Skip to content

Commit

Permalink
优化DoubleArrayTrie fix #1136
Browse files Browse the repository at this point in the history
  • Loading branch information
hankcs committed Mar 29, 2019
1 parent a39b14a commit 478f895
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public String toString()
protected int check[];
protected int base[];

private BitSet used;
/**
* base 和 check 的大小
*/
Expand Down Expand Up @@ -159,7 +158,7 @@ private int fetch(Node parent, List<Node> siblings)
* @param siblings 等待插入的兄弟节点
* @return 插入位置
*/
private int insert(List<Node> siblings)
private int insert(List<Node> siblings, BitSet used)
{
if (error_ < 0)
return 0;
Expand Down Expand Up @@ -254,7 +253,7 @@ else if (first == 0)
}
else
{
int h = insert(new_siblings); // dfs
int h = insert(new_siblings, used); // dfs
base[begin + siblings.get(i).code] = h;
// System.out.println(this);
}
Expand All @@ -266,7 +265,6 @@ public DoubleArrayTrie()
{
check = null;
base = null;
used = new BitSet();
size = 0;
allocSize = 0;
// no_delete_ = false;
Expand Down Expand Up @@ -300,7 +298,6 @@ void clear()
// if (! no_delete_)
check = null;
base = null;
used = null;
allocSize = 0;
size = 0;
// no_delete_ = false;
Expand Down Expand Up @@ -399,6 +396,7 @@ public int build(List<String> _key, int _length[], int _value[],
keySize = _keySize;
value = _value;
progress = 0;
allocSize = 0;

resize(65536 * 32); // 32个双字节

Expand All @@ -412,13 +410,12 @@ public int build(List<String> _key, int _length[], int _value[],

List<Node> siblings = new ArrayList<Node>();
fetch(root_node, siblings);
insert(siblings);
insert(siblings, new BitSet());
shrink();

// size += (1 << 8 * 2) + 1; // ???
// if (size >= allocSize) resize (size);

used = null;
key = null;
length = null;

Expand Down Expand Up @@ -545,7 +542,6 @@ public boolean load(ByteArray byteArray, V[] value)
check[i] = byteArray.nextInt();
}
v = value;
used = null; // 无用的对象,释放掉
return true;
}

Expand All @@ -571,7 +567,6 @@ public boolean load(byte[] bytes, int offset, V[] value)
offset += 4;
}
v = value;
used = null; // 无用的对象,释放掉
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,6 @@ public static boolean reload()
String path[] = HanLP.Config.CustomDictionaryPath;
if (path == null || path.length == 0) return false;
IOUtil.deleteFile(path[0] + Predefine.BIN_EXT); // 删掉缓存
dat = new DoubleArrayTrie<CoreDictionary.Attribute>();
return loadMainDictionary(path[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public void testDatFromFile() throws Exception
{
assertEquals(key, trie.get(key));
}

trie.build(map);
for (String key : map.keySet())
{
assertEquals(key, trie.get(key));
}
}

public void testGet() throws Exception
Expand Down

0 comments on commit 478f895

Please sign in to comment.