home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / leda / incl / array.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  5.7 KB  |  193 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  array.h
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16.  
  17.  
  18. #ifndef ARRAYH
  19. #define ARRAYH
  20.  
  21. #include <LEDA/basic.h>
  22.  
  23. //------------------------------------------------------------------------------
  24. // array
  25. // 
  26. // Stefan Naeher  (1989)
  27. //------------------------------------------------------------------------------
  28.  
  29.  
  30.  
  31. class ent_array {
  32.     ent* v;
  33.     int sz;    
  34.         int Low;
  35.         int High;
  36.         int t;
  37.         int dir;
  38.         void quick_sort(int l, int r);
  39.         void quick_sort(int l, int r, CMP_ENT f);
  40.  
  41. virtual int cmp(ent& x, ent& y)  { return int(x)-int(y); };
  42. virtual void print_el(ent& x,ostream& out) const { out << int(x); }
  43. virtual void read_el(ent& x,istream& in)   const { in >> *(int*)&x; }
  44. virtual void clear_entry(ent& x) { x = 0; }
  45. virtual void copy_entry(ent& x)  { x = 0; }
  46. virtual void init_entry(ent& x)  { x = 0; }
  47.  
  48. protected:
  49.         void clear();
  50.  
  51. public:
  52.    void init();
  53.     ~ent_array() { delete v; }
  54.  
  55.     ent_array(int, int);
  56.     ent_array(int);
  57.     ent_array(ent_array&);
  58.     ent_array& operator=(ent_array&);
  59.         int size() { return sz; }
  60.         int low()  { return Low; }
  61.         int high() { return High; }
  62.     ent& elem(int i)       { return v[i]; }
  63.     ent  elem(int i) const { return v[i]; }
  64.     ent& entry(int i)
  65.     { if (i<Low || i>High)
  66.           error_handler(2,"array::entry index out of range");
  67.           return v[i-Low];
  68.          }
  69.     ent  inf(int i) const
  70.     { if (i<Low || i>High)
  71.           error_handler(2,"array::inf index out of range");
  72.           return v[i-Low];
  73.          }
  74.  
  75.         void permute(int,int);
  76.  
  77.         void permute()  { permute(Low,High); }
  78.  
  79.         void sort(CMP_ENT f, int l, int h, bool randomize) 
  80.         { if (randomize) permute();
  81.           quick_sort(l-Low,h-Low,f);
  82.          }
  83.  
  84.         void sort(CMP_ENT f, bool randomize) 
  85.         { if (randomize) permute();
  86.           quick_sort(0,sz-1,f); 
  87.          }
  88.  
  89.         int binary_search(ent);
  90.         int binary_search(ent,CMP_ENT);
  91.  
  92.    void print(ostream&,string, char space)   const;    
  93.    void print(ostream& out,char space=' ') const { print(out,"",space);  }
  94.    void print(string s, char space=' ')    const { print(cout,s,space);  }
  95.    void print(char space=' ')              const { print(cout,"",space); }   
  96.  
  97.  
  98.    void read(istream&,string);  
  99.    void read(istream& in)      { read(in,"");  }
  100.    void read(string s )        { read(cin,s);  }   
  101.    void read()                 { read(cin,""); }   
  102.  
  103.  
  104. };
  105.  
  106.  
  107. #define array(type) name2(type,array)
  108.  
  109. #define arraydeclare(type)\
  110. \
  111. typedef int (*name2(type,_array_cmp))(type&,type&);\
  112. \
  113. class array(type) : public ent_array {\
  114. \
  115. /*int     cmp(ent& x, ent& y) { return compare(type(x),type(y)); }*/\
  116. \
  117. void print_el(ent& x,ostream& out) const { Print(*(type*)&x,out);}\
  118. void read_el(ent& x,istream& in)   const { type y=0; Read(y,in); x = Copy(y); }\
  119. void clear_entry(ent& x)  { Clear(*(type*)&x); }\
  120. void copy_entry(ent& x)   { Copy(*(type*)&x);  }\
  121. void init_entry(ent& x)   { type y=0; x = Copy(y); }\
  122. \
  123. public:\
  124. array(type)(int a, int b)   : ent_array(a,b)             { init(); }\
  125. array(type)(int n)          : ent_array(n)               { init(); }\
  126. array(type)(array(type)& A) : ent_array((ent_array&) A)  {}\
  127. ~array(type)()                                           { clear(); }\
  128. \
  129. array(type)& operator=(array(type)& A)\
  130.               { return (array(type)&)ent_array::operator=((ent_array&) A); }\
  131. type& operator[](int i)             { return *(type*)&ent_array::entry(i);}\
  132. type  operator[](int i)   const     { return type(ent_array::inf(i));}\
  133. void sort(name2(type,_array_cmp) f, bool rand=true)\
  134.                                     { CMP_ENT g = CMP_ENT(f);\
  135.                                       ent_array::sort(g,rand); }\
  136. void sort(name2(type,_array_cmp) f,int l, int h, bool rand=true)\
  137.                                     { CMP_ENT g = CMP_ENT(f);\
  138.                                       ent_array::sort(g,l,h,rand); }\
  139. int binary_search(type x)           { return ent_array::binary_search(Ent(x));}\
  140. int binary_search(type x,name2(type,_array_cmp) f)\
  141.                          { return ent_array::binary_search(Ent(x),CMP_ENT(f));}\
  142. };
  143.  
  144. /*------------------------------------------------------------------------*/
  145. /* 2 dimensional arrays                                                   */
  146. /*------------------------------------------------------------------------*/
  147.  
  148.  
  149.  
  150. class ent_array2 {
  151. ent_array A;
  152. int Low1, Low2, High1, High2;
  153. virtual void clear_entry(ent& x)  { x = 0; }
  154. virtual void copy_entry(ent& x)   { x = 0; }
  155. virtual void init_entry(ent& x)   { x = 0; }
  156.  
  157. protected:
  158. void clear();
  159. ent_array* row(int i) const { return (ent_array*)A.inf(i); }
  160.  
  161. public:
  162. void init(int,int,int,int);
  163. int low1()  { return Low1; }
  164. int low2()  { return Low2; }
  165. int high1() { return High1; }
  166. int high2() { return High2; }
  167. ent_array2(int,int,int,int);
  168. ent_array2(int,int);
  169. ~ent_array2();
  170. };
  171.  
  172.  
  173. #define array2(type) name2(type,array2)
  174.  
  175. #define array2declare(type)\
  176. \
  177. struct array2(type) : public ent_array2 {\
  178. \
  179. void clear_entry(ent& x)  { Clear(*(type*)&x); }\
  180. void copy_entry(ent& x)   { x = Copy(*(type*)&x);  }\
  181. void init_entry(ent& x)   { type y=0; x = Copy(y); }\
  182. \
  183. type& operator()(int i, int j)       { return *(type*)&(row(i)->entry(j)); }\
  184. type  operator()(int i, int j) const { return type(row(i)->entry(j)); }\
  185. \
  186.  array2(type)(int a,int b,int c,int d) :ent_array2(a,b,c,d){ init(a,b,c,d);}\
  187.  array2(type)(int n,int m)             :ent_array2(n,m)    { init(0,n-1,0,m-1);}\
  188. ~array2(type)()                                            { clear();         }\
  189. };
  190.  
  191. #endif
  192.