basix_doc 0.1
|
00001 00002 /****************************************************************************** 00003 * MODULE : tuple.hpp 00004 * DESCRIPTION: Syntactic tuple type constructor 00005 * COPYRIGHT : (C) 2006 Joris van der Hoeven 00006 ******************************************************************************* 00007 * This software falls under the GNU general public license and comes WITHOUT 00008 * ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details. 00009 * If you don't have this file, write to the Free Software Foundation, Inc., 00010 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 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 * The tuple class 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 * Useful conversions 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 * Type information for tuple types 00089 ******************************************************************************/ 00090 00091 TMPL struct type_information<Tuple > { static nat id; }; 00092 TMPL nat type_information<Tuple >::id= 00093 new_tuple_type_id (type_information<C>::id); 00094 00095 void tuple_type_info (nat& id, bool& mode); 00096 00097 inline bool 00098 is_tuple_type (nat id) { 00099 bool mode= false; 00100 tuple_type_info (id, mode); 00101 return mode; 00102 } 00103 00104 inline nat 00105 tuple_to_scalar (nat id) { 00106 bool mode= false; 00107 tuple_type_info (id, mode); 00108 return id; 00109 } 00110 00111 inline nat 00112 new_tuple_type_id (nat id) { 00113 bool mode= true; 00114 tuple_type_info (id, mode); 00115 return id; 00116 } 00117 00118 #undef TMPL 00119 #undef Tuple 00120 } // namespace mmx 00121 #endif // __TUPLE_HPP