/* * 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 CcuArray \typ{CcuArray}s are dynamic arrays whose lower bound is 0. For a \typ{CcuArray} with size \var{size}, the valid indices are \var{0, \ldots, size -- 1.} Their elements are of type \typ{CcuArrayItem*}. Methods are provided for initialization, data storage and retrieval, and a few other utilities. ?*/ CcuArrayItem* CcuArray::InvalidCell = 0; /*? Initialize a \typ{CcuArray} with size \var{size}. Entries are initialized to \var{value}. ?*/ CcuArray :: CcuArray (int size, CcuArrayItem* value) { if (size > 0) { Data = new CcuArrayItem* [size]; register CcuArrayItem** p = Data; for (register int i = 0; i < size; ++i, ++p) *p = value; } else { Data = 0; size = 0; } Length = size; } /*?nodoc?*/ CcuArray :: CcuArray (const CcuArray& a) : Length (a.Length) { if (Length == 0) { Data = 0; } else { Data = new CcuArrayItem* [Length]; register CcuArrayItem** p; register CcuArrayItem** q; register int i; for (p = Data, q = a.Data, i = Length ; i--; ++p, ++q) *p = *q; } } /*?nodoc?*/ CcuArray :: ~CcuArray () { /* Free the data */ Clean (); } /* * Empty the array. */ /*?hidden?*/ void CcuArray :: Clean () { if (Data) #ifndef CPLUS_BUG4 delete [] Data; #else delete [Length] Data; #endif Data = 0; Length = 0; } /*? Set all entries to \var{value}. ?*/ void CcuArray :: Reset (CcuArrayItem* value) { register CcuArrayItem** 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. ?*/ CcuArray& CcuArray :: operator = (const CcuArray& from) { if (this != &from) { Clean (); Length = from.Length; if (Length > 0 && from.Data) { Data = new CcuArrayItem* [Length]; memcpy (Data, from.Data, Length * sizeof (CcuArrayItem*)); } 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 CcuArray :: Resize (int size, CcuArrayItem* value) { if (size <= 0) { Clean (); return; } CcuArrayItem** NewData = new CcuArrayItem* [size]; /* if the new sequence is longer, copy and fill end, else just copy */ if (size > Length) { if (Length) memcpy (NewData, Data, Length * sizeof (CcuArrayItem*)); register CcuArrayItem** pfil = NewData + Length; for (register int i = size - Length; i--; *pfil++ = value) ; } else memcpy (NewData, Data, size * sizeof (CcuArrayItem*)); /* free old data and set new array */ Clean (); Data = NewData; Length = size; } /*? Return the address of the \var{i}th element. ?*/ CcuArrayItem** CcuArray :: 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. ?*/ CcuArrayItem*& CcuArray :: 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 CcuArrayOf The generic class \typ{CcuArrayOf} is a derived class of \typ{CcuArray} that provides arrays of pointers to class objects. When parameterized by the class \typ{ITEM}, the following functions are redefined: ?*/ /*?nextdoc?*/ CcuArrayOf :: CcuArrayOf (int size, ITEM* value = 0) { } /*?nextdoc?*/ void CcuArrayOf :: Reset (ITEM* value = 0) { } /*?nextdoc?*/ void CcuArrayOf :: Resize (int size, ITEM* value = 0) { } /*?nextdoc?*/ ITEM*& CcuArrayOf :: operator [] (int i) const { } /*? These functions are equivalent to their homonyms in the class \typ{CcuArray}, except that they are customized in order to manipulate pointers to \typ{ITEM} instead of \typ{void*}. ?*/ ITEM** CcuArrayOf :: operator + (int i) const { } #endif /* DOC */