home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / rplex.hp < prev    next >
Text File  |  1993-07-23  |  6KB  |  230 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.     based on code by Marc Shapiro (shapiro@sor.inria.fr)
  6.  
  7. This file is part of GNU CC.
  8.  
  9. GNU CC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the GNU CC General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. GNU CC, but only under the conditions described in the
  18. GNU CC General Public License.   A copy of this license is
  19. supposed to have been given to you along with GNU CC so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies.  
  23. */
  24.  
  25. #ifndef _<T>RPlex_h
  26. #pragma once
  27. #define _<T>RPlex_h 1
  28.  
  29. #include "<T>.Plex.h"
  30.  
  31. // minimum number of chunks to index
  32.  
  33. #ifndef MIN_NCHUNKS
  34. #define MIN_NCHUNKS  16
  35. #endif
  36.  
  37. class <T>RPlex: public <T>Plex
  38. {
  39.   int              base;        // base index of lowest chunk
  40.   int              lch;         // index of lowest used chunk
  41.   int              fch;         // 1 + index of highest used chunk
  42.   int              maxch;       // max chunks in array
  43.   <T>IChunk**      chunks;      // array of chunks
  44.   <T>IChunk*       ch;          // cached chunk
  45.  
  46.   void             make_initial_chunks(int up = 1);
  47.  
  48.   void             cache(int idx);
  49.   void             cache(const <T>* p);
  50.   <T>*             dopred(<T>* p);
  51.   <T>*             dosucc(<T>* p);
  52.  
  53. public:
  54.                    <T>RPlex();                 // set low = 0;
  55.                                                // fence = 0;
  56.                                                // csize = default
  57.  
  58.                    <T>RPlex(int ch_size);      // low = 0; 
  59.                                                // fence = 0;
  60.                                                // csize = ch_size
  61.  
  62.                    <T>RPlex(int lo,            // low = lo; 
  63.                             int ch_size);      // fence=lo
  64.                                                // csize = ch_size
  65.  
  66.                    <T>RPlex(int lo,            // low = lo
  67.                             int hi,            // fence = hi+1
  68.                             const <T&> initval,// fill with initval,
  69.                             int ch_size = 0);  // csize= ch_size
  70.                                                // or fence-lo if 0
  71.  
  72.                    <T>RPlex(const <T>RPlex&);
  73.  
  74.                    ~<T>RPlex();
  75.  
  76.   void             operator= (const <T>RPlex&);
  77.  
  78. // virtuals
  79.  
  80.  
  81.   <T>&             high_element ();
  82.   <T>&             low_element ();
  83.  
  84.   Pix              first();
  85.   Pix              last();
  86.   void             prev(Pix& ptr);
  87.   void             next(Pix& ptr);
  88.   int              owns(Pix p);
  89.   <T>&             operator () (Pix p);
  90.  
  91.   int              low(); 
  92.   int              high();
  93.   int              valid(int idx);
  94.   void             prev(int& idx);
  95.   void             next(int& x);
  96.   <T>&             operator [] (int index);
  97.     
  98.   int              Pix_to_index(Pix p);
  99.   Pix              index_to_Pix(int idx);    
  100.  
  101.   int              can_add_high();
  102.   int              can_add_low();
  103.   int              full();
  104.  
  105.   int              add_high(const <T&> elem);
  106.   int              del_high ();
  107.   int              add_low (const <T&> elem);
  108.   int              del_low ();
  109.  
  110.   void             fill(const <T&> x);
  111.   void             fill(const <T&> x, int from, int to);
  112.   void             clear();
  113.   int              reset_low(int l);
  114.   void             reverse();
  115.   void             append(const <T>Plex& a); 
  116.   void             prepend(const <T>Plex& a);
  117.     
  118.   int              OK (); 
  119. };
  120.  
  121. inline void <T>RPlex::prev(int& idx)
  122. {
  123.   --idx;
  124. }
  125.  
  126. inline void <T>RPlex::next(int& idx)
  127. {
  128.   ++idx;
  129. }
  130.  
  131. inline  int <T>RPlex::full ()
  132. {
  133.   return 0;
  134. }
  135.  
  136. inline int <T>RPlex::can_add_high()
  137. {
  138.   return 1;
  139. }
  140.  
  141. inline int <T>RPlex::can_add_low()
  142. {
  143.   return 1;
  144. }
  145.  
  146. inline  int <T>RPlex::valid (int idx)
  147. {
  148.   return idx >= lo && idx < fnc;
  149. }
  150.  
  151. inline int <T>RPlex::low()
  152. {
  153.   return lo;
  154. }
  155.  
  156. inline int <T>RPlex::high()
  157. {
  158.   return fnc - 1;
  159. }
  160.  
  161. inline  <T>& <T>RPlex::low_element ()
  162. {
  163.   if (empty()) index_error();
  164.   return *(hd->pointer_to(lo));
  165. }
  166.  
  167. inline  <T>& <T>RPlex::high_element ()
  168. {
  169.   if (empty()) index_error();
  170.   return *(tl()->pointer_to(fnc - 1));
  171. }
  172.  
  173. inline  int <T>RPlex::Pix_to_index(Pix p)
  174. {
  175.   if (!ch->actual_pointer(p)) cache(p);
  176.   return ch->index_of(p);
  177. }
  178.  
  179. inline  Pix <T>RPlex::index_to_Pix(int idx)
  180. {
  181.   if (!ch->actual_index(idx)) cache(idx);
  182.   return (Pix)(ch->pointer_to(idx));
  183. }
  184.  
  185. inline Pix <T>RPlex::first()
  186. {
  187.   return Pix(hd-><T>IChunk::first_pointer());
  188. }
  189.  
  190. inline Pix <T>RPlex::last()
  191. {
  192.   return Pix(tl()-><T>IChunk::last_pointer());
  193. }
  194.  
  195. inline void <T>RPlex::prev(Pix& p)
  196. {
  197.   Pix q = Pix(ch-><T>IChunk::pred((<T>*)p));
  198.   p = (q == 0)? Pix(dopred((<T>*)p)) : q;
  199. }
  200.  
  201. inline void <T>RPlex::next(Pix& p)
  202. {
  203.   Pix q = Pix(ch-><T>IChunk::succ((<T>*)p));
  204.   p = (q == 0)? Pix(dosucc((<T>*)p)) : q;
  205. }
  206.  
  207. inline <T>& <T>RPlex:: operator () (Pix p)
  208. {
  209.   return *((<T>*)p);
  210. }
  211.  
  212. inline void <T>RPlex::cache(int idx)
  213. {
  214.   if (idx < lo || idx >= fnc) index_error();
  215.   ch = chunks[(idx - base) / csize];
  216. }  
  217.  
  218. inline <T>& <T>RPlex:: operator [] (int idx)
  219. {
  220.   cache(idx); return *(ch->pointer_to(idx));
  221. }
  222.  
  223. inline <T>RPlex::~<T>RPlex()
  224. {
  225.   delete chunks;
  226. }
  227.  
  228.  
  229. #endif
  230.