switch Set -> Array in FileTree

pull/62/head
Zlatin Balevsky 2021-06-05 23:54:04 +01:00
parent d00d6d7e18
commit bcc94abadd
No known key found for this signature in database
GPG Key ID: A72832072D525E41
1 changed files with 27 additions and 7 deletions

View File

@ -26,7 +26,7 @@ class FileTree<T> {
existing.isFile = element.isFile() existing.isFile = element.isFile()
existing.parent = current existing.parent = current
fileToNode.put(element, existing) fileToNode.put(element, existing)
current.children.add(existing) current.addChild(existing)
} }
current = existing current = existing
} }
@ -38,10 +38,11 @@ class FileTree<T> {
if (node == null) { if (node == null) {
return false return false
} }
node.parent.children.remove(node) node.parent.removeChild(node)
if (node.parent.children.isEmpty() && node.parent != root) if (node.parent.children.length == 0 && node.parent != root)
remove(node.parent.file) remove(node.parent.file)
def copy = new ArrayList(node.children) def copy = new ArrayList()
copy.addAll node.children
for (TreeNode child : copy) for (TreeNode child : copy)
remove(child.file) remove(child.file)
true true
@ -98,8 +99,8 @@ class FileTree<T> {
synchronized File commonAncestor() { synchronized File commonAncestor() {
TreeNode current = root TreeNode current = root
while(current.children.size() == 1) while(current.children.length == 1)
current = current.children.first() current = current.children[0]
current.file current.file
} }
@ -108,7 +109,7 @@ class FileTree<T> {
File file File file
boolean isFile boolean isFile
T value; T value;
final Set<TreeNode> children = new HashSet<>() TreeNode[] children = EMPTY_CHILDREN
public int hashCode() { public int hashCode() {
Objects.hash(file) Objects.hash(file)
@ -120,5 +121,24 @@ class FileTree<T> {
TreeNode other = (TreeNode)o TreeNode other = (TreeNode)o
file == other.file file == other.file
} }
private void addChild(TreeNode child) {
Set<TreeNode> unique = new HashSet<>()
unique.addAll(children)
unique.add(child)
children = unique.toArray(children)
} }
private void removeChild(TreeNode child) {
Set<TreeNode> 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]
} }