diff options
| author | 2019-02-16 17:15:14 +0200 | |
|---|---|---|
| committer | 2019-02-16 17:15:26 +0200 | |
| commit | b34305a235cdaaeadecad874931f2f2b62971711 (patch) | |
| tree | 23ec3fad888a74feaeddf499843737ed2c093753 /src | |
| parent | update SyntaxPane/Procyon - partially solves #103 (diff) | |
| download | enigma-b34305a235cdaaeadecad874931f2f2b62971711.tar.gz enigma-b34305a235cdaaeadecad874931f2f2b62971711.tar.xz enigma-b34305a235cdaaeadecad874931f2f2b62971711.zip | |
Resolve HashEntryTree#getSiblings building the full ancestor path
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/java/cuchaz/enigma/translation/mapping/tree/HashEntryTree.java | 24 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/translation/mapping/tree/HashTreeNode.java | 15 |
2 files changed, 26 insertions, 13 deletions
diff --git a/src/main/java/cuchaz/enigma/translation/mapping/tree/HashEntryTree.java b/src/main/java/cuchaz/enigma/translation/mapping/tree/HashEntryTree.java index 551fb1c8..fa9ed13d 100644 --- a/src/main/java/cuchaz/enigma/translation/mapping/tree/HashEntryTree.java +++ b/src/main/java/cuchaz/enigma/translation/mapping/tree/HashEntryTree.java | |||
| @@ -24,7 +24,7 @@ public class HashEntryTree<T> implements EntryTree<T> { | |||
| 24 | 24 | ||
| 25 | @Override | 25 | @Override |
| 26 | public void insert(Entry<?> entry, T value) { | 26 | public void insert(Entry<?> entry, T value) { |
| 27 | List<HashTreeNode<T>> path = computePath(entry); | 27 | List<HashTreeNode<T>> path = computePath(entry, true); |
| 28 | path.get(path.size() - 1).putValue(value); | 28 | path.get(path.size() - 1).putValue(value); |
| 29 | if (value == null) { | 29 | if (value == null) { |
| 30 | removeDeadAlong(path); | 30 | removeDeadAlong(path); |
| @@ -34,7 +34,11 @@ public class HashEntryTree<T> implements EntryTree<T> { | |||
| 34 | @Override | 34 | @Override |
| 35 | @Nullable | 35 | @Nullable |
| 36 | public T remove(Entry<?> entry) { | 36 | public T remove(Entry<?> entry) { |
| 37 | List<HashTreeNode<T>> path = computePath(entry); | 37 | List<HashTreeNode<T>> path = computePath(entry, false); |
| 38 | if (path.isEmpty()) { | ||
| 39 | return null; | ||
| 40 | } | ||
| 41 | |||
| 38 | T value = path.get(path.size() - 1).removeValue(); | 42 | T value = path.get(path.size() - 1).removeValue(); |
| 39 | 43 | ||
| 40 | removeDeadAlong(path); | 44 | removeDeadAlong(path); |
| @@ -68,7 +72,7 @@ public class HashEntryTree<T> implements EntryTree<T> { | |||
| 68 | 72 | ||
| 69 | @Override | 73 | @Override |
| 70 | public Collection<Entry<?>> getSiblings(Entry<?> entry) { | 74 | public Collection<Entry<?>> getSiblings(Entry<?> entry) { |
| 71 | List<HashTreeNode<T>> path = computePath(entry); | 75 | List<HashTreeNode<T>> path = computePath(entry, false); |
| 72 | if (path.size() <= 1) { | 76 | if (path.size() <= 1) { |
| 73 | return getSiblings(entry, root.keySet()); | 77 | return getSiblings(entry, root.keySet()); |
| 74 | } | 78 | } |
| @@ -95,13 +99,13 @@ public class HashEntryTree<T> implements EntryTree<T> { | |||
| 95 | if (node == null) { | 99 | if (node == null) { |
| 96 | return null; | 100 | return null; |
| 97 | } | 101 | } |
| 98 | node = node.getChild(parentChain.get(i), false); | 102 | node = node.getChild(parentChain.get(i)); |
| 99 | } | 103 | } |
| 100 | 104 | ||
| 101 | return node; | 105 | return node; |
| 102 | } | 106 | } |
| 103 | 107 | ||
| 104 | private List<HashTreeNode<T>> computePath(Entry<?> target) { | 108 | private List<HashTreeNode<T>> computePath(Entry<?> target, boolean make) { |
| 105 | List<Entry<?>> ancestry = target.getAncestry(); | 109 | List<Entry<?>> ancestry = target.getAncestry(); |
| 106 | if (ancestry.isEmpty()) { | 110 | if (ancestry.isEmpty()) { |
| 107 | return Collections.emptyList(); | 111 | return Collections.emptyList(); |
| @@ -110,11 +114,17 @@ public class HashEntryTree<T> implements EntryTree<T> { | |||
| 110 | List<HashTreeNode<T>> path = new ArrayList<>(ancestry.size()); | 114 | List<HashTreeNode<T>> path = new ArrayList<>(ancestry.size()); |
| 111 | 115 | ||
| 112 | Entry<?> rootEntry = ancestry.get(0); | 116 | Entry<?> rootEntry = ancestry.get(0); |
| 113 | HashTreeNode<T> node = root.computeIfAbsent(rootEntry, HashTreeNode::new); | 117 | HashTreeNode<T> node = make ? root.computeIfAbsent(rootEntry, HashTreeNode::new) : root.get(rootEntry); |
| 114 | path.add(node); | 118 | path.add(node); |
| 115 | 119 | ||
| 116 | for (int i = 1; i < ancestry.size(); i++) { | 120 | for (int i = 1; i < ancestry.size(); i++) { |
| 117 | node = node.getChild(ancestry.get(i), true); | 121 | if (node == null) { |
| 122 | return Collections.emptyList(); | ||
| 123 | } | ||
| 124 | |||
| 125 | Entry<?> ancestor = ancestry.get(i); | ||
| 126 | node = make ? node.computeChild(ancestor) : node.getChild(ancestor); | ||
| 127 | |||
| 118 | path.add(node); | 128 | path.add(node); |
| 119 | } | 129 | } |
| 120 | 130 | ||
diff --git a/src/main/java/cuchaz/enigma/translation/mapping/tree/HashTreeNode.java b/src/main/java/cuchaz/enigma/translation/mapping/tree/HashTreeNode.java index 90e91647..0a990bd5 100644 --- a/src/main/java/cuchaz/enigma/translation/mapping/tree/HashTreeNode.java +++ b/src/main/java/cuchaz/enigma/translation/mapping/tree/HashTreeNode.java | |||
| @@ -2,6 +2,7 @@ package cuchaz.enigma.translation.mapping.tree; | |||
| 2 | 2 | ||
| 3 | import cuchaz.enigma.translation.representation.entry.Entry; | 3 | import cuchaz.enigma.translation.representation.entry.Entry; |
| 4 | 4 | ||
| 5 | import javax.annotation.Nonnull; | ||
| 5 | import javax.annotation.Nullable; | 6 | import javax.annotation.Nullable; |
| 6 | import java.util.Collection; | 7 | import java.util.Collection; |
| 7 | import java.util.HashMap; | 8 | import java.util.HashMap; |
| @@ -27,12 +28,14 @@ public class HashTreeNode<T> implements EntryTreeNode<T>, Iterable<HashTreeNode< | |||
| 27 | return value; | 28 | return value; |
| 28 | } | 29 | } |
| 29 | 30 | ||
| 30 | HashTreeNode<T> getChild(Entry<?> entry, boolean create) { | 31 | @Nullable |
| 31 | if (create) { | 32 | HashTreeNode<T> getChild(Entry<?> entry) { |
| 32 | return children.computeIfAbsent(entry, HashTreeNode::new); | 33 | return children.get(entry); |
| 33 | } else { | 34 | } |
| 34 | return children.get(entry); | 35 | |
| 35 | } | 36 | @Nonnull |
| 37 | HashTreeNode<T> computeChild(Entry<?> entry) { | ||
| 38 | return children.computeIfAbsent(entry, HashTreeNode::new); | ||
| 36 | } | 39 | } |
| 37 | 40 | ||
| 38 | void remove(Entry<?> entry) { | 41 | void remove(Entry<?> entry) { |