00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef __MMX_TABLE_HPP
00014 #define __MMX_TABLE_HPP
00015 #include <basix/list.hpp>
00016 #include <basix/pair.hpp>
00017 #include <basix/vector.hpp>
00018
00020
00021 namespace mmx {
00022 struct exact_eq_table;
00023 #define TMPL_DEF template<typename C, typename T, typename V= exact_eq_table>
00024 #define TMPL template<typename C, typename T, typename V>
00025 #define TMPLK template<typename C, typename T, typename V, typename K>
00026 #define Format1 format<C>
00027 #define Format2 format<T>
00028 #define Table table<C,T,V>
00029 #define Table_rep table_rep<C,T,V>
00030 #define GEN generic
00031 #define VEC vector<generic>
00032 #define GTAB table<C,generic>
00033 TMPL class table_rep;
00034 TMPL class table;
00035 TMPL class entries_iterator_rep;
00036 TMPL class table_iterator_rep;
00037 TMPL inline C& I (Table& t);
00038 TMPL inline const C& I (const Table& t);
00039 TMPL inline const C& read_I (const Table& t);
00040 TMPL inline nat N (const Table& t);
00041 TMPL inline void simplify (Table& t);
00042 TMPL bool operator == (const Table& t1, const Table& t2);
00043
00044
00045
00046
00047
00048 struct hard_eq_table {
00049 typedef hard_eq_op key_op;
00050 typedef equal_op val_op;
00051 };
00052
00053 struct exact_eq_table {
00054 typedef exact_eq_op key_op;
00055 typedef equal_op val_op;
00056 };
00057
00058 struct equal_table {
00059 typedef equal_op key_op;
00060 typedef equal_op val_op;
00061 };
00062
00063
00064
00065
00066
00067 TMPL_DEF
00068 class table_rep REP_STRUCT_2(C,T) {
00069 nat size;
00070 nat n;
00071 C* init;
00072 list<pair<T,C> >* a;
00073
00074 public:
00075 Table_rep (nat n2, const Format1& fm1, const Format2& fm2):
00076 encapsulate1<Format1 > (fm1),
00077 encapsulate2<Format2 > (fm2),
00078 size (0), n (n2),
00079 init (NULL),
00080 a (mmx_new<list<pair<T,C> > > (n))
00081 {
00082
00083
00084 }
00085 Table_rep (const C& init2, nat n2, const Format2& fm2):
00086 encapsulate1<Format1 > (get_format (init2)),
00087 encapsulate2<Format2 > (fm2),
00088 size (0), n (n2),
00089 init (mmx_new_one<C> (init2)),
00090 a (mmx_new<list<pair<T,C> > > (n))
00091 {
00092
00093
00094
00095 }
00096 ~Table_rep () {
00097
00098
00099
00100
00101 if (init != NULL) mmx_delete_one<C> (init);
00102 mmx_delete<list<pair<T,C> > > (a, n); }
00103 inline void lazy_initialize () const {
00104 if (init == NULL) {
00105 *((C**) ((void*) &init))= mmx_new_one<C> ();
00106
00107
00108
00109 }
00110 }
00111 void resize (nat n);
00112 void reset (const T& x);
00113 bool contains (const T& x) const;
00114 bool get (const T& x, C& y) const;
00115 const C& get (const T& x) const;
00116 C& set (const T& x);
00117 void simplify ();
00118 double complexity () const;
00119
00120 friend class Table;
00121 friend class entries_iterator_rep<C,T,V>;
00122 friend class table_iterator_rep<C,T,V>;
00123 friend C& I LESSGTR (Table& t);
00124 friend const C& I LESSGTR (const Table& t);
00125 friend const C& read_I LESSGTR (const Table& t);
00126 friend nat N LESSGTR (const Table& t);
00127 friend bool operator == LESSGTR (const Table& t1, const Table& t2);
00128 };
00129
00130 TMPL_DEF
00131 class table {
00132 INDIRECT_PROTO_3 (table, table_rep, C, T, V)
00133 inline table ():
00134 rep (new Table_rep (1, Format1 (no_format ()), Format2 (no_format ()))) {}
00135 inline table (const Format1& fm1, const Format2& fm2):
00136 rep (new Table_rep (1, fm1, fm2)) {}
00137 template<typename K> inline table (const K& i, nat n=1):
00138 rep (new Table_rep (as<C> (i), n, Format2 ())) {}
00139 template<typename K> inline table (const K& i, const Format2& fm2, nat n=1):
00140 rep (new Table_rep (as<C> (i), n, fm2)) {}
00141 inline table (const iterator<pair<T,C> >& it):
00142 rep (new Table_rep (1, CF(it).format1 (), CF(it).format2 ())) {
00143 for (; busy (it); ++it) rep->set ((*it).x1)= (*it).x2; }
00144 inline table (const C& i, const iterator<pair<T,C> >& it):
00145 rep (new Table_rep (i, 1, CF(it).format2 ())) {
00146 for (; busy (it); ++it) rep->set ((*it).x1)= (*it).x2; }
00147 template<typename K> inline const C& operator [] (const K& x) const {
00148 return rep->get (as<T> (x)); }
00149 inline const C& operator [] (const T& x) const {
00150 return rep->get (x); }
00151 template<typename K> inline C& operator [] (const K& x) {
00152 secure (); return rep->set (as<T> (x)); }
00153 inline C& operator [] (const T& x) {
00154 secure (); return rep->set (x); }
00155 friend void simplify LESSGTR (Table& t);
00156 };
00157 INDIRECT_IMPL_3 (table, table_rep, typename C, C, typename T, T, typename V, V)
00158 DEFINE_BINARY_FORMAT_3 (table)
00159
00160 STYPE_TO_TYPE(TMPL,scalar_type,Table,C);
00161
00162 TMPL inline Format1 CF1 (const Table& t) { return t->tfm1 (); }
00163 TMPL inline Format2 CF2 (const Table& t) { return t->tfm2 (); }
00164
00165 TMPLK inline const C& read (const Table& t, const K& x) {
00166 return t[as<T> (x)]; }
00167 TMPLK inline void reset (Table& t, const K& x) {
00168 t.secure(); inside (t) -> reset (as<T> (x)); }
00169 TMPLK inline bool contains (const Table& t, const K& x) {
00170 return t->contains (as<T> (x)); }
00171 TMPLK inline void inside_set (const Table& t, const K& x, const C& v) {
00172 inside (t) -> set (as<T> (x)) = v; }
00173
00174 TMPL inline void simplify (Table& t) {
00175 t.secure(); t.rep->simplify (); }
00176 TMPL inline const C& read (const Table& t, const T& x) {
00177 return t[x]; }
00178 TMPL inline void reset (Table& t, const T& x) {
00179 t.secure(); inside (t) -> reset (x); }
00180 TMPL inline bool contains (const Table& t, const T& x) {
00181 return t->contains (x); }
00182 TMPL inline void inside_set (const Table& t, const T& x, const C& v) {
00183 inside (t) -> set (x) = v; }
00184
00185 TMPL inline double complexity (const Table& t) {
00186 return t->complexity (); }
00187 TMPL inline nat discrete_complexity (const Table& t) {
00188 return (nat) t->complexity (); }
00189
00190 TMPL struct species_type_information<Table > {
00191 static const nat id= SPECIES_TABLE; };
00192
00193 template<typename D, typename C, typename T>
00194 struct as_helper<table<D,T>,table<C,T> > {
00195 static inline table<D,T>
00196 cv (const table<C,T>& t) {
00197 table<D,T> r (as<D> (I (t)), CF2(t));
00198 for (iterator<T> it= entries (t); busy (it); ++it)
00199 r[*it]= as<D> (t[*it]);
00200 return r;
00201 }
00202 };
00203
00204
00205
00206
00207
00208 TMPL inline C&
00209 I (Table& t) {
00210 t->lazy_initialize ();
00211 return *t->init;
00212 }
00213
00214 TMPL inline const C&
00215 I (const Table& t) {
00216 t->lazy_initialize ();
00217 return *t->init;
00218 }
00219
00220 TMPL inline const C&
00221 read_I (const Table& t) {
00222 t->lazy_initialize ();
00223 return *t->init;
00224 }
00225
00226 TMPL inline nat
00227 N (const Table& t) {
00228 return t->size;
00229 }
00230
00231 TMPL void
00232 Table_rep::resize (nat n2) {
00233 nat i, oldn= n;
00234 list<pair<T,C> >* olda= a;
00235 n= n2;
00236 a= mmx_new<list<pair<T,C> > > (n);
00237 for (i=0; i<oldn; i++) {
00238 list<pair<T,C> > l (olda [i]);
00239 while (!is_nil (l)) {
00240 nat code= V::key_op::hash_op (read_car(l).x1);
00241 list<pair<T,C> >& newl= a [code & (n-1)];
00242 newl= cons (read_car (l), newl);
00243 l= read_cdr (l);
00244 }
00245 }
00246 mmx_delete<list<pair<T,C> > > (olda, oldn);
00247
00248
00249
00250
00251 }
00252
00253 TMPL bool
00254 Table_rep::contains (const T& x) const {
00255 list<pair<T,C> > l (a [V::key_op::hash_op (x) & (n-1)]);
00256 while (!is_nil (l)) {
00257 if (V::key_op::op (read_car (l).x1, x)) return true;
00258 l= read_cdr (l);
00259 }
00260 return false;
00261 }
00262
00263 TMPL bool
00264 Table_rep::get (const T& x, C& y) const {
00265 list<pair<T,C> > l (a [V::key_op::hash_op (x) & (n-1)]);
00266 while (!is_nil (l)) {
00267 if (V::key_op::op (read_car (l).x1, x)) {
00268 y= read_car (l).x2;
00269 return true;
00270 }
00271 l= read_cdr (l);
00272 }
00273 return false;
00274 }
00275
00276 TMPL C&
00277 Table_rep::set (const T& x) {
00278 register nat hv= V::key_op::hash_op (x);
00279 list<pair<T,C> >* l= &(a [hv & (n-1)]);
00280 while (!is_nil (*l)) {
00281 if (V::key_op::op (read_car (*l).x1, x))
00282 return car (*l).x2;
00283 l= &cdr (*l);
00284 }
00285
00286 if (size>=n) resize (n<<1);
00287
00288 lazy_initialize ();
00289 l= &a [hv & (n-1)];
00290 *l= cons (pair<T,C> (x, *init), *l);
00291 size ++;
00292 return car (*l).x2;
00293 }
00294
00295 TMPL const C&
00296 Table_rep::get (const T& x) const {
00297 list<pair<T,C> > l (a [V::key_op::hash_op (x) & (n-1)]);
00298 while (!is_nil (l)) {
00299 if (V::key_op::op (read_car (l).x1, x)) return read_car (l).x2;
00300 l= read_cdr (l);
00301 }
00302 lazy_initialize ();
00303 return *init;
00304 }
00305
00306 TMPL void
00307 Table_rep::reset (const T& x) {
00308 list<pair<T,C> > *l= &(a [V::key_op::hash_op (x) & (n-1)]);
00309 while (!is_nil (*l)) {
00310 if (V::key_op::op (read_car (*l).x1, x)) {
00311 *l= read_cdr (*l);
00312 size --;
00313 if (size < (n>>1)) resize (n>>1);
00314 return;
00315 }
00316 l= &cdr (*l);
00317 }
00318 }
00319
00320 TMPL void
00321 Table_rep::simplify () {
00322 lazy_initialize ();
00323 for (nat i=0; i<n; i++) {
00324 list<pair<T,C> > *l= &(a[i]);
00325 while (!is_nil (*l)) {
00326 if (V::val_op::op (read_car (*l).x2, *init)) {
00327 *l= read_cdr (*l);
00328 size --;
00329 }
00330 else l= &cdr (*l);
00331 }
00332 }
00333 while (size < (n>>1)) resize (n>>1);
00334 }
00335
00336 TMPL double
00337 Table_rep::complexity () const {
00338 double sum= 0.0;
00339
00340 for (nat i=0; i<n; i++) {
00341 nat l= N(a[i]);
00342 sum += l*l;
00343
00344 }
00345
00346 return sum / n;
00347 }
00348
00349
00350
00351
00352
00353 TMPL_DEF
00354 class entries_iterator_rep: public iterator_rep<T> {
00355 Table t;
00356 nat i;
00357 list<pair<T,C> > l;
00358
00359 protected:
00360 entries_iterator_rep (const Table& t2, nat i2, const list<pair<T,C> >& l2):
00361 iterator_rep<T> (CF2(t2)), t(t2), i(i2), l(l2) {}
00362 void spool () {
00363 while (is_nil (l)) {
00364 i++;
00365 if (i >= t->n) break;
00366 l= t->a[i];
00367 }
00368 }
00369 bool is_busy () { return i<t->n; }
00370 void advance () { l= read_cdr (l); spool (); }
00371 T current () { return read_car (l).x1; }
00372 iterator_rep<T>* clone () { return new entries_iterator_rep (t, i, l); }
00373
00374 public:
00375 entries_iterator_rep (const Table& t2):
00376 iterator_rep<T> (CF2(t2)), t(t2), i(0), l(t->a[0]) { spool (); }
00377 };
00378
00379 TMPL iterator<T>
00380 entries (const Table& t) {
00381 return iterator<T> (new entries_iterator_rep<C,T,V> (t));
00382 }
00383
00384 TMPL_DEF
00385 class table_iterator_rep: public iterator_rep<pair<T,C> > {
00386 Table t;
00387 nat i;
00388 list<pair<T,C> > l;
00389
00390 protected:
00391 table_iterator_rep (const Table& t2, nat i2, const list<pair<T,C> >& l2):
00392 iterator_rep<pair<T,C> > (format<pair<T,C> > (CF2 (t2), CF1 (t2))),
00393 t(t2), i(i2), l(l2) {}
00394 void spool () {
00395 while (is_nil (l)) {
00396 i++;
00397 if (i >= t->n) break;
00398 l= t->a[i];
00399 }
00400 }
00401 bool is_busy () { return i<t->n; }
00402 void advance () { l= read_cdr (l); spool (); }
00403 pair<T,C> current () { return read_car (l); }
00404 iterator_rep<pair<T,C> >* clone () {
00405 return new table_iterator_rep (t, i, l); }
00406
00407 public:
00408 table_iterator_rep (const Table& t2):
00409 iterator_rep<pair<T,C> > (format<pair<T,C> > (CF2 (t2), CF1 (t2))),
00410 t(t2), i(0), l(t->a[0]) { spool (); }
00411 };
00412
00413 TMPL iterator<pair<T,C> >
00414 iterate (const Table& t) {
00415 return iterator<pair<T,C> > (new table_iterator_rep<C,T,V> (t));
00416 }
00417
00418
00419
00420
00421
00422 TMPL syntactic
00423 flatten (const Table& t) {
00424 vector<syntactic> v;
00425 for (iterator<pair<T,C> > it= iterate (t); busy (it); ++it)
00426 v << apply ("~>", flatten ((*it).x1), flatten ((*it).x2));
00427 return apply ("table", v);
00428 }
00429
00430 TMPL
00431 struct binary_helper<Table >: public void_binary_helper<Table > {
00432 static inline string short_type_name () {
00433 return "T" * Short_type_name (C) * Short_type_name (T); }
00434 static inline generic full_type_name () {
00435 return gen ("Table", Full_type_name (T), Full_type_name (C)); }
00436 static inline generic disassemble (const Table& t) {
00437 vector<generic> v;
00438 v << as<generic> (I(t));
00439 for (iterator<pair<T,C> > it= iterate (t); busy (it); ++it)
00440 v << as<generic> (*it);
00441 return as<generic> (v); }
00442 static inline Table assemble (const generic& x) {
00443 vector<generic> v= as<vector<generic> > (x);
00444 Table t (as<C> (v[0]));
00445 for (nat i=1; i<N(v); i++) {
00446 pair<T,C> p= as<pair<T,C> > (v[i]);
00447 t[p.x1]= p.x2;
00448 }
00449 return t; }
00450 static inline void write (const port& out, const Table& t) {
00451 binary_write<C> (out, I (t));
00452 binary_write<Format2 > (out, CF2(t));
00453 binary_write<nat> (out, N (t));
00454 for (iterator<T> it= entries (t); busy (it); ++it) {
00455 binary_write<T> (out, *it);
00456 binary_write<C> (out, t[*it]); } }
00457 static inline Table read (const port& in) {
00458 C init= binary_read<C> (in);
00459 Format2 fm2= binary_read<Format2 > (in);
00460 Table t (init, fm2);
00461 nat n= binary_read<nat> (in);
00462 for (nat i=0; i<n; i++) {
00463 T key= binary_read<T> (in);
00464 t[key]= binary_read<C> (in);
00465 }
00466 return t; }
00467 };
00468
00469
00470
00471
00472
00473 template<typename Op, typename C, typename T, typename V> nat
00474 unary_hash (const Table& t) {
00475
00476
00477 nat h=54321;
00478 for (iterator<pair<T,C> > it= iterate (t); busy (it); ++it) {
00479 pair<T,C> p= *it;
00480 nat key_h= V::key_op::hash_op (p.x1);
00481 nat val_h= Op::op (p.x2);
00482 h += (key_h << 5) ^ (key_h >> 27) ^ val_h;
00483 }
00484 return h;
00485 }
00486
00487 TMPL Table
00488 copy (const Table& t) {
00489 Table r (I(t), CF2(t));
00490 for (iterator<T> it= entries (t); busy (it); ++it)
00491 r[*it]= t[*it];
00492 return r;
00493 }
00494
00495 template<typename Op, typename C, typename T, typename V> Table
00496 unary_map (const Table& t) {
00497 Table r (Op::op (I(t)), CF2(t));
00498 for (iterator<T> it= entries (t); busy (it); ++it)
00499 r[*it]= Op::op (t[*it]);
00500 simplify (r);
00501 return r;
00502 }
00503
00504 template<typename Op, typename C, typename T, typename V> Table
00505 binary_map (const Table& t, const Table& u) {
00506 Table r (Op::op (I(t), I(u)), CF2(t));
00507 for (iterator<T> it= entries (t); busy (it); ++it)
00508 r[*it]= Op::op (t[*it], u[*it]);
00509 for (iterator<T> it= entries (u); busy (it); ++it)
00510 if (!(t->contains (*it)))
00511 r[*it]= Op::op (t[*it], u[*it]);
00512 simplify (r);
00513 return r;
00514 }
00515
00516 template<typename Op, typename C, typename T, typename V, typename X> Table
00517 binary_map_scalar (const Table& t, const X& x) {
00518 Table r (Op::op (I(t), x), CF2(t));
00519 for (iterator<T> it= entries (t); busy (it); ++it)
00520 r[*it]= Op::op (t[*it], x);
00521 simplify (r);
00522 return r;
00523 }
00524
00525 template<typename Op, typename C, typename T, typename V> Table&
00526 unary_set (Table& t, const Table& u) {
00527 Op::set_op (I(t), I(u));
00528 for (iterator<T> it= entries (u); busy (it); ++it) {
00529 Op::set_op (t[*it], u[*it]);
00530 if (V::val_op::op (read (t, *it), read_I (t))) reset (t, *it);
00531 }
00532 return t;
00533 }
00534
00535 template<typename Op, typename C, typename T, typename V, typename X> Table&
00536 unary_set_scalar (Table& t, const X& x) {
00537 Op::set_op (I (t), x);
00538 for (iterator<T> it= entries (t); busy (it); ++it)
00539 Op::set_op (t[*it], x);
00540 simplify (t);
00541 return t;
00542 }
00543
00544 template<typename Op, typename C, typename T, typename V> bool
00545 binary_test (const Table& t, const Table& u) {
00546 if (Op::not_op (I(t), I(u))) return false;
00547 for (iterator<T> it= entries (t); busy (it); ++it)
00548 if (Op::not_op (t[*it], u[*it])) return false;
00549 for (iterator<T> it= entries (u); busy (it); ++it)
00550 if (!t->contains (*it) && Op::not_op (t[*it], u[*it])) return false;
00551 return true;
00552 }
00553
00554 template<typename Op, typename C, typename T, typename V, typename X> bool
00555 binary_test_scalar (const Table& t, const X& c) {
00556 if (Op::not_op (I(t), c)) return false;
00557 for (iterator<T> it= entries (t); busy (it); ++it)
00558 if (Op::not_op (t[*it], c)) return false;
00559 return true;
00560 }
00561
00562 template<typename Op, typename C, typename T, typename V> inline C
00563 big (const Table& t) {
00564 iterator<T> it= entries (t);
00565 if (done (it)) return Op::template neutral<C> ();
00566 C r= t[*it];
00567 for (++it; busy (it); ++it)
00568 Op::set_op (r, t[*it]);
00569 return r;
00570 }
00571
00572 template<typename Op, typename C, typename T, typename V> Table
00573 binary_combine (const Table& t, const Table& u) {
00574 Table r (Op::template neutral<C> ());
00575 for (iterator<T> it1= entries (t); busy (it1); ++it1)
00576 for (iterator<T> it2= entries (u); busy (it2); ++it2)
00577 Op::set_op (r[Op::rhs_op::op (*it1, *it2)], t[*it1], u[*it2]);
00578 simplify (r);
00579 return r;
00580 }
00581
00582 template<typename Op, typename C, typename T, typename V> Table
00583 unary_filter (const Table& t) {
00584 Table r (I(t));
00585 for (iterator<T> it= entries (t); busy (it); ++it)
00586 if (Op::op (*it)) r[*it]= t[*it];
00587 return r;
00588 }
00589
00590
00591
00592
00593
00594 template<typename T1, typename C1, typename T2, typename C2,
00595 typename Fun1, typename Fun2> table<C2,T2>
00596 map (const Fun1& funT, const Fun2& funC,
00597 const table<C1,T1>& t,
00598 const format<T2>& fmT, const format<C2>& fmC)
00599 {
00600 table<C2,T2> r (funC (I(t)), fmT);
00601 for (iterator<T1> it= entries (t); busy (it); ++it)
00602 r[funT(*it)]= funC (t[*it]);
00603 simplify (r);
00604 return r;
00605 }
00606
00607
00608
00609
00610
00611 TMPL inline Table duplicate (const Table& t) {
00612 return unary_map<duplicate_op,C,T,V> (t); }
00613 TMPL inline Table operator - (const Table& t) {
00614 return unary_map<neg_op,C,T,V> (t); }
00615 TMPL inline Table operator + (const Table& t, const Table& u) {
00616 return binary_map<add_op,C,T,V> (t, u); }
00617 TMPL inline Table operator + (const Table& t, const C& u) {
00618 return binary_map<add_op,C,T,V> (t, Table (u)); }
00619 TMPL inline Table operator + (const C& t, const Table& u) {
00620 return binary_map<add_op,C,T,V> (Table (t), u); }
00621 TMPL inline Table operator - (const Table& t, const Table& u) {
00622 return binary_map<sub_op,C,T,V> (t, u); }
00623 TMPL inline Table operator - (const Table& t, const C& u) {
00624 return binary_map<sub_op,C,T,V> (t, Table (u)); }
00625 TMPL inline Table operator - (const C& t, const Table& u) {
00626 return binary_map<sub_op,C,T,V> (Table (t), u); }
00627 TMPL inline Table operator * (const Table& t, const Table& u) {
00628 return binary_map<mul_op,C,T,V> (t, u); }
00629 TMPL inline Table operator / (const Table& t, const Table& u) {
00630 return binary_map<div_op,C,T,V> (t, u); }
00631 TMPL inline Table quo (const Table& t, const Table& u) {
00632 return binary_map<quo_op,C,T,V> (t, u); }
00633 TMPL inline Table rem (const Table& t, const Table& u) {
00634 return binary_map<rem_op,C,T,V> (t, u); }
00635 TMPL inline Table operator | (const Table& t, const Table& u) {
00636 return binary_map<or_op,C,T,V> (t, u); }
00637 TMPL inline Table operator & (const Table& t, const Table& u) {
00638 return binary_map<and_op,C,T,V> (t, u); }
00639 TMPL inline Table inf (const Table& t, const Table& u) {
00640 return binary_map<inf_op,C,T,V> (t, u); }
00641 TMPL inline Table sup (const Table& t, const Table& u) {
00642 return binary_map<sup_op,C,T,V> (t, u); }
00643 template<typename C, typename T, typename V, typename K> inline Table
00644 operator * (const Table& t, const K& sc) {
00645 return binary_map_scalar<rmul_op> (t, sc); }
00646 template<typename C, typename T, typename V, typename K> inline Table
00647 operator * (const K& sc, const Table& t) {
00648 return binary_map_scalar<lmul_op> (t, sc); }
00649 template<typename C, typename T, typename V, typename K> inline Table
00650 operator / (const Table& t, const K& sc) {
00651 return binary_map_scalar<rdiv_op> (t, sc); }
00652 template<typename C, typename T, typename V, typename K> inline Table
00653 operator / (const K& sc, const Table& t) {
00654 return binary_map_scalar<ldiv_op> (t, sc); }
00655 template<typename C, typename T, typename V, typename K> inline Table
00656 quo (const Table& t, const K& sc) {
00657 return binary_map_scalar<rquo_op> (t, sc); }
00658 template<typename C, typename T, typename V, typename K> inline Table
00659 rem (const Table& t, const K& sc) {
00660 return binary_map_scalar<rrem_op> (t, sc); }
00661 TMPL inline Table operator += (Table& t, const Table& u) {
00662 return unary_set<add_op,C,T,V> (t, u); }
00663 TMPL inline Table operator -= (Table& t, const Table& u) {
00664 return unary_set<sub_op,C,T,V> (t, u); }
00665 TMPL inline Table operator *= (Table& t, const Table& u) {
00666 return unary_set<mul_op,C,T,V> (t, u); }
00667 TMPL inline Table operator /= (Table& t, const Table& u) {
00668 return unary_set<div_op,C,T,V> (t, u); }
00669 TMPL inline Table operator |= (Table& t, const Table& u) {
00670 return unary_set<or_op,C,T,V> (t, u); }
00671 TMPL inline Table operator &= (Table& t, const Table& u) {
00672 return unary_set<and_op,C,T,V> (t, u); }
00673 template<typename C, typename T, typename V, typename K> inline Table&
00674 operator *= (Table& t, const K& sc) {
00675 return unary_set_scalar<rmul_op> (t, sc); }
00676 template<typename C, typename T, typename V, typename K> inline Table&
00677 operator /= (Table& t, const K& sc) {
00678 return unary_set_scalar<rdiv_op> (t, sc); }
00679 TMPL inline bool operator <= (const Table& t, const Table& u) {
00680 return binary_test<lesseq_op> (t, u); }
00681 TMPL inline bool operator >= (const Table& t, const Table& u) {
00682 return binary_test<gtreq_op,C,T,V> (t, u); }
00683 template<typename C, typename T, typename V, typename K> inline bool
00684 operator == (const Table& t, const K& c) {
00685 return binary_test_scalar<equal_op> (t, c); }
00686 template<typename C, typename T, typename V, typename K> inline bool
00687 operator <= (const Table& t, const K& c) {
00688 return binary_test_scalar<lesseq_op> (t, c); }
00689 template<typename C, typename T, typename V, typename K> inline bool
00690 operator >= (const Table& t, const K& c) {
00691 return binary_test_scalar<gtreq_op> (t, c); }
00692
00693 TRUE_IDENTITY_OP_SUGAR(TMPL,Table)
00694 EXACT_IDENTITY_OP_SUGAR(TMPL,Table)
00695 ADDITIVE_SCALAR_INT_SUGAR(TMPL,Table)
00696 STRICT_COMPARE_SUGAR(TMPL,Table)
00697
00698 TMPL Table sqrt (const Table& t) { return unary_map<sqrt_op> (t); }
00699 TMPL Table exp (const Table& t) { return unary_map<exp_op> (t); }
00700 TMPL Table log (const Table& t) { return unary_map<log_op> (t); }
00701 TMPL Table cos (const Table& t) { return unary_map<cos_op> (t); }
00702 TMPL Table sin (const Table& t) { return unary_map<sin_op> (t); }
00703 TMPL Table tan (const Table& t) { return unary_map<tan_op> (t); }
00704 TMPL Table acos (const Table& t) { return unary_map<acos_op> (t); }
00705 TMPL Table asin (const Table& t) { return unary_map<asin_op> (t); }
00706 TMPL Table atan (const Table& t) { return unary_map<atan_op> (t); }
00707
00708 TMPL Table pow (const Table& t, const Table& u) {
00709 return binary_map<pow_op> (t, u); }
00710 TMPL Table pow (const Table& t, const int& u) {
00711 return binary_map_scalar<rpow_op> (t, u); }
00712 TMPL Table pow (const int& u, const Table& t) {
00713 return binary_map_scalar<lpow_op> (t, u); }
00714
00715 TMPL inline C big_add (const Table& t) { return big<add_op> (t); }
00716 TMPL inline C big_mul (const Table& t) { return big<mul_op> (t); }
00717 TMPL inline C big_or (const Table& t) { return big< or_op> (t); }
00718 TMPL inline C big_and (const Table& t) { return big<and_op> (t); }
00719 TMPL inline C big_min (const Table& t) { return big<min_op> (t); }
00720 TMPL inline C big_max (const Table& t) { return big<max_op> (t); }
00721 TMPL inline C big_inf (const Table& t) { return big<inf_op> (t); }
00722 TMPL inline C big_sup (const Table& t) { return big<sup_op> (t); }
00723
00724
00725
00726
00727
00728 TMPL inline bool is_finite (const Table& t) {
00729 return big<and_is_finite_op> (t); }
00730 TMPL inline bool is_nan (const Table& t) {
00731 return big<or_is_nan_op> (t); }
00732 TMPL inline bool is_infinite (const Table& t) {
00733 return !is_nan (t) && big<or_is_infinite_op> (t); }
00734 TMPL inline bool is_fuzz (const Table& t) {
00735 return !is_nan (t) && big<or_is_fuzz_op> (t); }
00736 TMPL inline bool is_reliable (const Table& t) {
00737 return is_reliable (C (0)); }
00738
00739 TMPL inline Table sharpen (const Table& t) {
00740 return unary_map<sharpen_op> (t); }
00741 TMPLK inline Table blur (const Table& t, const K& x) {
00742 return binary_map_scalar<blur_op> (t, x); }
00743
00744
00745
00746
00747
00748 TMPL table<T,C,V>
00749 reverse (const Table& t) {
00750 table<T,C,V> r (CF2 (t), CF1 (t));
00751 for (iterator<T> it= entries (t); busy (it); ++it)
00752 r[t[*it]]= *it;
00753 simplify (r);
00754 return r;
00755 }
00756
00757 TMPL Table&
00758 operator << (Table& t, const Table& u) {
00759 for (iterator<T> it= entries (u); busy (it); ++it)
00760 t[*it]= u[*it];
00761 return t;
00762 }
00763
00764 TMPL Table
00765 append (const Table& t, const Table& u) {
00766 Table r (I (u), CF2(t));
00767 for (iterator<T> it= entries (t); busy (it); ++it)
00768 r[*it]= t[*it];
00769 for (iterator<T> it= entries (u); busy (it); ++it)
00770 r[*it]= u[*it];
00771 return r;
00772 }
00773
00774 TMPL Table
00775 common (const Table& t, const Table& u) {
00776 Table r (I (t), CF2(t));
00777 for (iterator<T> it= entries (t); busy (it); ++it)
00778 if (V::val_op::op (u[*it], t[*it]))
00779 r[*it]= t[*it];
00780 return r;
00781 }
00782
00783 TMPL Table
00784 difference (const Table& t, const Table& u) {
00785 Table r (I (t), CF2(t));
00786 for (iterator<T> it= entries (t); busy (it); ++it)
00787 if (!V::val_op::op (u[*it], t[*it]))
00788 r[*it]= t[*it];
00789 return r;
00790 }
00791
00792
00793
00794
00795
00796 #define mmx_table(T,C) table<C,T >
00797
00798 template<typename T, typename C> inline table<C,T>
00799 make_mmx_table (const format<T>& fm1, const format<C>& fm2) {
00800 return table<C,T> (default_cst (fm2), fm1);
00801 }
00802
00803 template<typename T, typename C> inline table<C,T>
00804 make_mmx_table (const C& init, const format<T>& fm) {
00805 return table<C,T> (init, fm);
00806 }
00807
00808 template<typename T, typename C> inline table<C,T>
00809 make_mmx_table (const vector<pair<T,C> >& v) {
00810 return table<C,T> (default_cst (get_format2 (CF(v))), iterate (v));
00811 }
00812
00813 template<typename T, typename C> inline table<C,T>
00814 make_mmx_table (const C& init, const vector<pair<T,C> >& v) {
00815 return table<C,T> (init, iterate (v));
00816 }
00817
00818 template<typename T, typename C> inline T
00819 get_key (const pair<T,C>& p) { return p.x1; }
00820
00821 template<typename T, typename C> inline C
00822 get_value (const pair<T,C>& p) { return p.x2; }
00823
00824 template<typename C> inline bool
00825 ttable_contains (const GTAB& t, const GEN& a, const GEN& b, const VEC& c) {
00826 return contains (t, cons (a, cons (b, c)));
00827 }
00828
00829 template<typename C> inline C
00830 ttable_access (const GTAB& t, const GEN& a, const GEN& b, const VEC& c) {
00831 return t[cons (a, cons (b, c))];
00832 }
00833
00834 template<typename C> inline C&
00835 ttable_access (GTAB& t, const GEN& a, const GEN& b, const VEC& c) {
00836 return t[cons (a, cons (b, c))];
00837 }
00838
00839 template<typename C> inline void
00840 ttable_reset (GTAB& t, const GEN& a, const GEN& b, const VEC& c) {
00841 reset (t, cons (a, cons (b, c)));
00842 }
00843
00844 #undef TMPL_DEF
00845 #undef TMPL
00846 #undef TMPLK
00847 #undef Format1
00848 #undef Format2
00849 #undef Table
00850 #undef Table_rep
00851 #undef GEN
00852 #undef VEC
00853 #undef GTAB
00854 }
00855 #endif // __MMX_TABLE_HPP