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