ladybird/Userland/Applications/SpaceAnalyzer/Tree.h
Sam Atkins 1ec59cc52a SpaceAnalyzer: Make TreeMapWidget responsible for filesystem analysis
Also, the Tree object was only ever used by the TreeMapWidget, so its
creation can happen inside `analyze()`, fixing the memory leak issue.
Plus, it doesn't have to be RefCounted.
2023-02-04 12:27:10 +01:00

63 lines
1.4 KiB
C++

/*
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/DeprecatedString.h>
#include <AK/Forward.h>
#include <AK/OwnPtr.h>
#include <AK/Vector.h>
struct MountInfo {
DeprecatedString mount_point;
DeprecatedString source;
};
class TreeNode final {
public:
TreeNode(DeprecatedString name)
: m_name(move(name)) {};
DeprecatedString name() const { return m_name; }
i64 area() const { return m_area; }
size_t num_children() const
{
if (m_children) {
return m_children->size();
}
return 0;
}
TreeNode const& child_at(size_t i) const { return m_children->at(i); }
Optional<TreeNode const&> child_with_name(DeprecatedString name) const;
void sort_children_by_area() const;
HashMap<int, int> populate_filesize_tree(Vector<MountInfo>& mounts, Function<void(size_t)> on_progress);
private:
long long int update_totals();
DeprecatedString m_name;
i64 m_area { 0 };
OwnPtr<Vector<TreeNode>> m_children;
};
class Tree {
public:
static ErrorOr<NonnullOwnPtr<Tree>> create(DeprecatedString root_name)
{
return adopt_nonnull_own_or_enomem(new (nothrow) Tree(move(root_name)));
}
~Tree() {};
TreeNode& root()
{
return m_root;
};
private:
Tree(DeprecatedString root_name)
: m_root(move(root_name)) {};
TreeNode m_root;
};