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
|
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <ctime>
#include <iomanip>
#include <cstdlib>
bool get_time(const std::string& s, time_t& time)
{
tm date;
date.tm_isdst = 0;
std::istringstream iss(s);
std::string line;
char c;
if(!(iss >> date.tm_year >> c)) return false;
if(!(iss >> date.tm_mon >> c)) return false;
if(!(iss >> date.tm_mday >> c)) return false;
if(!(iss >> date.tm_hour >> c)) return false;
if(!(iss >> date.tm_min >> c)) return false;
if(!(iss >> date.tm_sec)) return false;
date.tm_year -= 1900;
time = mktime(&date);
return true;
}
bool time_diff(const std::string& a, const std::string& z, time_t& diff)
{
time_t atime;
time_t ztime;
if(get_time(a, atime) && get_time(z, ztime))
{
diff = std::abs(ztime - atime);
return true;
}
return false;
}
struct MyPred
{
std::string a;
std::string x;
std::string y;
std::string z;
MyPred(const std::string& a, const std::string& x, const std::string& y, const std::string& z): a(a), x(x), y(y), z(z) {}
bool operator==(const MyPred& p) const
{
return x == p.x && y == p.y && z == p.z; // a == p.a &&
}
bool operator<(const MyPred& p) const
{
//if(a < p.a) return true;
//if(a > p.a) return false;
if(x < p.x) return true;
if(x > p.x) return false;
if(y < p.y) return true;
if(y > p.y) return false;
if(z < p.z) return true;
if(z > p.z) return false;
return false;
}
};
int main()
{
std::vector<MyPred>* vPred = new std::vector<MyPred>;
vPred->push_back(MyPred("2010/01/01 00:00:00", "a", "a", "2010/01/01 00:00:00"));
vPred->push_back(MyPred("2010/01/02 00:00:00", "a", "b", "2010/01/02 00:00:00"));
vPred->push_back(MyPred("2010/01/03 00:00:00", "b", "a", "2010/01/03 00:00:00"));
vPred->push_back(MyPred("2010/01/04 00:08:00", "b", "b", "2010/01/04 00:10:00"));
vPred->push_back(MyPred("2010/01/04 00:11:00", "b", "b", "2010/01/04 00:10:00"));
vPred->push_back(MyPred("2010/01/04 00:14:00", "b", "b", "2010/01/04 00:10:00"));
// The values need to be in order for equal_range() to work
std::sort(vPred->begin(), vPred->end());
std::vector<MyPred> uPred; // values that were always unique
std::vector<MyPred> dPred; // values that were duplicated
std::pair<std::vector<MyPred>::iterator, std::vector<MyPred>::iterator> ret;
for(std::vector<MyPred>::iterator i = vPred->begin(); i != vPred->end(); i = ret.second)
{
ret = std::equal_range(i, vPred->end(), *i);
if(ret.second - ret.first != 1) // duplicates
{
time_t diff; // general diff register
time_t min_diff; // register smallest difference in time
std::vector<MyPred>::iterator min_iter; // register corresponding iterator
std::vector<MyPred>::iterator j; // range iterator
// initialise min register to the first difference
// in our range
time_diff(ret.first->a, ret.first->z, min_diff);
// iterate over the range of duplicates finding a
// the smallest difference as we go and noting the
// corresponding iterator
for(min_iter = j = ret.first; j != ret.second; ++j)
{
// get difference
time_diff(j->a, j->z, diff);
// is it smaller than our current minimum?
if(diff < min_diff)
{
min_diff = diff; // keep it as our new minimum
min_iter = j; // remember the iterator to the smallest difference so far
}
}
// push the value recorded to be smallest onto our vector
dPred.push_back(*min_iter);
}
else if(ret.second - ret.first == 1)
{
uPred.push_back(*i);
}
}
std::cout << "vPred: Sorted input\n";
for(std::vector<MyPred>::iterator i = vPred->begin(); i != vPred->end(); ++i)
{
std::cout << "[" << i->a << ", " << i->x << ", " << i->y << ", " << i->z << "]" << '\n';
}
std::cout << "dPred: Only the values that were duplicated\n";
for(std::vector<MyPred>::iterator i = dPred.begin(); i != dPred.end(); ++i)
{
std::cout << "[" << i->a << ", " << i->x << ", " << i->y << ", " << i->z << "]" << '\n';
}
std::cout << "uPred: Only the values that were unique\n";
for(std::vector<MyPred>::iterator i = uPred.begin(); i != uPred.end(); ++i)
{
std::cout << "[" << i->a << ", " << i->x << ", " << i->y << ", " << i->z << "]" << '\n';
}
delete vPred;
}
|
vPred: Sorted input
[2010/01/01 00:00:00, a, a, 2010/01/01 00:00:00]
[2010/01/02 00:00:00, a, b, 2010/01/02 00:00:00]
[2010/01/03 00:00:00, b, a, 2010/01/03 00:00:00]
[2010/01/04 00:08:00, b, b, 2010/01/04 00:10:00]
[2010/01/04 00:11:00, b, b, 2010/01/04 00:10:00]
[2010/01/04 00:14:00, b, b, 2010/01/04 00:10:00]
dPred: Only the values that were duplicated
[2010/01/04 00:11:00, b, b, 2010/01/04 00:10:00]
uPred: Only the values that were unique
[2010/01/01 00:00:00, a, a, 2010/01/01 00:00:00]
[2010/01/02 00:00:00, a, b, 2010/01/02 00:00:00]
[2010/01/03 00:00:00, b, a, 2010/01/03 00:00:00] | |