From aeede402e309ac41c8386065466965d54a4e305d Mon Sep 17 00:00:00 2001 From: GGG Date: Sun, 28 Jan 2024 01:01:48 -0300 Subject: [PATCH] Add the green node interface. This is made invisible because only generated code should use it, so we won't pollute the autocomplete suggestions with it. --- Tsu.Trees.RedGreen/src/Internal/IGreenNode.cs | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 Tsu.Trees.RedGreen/src/Internal/IGreenNode.cs diff --git a/Tsu.Trees.RedGreen/src/Internal/IGreenNode.cs b/Tsu.Trees.RedGreen/src/Internal/IGreenNode.cs new file mode 100644 index 0000000..86fed0a --- /dev/null +++ b/Tsu.Trees.RedGreen/src/Internal/IGreenNode.cs @@ -0,0 +1,134 @@ +// Copyright © 2024 GGG KILLER +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software +// and associated documentation files (the “Software”), to deal in the Software without +// restriction, including without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom +// the Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or +// substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING +// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; + +namespace Tsu.Trees.RedGreen.Internal; + +/// +/// The base class for a green tree node. +/// +/// +/// This is strictly for use by the generated code and libraries, you should never access green nodes yourself. +/// +[EditorBrowsable(EditorBrowsableState.Never)] +public interface IGreenNode + where TGreenRoot : class, IGreenNode + where TRedRoot : class + where TKind : Enum +{ + /// + /// The kind of node this is. + /// + TKind Kind { get; } + + /// + /// The number of slots this node contains. + /// + int SlotCount { get; } + + /// + /// Obtains the node at the given slot. + /// + /// + /// + TGreenRoot? GetSlot(int index); + + /// + /// Ensures the node at the provided slot will not be null and returns it. + /// + /// + /// + TGreenRoot GetRequiredSlot(int index); + + /// + /// Enumerate all descendants of this tree in depth-first order. + /// + /// + IEnumerable EnumerateDescendants(); + + /// + /// Enumerates the direct children that are under this node. + /// + /// + IEnumerable ChildNodes(); + + #region Caching + + /// + /// Whether this node can be cached. + /// + bool IsCacheable { get; } + + /// + /// Returns the hash code to be used for caching this node. + /// + /// + int GetCacheHash(); + + /// + /// Returns whether this node is equivalent to another cached counterpart. + /// + /// + /// + /// + bool IsCacheEquivalent(int kind, TGreenRoot? child1); + + /// + /// Returns whether this node is equivalent to another cached counterpart. + /// + /// + /// + /// + /// + bool IsCacheEquivalent(int kind, TGreenRoot? child1, TGreenRoot? child2); + + /// + /// Returns whether this node is equivalent to another cached counterpart. + /// + /// + /// + /// + /// + /// + bool IsCacheEquivalent(int kind, TGreenRoot? child1, TGreenRoot? child2, TGreenRoot? child3); + + #endregion Caching + + /// + /// Returns whether this given node is equivalent to another. + /// + /// + /// + bool IsEquivalentTo([NotNullWhen(true)] TGreenRoot? other); + + /// + /// Creates a red version of this node without a parent. + /// + /// + TRedRoot CreateRed(); + + /// + /// Creates a red version of this node with the specified parent. + /// + /// + /// The node to specify as the parent for this node. + /// + /// + TRedRoot CreateRed(TRedRoot? parent); +}