diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2023-03-28 09:13:51 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2023-03-28 09:13:51 +0200 |
commit | 3d06fbbe40ecbc930dd01bc03324a6cd889f96e4 (patch) | |
tree | d8487a3361ca8b2d76ebb71b2866246b15963c32 | |
parent | 446eb32c890ffa477861109cd75cdb3c0c9f2cea (diff) |
- Count number of items in the tree that has a unique id.
- Use this to either exit early, or presize the array to collect them in.
-rw-r--r-- | container-search/src/main/java/com/yahoo/prelude/query/CompositeItem.java | 45 |
1 files changed, 28 insertions, 17 deletions
diff --git a/container-search/src/main/java/com/yahoo/prelude/query/CompositeItem.java b/container-search/src/main/java/com/yahoo/prelude/query/CompositeItem.java index f48dc9a8630..3f0bcf177dc 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/CompositeItem.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/CompositeItem.java @@ -210,7 +210,7 @@ public abstract class CompositeItem extends Item { public CompositeItem clone() { CompositeItem copy = (CompositeItem) super.clone(); - copy.subitems = new java.util.ArrayList<>(); + copy.subitems = new java.util.ArrayList<>(subitems.size()); for (Item subItem : subitems) { Item subItemCopy = subItem.clone(); subItemCopy.setParent(copy); @@ -221,14 +221,16 @@ public abstract class CompositeItem extends Item { } private void fixConnexity(CompositeItem copy) { - List<Item> flatland = new ArrayList<>(); - List<Item> flatCopy = new ArrayList<>(); - taggingFlatten(this, flatland); + int hasUniqueIdCount = countUniqueId(this); + if (hasUniqueIdCount == 0) return; + List<Item> flat = new ArrayList<>(hasUniqueIdCount); + List<Item> flatCopy = new ArrayList<>(hasUniqueIdCount); + taggingFlatten(this, flat); taggingFlatten(copy, flatCopy); - int barrier = flatland.size(); + int barrier = flat.size(); for (int i = 0; i < barrier; ++i) { - Item orig = flatland.get(i); - int connectedTo = find(orig.connectedItem, flatland); + Item orig = flat.get(i); + int connectedTo = find(orig.connectedItem, flat); if (connectedTo >= 0) { TaggableItem tagged = (TaggableItem) flatCopy.get(i); tagged.setConnectivity(flatCopy.get(connectedTo), orig.connectivity); @@ -236,18 +238,30 @@ public abstract class CompositeItem extends Item { } } - private void taggingFlatten(Item tree, List<Item> container) { + private static int countUniqueId(Item tree) { + if (tree.hasUniqueID()) { + return 1; + } else if (tree instanceof CompositeItem asComposite) { + int sum = 0; + for (Iterator<Item> i = asComposite.getItemIterator(); i.hasNext();) { + sum += countUniqueId(i.next()); + } + return sum; + } + return 0; + } + + private static void taggingFlatten(Item tree, List<Item> container) { if (tree.hasUniqueID()) { container.add(tree); - } else if (tree instanceof CompositeItem) { - CompositeItem asComposite = (CompositeItem) tree; + } else if (tree instanceof CompositeItem asComposite) { for (Iterator<Item> i = asComposite.getItemIterator(); i.hasNext();) { taggingFlatten(i.next(), container); } } } - private int find(Item needle, List<Item> haystack) { + private static int find(Item needle, List<Item> haystack) { if (needle == null) { return -1; } @@ -352,8 +366,7 @@ public abstract class CompositeItem extends Item { } @Override - public void set(Item o) { - Item newItem = o; + public void set(Item newItem) { owner.removing(current); owner.adding(newItem); current = newItem; @@ -361,12 +374,10 @@ public abstract class CompositeItem extends Item { } @Override - public void add(Item o) { - Item newItem = o; - + public void add(Item newItem) { owner.adding(newItem); // TODO: Change current here? Check javadoc - wrapped.add(o); + wrapped.add(newItem); } } |