home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / GNU_1OF3.ZIP / HEADERS.ZIP / g++-include / gen / Vec.hP < prev    next >
Encoding:
Text File  |  1992-03-06  |  3.3 KB  |  135 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 the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19.  
  20. #ifndef _<T>Vec_h
  21. #ifdef __GNUG__
  22. #pragma interface
  23. #endif
  24. #define _<T>Vec_h 1
  25.  
  26. #include <builtin.h>
  27.  
  28. #ifndef _<T>_typedefs
  29. #define _<T>_typedefs 1
  30. typedef void (*<T>Procedure)(<T&>);
  31. typedef <T>  (*<T>Mapper)(<T&>);
  32. typedef <T>  (*<T>Combiner)(<T&>, <T&>);
  33. typedef int  (*<T>Predicate)(<T&>);
  34. typedef int  (*<T>Comparator)(<T&>, <T&>);
  35. #endif
  36.  
  37.  
  38. class <T>Vec 
  39. {
  40. protected:      
  41.   int                   len;
  42.   <T>                   *s;                  
  43.  
  44.                         <T>Vec(int l, <T>* d);
  45. public:
  46.                         <T>Vec ();
  47.                         <T>Vec (int l);
  48.                         <T>Vec (int l, <T&> fill_value);
  49.                         <T>Vec (<T>Vec&);
  50.                         ~<T>Vec ();
  51.  
  52.   <T>Vec &              operator = (<T>Vec & a);
  53.   <T>Vec                at(int from = 0, int n = -1);
  54.  
  55.   int                   capacity();
  56.   void                  resize(int newlen);                        
  57.  
  58.   <T>&                  operator [] (int n);
  59.   <T>&                  elem(int n);
  60.  
  61.   friend <T>Vec         concat(<T>Vec & a, <T>Vec & b);
  62.   friend <T>Vec         map(<T>Mapper f, <T>Vec & a);
  63.   friend <T>Vec         merge(<T>Vec & a, <T>Vec & b, <T>Comparator f);
  64.   friend <T>Vec         combine(<T>Combiner f, <T>Vec & a, <T>Vec & b);
  65.   friend <T>Vec         reverse(<T>Vec & a);
  66.  
  67.   void                  reverse();
  68.   void                  sort(<T>Comparator f);
  69.   void                  fill(<T&> val, int from = 0, int n = -1);
  70.  
  71.   void                  apply(<T>Procedure f);
  72.   <T>                   reduce(<T>Combiner f, <T&> base);
  73.   int                   index(<T&> targ);
  74.  
  75.   friend int            operator == (<T>Vec& a, <T>Vec& b);
  76.   friend int            operator != (<T>Vec& a, <T>Vec& b);
  77.  
  78.   void                  error(const char* msg);
  79.   void                  range_error();
  80. };
  81.  
  82. extern void default_<T>Vec_error_handler(const char*);
  83. extern one_arg_error_handler_t <T>Vec_error_handler;
  84.  
  85. extern one_arg_error_handler_t 
  86.         set_<T>Vec_error_handler(one_arg_error_handler_t f);
  87.  
  88.  
  89. inline <T>Vec::<T>Vec()
  90. {
  91.   len = 0; s = 0;
  92. }
  93.  
  94. inline <T>Vec::<T>Vec(int l)
  95. {
  96.   s = new <T> [len = l];
  97. }
  98.  
  99.  
  100. inline <T>Vec::<T>Vec(int l, <T>* d) :len(l), s(d) {}
  101.  
  102.  
  103. inline <T>Vec::~<T>Vec()
  104. {
  105.   delete [] s;
  106. }
  107.  
  108.  
  109. inline <T>& <T>Vec::operator [] (int n)
  110. {
  111.   if ((unsigned)n >= (unsigned)len)
  112.     range_error();
  113.   return s[n];
  114. }
  115.  
  116. inline <T>& <T>Vec::elem(int n)
  117. {
  118.   return s[n];
  119. }
  120.  
  121.  
  122. inline int <T>Vec::capacity()
  123. {
  124.   return len;
  125. }
  126.  
  127.  
  128.  
  129. inline int operator != (<T>Vec& a, <T>Vec& b)
  130. {
  131.   return !(a == b);
  132. }
  133.  
  134. #endif
  135.