// 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 the GNU C++ Library. This library is free
software; you can redistribute it and/or modify it under the terms of
the GNU Library General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version. This library is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
<T>MChunk::<T>MChunk(<T>* d,
: <T>IChunk(d, baseidx, lowidx, fenceidx, topidx)
unsigned msize = (top - base)/_MAP_BITS + 1;
map = (unsigned long *) (new long[msize]);
memset((void*)map, 0, msize * sizeof(long));
void <T>MChunk:: shrink_high ()
if (fence <= low) empty_error();
void <T>MChunk:: shrink_low ()
if (fence <= low) empty_error();
void <T>MChunk::clear(int lo)
memset((void*)map, 0, ((top - base)/_MAP_BITS + 1) * sizeof(long));
void <T>MChunk::cleardown(int hi)
memset((void*)map, 0, ((top - base)/_MAP_BITS + 1) * sizeof(long));
int <T>MChunk::del(int idx)
if (idx < low || idx >= fence) index_error();
int <T>MChunk::undel(int idx)
if (idx < low || idx >= fence) index_error();
int <T>MChunk::unused_index() const
if (unused_indices() == 0) index_error();
if (low == base) // can traverse 32 slots at a time
while (map[blk] == ~0L) ++blk;
slot = blk * _MAP_BITS + base;
while(valid(slot)) ++slot;
int <T>MChunk::first_index() const
if (empty()) return fence;
while (map[blk] == 0) ++blk;
slot = blk * _MAP_BITS + base;
while(!valid(slot)) ++slot;
int <T>MChunk::last_index() const
if (empty()) return low - 1;
int blk = (top - base) / _MAP_BITS;
while (map[blk] == 0) --blk;
slot = blk * _MAP_BITS + base + _MAP_BITS - 1;
while(!valid(slot)) --slot;
int <T>MChunk:: OK() const
int v = data != 0; // have some data
v &= map != 0; // and a map
v &= base <= low; // ok, index-wise
v &= ((<T>MChunk*)(nxt->prev())) == this; // and links are OK
v &= ((<T>MChunk*)(prv->next())) == this;
int bitcount = 0; // and unused count correct
for (int i = low; i < fence; ++i) if (!valid(i)) ++bitcount;
if (!v) error("invariant failure");
<T>* <T>MChunk::succ(<T>* p) const
int i = ((int) p - (int) data) / sizeof(<T>) + base + 1;
if (p == 0 || i < low) return 0;
while (i < fence && !valid(i)) ++i;
if (i >= fence) return 0;
<T>* <T>MChunk::pred(<T>* p) const
int i = ((int) p - (int) data) / sizeof(<T>) + base - 1;
if (p == 0 || i >= fence) return 0;
while (i >= low && !valid(i)) --i;
<T>* <T>MChunk::first_pointer() const
while (map[blk] == 0) ++blk;
slot = blk * _MAP_BITS + base;
while(!valid(slot)) ++slot;
<T>* <T>MChunk::last_pointer() const
int blk = (top - base) / _MAP_BITS;
while (map[blk] == 0) --blk;
slot = blk * _MAP_BITS + base + _MAP_BITS - 1;
while(!valid(slot)) --slot;
csize = DEFAULT_INITIAL_CAPACITY;
<T>* data = new <T>[csize];
hd = ch = new <T>MChunk(data, lo, lo, fnc, lo+csize);
<T>MPlex:: <T>MPlex(int chunksize)
if (chunksize == 0) error("invalid constructor specification");
<T>* data = new <T>[csize];
hd = ch = new <T>MChunk(data, lo, lo, fnc, csize);
<T>* data = new <T>[csize];
hd = ch = new <T>MChunk(data, chunksize, lo, fnc, fnc);
<T>MPlex:: <T>MPlex(int l, int chunksize)
if (chunksize == 0) error("invalid constructor specification");
<T>* data = new <T>[csize];
hd = ch = new <T>MChunk(data, lo, lo, fnc, csize+lo);
<T>* data = new <T>[csize];
hd = ch = new <T>MChunk(data, chunksize+lo, lo, fnc, fnc);
void <T>MPlex::make_initial_chunks(int up)
<T>* data = new <T> [csize];
<T>MChunk* h = new <T>MChunk(data, l, l, l+sz, l+csize);
<T>* data = new <T> [csize];
<T>MChunk* h = new <T>MChunk(data, hi-csize, hi-sz, hi, hi);
<T>MPlex:: <T>MPlex(int l, int hi, const <T&> initval, int chunksize)
for (int i=lo; i<fnc; ++i)
<T>MPlex::<T>MPlex(const <T>MPlex& a)
const <T>IChunk* p = a.hd;
<T>* data = new <T> [p->size()];
<T>MChunk* h = new <T>MChunk(data, p->base_index(),
p->low_index(), p->fence_index(), p->top_index());
for (int i = a.low(); i < a.fence(); a.next(i))
void <T>MPlex::operator= (const <T>MPlex& a)
const <T>IChunk* p = a.hd;
<T>* data = new <T> [p->size()];
<T>MChunk* h = new <T>MChunk(data, p->base_index(),
p->low_index(), p->fence_index(),
for (int i = a.low(); i < a.fence(); a.next(i))
int <T>MPlex::valid(int idx) const
const <T>MChunk* tail = (<T>MChunk*)tl();
while (idx >= t->fence_index())
t = ((<T>MChunk*)(t->next()));
while (idx < t->low_index())
if (t == (<T>MChunk*)(hd)) return 0;
t = ((<T>MChunk*)(t->prev()));
return t-><T>MChunk::valid_index(idx);
void <T>MPlex::cache(int idx) const
const <T>MChunk* tail = (<T>MChunk*)tl();
while (idx >= t->fence_index())
if (t == tail) index_error();
t = ((<T>MChunk*)(t->next()));
while (idx < t->low_index())
if (t == (<T>MChunk*)hd) index_error();
t = ((<T>MChunk*)(t->prev()));
if (!t-><T>MChunk::valid_index(idx)) index_error();
void <T>MPlex::cache(const <T>* p) const
const <T>MChunk* old = ch;
while (!t->actual_pointer(p))
t = ((<T>MChunk*)(t->next()));
if (t == old) index_error();
if (!t-><T>MChunk::valid_pointer(p)) index_error();
int <T>MPlex::owns(Pix px) const
const <T>MChunk* old = ch;
while (!t->actual_pointer(p))
t = ((<T>MChunk*)(t->next()));
return t-><T>MChunk::valid_pointer(p);
int <T>MPlex::add_high(const <T&> elem)
<T>MChunk* t = ((<T>MChunk*) tl());
<T>* data = new <T> [csize];
t = (new <T>MChunk(data, fnc,fnc,fnc,fnc+csize));
*((t-><T>MChunk::grow_high())) = elem;
int <T>MPlex::add_low (const <T&> elem)
<T>MChunk* t = ((<T>MChunk*) hd);
<T>* data = new <T> [csize];
hd = new <T>MChunk(data, lo-csize, lo, lo, lo);
*((t-><T>MChunk::grow_low())) = elem;
void <T>MPlex::adjust_bounds()
<T>MChunk* t = ((<T>MChunk*) tl());
while (t-><T>MChunk::empty() && !one_chunk())
<T>MChunk* pred = (<T>MChunk*)(t->prev());
while (t-><T>MChunk::empty() && !one_chunk())
hd = (<T>MChunk*)(t->next());
int <T>MPlex::del_high ()
if (empty()) empty_error();
<T>MChunk* t = ((<T>MChunk*) tl());
while (t-><T>MChunk::empty() && !one_chunk()) // possible stragglers
<T>MChunk* pred = (<T>MChunk*)(t->prev());
t-><T>MChunk::shrink_high();
while (t-><T>MChunk::empty() && !one_chunk())
<T>MChunk* pred = (<T>MChunk*)(t->prev());
unused -= oldfnc - fnc - 1;
if (empty()) empty_error();
<T>MChunk* t = ((<T>MChunk*) hd);
while (t-><T>MChunk::empty() && !one_chunk())
hd = (<T>MChunk*)(t->next());
t-><T>MChunk::shrink_low();
while (t-><T>MChunk::empty() && !one_chunk())
hd = (<T>MChunk*)(t->next());
unused -= lo - oldlo - 1;
int <T>MPlex::add(const <T&> elem)
t->unused_indices() == 0;
t = (<T>MChunk*)(t->prev()))
int i = t->unused_index();
int <T>MPlex::unused_index() const
if (unused == 0) index_error();
t->unused_indices() == 0;
t = (<T>MChunk*)(t->prev()))
return t->unused_index();
Pix <T>MPlex::unused_Pix() const
if (unused == 0) return 0;
t->unused_indices() == 0;
t = (<T>MChunk*)(t->prev()))
return t->pointer_to(t->unused_index());
int <T>MPlex::del_index(int idx)
if (idx < lo || idx >= fnc) index_error();
if (<T>MPlex::valid(idx))
int <T>MPlex::dopred(int idx) const
if (idx >= fnc) idx = fnc;
if (idx <= lo) return lo - 1;
while (idx > t->fence_index())
t = ((<T>MChunk*)(t->next()));
while (idx <= t->low_index())
t = ((<T>MChunk*)(t->prev()));
int i = t-><T>MChunk::pred(idx);
while (i < t->low_index() && i >= lo)
t = ((<T>MChunk*)(t->prev()));
i = t-><T>MChunk::last_index();
int <T>MPlex::dosucc(int idx) const
if (idx >= fnc - 1) return fnc;
while (idx >= t->fence_index())
t = ((<T>MChunk*)(t->next()));
while (idx < t->low_index())
t = ((<T>MChunk*)(t->prev()));
int i = t-><T>MChunk::succ(idx);
while (i >= t->fence_index() && i < fnc)
t = (<T>MChunk*)(t->next());
i = t-><T>MChunk::first_index();
void <T>MPlex::prev(Pix& i) const
const <T>MChunk* old = ch;
while (!t->actual_pointer(p))
t = ((<T>MChunk*)(t->prev()));
<T>* q = t-><T>MChunk::pred(p);
while (q == 0 && t != (<T>MChunk*)hd)
t = ((<T>MChunk*)(t->prev()));
q = t-><T>MChunk::last_pointer();
void <T>MPlex::next(Pix& i) const
const <T>MChunk* tail = (<T>MChunk*)(tl());
const <T>MChunk* old = ch;
while (!t->actual_pointer(p))
t = ((<T>MChunk*)(t->next()));
<T>* q = t-><T>MChunk::succ(p);
while (q == 0 && t != tail)
t = ((<T>MChunk*)(t->next()));
q = t-><T>MChunk::first_pointer();
void <T>MPlex::undel_index(int idx)
if (idx < lo || idx >= fnc) index_error();
while (idx >= t->fence_index())
t = ((<T>MChunk*)(t->next()));
while (idx < t->low_index())
t = ((<T>MChunk*)(t->prev()));
int was_present = t-><T>MChunk::undel(idx);
<T>MChunk* t = ((<T>MChunk*)tl());
<T>MChunk* prv = (<T>MChunk*)(t->prev());
int <T>MPlex::OK () const
int v = hd != 0; // at least one chunk
int found_ch = 0; // to make sure ch is in list;
int count = 0; // to count unused slots
const <T>MChunk* t = (<T>MChunk*)(hd);
int gap = t->low_index() - lo;
v &= gap == 0; // hd lo not less than lo.
v &= t-><T>MChunk::OK(); // each chunk is OK
count += t->unused_indices();
if (t == (<T>MChunk*)(tl()))
else // and has indices less than succ
gap = t->next()->base_index() - t->top_index();
if (t != (<T>MChunk*)hd) // internal chunks can't grow
v &= !t->can_grow_low() && !t->can_grow_high();
t = (const <T>MChunk*)(t->next());
gap = fnc - t->fence_index();
v &= count == unused; // chunk counts agree with plex
if (!v) error("invariant failure");