// This may look like C code, but it is really -*- C++ -*-
Copyright (C) 1988 Free Software Foundation
written by Doug Lea (dl@rocky.oswego.edu)
based on code by Marc Shapiro (shapiro@sor.inria.fr)
This file is part of GNU CC.
GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY. No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing. Refer to the GNU CC General Public
License for full details.
Everyone is granted permission to copy, modify and redistribute
GNU CC, but only under the conditions described in the
GNU CC General Public License. A copy of this license is
supposed to have been given to you along with GNU CC so you
can know your rights and responsibilities. It should be in a
file named COPYING. Among other things, the copyright notice
and this notice must be preserved on all copies.
typedef <T>IChunk* _<T>IChunk_ptr;
csize = DEFAULT_INITIAL_CAPACITY;
<T>* data = new <T>[csize];
set_cache(new <T>IChunk(data, lo, lo, fnc, lo+csize));
base = ch->base_index() - lch * csize;
chunks = new _<T>IChunk_ptr[maxch];
<T>RPlex:: <T>RPlex(int chunksize)
if (chunksize == 0) error("invalid constructor specification");
<T>* data = new <T>[csize];
set_cache(new <T>IChunk(data, lo, lo, fnc, csize+lo));
<T>* data = new <T>[csize];
set_cache(new <T>IChunk(data, chunksize+lo, lo, fnc, fnc));
base = ch->base_index() - lch * csize;
chunks = new _<T>IChunk_ptr[maxch];
<T>RPlex:: <T>RPlex(int l, int chunksize)
if (chunksize == 0) error("invalid constructor specification");
<T>* data = new <T>[csize];
set_cache(new <T>IChunk(data, lo, lo, fnc, lo+csize));
<T>* data = new <T>[csize];
set_cache(new <T>IChunk(data, chunksize+lo, lo, fnc, fnc));
base = ch->base_index() - lch * csize;
chunks = new _<T>IChunk_ptr[maxch];
void <T>RPlex::make_initial_chunks(int up)
<T>* data = new <T> [csize];
<T>IChunk* h = new <T>IChunk(data, l, l, l+sz, l+csize);
<T>* data = new <T> [csize];
<T>IChunk* h = new <T>IChunk(data, hi-csize, hi-sz, hi, hi);
set_cache((<T>IChunk*)hd);
chunks = new _<T>IChunk_ptr[maxch];
base = ch->base_index() - csize * lch;
<T>RPlex:: <T>RPlex(int l, int hi, const <T&> initval, int chunksize)
<T>RPlex::<T>RPlex(const <T>RPlex& a)
for (int i = a.low(); i < a.fence(); a.next(i)) (*this)[i] = a[i];
void <T>RPlex::operator= (const <T>RPlex& a)
for (int i = a.low(); i < a.fence(); a.next(i)) (*this)[i] = a[i];
void <T>RPlex::cache(const <T>* p) const
const <T>IChunk* old = ch;
while (!t->actual_pointer(p))
if (t == old) index_error();
int <T>RPlex::owns(Pix px) const
const <T>IChunk* old = ch;
while (!t->actual_pointer(p))
<T>* <T>RPlex::dosucc(const <T>* p) const
const <T>IChunk* old = ch;
while (!t->actual_pointer(p))
int i = t->index_of(p) + 1;
if (i >= t->fence_index()) t = (t->next());
<T>* <T>RPlex::dopred(const <T>* p) const
const <T>IChunk* old = ch;
while (!t->actual_pointer(p))
int i = t->index_of(p) - 1;
if (i < t->low_index()) t = (t->prev());
return (t->pointer_to(i));
int <T>RPlex::add_high(const <T&> elem)
if (t-><T>IChunk::empty() && one_chunk())
base = t->base_index() - lch * csize;
<T>* data = new <T> [csize];
t = (new <T>IChunk(data, fnc, fnc, fnc,fnc+csize));
<T>IChunk** newch = new _<T>IChunk_ptr [maxch];
bcopy(chunks, newch, fch * sizeof(_<T>IChunk_ptr));
*((t-><T>IChunk::grow_high())) = elem;
int <T>RPlex::del_high ()
if (empty()) empty_error();
if (t-><T>IChunk::empty()) // kill straggler first
<T>IChunk* pred = t->prev();
t-><T>IChunk::shrink_high();
if (t-><T>IChunk::empty() && !one_chunk())
<T>IChunk* pred = t->prev();
int <T>RPlex::add_low (const <T&> elem)
if (t-><T>IChunk::empty() && one_chunk())
base = t->base_index() - lch * csize;
<T>* data = new <T> [csize];
hd = new <T>IChunk(data, lo-csize, lo, lo, lo);
<T>IChunk** newch = new _<T>IChunk_ptr [maxch];
bcopy(chunks, &(newch[lch]), lch * sizeof(_<T>IChunk_ptr));
base = t->base_index() - (lch - 1) * csize;
*((t-><T>IChunk::grow_low())) = elem;
if (empty()) empty_error();
if (t-><T>IChunk::empty())
t-><T>IChunk::shrink_low();
if (t-><T>IChunk::empty() && !one_chunk())
void <T>RPlex::append(const <T>RPlex& a)
for (int i = a.low(); i < a.fence(); a.next(i)) add_high(a[i]);
void <T>RPlex::prepend (const <T>RPlex& a)
for (int i = a.high(); i > a.ecnef(); a.prev(i)) add_low(a[i]);
<T>* lptr = loch->pointer_to(l);
<T>* hptr = hich->pointer_to(h);
if (++l >= loch->fence_index()) loch = loch->next();
if (--h < hich->low_index()) hich = hich->prev();
void <T>RPlex::fill(const <T&> x)
for (int i = lo; i < fnc; ++i) (*this)[i] = x;
void <T>RPlex::fill(const <T&> x, int lo, int hi)
for (int i = lo; i <= hi; ++i) (*this)[i] = x;
for (int i = lch + 1; i < fch; ++i)
ch-><T>IChunk::clear(lo);
int <T>RPlex::reset_low(int l)
t->re_index(t->low_index() + diff);
base = hd->base_index() - lch * csize;
int <T>RPlex::OK () const
int v = hd != 0 && ch != 0; // at least one chunk
v &= fnc == tl()->fence_index(); // last chunk fnc == plex fnc
v &= lo == hd-><T>IChunk::low_index(); // first lo == plex lo
v &= base == hd->base_index() - lch * csize; // base is correct;
v &= fch <= maxch; // within allocation;
int k = lch; // to cross-check nch
int found_ch = 0; // to make sure ch is in list;
const <T>IChunk* t = (hd);
v &= chunks[k++] == t; // each chunk is at proper index
v &= t-><T>IChunk::OK(); // each chunk is OK
else // and has indices contiguous to succ
v &= t->top_index() == t->next()->base_index();
if (t != hd) // internal chunks full
v &= !t->can_grow_high();
if (!v) error("invariant failure");