From bcc94abadd46c33217496f6c40497a332936f22c Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sat, 5 Jun 2021 23:54:04 +0100 Subject: [PATCH] switch Set -> Array in FileTree --- .../com/muwire/core/files/FileTree.groovy | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/files/FileTree.groovy b/core/src/main/groovy/com/muwire/core/files/FileTree.groovy index 810f38bf..867bb613 100644 --- a/core/src/main/groovy/com/muwire/core/files/FileTree.groovy +++ b/core/src/main/groovy/com/muwire/core/files/FileTree.groovy @@ -26,7 +26,7 @@ class FileTree { existing.isFile = element.isFile() existing.parent = current fileToNode.put(element, existing) - current.children.add(existing) + current.addChild(existing) } current = existing } @@ -38,10 +38,11 @@ class FileTree { if (node == null) { return false } - node.parent.children.remove(node) - if (node.parent.children.isEmpty() && node.parent != root) + node.parent.removeChild(node) + if (node.parent.children.length == 0 && node.parent != root) remove(node.parent.file) - def copy = new ArrayList(node.children) + def copy = new ArrayList() + copy.addAll node.children for (TreeNode child : copy) remove(child.file) true @@ -98,8 +99,8 @@ class FileTree { synchronized File commonAncestor() { TreeNode current = root - while(current.children.size() == 1) - current = current.children.first() + while(current.children.length == 1) + current = current.children[0] current.file } @@ -108,7 +109,7 @@ class FileTree { File file boolean isFile T value; - final Set children = new HashSet<>() + TreeNode[] children = EMPTY_CHILDREN public int hashCode() { Objects.hash(file) @@ -120,5 +121,24 @@ class FileTree { TreeNode other = (TreeNode)o file == other.file } + + private void addChild(TreeNode child) { + Set unique = new HashSet<>() + unique.addAll(children) + unique.add(child) + children = unique.toArray(children) + } + + private void removeChild(TreeNode child) { + Set unique = new HashSet<>() + unique.addAll(children) + unique.remove(child) + if (unique.isEmpty()) + children = EMPTY_CHILDREN + else + children = unique.toArray(new TreeNode[0]) + } } + + private static final TreeNode[] EMPTY_CHILDREN = new TreeNode[0] }