home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / vec.hp < prev    next >
Text File  |  1993-07-23  |  4KB  |  175 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY.  No author or distributor
  10. accepts responsibility to anyone for the consequences of using it
  11. or for whether it serves any particular purpose or works at all,
  12. unless he says so in writing.  Refer to the GNU CC General Public
  13. License for full details.
  14.  
  15. Everyone is granted permission to copy, modify and redistribute
  16. GNU CC, but only under the conditions described in the
  17. GNU CC General Public License.   A copy of this license is
  18. supposed to have been given to you along with GNU CC so you
  19. can know your rights and responsibilities.  It should be in a
  20. file named COPYING.  Among other things, the copyright notice
  21. and this notice must be preserved on all copies.  
  22. */
  23.  
  24.  
  25. #ifndef _<T>Vec_h
  26. #pragma once
  27. #define _<T>Vec_h 1
  28.  
  29. #ifndef _<T>_typedefs
  30. #define _<T>_typedefs 1
  31. typedef void (*<T>Procedure)(<T&>);
  32. typedef <T>  (*<T>Mapper)(<T&>);
  33. typedef <T>  (*<T>Combiner)(<T&>, <T&>);
  34. typedef int  (*<T>Predicate)(<T&>);
  35. typedef int  (*<T>Comparator)(<T&>, <T&>);
  36. #endif
  37.  
  38.  
  39. class <T>Vec 
  40. {
  41. protected:      
  42.   int                   len;
  43.   <T>                   *s;                  
  44.  
  45.                         <T>Vec(int l, <T>* d);
  46. public:
  47.                         <T>Vec ();
  48.                         <T>Vec (int l);
  49.                         <T>Vec (int l, <T&> fill_value);
  50.                         <T>Vec (<T>Vec&);
  51.                         ~<T>Vec ();
  52.  
  53.   <T>Vec &              operator = (<T>Vec & a);
  54.   <T>Vec                at(int from = 0, int n = -1);
  55.  
  56.   int                   capacity();
  57.   void                  resize(int newlen);                        
  58.  
  59.   <T>&                  operator [] (int n);
  60.   <T>&                  elem(int n);
  61.  
  62.   friend <T>Vec         concat(<T>Vec & a, <T>Vec & b);
  63.   friend <T>Vec         map(<T>Mapper f, <T>Vec & a);
  64.   friend <T>Vec         merge(<T>Vec & a, <T>Vec & b, <T>Comparator f);
  65.   friend <T>Vec         combine(<T>Combiner f, <T>Vec & a, <T>Vec & b);
  66.   friend <T>Vec         reverse(<T>Vec & a);
  67.  
  68.   void                  reverse();
  69.   void                  sort(<T>Comparator f);
  70.   void                  fill(<T&> val, int from = 0, int n = -1);
  71.  
  72.   void                  apply(<T>Procedure f);
  73.   <T>                   reduce(<T>Combiner f, <T&> base);
  74.   int                   index(<T&> targ);
  75.  
  76.   friend int            operator == (<T>Vec& a, <T>Vec& b);
  77.   friend int            operator != (<T>Vec& a, <T>Vec& b);
  78.  
  79.   void                  error(const char* msg);
  80.   void                  range_error();
  81. };
  82.  
  83.  
  84. inline <T>Vec::<T>Vec()
  85. {
  86.   len = 0; s = 0;
  87. }
  88.  
  89. inline <T>Vec::<T>Vec(int l)
  90. {
  91.   s = new <T> [len = l];
  92. }
  93.  
  94. inline <T>Vec::<T>Vec(int l, <T&> fill_value)
  95. {
  96.   s = new <T> [len = l];
  97.   <T>* top = &(s[len]);
  98.   <T>* t = s;
  99.   while (t < top) *t++ = fill_value;
  100. }
  101.  
  102. inline <T>Vec::<T>Vec(int l, <T>* d)
  103. {
  104.   len = l;
  105.   s = d;
  106. }
  107.  
  108. inline <T>Vec::<T>Vec(<T>Vec& v)
  109. {
  110.   s = new <T> [len = v.len];
  111.   <T>* top = &(s[len]);
  112.   <T>* t = s;
  113.   <T>* u = v.s;
  114.   while (t < top) *t++ = *u++;
  115. }
  116.  
  117. inline <T>Vec::~<T>Vec()
  118. {
  119.   delete[len] s;
  120. }
  121.  
  122. inline <T>Vec& <T>Vec::operator = (<T>Vec& v)
  123. {
  124.   if (this != &v)
  125.   {
  126.     delete[len] s;
  127.     s = new <T> [len = v.len];
  128.     <T>* top = &(s[len]);
  129.     <T>* t = s;
  130.     <T>* u = v.s;
  131.     while (t < top) *t++ = *u++;
  132.   }
  133.   return *this;
  134. }
  135.  
  136. inline <T>& <T>Vec::operator [] (int n)
  137. {
  138.   if ((unsigned)n >= len)
  139.     range_error();
  140.   return s[n];
  141. }
  142.  
  143. inline <T>& <T>Vec::elem(int n)
  144. {
  145.   return s[n];
  146. }
  147.  
  148.  
  149. inline int <T>Vec::capacity()
  150. {
  151.   return len;
  152. }
  153.  
  154. inline void <T>Vec::apply(<T>Procedure f)
  155. {
  156.   <T>* top = &(s[len]);
  157.   <T>* t = s;
  158.   while (t < top) (*f)(*t++);
  159. }
  160.  
  161.  
  162. inline int operator != (<T>Vec& a, <T>Vec& b)
  163. {
  164.   return !(a == b);
  165. }
  166.  
  167.  
  168. extern void default_<T>Vec_error_handler(char*);
  169. extern one_arg_error_handler_t <T>Vec_error_handler;
  170.  
  171. extern one_arg_error_handler_t 
  172.         set_<T>Vec_error_handler(one_arg_error_handler_t f);
  173.  
  174. #endif
  175.