-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMovieCollectionExtension.cs
235 lines (186 loc) · 8.54 KB
/
MovieCollectionExtension.cs
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
using System.Reflection;
using System.Text.RegularExpressions;
public static class MovieCollectionExtension
{
public enum Direction
{
L = 0,
R = 1
};
public static string PrettyPrint(this IMovieCollection collection, int nodeSpacing = 1, int nodeLabelWidth = 1, bool overflow = false, bool optimise = true)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
// Get private field "root"
var field = collection.GetType().GetField("root", BindingFlags.NonPublic | BindingFlags.Instance);
BTreeNode root = (BTreeNode)field!.GetValue(collection)!;
if (root == null)
{
return "Tree is empty";
}
Dictionary<int, List<(string text, uint directions)>> treeNodes = new();
// First pass, traverse tree
Queue<(BTreeNode, int, uint)> treeQueue = new();
treeQueue.Enqueue((root, 0, 0));
while (treeQueue.Count > 0)
{
(BTreeNode node, int depth, uint direction) = treeQueue.Dequeue();
if (!treeNodes.ContainsKey(depth)) treeNodes.Add(depth, new List<(string, uint)>());
treeNodes[depth].Add((node.Movie.Title, direction));
if (node.LChild is not null)
treeQueue.Enqueue((node.LChild, depth + 1, (direction << 1) + (uint)Direction.L));
if (node.RChild is not null)
treeQueue.Enqueue((node.RChild, depth + 1, (direction << 1) + (uint)Direction.R));
}
int treeDepth = treeNodes.Count;
int top = 2 * treeDepth - 2;
int margin = nodeSpacing;
string textToPrint = "";
Func<int, int, int, int, int> calculateLeft = (int margin, int node, int spacing, int labelWidth) => margin + node * (spacing + labelWidth);
// Second pass, generate text
foreach ((int depth, List<(string text, uint directions)> row) in treeNodes.Reverse())
{
uint currentNode = 0;
List<uint> nodesList = row.Select(x => x.directions).ToList();
Queue<uint> nodesQueue = new(nodesList);
string labelText = "";
string edgeText = "";
Queue<uint> parentNodesQueue = new();
if (depth > 0)
parentNodesQueue = new(treeNodes[depth - 1].Select(x => x.directions).ToList());
int parentMargin;
int parentSpacing;
parentMargin = 2 * margin;
parentSpacing = 2 * nodeSpacing + nodeLabelWidth;
int parentLeft = 0;
bool parentLeftDefined = false;
while (nodesQueue.Count > 0)
{
if (currentNode == nodesQueue.Peek())
{
int left = calculateLeft(margin, (int)currentNode, nodeSpacing, nodeLabelWidth);
if (row[0].text.Length > nodeLabelWidth)
{
if (!overflow)
labelText = InsertInto(labelText, row[0].text[..nodeLabelWidth], left);
else
labelText = InsertInto(labelText, ' ' + row[0].text + new string(' ', 100), left - 1);
}
else
{
if ((nodesQueue.Peek() & 1) == 0)
labelText = InsertInto(labelText, row[0].text + new string(' ', nodeLabelWidth - row[0].text.Length), left);
else
labelText = InsertInto(labelText, new string(' ', nodeLabelWidth - row[0].text.Length) + row[0].text, left);
}
if (depth > 0)
{
// Ignore any leaf nodes that precede the parent of the current node
while (parentNodesQueue.Count > 0)
if (parentNodesQueue.Peek() < (currentNode >> 1))
parentNodesQueue.Dequeue();
else
{
if (parentNodesQueue.Peek() == (currentNode >> 1))
parentLeftDefined = false;
break;
}
if (!parentLeftDefined)
{
parentLeft = calculateLeft(parentMargin, (int)parentNodesQueue.Dequeue(), parentSpacing, nodeLabelWidth);
parentLeftDefined = true;
}
// If LChild
if ((nodesQueue.Peek() & 1) == 0)
{
edgeText = InsertInto(edgeText, "╭", left);
if (parentLeft - left < 1)
edgeText = InsertInto(edgeText, "╯", left + nodeLabelWidth - 2);
else
edgeText = InsertInto(edgeText, new string('─', parentLeft - left - 1) + "╯", left + 1);
}
// If RChild
else
{
edgeText = InsertInto(edgeText, "╮", left + nodeLabelWidth - 1);
if (left - parentLeft < 1)
edgeText = InsertIntoReverse(edgeText, "╰", left + nodeLabelWidth - 1);
else
edgeText = InsertIntoReverse(edgeText, new string('─', left - parentLeft - 1) + "╰", left + nodeLabelWidth - 1);
}
}
nodesQueue.Dequeue();
row.RemoveAt(0);
}
currentNode++;
}
margin = parentMargin;
nodeSpacing = parentSpacing;
textToPrint += labelText + "\n" + edgeText + "\n";
}
if (optimise)
{
string output = Print(textToPrint);
return OptimiseTree(output);
}
else
return Print(textToPrint);
}
private static string OptimiseTree(string tree)
{
List<string> lines = tree.Split('\n').ToList();
List<string> outputLines = new(new string[lines.Count]);
List<char> columnList;
int lineLength = lines.Max(l => l.Length);
// pad all lines to equal length
for (int i = 0; i < lines.Count; i++)
if (lines[i].Length < lineLength)
lines[i] += new string(' ', lineLength - lines[i].Length + 1);
for (int columnNumber = 0; columnNumber < lineLength; columnNumber++)
{
bool addColumn = false;
foreach (string line in lines)
if (line[columnNumber] != ' ' && line[columnNumber] != '─')
{
addColumn = true;
break;
}
if (addColumn)
{
columnList = lines.Select(l => l[columnNumber]).ToList();
for (int j = 0; j < lines.Count; j++)
outputLines[j] += columnList[j];
}
}
return string.Join("\n", outputLines);
}
private static string Print(string text)
{
List<string> lines = text.Split('\n').Reverse().ToList();
lines.RemoveAll(s => string.IsNullOrEmpty(s));
int minSpaces = int.MaxValue;
foreach (string line in lines)
if (line.TakeWhile(c => c == ' ').Count() < minSpaces)
minSpaces = line.TakeWhile(c => c == ' ').Count();
for (int i = 0; i < lines.Count; i++)
lines[i] = lines[i].TrimEnd();
string pattern = @$"^( {{{minSpaces}}})";
return Regex.Replace(string.Join('\n', lines), pattern, "", RegexOptions.Multiline);
}
public static string InsertInto(string text, string newText, int index)
{
if (text.Length <= index + newText.Length)
text += new string(' ', index + newText.Length - text.Length + 1);
return text[..index] + newText + text[(index + newText.Length)..];
}
public static string InsertIntoReverse(string text, string newText, int index)
{
index -= newText.Length;
if (index < 0)
throw new IndexOutOfRangeException("Reverse inserted text goes beyond index 0.");
if (text.Length <= index + newText.Length)
text += new string(' ', index + newText.Length - text.Length + 1);
char[] reversed = newText.ToCharArray();
Array.Reverse(reversed);
return text[..index] + new string(reversed) + text[(index + newText.Length)..];
}
}