home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / xplex.ccp < prev    next >
Encoding:
Text File  |  1993-07-23  |  7.7 KB  |  389 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>.XPlex.h"
  26.  
  27.  
  28. <T>XPlex:: <T>XPlex()
  29. {
  30.   mods = 0;
  31.   lo = fnc = 0;
  32.   csize = DEFAULT_INITIAL_CAPACITY;
  33.   <T>* data = new <T>[csize];
  34.   hd = ch = new <T>IChunk(data,  lo, lo, fnc, lo+csize);
  35. }
  36.  
  37. <T>XPlex:: <T>XPlex(int chunksize)
  38. {
  39.   if (chunksize == 0) error("invalid constructor specification");
  40.   mods = 0;
  41.   lo = fnc = 0;
  42.   if (chunksize > 0)
  43.   {
  44.     csize = chunksize;
  45.     <T>* data = new <T>[csize];
  46.     hd = ch = new <T>IChunk(data,  lo, lo, fnc, csize);
  47.   }
  48.   else
  49.   {
  50.     csize = -chunksize;
  51.     <T>* data = new <T>[csize];
  52.     hd = ch = new <T>IChunk(data,  chunksize, lo, fnc, fnc);
  53.   }
  54. }
  55.  
  56.  
  57. <T>XPlex:: <T>XPlex(int l, int chunksize)
  58. {
  59.   if (chunksize == 0) error("invalid constructor specification");
  60.   mods = 0;
  61.   lo = fnc = l;
  62.   if (chunksize > 0)
  63.   {
  64.     csize = chunksize;
  65.     <T>* data = new <T>[csize];
  66.     hd = ch = new <T>IChunk(data,  lo, lo, fnc, csize+lo);
  67.   }
  68.   else
  69.   {
  70.     csize = -chunksize;
  71.     <T>* data = new <T>[csize];
  72.     hd = ch = new <T>IChunk(data,  chunksize+lo, lo, fnc, fnc);
  73.   }
  74. }
  75.  
  76. void <T>XPlex::make_initial_chunks(int up = 1)
  77. {
  78.   int need = fnc - lo;
  79.   hd = 0;
  80.   if (up)
  81.   {
  82.     int l = lo;
  83.     do
  84.     {
  85.       int sz;
  86.       if (need >= csize)
  87.         sz = csize;
  88.       else
  89.         sz = need;
  90.       <T>* data = new <T> [csize];
  91.       <T>IChunk* h = new <T>IChunk(data,  l, l, l+sz, l+csize);
  92.       if (hd != 0)
  93.         h->link_to_next(hd);
  94.       else
  95.         hd = h;
  96.       l += sz;
  97.       need -= sz;
  98.     } while (need > 0);
  99.   }
  100.   else
  101.   {
  102.     int hi = fnc;
  103.     do
  104.     {
  105.       int sz;
  106.       if (need >= csize)
  107.         sz = csize;
  108.       else
  109.         sz = need;
  110.       <T>* data = new <T> [csize];
  111.       <T>IChunk* h = new <T>IChunk(data,  hi-csize, hi-sz, hi, hi);
  112.       if (hd != 0)
  113.         h->link_to_next(hd);
  114.       hd = h;
  115.       hi -= sz;
  116.       need -= sz;
  117.     } while (need > 0);
  118.   }
  119.   ch = hd;
  120. }
  121.  
  122. <T>XPlex:: <T>XPlex(int l, int hi, const <T&> initval, int chunksize = 0)
  123. {
  124.   mods = 0;
  125.   lo = l;
  126.   fnc = hi + 1;
  127.   if (chunksize == 0)
  128.   {
  129.     csize = fnc - l;
  130.     make_initial_chunks(1);
  131.   }
  132.   else if (chunksize < 0)
  133.   {
  134.     csize = -chunksize;
  135.     make_initial_chunks(0);
  136.   }
  137.   else
  138.   {
  139.     csize = chunksize;
  140.     make_initial_chunks(1);
  141.   }
  142.   fill(initval);
  143. }
  144.  
  145. <T>XPlex::<T>XPlex(const <T>XPlex& a)
  146. {
  147.   mods = 0;
  148.   lo = a.lo;
  149.   fnc = a.fnc;
  150.   csize = a.csize;
  151.   make_initial_chunks();
  152.   for (int i = a.low(); i < a.fence(); a.next(i)) (*this)[i] = a[i];
  153. }
  154.  
  155. void <T>XPlex::operator= (const <T>XPlex& a)
  156. {
  157.   if (&a != this)
  158.   {
  159.     invalidate();
  160.     record_change();
  161.     lo = a.lo;
  162.     fnc = a.fnc;
  163.     csize = a.csize;
  164.     make_initial_chunks();
  165.     for (int i = a.low(); i < a.fence(); a.next(i)) (*this)[i] = a[i];
  166.   }
  167. }
  168.  
  169.  
  170. void <T>XPlex::cache(int idx)
  171. {
  172.   <T>IChunk* tail = tl();
  173.   while (idx >= ch->fence_index())
  174.   {
  175.     if (ch == tail) index_error();
  176.     ch = ch->next();
  177.   }
  178.   while (idx < ch->low_index())
  179.   {
  180.     if (ch == hd) index_error();
  181.     ch = ch->prev();
  182.   }
  183. }
  184.  
  185.  
  186. void <T>XPlex::cache(const <T>* p)
  187. {
  188.   <T>IChunk* old = ch;
  189.   while (!ch->actual_pointer(p))
  190.   {
  191.     ch = ch->next();
  192.     if (ch == old) index_error();
  193.   }
  194. }
  195.  
  196. int <T>XPlex::owns(Pix p)
  197. {
  198.   <T>IChunk* old = ch;
  199.   while (!ch->actual_pointer(p))
  200.   {
  201.     ch = ch->next();
  202.     if (ch == old) return 0;
  203.   }
  204.   return 1;
  205. }
  206.  
  207.  
  208. <T>* <T>XPlex::dosucc(<T>* p)
  209. {
  210.   if (p == 0) return 0;
  211.   <T>IChunk* old = ch;
  212.   while (!ch->actual_pointer(p))
  213.   {
  214.     ch = ch->next();
  215.     if (ch == old) return 0;
  216.   }
  217.   int i = ch->index_of(p) + 1;
  218.   if (i >= fnc) return 0;
  219.   if (i >= ch->fence_index()) ch = ch->next();
  220.   return (ch->pointer_to(i));
  221. }
  222.  
  223. <T>* <T>XPlex::dopred(<T>* p)
  224. {
  225.   if (p == 0) return 0;
  226.   <T>IChunk* old = ch;
  227.   while (!ch->actual_pointer(p))
  228.   {
  229.     ch = ch->prev();
  230.     if (ch == old) return 0;
  231.   }
  232.   int i = ch->index_of(p) - 1;
  233.   if (i < lo) return 0;
  234.   if (i < ch->low_index()) ch = ch->prev();
  235.   return (ch->pointer_to(i));
  236. }
  237.  
  238.  
  239. int <T>XPlex::add_high(const <T&> elem)
  240. {
  241.   record_change();
  242.   ch =  tl();
  243.   if (!ch->can_grow_high())
  244.   {
  245.     <T>* data = new <T> [csize];
  246.     ch = new <T>IChunk(data,  fnc, fnc, fnc,fnc+csize);
  247.     ch->link_to_prev(tl());
  248.   }
  249.   *((ch-><T>IChunk::grow_high())) = elem;
  250.   return fnc++;
  251. }
  252.  
  253. int <T>XPlex::del_high ()
  254. {
  255.   if (empty()) empty_error();
  256.   record_change();
  257.   ch =  tl();
  258.   ch-><T>IChunk::shrink_high();
  259.   if (ch-><T>IChunk::empty() && !one_chunk())
  260.   {
  261.     <T>IChunk* pred = ch->prev();
  262.     del_chunk(ch);
  263.     ch = pred;
  264.   }
  265.   return --fnc - 1;
  266. }
  267.  
  268. int <T>XPlex::add_low (const <T&> elem)
  269. {
  270.   record_change();
  271.   ch =  hd;
  272.   if (!ch->can_grow_low())
  273.   {
  274.     <T>* data = new <T> [csize];
  275.     hd = new <T>IChunk(data,  lo-csize, lo, lo, lo);
  276.     hd->link_to_next(ch);
  277.     ch =  hd;
  278.   }
  279.   *((ch-><T>IChunk::grow_low())) = elem;
  280.   return --lo;
  281. }
  282.  
  283.  
  284. int <T>XPlex::del_low ()
  285. {
  286.   if (empty()) empty_error();
  287.   record_change();
  288.   ch =  hd;
  289.   ch-><T>IChunk::shrink_low();
  290.   if (ch-><T>IChunk::empty() && !one_chunk())
  291.   {
  292.     hd = ch->next();
  293.     del_chunk(ch);
  294.     ch =  hd;
  295.   }
  296.   return ++lo;
  297. }
  298.  
  299. void <T>XPlex::append (const <T>Plex& a)
  300. {
  301.   for (int i = a.low(); i < a.fence(); a.next(i)) add_high(a[i]);
  302. }
  303.  
  304. void <T>XPlex::prepend (const <T>Plex& a)
  305. {
  306.   for (int i = a.high(); i > a.ecnef(); a.prev(i)) add_low(a[i]);
  307. }
  308.  
  309. void <T>XPlex::reverse()
  310. {
  311.   <T> tmp;
  312.   int l = lo;
  313.   int h = fnc - 1;
  314.   ch = hd;
  315.   <T>IChunk* hich = tl();
  316.   while (l < h)
  317.   {
  318.     <T>* lptr = ch->pointer_to(l);
  319.     <T>* hptr = hich->pointer_to(h);
  320.     tmp = *lptr;
  321.     *lptr = *hptr;
  322.     *hptr = tmp;
  323.     if (++l >= ch->fence_index()) ch = ch->next();
  324.     if (--h < hich->low_index()) hich = hich->prev();
  325.   }
  326. }
  327.  
  328. void <T>XPlex::fill(<T&> x)
  329. {
  330.   for (int i = lo; i < fnc; ++i) (*this)[i] = x;
  331. }
  332.  
  333. void <T>XPlex::fill(<T&> x, int l, int hi)
  334. {
  335.   for (int i = l; i <= hi; ++i) (*this)[i] = x;
  336. }
  337.  
  338.  
  339. void <T>XPlex::clear()
  340. {
  341.   if (fnc != lo)
  342.   {
  343.     record_change();
  344.     ch = tl();
  345.     while (ch != hd)
  346.     {
  347.       <T>IChunk* prv = ch->prev();
  348.       del_chunk(ch);
  349.       ch = prv;
  350.     }
  351.     ch-><T>IChunk::clear(lo);
  352.     fnc = lo;
  353.   }
  354. }
  355.  
  356.  
  357. int <T>XPlex::OK ()
  358. {
  359.   int v = hd != 0 && ch != 0;     // at least one chunk
  360.  
  361.   v &= fnc == tl()->fence_index();// last chunk fence == plex fence
  362.   v &= lo == ((hd))-><T>IChunk::low_index();    // first lo == plex lo
  363.  
  364. // loop for others:
  365.   int found_ch = 0;                   // to make sure ch is in list;
  366.   <T>IChunk* t = (hd);
  367.   for (;;)
  368.   {
  369.     if (t == ch) ++found_ch;
  370.     v &= t-><T>IChunk::OK();              // each chunk is OK
  371.     if (t == tl())
  372.       break;
  373.     else                              // and has indices contiguous to succ
  374.     {
  375.       v &= t->top_index() == t->next()->base_index();
  376.       if (t != hd)                  // internal chunks full
  377.       {
  378.         v &= !t->empty();
  379.         v &= !t->can_grow_low();
  380.         v &= !t->can_grow_high();
  381.       }
  382.       t = t->next();
  383.     }
  384.   }
  385.   v &= found_ch == 1;
  386.   if (!v) error("invariant failure");
  387.   return v;
  388. }
  389.