-
Notifications
You must be signed in to change notification settings - Fork 0
/
fold.cc
58 lines (47 loc) · 952 Bytes
/
fold.cc
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
#include <iostream>
#include <vector>
struct Entity
{
int key;
bool operator==(Entity const & other) const
{
return key == other.key;
}
};
typedef std::vector<Entity> Entities;
typedef std::vector<Entities> FoldedEntities;
FoldedEntities fold(Entities const & entities)
{
std::vector<bool> visited(entities.size(), false);
FoldedEntities result;
Entity const * example = 0;
if (entities.empty()) {
return result;
}
bool complete = false;
while (complete) {
complete = true;
example = 0;
for (size_t i = 0; i < entities.size(); ++i) {
if (!visited[i]) {
complete = false;
if (example == 0) {
result.push_back(Entities());
example = &entities[i];
visited[i] = true;
result.back().push_back(entities[i]);
} else {
if (*example == entities[i]) {
visited[i] = true;
result.back().push_back(entities[i]);
}
}
}
}
}
return result;
}
int main()
{
return 0;
}