online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include <iostream> #include <ostream> #include <cassert> #include <vector> #include <set> //https://stackoverflow.com/questions/3738537/implementing-equivalence-relations-in-c-using-boostdisjoint-sets ////////////////////////////////////// class E { //could be a GUID or something instead but the time complexity of //std::multiset::equal_range with a simple int comparison should be logarithmic static size_t _faucet; public: struct LessThan { bool operator()(const E* l, const E* r) const { return (l->eqValue() < r->eqValue()); } }; using EL=std::vector<const E*>; using ES=std::multiset<const E*, E::LessThan>; using ER=std::pair<ES::iterator, ES::iterator>; static size_t NewValue() { return ++_faucet; } ~E() { eqRemove(); } E(size_t val) : _eqValue(val) {} E(std::string name) : Name(name), _eqValue(NewValue()) { E::Elementals.insert(this); } //not rly a great idea to use operator=() for this. demo only.. const E& operator=(const class E& other) { eqValue(other); return *this; } //overriddable default equivalence interface virtual size_t eqValue() const { return _eqValue; }; //clearly it matters how mutable you need your equivalence relationships to be,, //in this implementation, if an element's equivalence relation changes then //the element is going to be erased and re-inserted. virtual void eqValue(const class E& other) { if (_eqValue == other._eqValue) return; eqRemove(); _eqValue=other._eqValue; E::Elementals.insert(this); }; ES::iterator eqRemove() { auto range=E::Elementals.equal_range(this); //worst-case complexity should be aprox linear over the range for (auto it=range.first; it!=range.second; it++) if (this == (*it)) return E::Elementals.erase(it); return E::Elementals.end(); } std::string Name; //some other attribute unique to the instance static ES Elementals; //canonical set of elements with equivalence relations protected: size_t _eqValue=0; }; size_t E::_faucet=0; E::ES E::Elementals{}; ////////////////////////////////////// //random specialisation providing //dynamic class-level equivalence class StarFish : public E { public: static void EqAssign(const class E& other) { if (StarFish::_id == other.eqValue()) return; E el(StarFish::_id); auto range=E::Elementals.equal_range(&el); StarFish::_id=other.eqValue(); E::EL insertList(range.first, range.second); E::Elementals.erase(range.first, range.second); E::Elementals.insert(insertList.begin(), insertList.end()); } StarFish() : E("starfish") {} //base-class overrides virtual size_t eqValue() const { return StarFish::_id; }; protected: //equivalence is a the class level virtual void eqValue(const class E& other) { assert(0); } private: static size_t _id; }; size_t StarFish::_id=E::NewValue(); ////////////////////////////////////// void eqPrint(const E& e) { std::cout << std::endl << "elementals equivalent to " << e.Name << ": "; auto range=E::Elementals.equal_range(&e); for (auto it=range.first; it!=range.second; it++) std::cout << (*it)->Name << " "; std::cout << std::endl << std::endl; } ////////////////////////////////////// void eqPrint() { for (auto it=E::Elementals.begin(); it!=E::Elementals.end(); it++) std::cout << (*it)->Name << ": " << (*it)->eqValue() << " "; std::cout << std::endl << std::endl; } ////////////////////////////////////// int main() { E e0{"zero"}, e1{"one"}, e2{"two"}, e3{"three"}, e4{"four"}, e5{"five"}; //per the OP e0=e1=e2; e3=e4; e5=e3; eqPrint(e0); eqPrint(e3); eqPrint(e5); eqPrint(); StarFish::EqAssign(e3); StarFish starfish1, starfish2; starfish1.Name="fred"; eqPrint(e3); //re-assignment StarFish::EqAssign(e0); e3=e0; { //out of scope removal E e6{"six"}; e6=e4; eqPrint(e4); } eqPrint(e5); eqPrint(e0); eqPrint(); return 0; } ////////////////////////////////////// //done

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue