-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortVisualizer.cs
executable file
·134 lines (114 loc) · 4.54 KB
/
SortVisualizer.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
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace SortVisualizer
{
// Alias definitions
using DatasetOrderer = Func<int, int, int>;
using DatasetSorter = Action<Dataset>;
using DatasetAction = Tuple<DatasetActionType, int, int>;
public class SortVisualizer
{
public enum VisualizerDirection
{
FORWARD,
BACKWARD
}
// Data values, ordering, sorting variables
public Dataset Data { get; }
public DatasetOrderer Order { get; }
public DatasetSorter Sort { get; }
// Graphics variables
public Rectangle Bounds { get; set; }
public DataBarTexture Texture { get; set; }
// Logic variables
public VisualizerDirection Direction { get; set; } = VisualizerDirection.FORWARD;
// Internal logic variables
private DatasetAction[] SortActions;
private DatasetAction CurrentSortAction;
private int SortActionIndex = 0;
public SortVisualizer(Dataset data, DatasetOrderer orderer, DatasetSorter sorter, Rectangle bounds, DataBarTexture texture)
{
Data = data;
Order = orderer;
Sort = sorter;
Bounds = bounds;
Texture = texture;
RecordOrderSort();
}
private void RecordOrderSort()
{
Data.ClearRecord();
Data.StartRecord();
for (int i = 0; i < Data.Count; i++)
{
Data.SwapItems(i, Order.Invoke(Data.Count, i));
}
Sort.Invoke(Data);
Data.StopRecord();
SortActions = new DatasetAction[Data.Record.Count];
Data.Record.CopyTo(SortActions);
Data.PerformActions(SortActions, reversed: true);
}
public void Update(GameTime gameTime)
{
switch (Direction)
{
case VisualizerDirection.FORWARD:
if (SortActionIndex < SortActions.Length)
{
CurrentSortAction = SortActions[SortActionIndex];
Data.PerformAction(SortActions[SortActionIndex++]);
}
else
CurrentSortAction = null;
break;
case VisualizerDirection.BACKWARD:
if (SortActionIndex > 0)
{
Data.PerformAction(SortActions[--SortActionIndex]);
CurrentSortAction = SortActions[SortActionIndex];
}
else
CurrentSortAction = null;
break;
}
}
public void Draw(SpriteBatch spriteBatch)
{
if (Texture != null)
{
// Deal with determining special colors based on current action.
Color specialColor = Color.White;
if (CurrentSortAction != null)
{
switch (CurrentSortAction.Item1)
{
case DatasetActionType.ACCESS:
specialColor = Color.Red;
break;
case DatasetActionType.COMPARE:
specialColor = Color.Green;
break;
case DatasetActionType.SWAP:
specialColor = Color.Blue;
break;
}
}
double barWidth = (double)Bounds.Width / Data.Count;
for (int i = 0; i < Data.Count; i++)
{
int barHeight = (int)(Bounds.Height * Data[i]);
int barX1 = (int)(barWidth * i) + Bounds.X;
int barX2 = (int)(barWidth * (i + 1)) + Bounds.X;
int barY = Bounds.Bottom - barHeight;
if (CurrentSortAction != null && (i == CurrentSortAction.Item2 || i == CurrentSortAction.Item3))
Texture.DrawBar(spriteBatch, new Rectangle(barX1, barY, barX2 - barX1, barHeight), specialColor);
else
Texture.DrawBar(spriteBatch, new Rectangle(barX1, barY, barX2 - barX1, barHeight), Color.White);
}
}
}
}
}