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{ public class DoubleArrayDictionaryTrie implements Dictionary{
private static final Logger LOGGER = LoggerFactory.getLogger(DoubleArrayDictionaryTrie.class); 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 static class Node {
private int code; private int code;
Expand Down Expand Up @@ -148,18 +150,9 @@ private void init(List<String> words) {


this.words = words; this.words = words;


AtomicInteger max = new AtomicInteger(); base = new int[SIZE];
words.forEach(word->{ check = new int[SIZE];
for(int ch : word.toCharArray()){ used = new boolean[SIZE];
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[0] = 1; base[0] = 1;
nextCheckPos = 0; nextCheckPos = 0;
Expand All @@ -180,7 +173,7 @@ private void init(List<String> words) {


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


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


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

0 comments on commit edf3052

Please sign in to comment.