aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Common/Collections/IntrusiveRedBlackTree.cs
blob: 9e56f707bf58ff3d3d83fb3567bbf7eb81540c47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using System;

namespace Ryujinx.Common.Collections
{
    /// <summary>
    /// Tree that provides the ability for O(logN) lookups for keys that exist in the tree, and O(logN) lookups for keys immediately greater than or less than a specified key.
    /// </summary>
    /// <typeparam name="T">Derived node type</typeparam>
    public class IntrusiveRedBlackTree<T> : IntrusiveRedBlackTreeImpl<T> where T : IntrusiveRedBlackTreeNode<T>, IComparable<T>
    {
        #region Public Methods

        /// <summary>
        /// Adds a new node into the tree.
        /// </summary>
        /// <param name="node">Node to be added</param>
        /// <exception cref="ArgumentNullException"><paramref name="node"/> is null</exception>
        public void Add(T node)
        {
            ArgumentNullException.ThrowIfNull(node);

            Insert(node);
        }

        /// <summary>
        /// Removes a node from the tree.
        /// </summary>
        /// <param name="node">Note to be removed</param>
        /// <exception cref="ArgumentNullException"><paramref name="node"/> is null</exception>
        public void Remove(T node)
        {
            ArgumentNullException.ThrowIfNull(node);

            if (Delete(node) != null)
            {
                Count--;
            }
        }

        /// <summary>
        /// Retrieve the node that is considered equal to the specified node by the comparator.
        /// </summary>
        /// <param name="searchNode">Node to compare with</param>
        /// <returns>Node that is equal to <paramref name="searchNode"/></returns>
        /// <exception cref="ArgumentNullException"><paramref name="searchNode"/> is null</exception>
        public T GetNode(T searchNode)
        {
            ArgumentNullException.ThrowIfNull(searchNode);

            T node = Root;
            while (node != null)
            {
                int cmp = searchNode.CompareTo(node);
                if (cmp < 0)
                {
                    node = node.Left;
                }
                else if (cmp > 0)
                {
                    node = node.Right;
                }
                else
                {
                    return node;
                }
            }
            return null;
        }

        #endregion

        #region Private Methods (BST)

        /// <summary>
        /// Inserts a new node into the tree.
        /// </summary>
        /// <param name="node">Node to be inserted</param>
        private void Insert(T node)
        {
            T newNode = BSTInsert(node);
            RestoreBalanceAfterInsertion(newNode);
        }

        /// <summary>
        /// Insertion Mechanism for a Binary Search Tree (BST).
        /// <br></br>
        /// Iterates the tree starting from the root and inserts a new node
        /// where all children in the left subtree are less than <paramref name="newNode"/>,
        /// and all children in the right subtree are greater than <paramref name="newNode"/>.
        /// </summary>
        /// <param name="newNode">Node to be inserted</param>
        /// <returns>The inserted Node</returns>
        private T BSTInsert(T newNode)
        {
            T parent = null;
            T node = Root;

            while (node != null)
            {
                parent = node;
                int cmp = newNode.CompareTo(node);
                if (cmp < 0)
                {
                    node = node.Left;
                }
                else if (cmp > 0)
                {
                    node = node.Right;
                }
                else
                {
                    return node;
                }
            }
            newNode.Parent = parent;
            if (parent == null)
            {
                Root = newNode;
            }
            else if (newNode.CompareTo(parent) < 0)
            {
                parent.Left = newNode;
            }
            else
            {
                parent.Right = newNode;
            }
            Count++;
            return newNode;
        }

        /// <summary>
        /// Removes <paramref name="nodeToDelete"/> from the tree, if it exists.
        /// </summary>
        /// <param name="nodeToDelete">Node to be removed</param>
        /// <returns>The deleted Node</returns>
        private T Delete(T nodeToDelete)
        {
            if (nodeToDelete == null)
            {
                return null;
            }

            T old = nodeToDelete;
            T child;
            T parent;
            bool color;

            if (LeftOf(nodeToDelete) == null)
            {
                child = RightOf(nodeToDelete);
            }
            else if (RightOf(nodeToDelete) == null)
            {
                child = LeftOf(nodeToDelete);
            }
            else
            {
                T element = Minimum(RightOf(nodeToDelete));

                child = RightOf(element);
                parent = ParentOf(element);
                color = ColorOf(element);

                if (child != null)
                {
                    child.Parent = parent;
                }

                if (parent == null)
                {
                    Root = child;
                }
                else if (element == LeftOf(parent))
                {
                    parent.Left = child;
                }
                else
                {
                    parent.Right = child;
                }

                element.Color = old.Color;
                element.Left = old.Left;
                element.Right = old.Right;
                element.Parent = old.Parent;

                if (ParentOf(old) == null)
                {
                    Root = element;
                }
                else if (old == LeftOf(ParentOf(old)))
                {
                    ParentOf(old).Left = element;
                }
                else
                {
                    ParentOf(old).Right = element;
                }

                LeftOf(old).Parent = element;

                if (RightOf(old) != null)
                {
                    RightOf(old).Parent = element;
                }

                if (child != null && color == Black)
                {
                    RestoreBalanceAfterRemoval(child);
                }

                return old;
            }

            parent = ParentOf(nodeToDelete);
            color = ColorOf(nodeToDelete);

            if (child != null)
            {
                child.Parent = parent;
            }

            if (parent == null)
            {
                Root = child;
            }
            else if (nodeToDelete == LeftOf(parent))
            {
                parent.Left = child;
            }
            else
            {
                parent.Right = child;
            }

            if (child != null && color == Black)
            {
                RestoreBalanceAfterRemoval(child);
            }

            return old;
        }

        #endregion
    }

    public static class IntrusiveRedBlackTreeExtensions
    {
        /// <summary>
        /// Retrieve the node that is considered equal to the key by the comparator.
        /// </summary>
        /// <param name="tree">Tree to search at</param>
        /// <param name="key">Key of the node to be found</param>
        /// <returns>Node that is equal to <paramref name="key"/></returns>
        public static TNode GetNodeByKey<TNode, TKey>(this IntrusiveRedBlackTree<TNode> tree, TKey key)
            where TNode : IntrusiveRedBlackTreeNode<TNode>, IComparable<TNode>, IComparable<TKey>
            where TKey : struct
        {
            TNode node = tree.RootNode;
            while (node != null)
            {
                int cmp = node.CompareTo(key);
                if (cmp < 0)
                {
                    node = node.Right;
                }
                else if (cmp > 0)
                {
                    node = node.Left;
                }
                else
                {
                    return node;
                }
            }
            return null;
        }
    }
}