home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1997 November
/
PCWorld_1997-11_cd.bin
/
software
/
programy
/
komix
/
DATA.Z
/
FlexArray.cxx
< prev
next >
Wrap
C/C++ Source or Header
|
1996-05-31
|
3KB
|
107 lines
/*---------------------------------------------------------------------------
*
* Copyright (c) 1990 by Westmount Technology B.V., Delft, The Netherlands.
*
* This software is furnished under a license and may be used only in
* accordance with the terms of such license and with the inclusion of
* the above copyright notice. This software or any other copies thereof
* may not be provided or otherwise made available to any other person.
* No title to and ownership of the software is hereby transferred.
*
* The information in this software is subject to change without notice
* and should not be construed as a commitment by Westmount Technology B.V.
*
*---------------------------------------------------------------------------
*
* File : @(#)FlexArray.cxx 1.1
* Author : erel
* Original date : Jun 14 1990
* History :
* See also : ArrayOb (nihcl)
* Description : Implementation of flexible arrays
*
*---------------------------------------------------------------------------
*/
static const char SccsId[]=
"@(#)FlexArray.cxx 1.1 05 Nov 1993 Copyright 1993 Westmount Technology";
#include <stdlib.h>
/*
* Note: we use malloc()/free() here instead of new/delete because
* we use realloc() to implement reSize().
*/
#define NEW(type,size) ((type*)malloc(sizeof(type)*(size)))
inline void DELETE(void** ptr) { free((char*)ptr); }
inline void** REALLOC(void** ptr, unsigned size)
{
return (void**)realloc((char*)ptr,sizeof(void*)*size);
}
#include "FlexArray.hxx"
extern const int DEFAULT_ARRAY_SIZE = 16;
void
GenFlexArray::clear(void * e)
{
register i = sz;
register void** vp = v;
while (i--) *vp++ = e;
}
GenFlexArray::GenFlexArray (unsigned size):
sz ( (size) ? size: DEFAULT_ARRAY_SIZE )
{
v = NEW(void*,sz);
}
GenFlexArray::GenFlexArray(const GenFlexArray& a): sz(a.sz)
{
register i = sz;
v = NEW(void*,i);
register void** vp = v;
register void** av = a.v;
while (i--) *vp++ = *av++;
}
GenFlexArray::~GenFlexArray() { DELETE(v); }
GenFlexArray&
GenFlexArray::operator=(const GenFlexArray& a)
{
if ( &a == this ) return *this; // assigning to self?
DELETE(v);
v = NEW(void*,sz=a.sz);
register i = a.sz;
register void** vp = v;
register void** av = a.v;
while (i--) *vp++ = *av++;
return *this;
}
void
GenFlexArray::reSize(unsigned newsize)
{
v = REALLOC(v,newsize);
// clear newly allocated memmory
if ( newsize > sz ) {
register i = newsize - sz;
register void** vp = &v[sz];
while (i--) *vp++ = 0;
}
sz = newsize;
}