Back to index

texmacs  1.0.7.15
array.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : array.cpp
00004 * DESCRIPTION: fixed size arrays with reference counting
00005 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #ifndef ARRAY_CC
00013 #define ARRAY_CC
00014 #include "array.hpp"
00015 
00016 /******************************************************************************
00017 * Routines intern to the array<T> class
00018 ******************************************************************************/
00019 
00020 static inline int
00021 round_length (int n, size_t s) {
00022   (void) s;
00023   if (n<6) return n;
00024   register int i=8;
00025   while (n>i) i<<=1;
00026   return i;
00027 }
00028 
00029 template<class T>
00030 array_rep<T>::array_rep (int n2):
00031   n(n2), a((n==0)?((T*) NULL):(tm_new_array<T> (round_length(n, sizeof (T))))) {}
00032 
00033 template<class T> void
00034 array_rep<T>::resize (register int m) {
00035   register int nn= round_length (n, sizeof (T));
00036   register int mm= round_length (m, sizeof (T));
00037   if (mm != nn) {
00038     if (mm != 0) {
00039       register int i, k= (m<n? m: n);
00040       T* b= tm_new_array<T> (mm);
00041       for (i=0; i<k; i++) b[i]= a[i];
00042       if (nn != 0) tm_delete_array (a);
00043       a= b;
00044     }
00045     else {
00046       if (nn != 0) tm_delete_array (a);
00047       a= NULL;
00048     }
00049   }
00050   n= m;
00051 }
00052 
00053 template<class T>
00054 array<T>::array (T* a, int n) {
00055   register int i;
00056   rep= tm_new<array_rep<T> > (n);
00057   for (i=0; i<n; i++)
00058     rep->a[i]=a[i];
00059 }
00060 
00061 template<class T>
00062 array<T>::array (T x1, T x2) {
00063   rep= tm_new<array_rep<T> > (2);
00064   rep->a[0]= x1;
00065   rep->a[1]= x2;
00066 }
00067 
00068 /******************************************************************************
00069 * Other routines on arrays
00070 ******************************************************************************/
00071 
00072 template<class T> bool
00073 operator == (array<T> a, array<T> b) {
00074   register int i;
00075   if (N(a)!=N(b)) return false;
00076   for (i=0; i<N(a); i++)
00077     if (a[i]!=b[i]) return false;
00078   return true;
00079 }
00080 
00081 template<class T> bool
00082 operator != (array<T> a, array<T> b) {
00083   register int i;
00084   if (N(a)!=N(b)) return true;
00085   for (i=0; i<N(a); i++)
00086     if (a[i]!=b[i]) return true;
00087   return false;
00088 }
00089 
00090 template<class T> tm_ostream&
00091 operator << (tm_ostream& out, array<T> a) {
00092   int i;
00093   
00094   if (N(a)==0) return out << "[ ]";
00095   out << "[ ";
00096   for (i=0; i<N(a)-1; i++)
00097     out << a[i] << ", ";
00098   if (N(a)!=0) out << a[i];
00099   out << " ]";
00100   return out;
00101 }
00102 
00103 template<class T> array<T>&
00104 operator << (array<T>& a, T x) {
00105   a->resize (N(a)+ 1);
00106   a[N(a)-1]=x;
00107   return a;
00108 }
00109 
00110 template<class T> array<T>&
00111 operator << (array<T>& a, array<T> b) {
00112   register int i, k= N(a);
00113   a->resize (N(a)+ N(b));
00114   for (i=0; i<N(b); i++) a[i+k]= b[i];
00115   return a;
00116 }
00117 
00118 template<class T> array<T>
00119 append (T a, array<T> b) {
00120   register int i, l= N(b);
00121   array<T> c (l+1);
00122   c[0]= a;
00123   for (i=0; i<l; i++) c[i+1]= b[i];
00124   return c;
00125 }
00126 
00127 template<class T> array<T>
00128 append (array<T> a, array<T> b) {
00129   register int i, k= N(a), l= N(b);
00130   array<T> c (k+l);
00131   for (i=0; i<k; i++) c[i]= a[i];
00132   for (i=0; i<l; i++) c[i+k]= b[i];
00133   return c;
00134 }
00135 
00136 template<class T> array<T>
00137 range (array<T> a, int i, int j) {
00138   register int k;
00139   ASSERT (i>=0 && j<=N(a), "out of range");
00140   array<T> r (j-i);
00141   for (k=i; k<j; k++) r[k-i]= a[k];
00142   return r;
00143 }
00144 
00145 template<class T> array<T>
00146 reverse (array<T> a) {
00147   register int i, n= N(a);
00148   array<T> r (n);
00149   for (i=0; i<n; i++) r[i]= a[n-1-i];
00150   return r;
00151 }
00152 
00153 template<class T> int
00154 hash (array<T> a) {
00155   int i, n=N(a), h= 0;
00156   for (i=0; i<n; i++)
00157     h= hash(a[i]) ^ ((h<<7) + (h>>25));
00158   return h;
00159 }
00160 
00161 #endif // defined ARRAY_CC