DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: P T

⟦e8253d5ca⟧ TextFile

    Length: 12826 (0x321a)
    Types: TextFile
    Names: »Plex.hP«

Derivation

└─⟦a05ed705a⟧ Bits:30007078 DKUUG GNU 2/12/89
    └─⟦cc8755de2⟧ »./libg++-1.36.1.tar.Z« 
        └─⟦23757c458⟧ 
            └─⟦this⟧ »libg++/g++-include/Plex.hP« 

TextFile

// 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.  
*/

#ifndef _<T>Plex_h
#pragma once
#define _<T>Plex_h 1

#include <std.h>
#include <Pix.h>
#include "<T>.defs.h"

// Plexes are made out of <T>IChunks

#include <stddef.h>

class <T>IChunk
{
//public: // kludge until C++ `protected' policies settled
protected:      

  <T>*           data;           // data, from client

  int            base;           // lowest possible index
  int            low;            // lowest valid index
  int            fence;          // highest valid index + 1
  int            top;            // highest possible index + 1

  <T>IChunk*     nxt;            // circular links
  <T>IChunk*     prv;

public:

// constructors

                 <T>IChunk(<T>*     d,       // ptr to array of elements
                        int      base_idx, // initial indices
                        int      low_idx,  
                        int      fence_idx,
                        int      top_idx);

                 ~<T>IChunk();

// status reports

  int            size();         // number of slots

  virtual int    empty();
  virtual int    full(); 

  int            can_grow_high ();  // there is space to add data in chunk
  int            can_grow_low ();        
  
  int            base_index();   // lowest possible index;
  int            low_index();    // lowest actual index;
  virtual int    first_index();  // lowest valid index or fence if none
  virtual int    last_index();   // highest valid index or low-1 if none
  int            fence_index();  // highest actual index + 1
  int            top_index();    // highest possible index + 1

// indexing conversion

  int            possible_index(int i); // i between base and top
  int            actual_index(int i);   // i between low and fence
  virtual int    valid_index(int i);    // i not deleted (mainly for mchunks)

  int            possible_pointer(const <T>* p); // same for ptr
  int            actual_pointer(const <T>* p); 
  virtual int    valid_pointer(const <T>* p); 

  <T>*           pointer_to(int i);   // pointer to data indexed by i
                                      // caution: i is not checked for validity
  int            index_of(const <T>* p); // index of data pointed to by p
                                      // caution: p is not checked for validity

  virtual int    succ(int idx);     // next valid index or fence if none
  virtual int    pred(int idx);     // previous index or low - 1 if none

  virtual <T>*   first_pointer();   // pointer to first valid pos or 0
  virtual <T>*   last_pointer();    // pointer to first valid pos or 0
  virtual <T>*   succ(<T>*  p);     // next pointer or 0
  virtual <T>*   pred(<T>* p);     // previous pointer or 0

// modification

  virtual <T>*   grow_high ();      // return spot to add an element
  virtual <T>*   grow_low ();  

  virtual void   shrink_high ();    // logically delete top index
  virtual void   shrink_low ();     

  virtual void   clear(int lo);     // reset to empty ch with base = lo
  void           re_index(int lo);  // re-index so lo is new low

// chunk traversal

  <T>IChunk*     next();
  <T>IChunk*     prev();

  void           link_to_prev(<T>IChunk* prev);
  void           link_to_next(<T>IChunk* next);
  void           unlink();

// state checks

  <T>*           invalidate();     // mark self as invalid; return data
                                   // for possible deletion

  virtual int    OK();             // representation invariant

  void           error(char*);
  void           empty_error();
  void           full_error();
  void           index_error();
};

// <T>Plex is a partly `abstract' class: few of the virtuals
// are implemented at the Plex level, only in the subclasses

class <T>Plex
{
//public: // kludge until C++ `protected' policies settled
protected:      

  <T>IChunk*       hd;          // a chunk holding the data
  int              lo;          // lowest  index
  int              fnc;         // highest index + 1
  int              csize;       // size of the chunk
  int              mods;        // number of structural changes since creation

  void             record_change();           // record a change

