/* * Copyright (c) 1996 Barton P. Miller * * We provide the Paradyn Parallel Performance Tools (below * described as Paradyn") on an AS IS basis, and do not warrant its * validity or performance. We reserve the right to update, modify, * or discontinue this software at any time. We shall have no * obligation to supply such updates or modifications or any other * form of support to you. * * This license is for research uses. For such uses, there is no * charge. We define "research use" to mean you may freely use it * inside your organization for whatever purposes you see fit. But you * may not re-distribute Paradyn or parts of Paradyn, in any form * source or binary (including derivatives), electronic or otherwise, * to any other organization or entity without our permission. * * (for other uses, please contact us at paradyn@cs.wisc.edu) * * All warranties, including without limitation, any warranty of * merchantability or fitness for a particular purpose, are hereby * excluded. * * By your use of Paradyn, you understand and agree that we (or any * other person or entity with proprietary rights in Paradyn) are * under no obligation to provide either maintenance services, * update services, notices of latent defects, or correction of * defects for Paradyn. * * Even if advised of the possibility of such damages, under no * circumstances shall we (or any other person or entity with * proprietary rights in the software licensed hereunder) be liable * to you or any third party for direct, indirect, or consequential * damages of any character regardless of type of action, including, * without limitation, loss of profits, loss of use, loss of good * will, or computer failure or malfunction. You agree to indemnify * us (and any other person or entity with proprietary rights in the * software licensed hereunder) for any and all liability it may * incur to third parties resulting from your use of Paradyn. */ // $Id: BPatch_Vector.h,v 1.13 2002/12/20 07:49:55 jaw Exp $ #ifndef _BPatch_Vector_h_ #define _BPatch_Vector_h_ #include #include #include #include "BPatch_dll.h" #ifdef USE_STL_VECTOR //#include #include #include #if ( __GNUC__ == 3 ) && ( __GNUC_MINOR__ == 1 ) #define BPatch_Vector std::vector #else #define BPatch_Vector vector #endif #else #ifdef external_templates #pragma interface #endif /* external_templates */ /* * This causes problems when including this file in code that uses STL, * so I'm taking it out for now. - brb #ifndef BPATCH_LIBRARY #define vector BPatch_Vector #endif */ template class BPATCH_DLL_EXPORT BPatch_Vector { int reserved; // Number of objects for which space is reserved int len; // The current number of objects in the vector T* data; // Points to the array of objects void reserve(int n); inline void copy_from(const BPatch_Vector &); public: BPatch_Vector(int n = 0); BPatch_Vector(const BPatch_Vector &); ~BPatch_Vector(); BPatch_Vector& operator=(const BPatch_Vector &); unsigned int size() const { return len; } void push_back(const T& x); void push_front(const T& x); T& operator[](int n) const; }; // VG(06/15/02): VC.NET doesn't like definitions for dll imports #ifndef BPATCH_DLL_IMPORT // Reserve space for at least n entries in the vector template void BPatch_Vector::reserve(int n) { if (n > reserved) { // If we haven't already reserved enough space // Generally we double the size each time we reserve memory, so // that we're not doing it for every insertion. if (reserved*2 > n) n = reserved*2; // Create a new array with enough space T* new_data = new T[n]; // Copy the entries from the old array to the new one for (int i = 0; i < len; i++) new_data[i] = data[i]; // Get rid of the old array and set up to use the new one if( data != NULL ) delete [] data; data = new_data; reserved = n; } assert(data != NULL || n == 0); } // Copy the contents of another vector into this one. template inline void BPatch_Vector::copy_from(const BPatch_Vector &src) { reserved = src.reserved; len = src.len; if( reserved == 0 ) data = NULL; else { data = new T[reserved]; for (int i = 0; i < src.len; i++) data[i] = src.data[i]; } } // Contructor. Takes one optional parameter, the number of entries for which // to reserve space. template BPatch_Vector::BPatch_Vector(int n) : reserved(0), len(0), data(NULL) { assert(n >= 0); if (n > 0) reserve(n); } // Copy constructor. template BPatch_Vector::BPatch_Vector(const BPatch_Vector &src) { copy_from(src); } // Destructor. Frees allocated memory. template BPatch_Vector::~BPatch_Vector() { if( data != NULL ) delete [] data; } // Assignment operator. Delete the contents of this vector and copy the // contents of the other vector into it. template BPatch_Vector& BPatch_Vector::operator=(const BPatch_Vector &src) { if( data != NULL ) delete [] data; copy_from(src); return *this; } // Add an element to the end of the vector. template void BPatch_Vector::push_back(const T& x) { reserve(len+1); data[len] = x; len++; } // Add an element to the end of the vector. template void BPatch_Vector::push_front(const T& x) { int i; reserve(len+1); for (i=len; i > 0; i--) data[i] = data[i-1]; data[0] = x; len++; } // Reference the nth element of the vector. template T& BPatch_Vector::operator[](int n) const { assert(data != NULL && n >= 0 && n < len); return data[n]; } #endif /* BPATCH_DLL_IMPORT */ #endif /* USE_STL_VECTOR */ #endif /* _BPatch_Vector_h_ */