home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / fplex.ccp < prev    next >
Text File  |  1993-07-23  |  4KB  |  187 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. #include "<T>.FPlex.h"
  26.  
  27.  
  28. <T>FPlex:: <T>FPlex()
  29. {
  30.   mods = 0;
  31.   lo = fnc = 0;
  32.   csize = DEFAULT_INITIAL_CAPACITY;
  33.   <T>* data = new <T>[csize];
  34.   hd = new <T>IChunk(data,  lo, lo, fnc, csize);
  35. }
  36.  
  37. <T>FPlex:: <T>FPlex(int maxsize)
  38. {
  39.   if (maxsize == 0) error("invalid constructor specification");
  40.   mods = 0;
  41.   lo = fnc = 0;
  42.   if (maxsize > 0)
  43.   {
  44.     csize = maxsize;
  45.     <T>* data = new <T>[csize];
  46.     hd = new <T>IChunk(data,  lo, lo, fnc, csize);
  47.   }
  48.   else
  49.   {
  50.     csize = -maxsize;
  51.     <T>* data = new <T>[csize];
  52.     hd = new <T>IChunk(data,  maxsize, lo, fnc, fnc);
  53.   }
  54. }
  55.  
  56.  
  57. <T>FPlex:: <T>FPlex(int l, int maxsize)
  58. {
  59.   if (maxsize == 0) error("invalid constructor specification");
  60.   mods = 0;
  61.   lo = fnc = l;
  62.   if (maxsize > 0)
  63.   {
  64.     csize = maxsize;
  65.     <T>* data = new <T>[csize];
  66.     hd = new <T>IChunk(data,  lo, lo, fnc, csize+lo);
  67.   }
  68.   else
  69.   {
  70.     csize = -maxsize;
  71.     <T>* data = new <T>[csize];
  72.     hd = new <T>IChunk(data,  maxsize+lo, lo, fnc, fnc);
  73.   }
  74. }
  75.  
  76. <T>FPlex:: <T>FPlex(int l, int hi, const <T&> initval, int maxsize = 0)
  77. {
  78.   mods = 0;
  79.   lo = l;
  80.   fnc = hi + 1;
  81.   if (maxsize >= 0)
  82.   {
  83.     csize = maxsize;
  84.     if (csize < fnc - lo)
  85.       csize = fnc - lo;
  86.     <T>* data = new <T>[csize];
  87.     hd = new <T>IChunk(data,  lo, lo, fnc, csize);
  88.   }
  89.   else
  90.   {
  91.     csize = -maxsize;
  92.     if (csize < fnc - lo)
  93.       csize = fnc - lo;
  94.     <T>* data = new <T>[csize];
  95.     hd = new <T>IChunk(data,  -csize, lo, fnc, fnc);
  96.   }
  97.   fill(initval);
  98. }
  99.  
  100. <T>FPlex::<T>FPlex(const <T>FPlex& a)
  101. {
  102.   mods = 0;
  103.   lo = a.lo;
  104.   fnc = a.fnc;
  105.   csize = fnc - lo;
  106.   if (csize < a.csize) csize = a.csize;
  107.   <T>* data = new <T> [csize];
  108.   hd = new <T>IChunk(data,  lo, lo, fnc, lo+csize);
  109.   for (int i = a.low(); i < a.fence(); a.next(i)) (*this)[i] = a[i];
  110. }
  111.  
  112. void <T>FPlex::operator= (const <T>FPlex& a)
  113. {
  114.   if (&a != this)
  115.   {
  116.     del_chunk(hd);
  117.     record_change();
  118.     lo = a.lo;
  119.     fnc = a.fnc;
  120.     csize = fnc - lo;
  121.     if (csize < a.csize) csize = a.csize;
  122.     <T>* data = new <T> [csize];
  123.     hd = new <T>IChunk(data,  lo, lo, fnc, lo+csize);
  124.     for (int i = a.low(); i < a.fence(); a.next(i)) (*this)[i] = a[i];
  125.   }
  126. }
  127.  
  128.  
  129. void <T>FPlex::append (const <T>Plex& a)
  130. {
  131.   for (int i = a.low(); i < a.fence(); a.next(i)) add_high(a[i]);
  132. }
  133.  
  134. void <T>FPlex::prepend (const <T>Plex& a)
  135. {
  136.   for (int i = a.high(); i > a.ecnef(); a.prev(i)) add_low(a[i]);
  137. }
  138.  
  139. void <T>FPlex::reverse()
  140. {
  141.   <T> tmp;
  142.   int l = lo;
  143.   int h = fnc - 1;
  144.   while (l < h)
  145.   {
  146.     tmp = (*this)[l];
  147.     (*this)[l] = (*this)[h];
  148.     (*this)[h] = tmp;
  149.     next(l);
  150.     prev(h);
  151.   }
  152. }
  153.  
  154. void <T>FPlex::fill(<T&> x)
  155. {
  156.   for (int i = lo; i < fnc; ++i) (*this)[i] = x;
  157. }
  158.  
  159. void <T>FPlex::fill(<T&> x, int lo, int hi)
  160. {
  161.   for (int i = lo; i <= hi; ++i) (*this)[i] = x;
  162. }
  163.  
  164. void <T>FPlex::clear()
  165. {
  166.   if (fnc != lo)
  167.   {
  168.     record_change();
  169.     hd->clear(lo);
  170.     fnc = lo;
  171.   }
  172. }
  173.  
  174. int <T>FPlex::OK ()
  175. {
  176.   int v = hd != 0;                    // hd exists
  177.   v &= hd-><T>IChunk::OK();           // and is OK
  178.   v &= fnc - lo <= hd->size();        // and has enough space
  179.   v &= lo <= fnc;                     // plex indices consistent
  180.   v &= lo == hd->low_index();         // and match those 
  181.   v &= fnc == hd->fence_index();      //   of chunk
  182.   v &= one_chunk();                   // and only one chunk
  183.   if (!v) error("invariant failure");
  184.   return v;
  185. }
  186.  
  187.