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