diff --git a/src/TMG-Framework/Data/Categories.cs b/src/TMG-Framework/Data/Categories.cs index ceb3d23..a5bf743 100644 --- a/src/TMG-Framework/Data/Categories.cs +++ b/src/TMG-Framework/Data/Categories.cs @@ -42,11 +42,11 @@ public sealed class Categories : IEnumerable private readonly List _elements; /// - /// + /// Create a Categories instance from a list of elements /// - /// - /// - /// + /// The list of elements to use, values will be sorted, any duplicates will return in failure. + /// The error message if creation fails + /// True if creation succeeds, false otherwise public static bool CreateCategories(List elements, [NotNullWhen(true)] out Categories? categories, [NotNullWhen(false)] ref string? error) @@ -67,9 +67,34 @@ public static bool CreateCategories(List elements, } /// - /// Create a SparseMap from a list of elements + /// Create a Categories instance from a list of elements + /// + /// The list of elements to use, values will be sorted, any duplicates will return in failure. + /// The error message if creation fails + /// True if creation succeeds, false otherwise + public static bool CreateCategories(Span elements, + [NotNullWhen(true)] out Categories? categories, + [NotNullWhen(false)] ref string? error) + { + var list = new List(elements.ToArray()); + list.Sort(); + for (int i = 1; i < list.Count; i++) + { + if(list[i - 1] == list[i]) + { + error = $"Found a duplicate category {list[i]}!"; + categories = null; + return false; + } + } + categories = new Categories(list); + return true; + } + + /// + /// Create a Categories instance from a list of elements /// - /// The sorted elements to use + /// The sorted list of elements to use private Categories(List elements) { if(elements == null)