-
Notifications
You must be signed in to change notification settings - Fork 1
/
DequeControl.xaml.cs
124 lines (108 loc) · 3.94 KB
/
DequeControl.xaml.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
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using TreeContainer;
namespace Immutable_Deque_visualization
{
public partial class DequeControl : UserControl
{
public static readonly DependencyProperty DequeProperty =
DependencyProperty.Register("Deque", typeof(IDeque<string>), typeof(DequeControl), new PropertyMetadata(null, null, CoerceDeque));
public IDeque<string> Deque
{
get { return (IDeque<string>)GetValue(DequeProperty); }
set { SetValue(DequeProperty, value); }
}
public DequeControl()
{
InitializeComponent();
}
static object CoerceDeque(DependencyObject d, object baseValue)
{
// I don't like this, but I didn't find a better way
((DequeControl)d).Visualize((IDeque<string>)baseValue);
return baseValue;
}
public void Visualize()
{
Visualize(Deque);
}
void Visualize<T>(IDeque<T> iDeque)
{
treeContainer.Clear();
Visualize(iDeque, null);
}
void Visualize<T>(IDeque<T> iDeque, TreeNode parent)
{
if (iDeque == null)
return;
var dequeNode = AddDequeNode(iDeque, parent);
if (iDeque is Deque<T>.SingleDeque)
{
var singleDeque = (Deque<T>.SingleDeque)iDeque;
VisualizeDynamic(singleDeque.Item, dequeNode);
}
else if (iDeque is Deque<T>)
{
var deque = (Deque<T>)iDeque;
Visualize(deque.Left, dequeNode);
Visualize(deque.Middle, dequeNode);
Visualize(deque.Right, dequeNode);
}
}
void Visualize<T>(Deque<T>.Dequelette dequelette, TreeNode parent)
{
var dequeletteNode = AddDequeletteNode(dequelette, parent);
if (dequelette is Deque<T>.One)
{
var one = (Deque<T>.One)dequelette;
VisualizeDynamic(one.V1, dequeletteNode);
}
else if (dequelette is Deque<T>.Two)
{
var two = (Deque<T>.Two)dequelette;
VisualizeDynamic(two.V1, dequeletteNode);
VisualizeDynamic(two.V2, dequeletteNode);
}
else if (dequelette is Deque<T>.Three)
{
var three = (Deque<T>.Three)dequelette;
VisualizeDynamic(three.V1, dequeletteNode);
VisualizeDynamic(three.V2, dequeletteNode);
VisualizeDynamic(three.V3, dequeletteNode);
}
else if (dequelette is Deque<T>.Four)
{
var four = (Deque<T>.Four)dequelette;
VisualizeDynamic(four.V1, dequeletteNode);
VisualizeDynamic(four.V2, dequeletteNode);
VisualizeDynamic(four.V3, dequeletteNode);
VisualizeDynamic(four.V4, dequeletteNode);
}
}
void Visualize(object value, TreeNode parent)
{
AddValueNode(value, parent);
}
void VisualizeDynamic(dynamic value, TreeNode parent)
{
Visualize(value, parent);
}
private TreeNode AddDequeletteNode<T>(Deque<T>.Dequelette dequelette, TreeNode parent)
{
var valueNode = new DequeletteNode<T>(dequelette);
return treeContainer.AddNode(valueNode, parent);
}
TreeNode AddDequeNode<T>(IDeque<T> deque, TreeNode parent)
{
var dequeNode = new DequeNode<T>(deque);
return parent == null ? treeContainer.AddRoot(dequeNode) : treeContainer.AddNode(dequeNode, parent);
}
void AddValueNode<T>(T value, TreeNode parent)
{
var valueNode = new ValueNode<T>(value);
treeContainer.AddNode(valueNode, parent);
}
}
}