-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.cpp
More file actions
276 lines (237 loc) · 7.3 KB
/
entry.cpp
File metadata and controls
276 lines (237 loc) · 7.3 KB
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
//Copyright 2015 Fred Douglas
//
//This file is part of HumanComparisonSort.
//
//HumanComparisonSort is free software; you can redistribute it and / or
//modify it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//
//The full text of the license can be found at:
//http://www.gnu.org/licenses/gpl.html
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <assert.h>
#include <unistd.h>
using std::cout;
using std::cerr;
using std::endl;
#include "entry.h"
int g_num_entries_created = 0;
std::vector<Entry*> generateTestItems(int up_to)
{
srand(rand());
std::vector<Entry*> to_ret;
for(int i=0; i<up_to; i++)
to_ret.push_back(new Entry(i, Testing));
//now shuffle!
for(int i=0; i<to_ret.size(); i++)
{
int dest = i + rand() % (to_ret.size() - i);
Entry* temp = to_ret[i];
to_ret[i] = to_ret[dest];
to_ret[dest] = temp;
}
return to_ret;
}
void Entry::dump() const
{
if(my_type == Testing)
cout << "VAL:" << value << ",COMPS=" << my_comparisons << endl;
else if(my_type == DataDir)
cout << my_target << endl;
else if(my_type == TextFile)
cout << my_text << endl;
else
cerr << "Unhandled enum type in Entry::dump()!" << endl;
}
void Entry::display() const
{
if(my_type == Testing)
cout << "VAL:" << value << ",COMPS=" << my_comparisons << ", ";
else if(my_type == DataDir)
system( (my_command+" "+my_target).c_str() );
else if(my_type == TextFile)
cout << my_text;
else
cerr << "Unhandled enum type in Entry::display()!" << endl;
}
Entry::Entry(int val, EntryType the_type)
{
my_type = the_type;
my_comparisons = 0;
orig_order_id = g_num_entries_created++; //not thread safe!
value = val;
}
Entry::Entry(std::string the_command, std::string the_target, EntryType the_type)
{
my_type = the_type;
my_comparisons = 0;
orig_order_id = g_num_entries_created++; //not thread safe!
my_command = the_command;
my_target = the_target;
}
Entry::Entry(std::string a_string, EntryType the_type)
{
my_type = the_type;
my_comparisons = 0;
orig_order_id = g_num_entries_created++; //not thread safe!
if(my_type == TextFile)
my_text = a_string;
else if(my_type == DataDir)
{
my_command = "xdg-open ";
my_target = a_string;
}
}
//Make each line in a text file into a separate TextEntry item, and return them all
std::vector<Entry*> processTextFile(std::string file_name)
{
std::vector<Entry*> all_items;
std::ifstream file_reader(file_name.c_str());
std::string line_buf;
while(std::getline(file_reader, line_buf))
if(line_buf.length() > 0)
all_items.push_back(new Entry(line_buf, TextFile));
return all_items;
}
//Given a target data directory structure, asks a perl script to recursively collect all of the filepaths in it
//and print them for us, separated by newlines. Make a CommandEntry out of each one, and return them all.
std::vector<Entry*> processDataDir(std::string dir_name)
{
std::vector<Entry*> all_items;
FILE* perl_pipe = popen((std::string("perl readdir.pl ")+dir_name).c_str(), "r");
size_t line_getter_len = 500;
char* line_getter = (char*)malloc(line_getter_len);
memset(line_getter, 0, line_getter_len);
while(getline(&line_getter, &line_getter_len, perl_pipe) > 0)
{
if(strchr(line_getter, '\n'))
*strchr(line_getter, '\n') = 0;
if(strlen(line_getter) > 0)
all_items.push_back(new Entry(line_getter, DataDir));
}
pclose(perl_pipe);
free(line_getter);
return all_items;
}
bool Entry::actualCompareLT(Entry& other)
{
if(my_type == Testing)
{
my_comparisons++;
other.my_comparisons++;
bool result = (value <= other.value);
//cerr << "Comparing " << value << " and " << other.value << ", result will be " << (result ? "<" : ">") << endl;
return result;
}
else if(my_type == DataDir)
{
unlink("compare_aaaaaaaaaaaaaaa");
unlink("compare_bbbbbbbbbbbbbbb");
symlink(my_target.c_str(), "compare_aaaaaaaaaaaaaaa");
symlink(other.my_target.c_str(), "compare_bbbbbbbbbbbbbbb");
system((my_command + "compare_aaaaaaaaaaaaaaa").c_str());
system((other.my_command + "compare_bbbbbbbbbbbbbbb").c_str());
DATAAGAINPLEASE:
std::string reader;
cout << "(a) " << my_text << endl << "(b) " << other.my_text << endl << "or (xa) or (xb) or (xab) to remove a, b, or both: " << std::flush;
std::getline(std::cin, reader);
bool ret;
if(reader == "a")
ret = true;
else if(reader == "b")
ret = false;
else
{
cout << "a or b only for now, please!" << endl;
goto DATAAGAINPLEASE;
}
system("killall gwenview");
return ret;
}
else if(my_type == TextFile)
{
TEXTAGAINPLEASE:
std::string reader;
cout << "(a) " << my_text << endl << "(b) " << other.my_text << endl << "or (xa) or (xb) or (xab) to remove a, b, or both: " << std::flush;
std::getline(std::cin, reader);
if(reader == "a")
return true;
else if(reader == "b")
return false;
else
{
cout << "a or b only for now, please!" << endl;
goto TEXTAGAINPLEASE;
}
}
else
cerr << "Unhandled enum type in Entry::operator<=()!" << endl;
assert(false);
return false;
}
void Entry::addMeToAllWorse(std::vector<Entry*> superior_ones)
{
for(Entry* each : superior_ones)
{
each->worse_than_me[orig_order_id] = this;
better_than_me[each->orig_order_id] = each;
}
}
bool Entry::operator<=(Entry& other) //non-const ref and non-const function so we can count comparisons
{
#ifdef _MEMOIZE_COMPARISONS_
auto found_better = better_than_me.find(other.orig_order_id);
auto found_worse = worse_than_me.find(other.orig_order_id);
if(found_better != better_than_me.end())
return false;
else if(found_worse != worse_than_me.end())
return true;
else //if(not in either found_better or found_worse)
{
//two sets: superior = {this + this->better_than_me} and inferior = {other + other->worse_than_me}
std::vector<Entry*> superior; //each item should have everything in inferior in its worse_than_me
std::vector<Entry*> inferior; //each item should have everything in superior in its better_than_me
bool better_than_other = actualCompareLT(other);
if(better_than_other)
{
superior.push_back(this);
for(auto& each : better_than_me)
superior.push_back(each.second);
inferior.push_back(&other);
for(auto& each : other.worse_than_me)
inferior.push_back(each.second);
}
else //not better than other
{
inferior.push_back(this);
for(auto& each : worse_than_me)
inferior.push_back(each.second);
superior.push_back(&other);
for(auto& each : other.better_than_me)
superior.push_back(each.second);
}
for(Entry* each : inferior)
each->addMeToAllWorse(superior);
return better_than_other;
}
#else //(not) _MEMOIZE_COMPARISONS_
return actualCompareLT(other);
#endif
}
bool Entry::operator<(Entry& other) //non-const ref and non-const function so we can count comparisons
{
return ((*this) <= other); //Equality is not allowed, so they're the same.
}
bool Entry::operator>(Entry& other) //non-const ref and non-const function so we can count comparisons
{
return !((*this) < other); //Equality is not allowed, so they're the same.
}