Skip to content

Instantly share code, notes, and snippets.

@pointwiseproduct
Last active November 19, 2016 03:36
Show Gist options
  • Save pointwiseproduct/83e39ae8b9a2d96cdb2e754396d22844 to your computer and use it in GitHub Desktop.
Save pointwiseproduct/83e39ae8b9a2d96cdb2e754396d22844 to your computer and use it in GitHub Desktop.
要素に自身と同じ型の集合を許容する集合
#include <set>
#include <boost/variant.hpp>
#include <boost/any.hpp>
template<class T>
struct multiplex_less;
template<class T>
class set : public std::set<boost::variant<T, set<T>>, multiplex_less<T>>{
public:
set() = default;
set(const set&) = default;
set(set&&) = default;
~set() = default;
};
template<class T>
struct multiplex_less{
bool operator ()(const boost::variant<T, set<T>> &a, const boost::variant<T, set<T>> &b) const{
if(a.which() < b.which()){
return true;
}else if(a.which() > b.which()){
return false;
}else{
switch(a.which()){
case 0:
return std::less<T>()(boost::get<const T&>(a), boost::get<const T&>(b));
case 1:
{
const set<T> &sa = boost::get<const set<T>&>(a);
const set<T> &sb = boost::get<const set<T>&>(b);
if(sa.size() < sb.size()){
return true;
}else if(sa.size() > sb.size()){
return false;
}else{
auto iter = sa.begin();
auto jter = sb.begin();
for(; iter != sa.end(); ++iter, ++jter){
if(multiplex_less()(*iter, *jter)){
return true;
}else if(multiplex_less()(*jter, *iter)){
return false;
}
}
return false;
}
}
}
// unreachable poiont.
return false;
}
}
};
int main(){
set<int> a, b, c;
// { 0, 1, 2 } ⊂ a
a.insert(0);
a.insert(1);
a.insert(2);
// a ∈ b
b.insert(a);
// b ∈ c
c.insert(b);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment