string segment optimization
This commit is contained in:
parent
947351af9a
commit
4a4f4b2f2a
2 changed files with 32 additions and 3 deletions
|
@ -0,0 +1,30 @@
|
||||||
|
package org.xbib.datastructures.trie.segment;
|
||||||
|
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class PatternSegment implements TrieKeySegment<Pattern> {
|
||||||
|
|
||||||
|
private final Pattern segment;
|
||||||
|
|
||||||
|
public PatternSegment(Pattern segment) {
|
||||||
|
this.segment = segment;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pattern getSegment() {
|
||||||
|
return segment;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static PatternSegment of(Pattern segment) {
|
||||||
|
return new PatternSegment(segment);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Pattern o) {
|
||||||
|
return segment.pattern().compareTo(o.pattern());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return segment.pattern();
|
||||||
|
}
|
||||||
|
}
|
|
@ -43,13 +43,12 @@ public class TrieTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLargeInsertionAndSearch() {
|
public void testLargeInsertionAndSearch() {
|
||||||
|
Random random = new Random();
|
||||||
Trie<String, TrieKey<String>, Long> trie = new TrieImpl<>();
|
Trie<String, TrieKey<String>, Long> trie = new TrieImpl<>();
|
||||||
List<TrieKey<String>> keys = new ArrayList<>();
|
List<TrieKey<String>> keys = new ArrayList<>();
|
||||||
for (int i = 0; i < 10000; i++) {
|
for (int i = 0; i < 10000; i++) {
|
||||||
Random random = new Random();
|
|
||||||
Long value = random.nextLong();
|
Long value = random.nextLong();
|
||||||
String key = value.toString();
|
TrieKey<String> trieKey = TrieKeyImpl.stringKey(value.toString());
|
||||||
TrieKey<String> trieKey = TrieKeyImpl.stringKey(key);
|
|
||||||
trie.put(trieKey, value);
|
trie.put(trieKey, value);
|
||||||
keys.add(trieKey);
|
keys.add(trieKey);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue