Skip to content

Instantly share code, notes, and snippets.

@shivallan
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save shivallan/92cbb3dc413d5fea38cf to your computer and use it in GitHub Desktop.

Select an option

Save shivallan/92cbb3dc413d5fea38cf to your computer and use it in GitHub Desktop.
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <sys/time.h>
void event(std::vector<char> &v, std::vector<unsigned long long> &sp, std::vector<unsigned long long> &ep)
{
char pre_val(0), cur_val(0);
unsigned long long pos(0);
for (std::vector<char>::iterator i = v.begin(); i != v.end(); ++i)
{
cur_val = *i;
if ((cur_val != pre_val) && (cur_val))
{
pos = std::distance(v.begin(), i);
sp.push_back(pos);
if (pre_val) ep.push_back(pos);
pre_val = cur_val;
}
}
ep.push_back(std::distance(v.begin(), v.end() - 1));
}
void period(std::vector<char> &v, std::vector<unsigned long long> &sp, std::vector<unsigned long long> &ep)
{
char pre_val(0), cur_val(0);
unsigned long long pos(0);
for (std::vector<char>::iterator i = v.begin(); i != v.end(); ++i)
{
cur_val = *i;
if (cur_val != pre_val)
{
pos = std::distance(v.begin(), i);
if (cur_val) sp.push_back(pos);
if (pre_val) ep.push_back(pos);
pre_val = cur_val;
}
}
if (cur_val) ep.push_back(std::distance(v.begin(), v.end() - 1));
}
void new_trajectory (std::vector<char> &v, std::vector<double> &tr, std::vector<double> &new_tr, double penalty, char type)
{
std::vector<unsigned long long> sp, ep;
// "Period" case
if (type == 1) period(v, sp, ep);
// "Event" case
else if (type == 0) event(v, sp, ep);
unsigned long long cur_sp (*sp.begin()), cur_ep (0), cur_tr_pos(0);
char cur_seria (0);
double p(penalty);
double temp(0);
std::vector<double>::iterator i_tr (tr.begin());
std::vector<unsigned long long>::iterator i_sp(sp.begin());
while (i_tr != tr.end())
{
cur_tr_pos = std::distance(tr.begin(), i_tr);
if (cur_tr_pos != cur_sp)
{
new_tr.push_back(0);
}
else
{
cur_ep = *(ep.begin() + std::distance(sp.begin(), i_sp));
cur_seria = *(v.begin() + cur_sp);
for (unsigned long long j = cur_sp; j < cur_ep; ++j)
{
temp = (double)cur_seria * (*(tr.begin() + j) - *(tr.begin() + cur_sp));
temp -= p;
temp > 0.0 ? new_tr.push_back(1) : temp < 0.0 ? new_tr.push_back(-1) : new_tr.push_back(0);
p = 0.0;
}
p = penalty;
i_tr = tr.begin() + cur_ep - 1;
if (!(cur_ep - cur_sp))
{
p > 0.0 ? new_tr.push_back(-1) : p < 0.0 ? new_tr.push_back(1) : new_tr.push_back(0);
++i_tr;
}
++i_sp;
if (i_sp != sp.end()) cur_sp = *i_sp;
}
++i_tr;
}
}
int main()
{
std::vector<char> v;
std::vector<unsigned long long> sp, ep;
timeval t1, t2;
double elapsedTime;
// Creation of input vector like [0 0 0 1 0 0 -1 0 0 -1 -1 1 0 0 0 1 0 0 1 -1 0 0 1]
v.push_back(0); v.push_back(0);
v.push_back(0); v.push_back(1);
v.push_back(0); v.push_back(0);
v.push_back(-1); v.push_back(0);
v.push_back(0);
v.push_back(-1); v.push_back(-1);
v.push_back(1); v.push_back(0);
v.push_back(0); v.push_back(0);
v.push_back(1); v.push_back(0);
v.push_back(0); v.push_back(1);
v.push_back(-1); v.push_back(0);
v.push_back(0); v.push_back(1);
std::vector<double> tr, new_tr;
// Creation of input trajectory like [0.3 1.0 2.6 0.1 -0.5 -3 -6 -7 -3 -3.6 -2 1 0 0 0 1 0 0 1 -1 0 0 1]
tr.push_back(0.3); tr.push_back(1.0);
tr.push_back(2.6); tr.push_back(0.1);
tr.push_back(-0.5); tr.push_back(-3.0);
tr.push_back(-6); tr.push_back(-7);
tr.push_back(-3); tr.push_back(-3.6);
tr.push_back(-2); tr.push_back(1);
tr.push_back(0); tr.push_back(0);
tr.push_back(0); tr.push_back(1);
tr.push_back(0); tr.push_back(0);
tr.push_back(1); tr.push_back(-1);
tr.push_back(0); tr.push_back(0);
tr.push_back(1);
std::cout << "Input vector:\n";
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
std::cout << "Input trajectory:\n";
std::copy(tr.begin(), tr.end(), std::ostream_iterator<double>(std::cout, " "));
std::cout << "\n";
// start timer
gettimeofday(&t1, NULL);
new_trajectory(v, tr, new_tr, 0.5, 0);
// stop timer
gettimeofday(&t2, NULL);
// compute and print the elapsed time in millisec
elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; // sec to ms
elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0; // us to ms
std::cout << "Output trajectory for event =>\n";
std::copy(new_tr.begin(), new_tr.end(), std::ostream_iterator<double>(std::cout, " "));
std::cout << "\n";
std::cout << "Event => elapsed time " << elapsedTime << " ms \n";
// Clear the new_tr vector
std::vector<double>().swap(new_tr);
// start timer
gettimeofday(&t1, NULL);
new_trajectory(v, tr, new_tr, -0.5, 1);
// stop timer
gettimeofday(&t2, NULL);
// compute and print the elapsed time in millisec
elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; // sec to ms
elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0; // us to ms
std::cout << "Output trajectory for period =>\n";
std::copy(new_tr.begin(), new_tr.end(), std::ostream_iterator<double>(std::cout, " "));
std::cout << "\n";
std::cout << "Period => elapsed time " << elapsedTime << " ms \n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment