Skip to content

Instantly share code, notes, and snippets.

@GilesBathgate
Created January 28, 2023 16:33
Show Gist options
  • Save GilesBathgate/8ac8e314df53ec844e385ebd56ff071d to your computer and use it in GitHub Desktop.
Save GilesBathgate/8ac8e314df53ec844e385ebd56ff071d to your computer and use it in GitHub Desktop.
No hashmap concept for CGAL
#include <CGAL/Handle_for.h>
#include <CGAL/Timer.h>
#include <CGAL/Unique_hash_map.h>
#include <boost/any.hpp>
#include <iostream>
#include <list>
#include <stack>
namespace CGAL {
const size_t itterations = 10'000'000;
using Info = boost::any;
class Context;
class Provider : public std::stack<Info>
{
public:
Provider(){ push(Info()); }
bool has_context(Context* c) { return context_ == c; }
void set_context(Context* c){ context_ = c; };
void reset() { pop(); context_=nullptr; }
private:
Context* context_;
};
class Facet
{
friend class Facet_handle;
Provider provider_;
};
class Facet_handle : public Handle_for<Facet>
{
public:
Info& info() { return ptr()->provider_.top(); }
Provider& provider() { return ptr()->provider_; }
};
class Context
{
std::list<Provider*> providers_;
Info default_value;
public:
Context() = default;
template <typename T>
Context(const T& d) : default_value(d) {}
~Context() {
for(auto& p: providers_)
p->reset();
}
bool has_info(Provider& p) {
return p.has_context(this);
}
template <typename T>
T get_info(Provider& p) {
if(!has_info(p))
set_info(p,default_value);
return boost::any_cast<T>(p.top());
}
void set_info(Provider& p,const Info& i) {
if(has_info(p)) {
p.top() = i;
} else {
p.set_context(this);
providers_.push_back(&p);
p.push(i);
}
}
};
void count_cycles(std::list<Facet_handle>& facets) {
Context FacetCycles(-1);
for(auto& f: facets) {
if(!FacetCycles.has_info(f.provider())) {
FacetCycles.set_info(f.provider(),0);
} else {
FacetCycles.set_info(f.provider(), FacetCycles.get_info<int>(f.provider())+1);
std::cout << FacetCycles.get_info<int>(f.provider()) << std::endl;
}
}
}
void visit1(std::list<Facet_handle>& facets) {
Context Done;
for(auto& f: facets) {
if(!Done.has_info(f.provider())) {
Done.set_info(f.provider(),true);
std::cout << "visited" << std::endl;
}
}
}
void visit2(std::list<Facet_handle>& facets) {
Unique_hash_map<Facet*,bool> Done(false);
for(auto& f: facets) {
auto* h = const_cast<Facet*>(f.Ptr());
if(!Done[h]) {
Done[h]=true;
std::cout << "visited" << std::endl;
}
}
}
template <typename Function>
void test(Function visit)
{
Facet_handle f1;
Facet_handle f2;
Facet_handle f3;
Facet_handle f4;
Context c;
c.set_info(f1.provider(),"Hello World");
f2.info() = 1.0f;
std::list<Facet_handle> facets;
facets.push_back(f1);
facets.push_back(f1);
facets.push_back(f1);
facets.push_back(f2);
for(size_t i=0; i< itterations; ++i)
facets.push_back(f3);
for(size_t i=0; i< itterations; ++i)
facets.push_back(f4);
Timer t;
t.start();
visit(facets);
t.stop();
std::cout << t.time() << std::endl;
std::cout << boost::any_cast<const char*>(f1.info()) << std::endl;
}
}
int main()
{
CGAL::test(CGAL::visit1);
CGAL::test(CGAL::visit2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment