Finished component docker mostly!
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
@@ -50,7 +51,17 @@ public abstract partial class ComponentDocker
|
||||
/// </summary>
|
||||
/// <param name="__component"> Component to modify</param>
|
||||
/// <param name="__priority"> New priority for Component</param>
|
||||
internal void UpdatePriority(Component __component, int __priority) { _Components.Remove(__component); __component._priority = __priority; _Components.Add(__component); }
|
||||
internal void UpdatePriority(Component __component, int __priority) {
|
||||
foreach (String tag in __component._tags) {
|
||||
_taggedComponents[tag].Remove(__component);
|
||||
} _Components.Remove(__component);
|
||||
|
||||
__component._priority = __priority;
|
||||
|
||||
foreach (String tag in __component._tags) {
|
||||
_taggedComponents[tag].Add(__component);
|
||||
} _Components.Add(__component);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -264,7 +275,54 @@ public abstract partial class ComponentDocker
|
||||
/// /// <summary>
|
||||
/// Holds Components at Keys of their tags.
|
||||
/// </summary>
|
||||
internal Dictionary<string, List<Component>> _taggedComponents = [];
|
||||
internal Dictionary<string, SortedSet<Component>> _taggedComponents = [];
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Add component to its proper place in the dictionary and resort values to match priorities.
|
||||
/// </summary>
|
||||
/// <param name="__component"> Component to hash</param>
|
||||
/// <param name="__tag"> Value to try and hash</param>
|
||||
internal void HashTaggedComponent(Component __component, string __tag) {
|
||||
|
||||
if (!__component._tags.Add(__tag))
|
||||
//already has tag
|
||||
return;
|
||||
|
||||
if (_taggedComponents.TryGetValue(__tag, out SortedSet<Component> components)) {
|
||||
components.Add(__component);
|
||||
|
||||
} else _taggedComponents.Add(__tag, new SortedSet<Component>(Comparer<Component>.Create((a, b) => {
|
||||
int result = b.Priority.CompareTo(a.Priority);
|
||||
return (result != 0) ? result : a.GetHashCode().CompareTo(b.GetHashCode());
|
||||
})));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="__component"></param>
|
||||
/// <param name="__tag"></param>
|
||||
internal void UnhashTaggedComponent(Component __component, string __tag) {
|
||||
|
||||
if (!__component._tags.Remove(__tag))
|
||||
//doesnt have tag
|
||||
return;
|
||||
|
||||
|
||||
if (_taggedComponents.TryGetValue(__tag, out SortedSet<Component> components)) {
|
||||
components.Remove(__component);
|
||||
|
||||
if(components.Count == 0)
|
||||
_taggedComponents.Remove(__tag);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -274,7 +332,12 @@ public abstract partial class ComponentDocker
|
||||
/// </summary>
|
||||
/// <param name="__tag"></param>
|
||||
/// <returns></returns>
|
||||
internal Component Get(string __tag) => _taggedComponents[__tag].OrderByDescending(x => x._priority).First();
|
||||
internal Component Get(string __tag) {
|
||||
if (_taggedComponents.TryGetValue(__tag, out SortedSet<Component> components))
|
||||
return ((Component[])[..components])[0];
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -283,7 +346,12 @@ public abstract partial class ComponentDocker
|
||||
/// </summary>
|
||||
/// <param name="__tag"></param>
|
||||
/// <returns></returns>
|
||||
internal ImmutableArray<Component> GetAll(string __tag) => [.._taggedComponents[__tag]];
|
||||
internal ImmutableArray<Component> GetAll(string __tag) {
|
||||
if (_taggedComponents.TryGetValue(__tag, out SortedSet<Component> components))
|
||||
return [..components];
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -302,7 +370,7 @@ public abstract partial class ComponentDocker
|
||||
/// <param name="__tags"></param>
|
||||
/// <returns></returns>
|
||||
internal ImmutableArray<Component> GetAll(List<string> __tags) {
|
||||
List<Component> foundComponents = _taggedComponents[__tags[0]];
|
||||
SortedSet<Component> foundComponents = _taggedComponents[__tags[0]];
|
||||
|
||||
for (int i = 1; i < __tags.Count; i++) {
|
||||
foreach (Component component in (Component[])[..foundComponents]) {
|
||||
|
||||
@@ -25,13 +25,6 @@ public abstract partial class Component : ComponentDocker
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Identifiers for Components.
|
||||
/// </summary>
|
||||
public HashSet<string> Tags;
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Order for when Components are called on. Only applies between Components on the same Docker.
|
||||
/// </summary>
|
||||
@@ -112,11 +105,33 @@ public abstract partial class Component : ComponentDocker
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Identifiers for Components.
|
||||
/// </summary>
|
||||
public ImmutableArray<string> Tags => [.._tags];
|
||||
internal HashSet<string> _tags;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new tag to the Component
|
||||
/// </summary>
|
||||
/// <param name="__tag"> The tag to add</param>
|
||||
public void AddTag(string __tag) => ComponentDocker.HashTaggedComponent(this, __tag);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Removes a tag from the Component
|
||||
/// </summary>
|
||||
/// <param name="__tag"> The tag to remove</param>
|
||||
public void RemoveTag(string __tag) => ComponentDocker.UnhashTaggedComponent(this, __tag);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user