Skip to content
Snippets Groups Projects
Commit 34b4eedd authored by Tom Fischer's avatar Tom Fischer
Browse files

Coping points within PointVec::makePntsUnique() is faster.

parent f93bcffd
No related branches found
No related tags found
No related merge requests found
...@@ -41,7 +41,8 @@ PointVec::PointVec (const std::string& name, std::vector<Point*>* points, ...@@ -41,7 +41,8 @@ PointVec::PointVec (const std::string& name, std::vector<Point*>* points,
makePntsUnique (_data_vec, _pnt_id_map, rel_eps); makePntsUnique (_data_vec, _pnt_id_map, rel_eps);
if (number_of_all_input_pnts - _data_vec->size() > 0) if (number_of_all_input_pnts - _data_vec->size() > 0)
WARN("PointVec::PointVec(): there are %d double points.", number_of_all_input_pnts - _data_vec->size()); WARN("PointVec::PointVec(): there are %d double points.",
number_of_all_input_pnts - _data_vec->size());
} }
PointVec::~PointVec () PointVec::~PointVec ()
...@@ -114,7 +115,7 @@ double PointVec::getShortestPointDistance () const ...@@ -114,7 +115,7 @@ double PointVec::getShortestPointDistance () const
return sqrt (_sqr_shortest_dist); return sqrt (_sqr_shortest_dist);
} }
void PointVec::makePntsUnique (std::vector<GeoLib::Point*>* pnt_vec, void PointVec::makePntsUnique (std::vector<GeoLib::Point*> *& pnt_vec,
std::vector<size_t> &pnt_id_map, double eps) std::vector<size_t> &pnt_id_map, double eps)
{ {
size_t n_pnts_in_file (pnt_vec->size()); size_t n_pnts_in_file (pnt_vec->size());
...@@ -178,21 +179,24 @@ void PointVec::makePntsUnique (std::vector<GeoLib::Point*>* pnt_vec, ...@@ -178,21 +179,24 @@ void PointVec::makePntsUnique (std::vector<GeoLib::Point*>* pnt_vec,
BaseLib::Quicksort<std::size_t, GeoLib::Point*> (perm, 0, n_pnts_in_file, *pnt_vec); BaseLib::Quicksort<std::size_t, GeoLib::Point*> (perm, 0, n_pnts_in_file, *pnt_vec);
// remove the second, third, ... occurrence from vector // remove the second, third, ... occurrence from vector
for (size_t k(0); k < n_pnts_in_file; k++) for (size_t k(0); k < n_pnts_in_file; k++) {
if (pnt_id_map[k] < k) if (pnt_id_map[k] < k)
{ {
delete (*pnt_vec)[k]; delete (*pnt_vec)[k];
(*pnt_vec)[k] = NULL; (*pnt_vec)[k] = NULL;
} }
// remove NULL-ptr from vector
for (std::vector<GeoLib::Point*>::iterator it(pnt_vec->begin()); it != pnt_vec->end(); )
{
if (*it == NULL)
it = pnt_vec->erase (it);
else
it++;
} }
std::vector<GeoLib::Point*>* tmp_pnt_vec(new std::vector<GeoLib::Point*>);
// erasing elements from a vector is expensive, for this reason copy non nullptr to new vector
for (std::vector<GeoLib::Point*>::iterator it(pnt_vec->begin()); it != pnt_vec->end(); ) {
if (*it != nullptr)
tmp_pnt_vec->push_back(*it);
it++;
}
std::swap(tmp_pnt_vec, pnt_vec);
delete tmp_pnt_vec;
// renumber id-mapping // renumber id-mapping
size_t cnt (0); size_t cnt (0);
for (size_t k(0); k < n_pnts_in_file; k++) for (size_t k(0); k < n_pnts_in_file; k++)
...@@ -205,18 +209,6 @@ void PointVec::makePntsUnique (std::vector<GeoLib::Point*>* pnt_vec, ...@@ -205,18 +209,6 @@ void PointVec::makePntsUnique (std::vector<GeoLib::Point*>* pnt_vec,
else else
pnt_id_map[k] = pnt_id_map[pnt_id_map[k]]; pnt_id_map[k] = pnt_id_map[pnt_id_map[k]];
} }
// KR correct renumbering of indices
// size_t cnt(0);
// std::map<size_t, size_t> reg_ids;
// for (size_t k(0); k < n_pnts_in_file; k++) {
// if (pnt_id_map[k] == k) {
// reg_ids.insert(std::pair<size_t, size_t>(k, cnt));
// cnt++;
// } else reg_ids.insert(std::pair<size_t, size_t>(k, reg_ids[pnt_id_map[k]]));
// }
// for (size_t k(0); k < n_pnts_in_file; k++)
// pnt_id_map[k] = reg_ids[k];
} }
void PointVec::calculateShortestDistance () void PointVec::calculateShortestDistance ()
...@@ -236,6 +228,4 @@ std::vector<GeoLib::Point*>* PointVec::getSubset(const std::vector<size_t> &subs ...@@ -236,6 +228,4 @@ std::vector<GeoLib::Point*>* PointVec::getSubset(const std::vector<size_t> &subs
return new_points; return new_points;
} }
} // end namespace } // end namespace
...@@ -108,7 +108,7 @@ public: ...@@ -108,7 +108,7 @@ public:
std::vector<GeoLib::Point*>* getSubset(const std::vector<std::size_t> &subset); std::vector<GeoLib::Point*>* getSubset(const std::vector<std::size_t> &subset);
private: private:
void makePntsUnique (std::vector<GeoLib::Point*>* pnt_vec, std::vector<std::size_t> &pnt_id_map, double eps = sqrt(std::numeric_limits<double>::min())); void makePntsUnique (std::vector<GeoLib::Point*>*& pnt_vec, std::vector<std::size_t> &pnt_id_map, double eps = sqrt(std::numeric_limits<double>::min()));
/** copy constructor doesn't have an implementation */ /** copy constructor doesn't have an implementation */
// compiler does not create a (possible unwanted) copy constructor // compiler does not create a (possible unwanted) copy constructor
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment