#include #define DBG fprintf(stderr,"%d: %d\n",ts::myRank,__LINE__) using namespace std; struct Expr; typedef ts::TVar TExpr; struct Expr : private ts::TExtData { private: TExpr* terms; void create (const Expr& e) { TExpr* p = *this; const TExpr* q = e; for (unsigned i = 0; i < e.getLength(); i++) new (p++) TExpr(*q++); } void clear () { if (terms) { delete[] terms; terms = 0; } else { TExpr* p = *this; for (unsigned i = 0; i < getLength(); i++) p++->~TExpr(); } } public: Expr () : terms(0) {}; void init (size_t s) { assert(!terms); terms = new TExpr[s]; extDataSize() = s * sizeof(TExpr); }; operator const TExpr* () const { return terms ? terms : (TExpr*)extData(); } operator TExpr* () { return terms ? terms : (TExpr*)extData(); } Expr (const Expr& e) : terms(0) { create(e); } Expr& operator= (const Expr& e) { if (this != &e) { clear(); init(e.getLength()); create(e); } return *this; } size_t getLength () const { return extDataSize() / sizeof(TExpr); } TExpr& operator[] (int i) { return ((TExpr *)*this)[i]; } ~Expr () { clear(); } }; // e is intentionally not const! ostream& operator<< (ostream& os, Expr& e) { os << e.getLength(); TExpr* p = e; for (unsigned i = 0; i < e.getLength(); i++) os << " (" << (Expr &)*p++ << ")"; return os; } istream& operator>> (istream& is, Expr& e) { size_t len; char c1, c2; is >> len; e.init(len); TExpr* p = e; for (unsigned i = 0; i < len; i++) { is >> c1 >> (Expr &)*p++ >> c2; assert (c1 == '(' && c2 == ')'); } return is; } tfun int empty (Expr tout out) { TExpr z; ((Expr&)z).init(0); out = (Expr&) z; return 0; } tfun TExpr one_term1 () { TExpr z; Expr& x = (Expr&) z; x.init(1); TExpr* p = x; // TExpr e; // ((Expr&)e).init(0); // *p = e; empty(*p); return z; } tfun int main(int argc, char *argv[]) { tct(atRank(ts::realsuperSize > 1 ? 1 : 0)); TExpr out; out = one_term1(); cerr << "res: " << out << endl; return 0; }