mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 08:21:11 +00:00
Priorize trie based on depth
This commit is contained in:
parent
4c0d2fb285
commit
42d6d0c78d
2 changed files with 29 additions and 1 deletions
|
|
@ -67,6 +67,8 @@ public class Trie {
|
||||||
|
|
||||||
public interface Matcher {
|
public interface Matcher {
|
||||||
Match match(int i, Path path, Match match);
|
Match match(int i, Path path, Match match);
|
||||||
|
|
||||||
|
int depth();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static StaticMatcher staticMatcher(String path, Matcher child) {
|
public static StaticMatcher staticMatcher(String path, Matcher child) {
|
||||||
|
|
@ -98,6 +100,11 @@ public class Trie {
|
||||||
return child.match(i + size, path, match);
|
return child.match(i + size, path, match);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int depth() {
|
||||||
|
return child.depth() + 1;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "[\"" + new String(path) + "\" " + child + "]";
|
return "[\"" + new String(path) + "\" " + child + "]";
|
||||||
|
|
@ -124,6 +131,11 @@ public class Trie {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int depth() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return (data != null ? data.toString() : "nil");
|
return (data != null ? data.toString() : "nil");
|
||||||
|
|
@ -171,6 +183,11 @@ public class Trie {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int depth() {
|
||||||
|
return child.depth() + 1;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "[" + key + " " + child + "]";
|
return "[" + key + " " + child + "]";
|
||||||
|
|
@ -200,6 +217,11 @@ public class Trie {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int depth() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "[" + parameter + " " + new DataMatcher(data) + "]";
|
return "[" + parameter + " " + new DataMatcher(data) + "]";
|
||||||
|
|
@ -217,6 +239,7 @@ public class Trie {
|
||||||
|
|
||||||
LinearMatcher(List<Matcher> childs) {
|
LinearMatcher(List<Matcher> childs) {
|
||||||
this.childs = childs.toArray(new Matcher[0]);
|
this.childs = childs.toArray(new Matcher[0]);
|
||||||
|
Arrays.sort(this.childs, Comparator.comparing(Matcher::depth).reversed());
|
||||||
this.size = childs.size();
|
this.size = childs.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -231,6 +254,11 @@ public class Trie {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int depth() {
|
||||||
|
return Arrays.stream(childs).mapToInt(Matcher::depth).max().orElseThrow(NoSuchElementException::new);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return Arrays.toString(childs);
|
return Arrays.toString(childs);
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,7 @@
|
||||||
(assoc-in node [:children path] (-insert (-node {}) ps data))))]
|
(assoc-in node [:children path] (-insert (-node {}) ps data))))]
|
||||||
(if-let [child (get-in node' [:children ""])]
|
(if-let [child (get-in node' [:children ""])]
|
||||||
;; optimize by removing empty paths
|
;; optimize by removing empty paths
|
||||||
(-> (merge-with merge node' child)
|
(-> (merge-with merge (dissoc node' :data) child)
|
||||||
(update :children dissoc ""))
|
(update :children dissoc ""))
|
||||||
node')))
|
node')))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue