Skip to content

Commit

Permalink
优化内存使用
Browse files Browse the repository at this point in the history
  • Loading branch information
ysc committed May 8, 2015
1 parent 8761006 commit edf3052
Showing 1 changed file with 11 additions and 19 deletions.
Expand Up @@ -38,7 +38,9 @@
*/
public class DoubleArrayDictionaryTrie implements Dictionary{
private static final Logger LOGGER = LoggerFactory.getLogger(DoubleArrayDictionaryTrie.class);
private int maxLength;

private static final int SIZE = 3000000;
private AtomicInteger maxLength = new AtomicInteger();

private static class Node {
private int code;
Expand Down Expand Up @@ -148,18 +150,9 @@ private void init(List<String> words) {

this.words = words;

AtomicInteger max = new AtomicInteger();
words.forEach(word->{
for(int ch : word.toCharArray()){
if(ch > max.get()){
max.set(ch);
}
}
});
int size = max.get()*2;
base = new int[size];
check = new int[size];
used = new boolean[size];
base = new int[SIZE];
check = new int[SIZE];
used = new boolean[SIZE];

base[0] = 1;
nextCheckPos = 0;
Expand All @@ -180,7 +173,7 @@ private void init(List<String> words) {

@Override
public int getMaxLength() {
return maxLength;
return maxLength.get();
}

@Override
Expand Down Expand Up @@ -222,15 +215,14 @@ public void addAll(List<String> items) {
if(check!=null){
throw new RuntimeException("addAll method can just be used once after clear method!");
}
//清理数据
items=items
.parallelStream()
.stream()
.map(item -> item.trim())
.filter(item -> {
//统计最大词长
int len = item.length();
if(len > maxLength){
maxLength = len;
if(len > maxLength.get()){
maxLength.set(len);
}
return len > 0;
})
Expand Down Expand Up @@ -260,7 +252,7 @@ public void clear() {
base = null;
used = null;
nextCheckPos = 0;
maxLength = 0;
maxLength.set(0);
}

public static void main(String[] args) {
Expand Down

0 comments on commit edf3052

Please sign in to comment.