  void             invalidate();              // mark so OK() is false
  void             del_chunk(<T>IChunk*);        // delete a chunk

  <T>IChunk*       tl();                      // last chunk;
  int              one_chunk();               // true if hd == tl()

public:

// constructors, etc.

                    <T>Plex();                  // no-op

  virtual           ~<T>Plex();

  
// Access functions 
    
  virtual <T>&      operator [] (int idx) = 0; // access by index;
  virtual <T>&      operator () (Pix p) = 0;   // access by Pix;

  virtual <T>&      high_element () = 0;      // access high element
  virtual <T>&      low_element () = 0;       // access low element


// Index functions

  virtual int       valid (int idx) = 0;      // idx is an OK index

  virtual int       low() = 0;                // lowest index or fence if none
  virtual int       high() = 0;               // highest index or low-1 if none

  int               ecnef();              // low limit index (low-1)
  int               fence();              // high limit index (high+1)

  virtual void      prev(int& idx) = 0;       // set idx to preceding index
                                          // caution: pred may be out of bounds

  virtual void      next(int& idx) = 0;       // set to next index
                                          // caution: succ may be out of bounds

  virtual Pix       first() = 0;              // Pix to low element or 0
  virtual Pix       last() = 0;               // Pix to high element or 0
  virtual void      prev(Pix& pix) = 0;       // preceding pix or 0
  virtual void      next(Pix& pix) = 0;       // next pix or 0
  virtual int       owns(Pix p) = 0;          // p is an OK Pix

// index<->Pix 

  virtual int       Pix_to_index(Pix p) = 0;   // get index via Pix
  virtual Pix       index_to_Pix(int idx) = 0; // Pix via index

// Growth

  virtual int       add_high(const <T&> elem) =0;// add new element at high end
                                                // return new high

  virtual int       add_low(const <T&> elem) = 0;   // add new low element,
                                                // return new low

// Shrinkage

  virtual int       del_high() = 0;           // remove the element at high end
                                          // return new high
  virtual int       del_low() = 0;        // delete low element, return new lo

                                          // caution: del_low/high
                                          // does not necessarily 
                                          // immediately call <T>::~<T>


// operations on multiple elements

  virtual void      fill(const <T&> x);          // set all elements = x
  virtual void      fill(const <T&> x, int from, int to); // fill from to to
  virtual void      clear() = 0;                // reset to zero-sized Plex
  virtual int       reset_low(int newlow); // change low index,return old
  virtual void      reverse();                   // reverse in-place
  virtual void      append(<T>Plex& a);    // concatenate a copy
  virtual void      prepend(<T>Plex& a);   // prepend a copy

// status

  virtual int       can_add_high() = 0;
  virtual int       can_add_low() = 0;
  
  int               length ();             // number of slots

  int               empty ();              // is the plex empty?
  virtual int       full() = 0;            // it it full?

  int               chunk_size();          // report chunk size;

  int               changes();             // return modification state
  int               changed(int s = 0);    // true if changes differ

  virtual int       OK() = 0;                  // representation invariant

  void              error(char* msg);
  void              index_error();
  void              empty_error();
  void              full_error();
};

// <T>IChunk ops

inline int <T>IChunk:: size()
{
  return top - base;
}


inline int <T>IChunk:: base_index()
{
  return base;
}

inline  int <T>IChunk:: low_index()
{
  return low;
}

inline  int  <T>IChunk:: fence_index()
{
  return fence;
}

inline  int  <T>IChunk:: top_index()
{
  return top;
}

inline  <T>* <T>IChunk:: pointer_to(int i)
{
  return &(data[i-base]);
}

inline  int  <T>IChunk:: index_of(const <T>* p)
{
  return ((int)p - (int)data) / sizeof(<T>) + base;
}

inline  int  <T>IChunk:: possible_index(int i)
{
  return i >= base && i < top;
}

inline  int  <T>IChunk:: possible_pointer(const <T>* p)
{
  return p >= data && p < &(data[top-base]);
}

inline  int  <T>IChunk:: actual_index(int i)
{
  return i >= low && i < fence;
}

