-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortingMachine.pde
182 lines (154 loc) · 3.73 KB
/
SortingMachine.pde
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
class SortingMachine {
private SortingMachineConfig config;
private Map<String, DataSet> dataById = Maps.newHashMap();
private List<SortJob> sortJobs = Lists.newArrayList();
SortingMachine(SortingMachineConfig config) {
this.config = config;
}
SortingMachine(String file) {
try {
ObjectMapper om = new ObjectMapper();
Map<String, Object> config = om.readValue(
new File(file),
Map.class);
System.out.println("config ->\n" + config);
this.config = new SortingMachineConfig(config);
}
catch(Exception e) {
e.printStackTrace(System.err);
throw new RuntimeException(e);
}
for(SortJobConfig c : config.getSortJobConfigs()) {
SortJob job = new SortJob(this, c);
job.plan();
System.out.println("adding job --> \n" + job + "\n");
sortJobs.add(job);
}
}
void setup() {
smooth();
}
Drawable getRenderer() {
String r = this.config.getRenderer();
if(r.equals("animated")) {
return new AnimatedRenderer(this);
}
else if(r.equals("sheet")) {
return new StaticRenderer(this);
}
else if(r.equals("arcs")) {
return new ArcRenderer(this);
}
return new BannerRenderer(this);
}
DataSet getData(String id) {
if(dataById.containsKey(id)) {
return dataById.get(id);
}
DataConfig dc = config.getDataConfig(id);
return new DataSet(dc);
}
List<SortJob> getSortJobs() {
return this.sortJobs;
}
int getNoteHeight() {
return config.getNoteHeight();
}
float getStepWidth() {
return config.getStepWidth();
}
WindowConfig getWindow() {
return config.getWindow();
}
}
class SortJob {
private String label;
private Sorter sorter;
private DataSet data;
private List<Instruction> instructions;
SortJob(SortingMachine m, SortJobConfig c) {
this.label = c.getLabel();
if(c.getSort().equals("quicksort")) {
this.sorter = new QuickSorter();
}
else if(c.getSort().equals("bubblesort")) {
this.sorter = new BubbleSorter();
}
else {
throw new RuntimeException("sorter " + c.getSort() + " not found");
}
this.data = m.getData(c.getDataRef());
}
public String getLabel() {
return this.label;
}
public void plan() {
instructions = Lists.newLinkedList();
instructions = sorter.plan(data.getValues());
}
public List<Instruction> getInstructions() {
return instructions;
}
public DataSet getData() {
return this.data;
}
public String toString() {
return Objects.toStringHelper(this)
.add("label", label)
.add("sorter", sorter)
.add("data", data)
.toString();
}
}
class DataSet {
String label;
int[] values;
public DataSet(DataConfig config) {
this.label = config.getLabel();
if(config.valuesInlined()) {
this.values = copy(config.getValues());
}
else {
RandomDataConfig r = config.getRandomDataConfig();
int[] pool = r.getUniqueValues();
this.values = new int[r.getSize()];
Random rand = new Random();
for(int i = 0; i < values.length; i++) {
this.values[i] = pool[Math.abs(rand.nextInt())%pool.length];
}
}
String[] steps = config.getSteps();
if(steps != null && steps.length > 0) {
List<Integer> list = Lists.newArrayListWithCapacity(values.length);
for(int i : values) {
list.add(i);
}
for(String step : config.getSteps()) {
if(step.equals("shuffle")) {
Collections.shuffle(list);
}
else if(step.equals("reverse")) {
Collections.reverse(list);
}
else if(step.equals("sort")) {
Collections.sort(list);
}
}
for(int i = 0; i < list.size(); i++) {
values[i] = list.get(i);
}
}
}
public String getLabel() {
return label;
}
public int[] getValues() {
return copy(values);
}
public String toString() {
return Objects.toStringHelper(this)
.add("label", this.label)
.add("values", join(this.values, ", "))
.toString();
}
}