[51587aa] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[a08ba92] | 7 | // Association.h -- |
---|
[51587aa] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[a08ba92] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Tue May 19 16:27:09 2015 |
---|
| 13 | // Update Count : 2 |
---|
[51587aa] | 14 | // |
---|
[a08ba92] | 15 | |
---|
| 16 | #ifndef _ASSOCIATION_H_ |
---|
| 17 | #define _ASSOCIATION_H_ |
---|
[51b7345] | 18 | |
---|
| 19 | #include <map> |
---|
| 20 | #include <list> |
---|
| 21 | #include <string> |
---|
| 22 | #include <vector> |
---|
| 23 | #include <iostream> |
---|
| 24 | #include <stdexcept> |
---|
| 25 | #include <algorithm> |
---|
| 26 | |
---|
| 27 | #include "SynTree/Expression.h" |
---|
| 28 | #include "diet_map.h" |
---|
| 29 | |
---|
| 30 | class Association; |
---|
| 31 | class SingleName; |
---|
| 32 | class PointAssociation; |
---|
| 33 | class RangeAssociation; |
---|
| 34 | |
---|
| 35 | // ** exceptions |
---|
| 36 | class AssocException : public std::exception { |
---|
[a08ba92] | 37 | public: |
---|
| 38 | AssocException() {} |
---|
| 39 | AssocException( std::string _what ) : what( _what ) {} |
---|
| 40 | ~AssocException() throw () {} |
---|
| 41 | |
---|
| 42 | std::string get_what() const { return what; } |
---|
| 43 | void set_what( std::string newValue ) { what = newValue; } |
---|
| 44 | private: |
---|
| 45 | std::string what; |
---|
[51b7345] | 46 | }; |
---|
| 47 | |
---|
| 48 | // ** visitors |
---|
| 49 | class AssociationVisitor { |
---|
[a08ba92] | 50 | public: |
---|
| 51 | AssociationVisitor() {} |
---|
| 52 | virtual ~AssociationVisitor() {} |
---|
[51b7345] | 53 | |
---|
[a08ba92] | 54 | virtual void visit( SingleName * ) = 0; |
---|
| 55 | virtual void visit( PointAssociation * ) = 0; |
---|
| 56 | virtual void visit( RangeAssociation * ) = 0; |
---|
[51b7345] | 57 | }; |
---|
| 58 | |
---|
| 59 | // ** containers |
---|
| 60 | class Association { |
---|
[a08ba92] | 61 | public: |
---|
| 62 | virtual ~Association(); |
---|
[51b7345] | 63 | |
---|
[a08ba92] | 64 | virtual Association *clone() = 0; |
---|
| 65 | virtual long int add_single( std::string, Expression *) = 0; |
---|
| 66 | virtual long int add_single( long int, Expression *expr) = 0; |
---|
[51b7345] | 67 | |
---|
[a08ba92] | 68 | virtual Association *operator[]( int idx ) = 0; |
---|
| 69 | virtual Association *operator[]( std::string ) = 0; |
---|
[51b7345] | 70 | |
---|
[a08ba92] | 71 | // virtual AssociationIterator *get_iterator() = 0; |
---|
[51b7345] | 72 | |
---|
[a08ba92] | 73 | virtual void accept( AssociationVisitor & ) = 0; |
---|
| 74 | virtual void display( std::ostream & ) = 0; |
---|
[51b7345] | 75 | }; |
---|
| 76 | |
---|
| 77 | class SingleName : public Association { |
---|
[a08ba92] | 78 | public: |
---|
| 79 | SingleName( Expression *initExpr = 0 ) : expr( initExpr ) {} |
---|
| 80 | virtual ~SingleName(); |
---|
| 81 | |
---|
| 82 | virtual SingleName *clone() { |
---|
| 83 | return 0; // XXX! |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | virtual long int add_single( long int idx, Expression *newExpr) { |
---|
| 87 | if ( expr == 0 ) //|| *expr == *newExpr ) |
---|
| 88 | expr = newExpr; |
---|
| 89 | return 0; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | virtual long int add_single( std::string str, Expression *newExpr ) { |
---|
| 93 | if ( expr == 0 ) //|| *expr == *newExpr ) |
---|
| 94 | expr = newExpr; |
---|
| 95 | return 0; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | virtual Association *operator[]( int idx ) { assert(false); } |
---|
| 99 | virtual Association *operator[]( std::string idx ) { assert(false); } |
---|
| 100 | |
---|
| 101 | virtual void accept( AssociationVisitor &v ) { v.visit( this ); } |
---|
| 102 | virtual void display( std::ostream &os ) { |
---|
| 103 | os << "Single association" << std::endl; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | Expression *get_expr() const { return expr; } |
---|
| 107 | |
---|
| 108 | private: |
---|
| 109 | Expression *expr; |
---|
| 110 | Expression *deflt; |
---|
[51b7345] | 111 | }; |
---|
| 112 | |
---|
| 113 | class PointAssociation : public Association { |
---|
[a08ba92] | 114 | public: |
---|
| 115 | typedef std::map< std::string, std::pair< long int, Association *> > map_type; |
---|
| 116 | |
---|
| 117 | PointAssociation() {} |
---|
| 118 | PointAssociation( const PointAssociation &other ) { |
---|
| 119 | copy( other.anonym.begin(), other.anonym.end(), back_inserter( anonym )); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | virtual ~PointAssociation(); |
---|
| 123 | |
---|
| 124 | virtual PointAssociation *clone() { |
---|
| 125 | return ( new PointAssociation( *this ) ); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | virtual long int add_single( long int idx, Expression *expr) { |
---|
| 129 | long int ret; |
---|
| 130 | |
---|
| 131 | if ( idx >= (long int)ordering.size() ) throw AssocException("extra (spurious) members"); |
---|
| 132 | |
---|
| 133 | if ( ordering[ idx ] == "") |
---|
| 134 | std::cerr << "Checkpoint 2" << std::endl; |
---|
| 135 | else { |
---|
| 136 | assert( table[ordering[idx]].second != 0 ); |
---|
| 137 | ret = idx; |
---|
| 138 | table[ ordering[idx] ].second->add_single("", expr ); |
---|
| 139 | } // if |
---|
| 140 | return ret; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | virtual long int add_single( std::string idx, Expression *expr) { |
---|
| 144 | if ( idx == "" ) |
---|
| 145 | std::cerr << "Checkpoint 1" << std::endl; |
---|
| 146 | else { |
---|
| 147 | map_type::iterator j; |
---|
| 148 | if ( (j = table.find( idx )) == table.end() ) // this doesn't amount for reachable members deeper down the structure, fix |
---|
| 149 | throw AssocException("No such member"); |
---|
| 150 | else |
---|
| 151 | return add_single( j->second.first, expr ); |
---|
| 152 | } // if |
---|
| 153 | |
---|
| 154 | return -1; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | void add_member( std::string str ) { |
---|
| 158 | if ( table.find( str ) != table.end() ) return; |
---|
| 159 | ordering.push_back( str ); |
---|
| 160 | if ( str != "" ) { |
---|
| 161 | std::pair<long int, Association *> p( ordering.size() - 1, 0 ); |
---|
| 162 | table.insert( std::pair< std::string, std::pair<long int, Association *> >(str, p) ); |
---|
| 163 | } // if |
---|
| 164 | return; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | virtual void set_member( std::string str, Association *assoc ) { |
---|
| 168 | if ( str == "" ) |
---|
| 169 | anonym.push_back( assoc ); |
---|
| 170 | else if ( table.find( str ) == table.end() ) |
---|
| 171 | throw AssocException( "no such member" ); |
---|
| 172 | else |
---|
| 173 | table[ str ] = std::pair<long int, Association *>(ordering.size() - 1, assoc); |
---|
| 174 | |
---|
| 175 | return; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | virtual Association *operator[]( int idx ) { |
---|
| 179 | if ( ordering[idx] == "" ) { |
---|
| 180 | std::cerr << "Error, anonymous members not implemented yet" << std::endl; |
---|
| 181 | throw 0; |
---|
| 182 | } else |
---|
| 183 | return table[ ordering[idx] ].second; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | virtual Association *operator[]( std::string idx ) { |
---|
| 187 | if ( table.find( idx ) == table.end() ) |
---|
| 188 | throw AssocException("Member not found"); |
---|
| 189 | else |
---|
| 190 | return table[ idx ].second; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | /* |
---|
| 194 | virtual AssociationIterator *get_iterator() { |
---|
| 195 | PointAssocIterator *it; |
---|
| 196 | return it; |
---|
| 197 | } |
---|
| 198 | */ |
---|
| 199 | |
---|
| 200 | void accept( AssociationVisitor &v ) { v.visit( this ); } |
---|
| 201 | |
---|
| 202 | virtual void display( std::ostream &os ) { |
---|
| 203 | os << "Point association: " << std::endl; |
---|
| 204 | for ( map_type::iterator i = table.begin(); i != table.end(); i++ ) { |
---|
| 205 | os << "Member [" << i->first << ", index = " << i->second.first << "]"; |
---|
| 206 | if ( i->second.second != 0 ) |
---|
| 207 | i->second.second->display( os ); |
---|
| 208 | else |
---|
| 209 | std::cerr << "No recursive association" << std::endl; |
---|
| 210 | |
---|
| 211 | os << std::endl; |
---|
| 212 | } // for |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | const int size() const { return ordering.size(); } |
---|
| 216 | |
---|
| 217 | private: |
---|
| 218 | PointAssociation &operator=(const PointAssociation &); |
---|
| 219 | std::vector<std::string> ordering; |
---|
| 220 | std::list< Association * > anonym; |
---|
| 221 | std::map< std::string, std::pair<long int, Association *> > table; |
---|
[51b7345] | 222 | }; |
---|
| 223 | |
---|
| 224 | class RangeAssociation : public Association { |
---|
[a08ba92] | 225 | public: |
---|
| 226 | static const int UNDEF; |
---|
| 227 | RangeAssociation( int _hi= UNDEF ) : hi( _hi ) { |
---|
| 228 | std::cerr << "Constructed RangeAssociation with: [" << hi << "]" << std::endl; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | virtual ~RangeAssociation(); |
---|
| 232 | |
---|
| 233 | virtual RangeAssociation *clone() { |
---|
| 234 | return 0; // XXX !!!! |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | virtual Association *operator[]( int idx ) { |
---|
| 238 | return 0; // XXX !!! |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | virtual Association *operator[]( std::string idx ) { assert(false); return 0; } |
---|
| 242 | |
---|
| 243 | /* |
---|
| 244 | virtual AssociationIterator *get_iterator() { |
---|
| 245 | RangeAssocIterator *it; |
---|
| 246 | return it; |
---|
| 247 | } |
---|
| 248 | */ |
---|
| 249 | |
---|
| 250 | virtual long int add_single( long int idx, Expression *newExpr) { return 0; } |
---|
| 251 | virtual long int add_single( std::string, Expression *) { return 0; } |
---|
| 252 | void accept( AssociationVisitor &v ) { v.visit( this ); } |
---|
| 253 | virtual void display( std::ostream &os ) { |
---|
| 254 | os << "Range association, with limit: " << std::endl; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | private: |
---|
| 258 | int hi; |
---|
| 259 | diet::diet_tree<int> tree; |
---|
| 260 | /* |
---|
| 261 | for ( diet_tree<int>::iterator i = tree.begin(); i != tree.end(); i++ ) |
---|
| 262 | std::cout << "--(" << (*i).first << ", " << (*i).second << ")--" << std::endl; |
---|
| 263 | diet_tree<int> tree; |
---|
| 264 | tree.insert(100,200); |
---|
| 265 | */ |
---|
[51b7345] | 266 | }; |
---|
| 267 | |
---|
| 268 | // ** builders |
---|
| 269 | class AssociationBuilder { |
---|
[a08ba92] | 270 | public: |
---|
| 271 | /* AssociationBuilder( Declaration * ) */ |
---|
| 272 | virtual ~AssociationBuilder() {} |
---|
| 273 | virtual Association *get_assoc() = 0; |
---|
| 274 | virtual Association *grab_assoc() = 0; |
---|
| 275 | virtual void set_assoc( Association * ) = 0; |
---|
[51b7345] | 276 | }; |
---|
| 277 | |
---|
| 278 | class AssociationFiller { |
---|
[a08ba92] | 279 | public: |
---|
| 280 | // AssociationFiller( Declaration * ) {} |
---|
| 281 | virtual ~AssociationFiller() {} |
---|
| 282 | virtual Association *get_assoc() = 0; |
---|
| 283 | virtual void set_assoc( Association * ) = 0; |
---|
[51b7345] | 284 | }; |
---|
| 285 | |
---|
[a08ba92] | 286 | #endif // _ASSOCIATION_H_ |
---|
[51b7345] | 287 | |
---|
[51587aa] | 288 | // Local Variables: // |
---|
| 289 | // tab-width: 4 // |
---|
| 290 | // mode: c++ // |
---|
| 291 | // compile-command: "make install" // |
---|
| 292 | // End: // |
---|