inline  int  <T>IChunk:: actual_pointer(const <T>* p)
{
  return p >= data && p < &(data[fence-base]);
}

inline  int  <T>IChunk:: can_grow_high ()
{
  return fence < top;
}

inline  int  <T>IChunk:: can_grow_low ()
{
  return base < low;
}

inline  <T>* <T>IChunk:: invalidate()
{
  <T>* p = data;
  data = 0;
  return p;
}

inline <T>IChunk:: ~<T>IChunk() {}

inline <T>IChunk* <T>IChunk::prev()
{
  return prv;
}

inline <T>IChunk* <T>IChunk::next()
{
  return nxt;
}

inline void <T>IChunk::link_to_prev(<T>IChunk* prev)
{
  nxt = prev->nxt;
  prv = prev;
  nxt->prv = this;
  prv->nxt = this;
}

inline void <T>IChunk::link_to_next(<T>IChunk* next)
{
  prv = next->prv;
  nxt = next;
  nxt->prv = this;
  prv->nxt = this;
}

inline void <T>IChunk::unlink()
{
  <T>IChunk* n = nxt;
  <T>IChunk* p = prv;
  n->prv = p;
  p->nxt = n;
  prv = nxt = this;
}

inline  int <T>IChunk:: empty()
{
  return low == fence;
}

inline  int  <T>IChunk:: full()
{
  return top - base == fence - low;
}

inline int <T>IChunk:: first_index()
{
  return (low == fence)? fence : low;
}

inline int <T>IChunk:: last_index()
{
  return (low == fence)? low - 1 : fence - 1;
}

inline  int  <T>IChunk:: succ(int i)
{
  return (i < low) ? low : i + 1;
}

inline  int  <T>IChunk:: pred(int i)
{
  return (i > fence) ? (fence - 1) : i - 1;
}

inline  int  <T>IChunk:: valid_index(int i)
{
  return i >= low && i < fence;
}

inline  int  <T>IChunk:: valid_pointer(const <T>* p)
{
  return p >= &(data[low - base]) && p < &(data[fence - base]);
}

inline  <T>* <T>IChunk:: grow_high ()
{
  if (!can_grow_high()) full_error();
  return &(data[fence++ - base]);
}

inline  <T>* <T>IChunk:: grow_low ()
{
  if (!can_grow_low()) full_error();
  return &(data[--low - base]);
}

inline  void <T>IChunk:: shrink_high ()
{
  if (empty()) empty_error();
  --fence;
}

inline  void <T>IChunk:: shrink_low ()
{
  if (empty()) empty_error();
  ++low;
}

inline <T>* <T>IChunk::first_pointer()
{
  return (low == fence)? 0 : &(data[low - base]);
}

inline <T>* <T>IChunk::last_pointer()
{
  return (low == fence)? 0 : &(data[fence - base - 1]);
}

inline <T>* <T>IChunk::succ(<T>* p)
{
  ++p;
  return (p <  &(data[low - base]) || p >= &(data[fence - base])) ? 0 : p;
}

inline <T>* <T>IChunk::pred(<T>* p)
{
  --p;
  return (p <  &(data[low - base]) || p >= &(data[fence - base])) ? 0 : p;
}


// generic Plex operations

inline <T>Plex::<T>Plex() {}

inline  void <T>Plex::record_change()
{
  ++mods;
}

inline int <T>Plex::changes()
{
  return mods;
}

inline int <T>Plex::changed(int changes = 0)
{
  return mods != changes;
}

inline int <T>Plex::chunk_size()
{
  return csize;
}

inline  int <T>Plex::ecnef ()
{
  return lo - 1;
}

inline <T>Plex:: ~<T>Plex()
{
  invalidate();
}  

inline  int <T>Plex::fence ()
{
  return fnc;
}

inline int <T>Plex::length ()
{
  return fnc - lo;
}

inline  int <T>Plex::empty ()
{
  return fnc == lo;
}

inline <T>IChunk* <T>Plex::tl()
{
  return hd->prev();
}

inline int <T>Plex::one_chunk()
{
  return hd == hd->prev();
}


#endif