/* * CENA C++ Utilities * * by Stephane Chatty * * Copyright 1990, 1991, 1992 * Laboratoire de Recherche en Informatique (LRI) * Centre d'Etudes de la Navigation Aerienne (CENA) * * plain and generic arrays (orginal C version by Michel Beaudouin-Lafon) * * $Id$ * $CurLog$ */ #include "Array.h" #include #include /*?class IvlArray \typ{IvlArray}s are dynamic arrays whose lower bound is 0. For a \typ{IvlArray} with size \var{size}, the valid indices are \var{0, \ldots, size -- 1.} Their elements are of type \typ{IvlArrayItem*}. Methods are provided for initialization, data storage and retrieval, and a few other utilities. ?*/ IvlArrayItem* IvlArray::InvalidCell = 0; /*? Initialize a \typ{IvlArray} with size \var{size}. Entries are initialized to \var{value}. ?*/ IvlArray :: IvlArray (int size, IvlArrayItem* value) { if (size > 0) { Data = new IvlArrayItem* [size]; register IvlArrayItem** p = Data; for (register int i = 0; i < size; ++i, ++p) *p = value; } else { Data = 0; size = 0; } Length = size; } /*?nodoc?*/ IvlArray :: IvlArray (const IvlArray& a) : Length (a.Length) { if (Length == 0) { Data = 0; } else { Data = new IvlArrayItem* [Length]; register IvlArrayItem** p; register IvlArrayItem** q; register int i; for (p = Data, q = a.Data, i = Length ; i--; ++p, ++q) *p = *q; } } /*?nodoc?*/ IvlArray :: ~IvlArray () { /* Free the data */ Clean (); } /* * Empty the array. */ /*?hidden?*/ void IvlArray :: Clean () { if (Data) #ifndef CPLUS_BUG4 delete [] Data; #else delete [Length] Data; #endif Data = 0; Length = 0; } /*? Set all entries to \var{value}. ?*/ void IvlArray :: Reset (IvlArrayItem* value) { register IvlArrayItem** p; register int i; for (p = Data, i = Length ; i--; ++p) *p = value; } /*? Copy the contents of the array \var{from} into this array. The old contents, if any, are freed, and new memory is allocated for the copy. ?*/ IvlArray& IvlArray :: operator = (const IvlArray& from) { if (this != &from) { Clean (); Length = from.Length; if (Length > 0 && from.Data) { Data = new IvlArrayItem* [Length]; memcpy (Data, from.Data, Length * sizeof (IvlArrayItem*)); } else Data = 0; } return *this; } /*? Change the size of the array to \var{size}. The new entries, if any, are set to \var{value}. This function reallocates the contents of the array. ?*/ void IvlArray :: Resize (int size, IvlArrayItem* value) { if (size <= 0) { Clean (); return; } IvlArrayItem** NewData = new IvlArrayItem* [size]; /* if the new sequence is longer, copy and fill end, else just copy */ if (size > Length) { if (Length) memcpy (NewData, Data, Length * sizeof (IvlArrayItem*)); register IvlArrayItem** pfil = NewData + Length; for (register int i = size - Length; i--; *pfil++ = value) ; } else memcpy (NewData, Data, size * sizeof (IvlArrayItem*)); /* free old data and set new array */ Clean (); Data = NewData; Length = size; } /*? Return the address of the \var{i}th element. ?*/ IvlArrayItem** IvlArray :: operator + (register int i) const { if (Check (i)) return Data+i; else { fprintf (stderr, "Index %d out of array bounds: size was %d\n", i, Length); return &InvalidCell; } } /*? Get a reference to the \var{i}-th entry, so that this entry can be read or written. If \var{i} is outside the bounds, the operator returns a reference to a static variable which initially contains 0. ?*/ IvlArrayItem*& IvlArray :: operator [] (register int i) const { if (Check (i)) return Data [i]; else { fprintf (stderr, "Index %d out of array bounds: size was %d\n", i, Length); return InvalidCell; } } #ifdef DOC /*?class IvlArrayOf The generic class \typ{IvlArrayOf} is a derived class of \typ{IvlArray} that provides arrays of pointers to class objects. When parameterized by the class \typ{ITEM}, the following functions are redefined: ?*/ /*?nextdoc?*/ IvlArrayOf :: IvlArrayOf (int size, ITEM* value = 0) { } /*?nextdoc?*/ void IvlArrayOf :: Reset (ITEM* value = 0) { } /*?nextdoc?*/ void IvlArrayOf :: Resize (int size, ITEM* value = 0) { } /*?nextdoc?*/ ITEM*& IvlArrayOf :: operator [] (int i) const { } /*? These functions are equivalent to their homonyms in the class \typ{IvlArray}, except that they are customized in order to manipulate pointers to \typ{ITEM} instead of \typ{void*}. ?*/ ITEM** IvlArrayOf :: operator + (int i) const { } #endif /* DOC */