Hierarchy Class

Creates trees of HierarchyNode<T> from nested objects, from rows with a parent identifier, or from paths.

public static class Hierarchy

Methods

NameDescription
FromPaths<T>(IEnumerable<T>, Func<T, string>, Func<T, double>?, char)Creates a tree from rows that give their path, such as src/app/main.cs. The intermediate nodes the paths imply are created, with the default value of T as their data.
FromTree<T>(T, Func<T, IEnumerable<T>?>, Func<T, string?>?, Func<T, double>?)Creates a tree from nested objects.
Stratify<T>(IEnumerable<T>, Func<T, string>, Func<T, string?>, Func<T, double>?)Creates a tree from rows that give their identifier and the identifier of their parent.

FromPaths<T>(IEnumerable<T>, Func<T, string>, Func<T, double>?, char)

Hierarchy.cs

Creates a tree from rows that give their path, such as src/app/main.cs. The intermediate nodes the paths imply are created, with the default value of T as their data.

public static HierarchyNode<T?> FromPaths<T>(IEnumerable<T> rows, Func<T, string> path, Func<T, double>? value = null, char separator = '/')

Type parameters

T
The type of the rows.

Parameters

rows IEnumerable<T>
The rows.
path Func<T, string>
The path of a row.
value Func<T, double>?
The own value of a row, to compute the values of the nodes; or null to leave them at zero.
separator char
The separator of the segments of the paths. The default is /.

Returns

HierarchyNode<T?> The root node: a node with an empty name, whose children are the first segments of the paths.

FromTree<T>(T, Func<T, IEnumerable<T>?>, Func<T, string?>?, Func<T, double>?)

Hierarchy.cs

Creates a tree from nested objects.

public static HierarchyNode<T> FromTree<T>(T root, Func<T, IEnumerable<T>?> children, Func<T, string?>? name = null, Func<T, double>? value = null)

Type parameters

T
The type of the objects.

Parameters

root T
The root object.
children Func<T, IEnumerable<T>?>
The children of an object, or null for none.
name Func<T, string?>?
The name of an object, or null to use Object.ToString.
value Func<T, double>?
The own value of an object, to compute the values of the nodes with HierarchyNode<T>.Sum; or null to leave them at zero.

Returns

HierarchyNode<T> The root node.

Stratify<T>(IEnumerable<T>, Func<T, string>, Func<T, string?>, Func<T, double>?)

Hierarchy.cs

Creates a tree from rows that give their identifier and the identifier of their parent.

public static HierarchyNode<T> Stratify<T>(IEnumerable<T> rows, Func<T, string> id, Func<T, string?> parentId, Func<T, double>? value = null)

Type parameters

T
The type of the rows.

Parameters

rows IEnumerable<T>
The rows. Exactly one of them, the root, has no parent.
id Func<T, string>
The identifier of a row, which is also the name of its node.
parentId Func<T, string?>
The identifier of the parent of a row, or null or empty for the root.
value Func<T, double>?
The own value of a row, to compute the values of the nodes; or null to leave them at zero.

Returns

HierarchyNode<T> The root node.

Exceptions

ArgumentException
There is no root or several roots, an identifier is duplicated, a parent does not exist, or there is a cycle.