00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 #ifndef __TUPLE_HPP
00014 #define __TUPLE_HPP
00015 #include <basix/vector.hpp>
00016 #include <basix/table.hpp>
00017 #include <basix/wrap.hpp>
00018 #include <basix/compound.hpp>
00019 
00021 
00022 namespace mmx {
00023 #define TMPL template<typename C>
00024 #define Tuple tuple<C>
00025 
00026 
00027 
00028 
00029 
00030 template<typename C>
00031 class tuple {
00032 MMX_ALLOCATORS
00033   generic rep;
00034 public:
00035   inline generic operator * () const { return rep; }
00036   inline tuple (const generic& g): rep (g) {}
00037   inline tuple (const Tuple& t): rep (t.rep) {}
00038   inline C operator[] (nat i) const { return as<C> (rep[i+1]); }
00039 };
00040 
00041 WRAP_INDIRECT_IMPL(TMPL inline,Tuple)
00042 
00043 TMPL inline nat N (const Tuple& t) { return N (*t) - 1; }
00044 
00045 TMPL syntactic
00046 flatten (const Tuple& t) {
00047   return flatten (gen (GEN_TUPLE, cdr (compound_to_vector (*t))));
00048 }
00049 
00050 WRAP_BINARY_IMPL_1(TMPL,Tuple,generic,"Tu","Tuple",C)
00051 
00052 
00053 
00054 
00055 
00056 TMPL Tuple
00057 as_tuple (const vector<C>& a) {
00058   nat i, n= N(a);
00059   vector<generic> t= fill<generic> (n+1);
00060   t[0]= GEN_TUPLE;
00061   for (i=0; i<n; i++)
00062     t[i+1]= as<generic> (a[i]);
00063   return Tuple (vector_to_compound (t));
00064 }
00065 
00066 TMPL vector<C>
00067 as_vector (const Tuple& t) {
00068   nat i, n= N(t);
00069   vector<C> a= fill<C> (n);
00070   for (i=0; i<n; i++)
00071     a[i]= t[i];
00072   return a;
00073 }
00074 
00075 template<typename T, typename F>
00076 struct as_helper<tuple<T>,tuple<F> > {
00077   static tuple<T> cv (const tuple<F>& r) {
00078     return as_tuple<T> (vector<T> (as_vector<F> (r))); }
00079 };
00080 
00081 
00082 TMPL iterator<C>
00083 iterate (const Tuple& t) {
00084   return iterate (as_vector (t));
00085 }
00086 
00087 
00088 
00089 
00090 
00091 inline nat new_tuple_type_id (nat id);
00092 
00093 TMPL struct type_information<Tuple > { static nat id; };
00094 TMPL nat type_information<Tuple >::id=
00095   new_tuple_type_id (type_information<C>::id);
00096 
00097 void tuple_type_info (nat& id, bool& mode);
00098 
00099 inline bool
00100 is_tuple_type (nat id) {
00101   bool mode= false;
00102   tuple_type_info (id, mode);
00103   return mode;
00104 }
00105 
00106 inline nat
00107 tuple_to_scalar (nat id) {
00108   bool mode= false;
00109   tuple_type_info (id, mode);
00110   return id;
00111 }
00112 
00113 inline nat
00114 new_tuple_type_id (nat id) {
00115   bool mode= true;
00116   tuple_type_info (id, mode);
00117   return id;
00118 }
00119 
00120 #undef TMPL
00121 #undef Tuple
00122 } 
00123 #endif // __TUPLE_HPP