Back to index

texmacs  1.0.7.15
array.hpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : array
00004 * DESCRIPTION: fixed size arrays with reference counting and
00005 *              pointer copying
00006 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
00007 *******************************************************************************
00008 * This software falls under the GNU general public license version 3 or later.
00009 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00010 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00011 ******************************************************************************/
00012 
00013 #ifndef ARRAY_H
00014 #define ARRAY_H
00015 #include "basic.hpp"
00016 
00017 class tree;
00018 template<class T> class array;
00019 template<class T> int N (array<T> a);
00020 template<class T> T*  A (array<T> a);
00021 template<class T> array<T> copy (array<T> x);
00022 
00023 template<class T> class array_rep: concrete_struct {
00024   int n;
00025   T* a;
00026 
00027 public:
00028   inline array_rep (): n(0), a(NULL) {}
00029          array_rep (int n);
00030   inline ~array_rep () { if (n!=0) tm_delete_array (a); }
00031   void resize (int n);
00032   friend class array<T>;
00033   friend int N LESSGTR (array<T> a);
00034   friend T*  A LESSGTR (array<T> a);
00035   friend array<T> copy LESSGTR (array<T> a);
00036 };
00037 
00038 template<class T> class array {
00039   CONCRETE_TEMPLATE(array,T);
00040   inline array (int n=0): rep (tm_new<array_rep<T> > (n)) {}
00041   array (T *a, int n);
00042   array (T x1, T x2);
00043   inline T& operator [] (int i) { return rep->a[i]; }
00044   operator tree (); // defined in tree.hpp
00045 };
00046 CONCRETE_TEMPLATE_CODE(array,class,T);
00047 
00048 #define TMPL template<class T>
00049 TMPL inline int N (array<T> a) { return a->n; }
00050 TMPL inline T*  A (array<T> a) { return a->a; }
00051 TMPL inline array<T> copy (array<T> a) {
00052   return array<T> (a->a, a->n); }
00053 TMPL tm_ostream& operator << (tm_ostream& out, array<T> a);
00054 TMPL array<T>& operator << (array<T>& a, T x);
00055 TMPL array<T>& operator << (array<T>& a, array<T> b);
00056 TMPL array<T> append (T a, array<T> b);
00057 TMPL array<T> append (array<T> a, array<T> b);
00058 TMPL array<T> range (array<T> a, int i, int j);
00059 TMPL array<T> reverse (array<T> a);
00060 TMPL bool operator == (array<T> a, array<T> b);
00061 TMPL bool operator != (array<T> a, array<T> b);
00062 
00063 TMPL int hash (array<T> a);
00064 #undef TMPL
00065 
00066 #ifdef STRING_H
00067 // Function parameters participating in template signature resolution are never
00068 // cast implicitely. 
00069 inline array<string>& operator << (array<string>& a, char* x) {
00070   return a << string(x); }
00071 #endif
00072 
00073 #include "array.cpp"
00074 
00075 #endif // defined ARRAY_H