Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ private void insertMapping(String key, char valueToInsert) {

if ((value & LEAF_MARKER) != 0) {
// append our new leaf node below the leaf branch and change it to a bud
jumpOffset = appendLeaf(dataIndex, key, keyIndex, valueToInsert);
jumpOffset = appendLeaf(dataIndex, key, keyIndex, valueToInsert, false);
trieData[valueIndex] = (char) ((value & ~LEAF_MARKER) | BUD_MARKER);
break;
} else if ((value & BUD_MARKER) != 0) {
Expand Down Expand Up @@ -569,7 +569,7 @@ private void insertMapping(String key, char valueToInsert) {
if ((value & LEAF_MARKER) != 0) {
if (keyIndex < keyLength) {
// key goes past leaf segment, extend old leaf with a bud node and add our new leaf
jumpOffset = appendLeaf(dataIndex, key, keyIndex, valueToInsert);
jumpOffset = appendLeaf(dataIndex, key, keyIndex, valueToInsert, true);
trieData[valueIndex]--;
break;
} else {
Expand Down Expand Up @@ -825,12 +825,11 @@ private int insertLeafRight(
return insertedCharacters;
}

private int appendLeaf(int dataIndex, String key, int keyIndex, int value) {
private int appendLeaf(int dataIndex, String key, int keyIndex, int value, boolean insertBud) {
int remainingKeyLength = key.length() - keyIndex;
int insertedCharacters = 3 /* branch count, segment-length, value */ + remainingKeyLength;

// if we're adding this to a leaf segment then we need to introduce a bud first
boolean insertBud = dataIndex < trieLength && (trieData[dataIndex] & LEAF_MARKER) != 0;
if (insertBud) {
insertedCharacters++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,32 @@ void tooManyLongJumps() {
IllegalArgumentException.class, () -> builder.put("" + ((char) 67) + ((char) 2), 1));
}

@ParameterizedTest
@ValueSource(chars = {'\u7FFF', '\u8000', '\uFFFF'})
void extendingLeafPreservesFollowingUnicodeSegment(char nextCharacter) {
for (String suffix : new String[] {"x", "xyz"}) {
ClassNameTrie.Builder builder = new ClassNameTrie.Builder();
String followingKey = "b" + nextCharacter;
builder.put("a", 1);
builder.put(followingKey, 2);

String extension = "a" + suffix;
builder.put(extension, 3);
assertEquals(1, builder.apply("a"));
assertEquals(2, builder.apply(followingKey));
assertEquals(3, builder.apply(extension));

builder.put(extension + "x", 4);
builder.put(followingKey + "x", 5);
ClassNameTrie trie = builder.buildTrie();
assertEquals(1, trie.apply("a"));
assertEquals(2, trie.apply(followingKey));
assertEquals(3, trie.apply(extension));
assertEquals(4, trie.apply(extension + "x"));
assertEquals(5, trie.apply(followingKey + "x"));
}
}

@ParameterizedTest
@ValueSource(booleans = {false, true})
void builtTriePreservesValuesAfterBuilderUpdates(boolean newBuilder) {
Expand Down
Loading