bf3a3fa297
- aggiunta libreria vroni 7.6.
110 lines
3.6 KiB
C++
110 lines
3.6 KiB
C++
/*****************************************************************************/
|
|
/* */
|
|
/* Copyright (C) 1999-2023 M. Held */
|
|
/* */
|
|
/* This code is not in the public domain. All rights reserved! Please make */
|
|
/* sure to read the full copyright statement contained in "README.txt" or in */
|
|
/* the "main" file of this code, such as "main.cc". */
|
|
/* */
|
|
/*****************************************************************************/
|
|
/* */
|
|
/* Written by: Martin Held */
|
|
/* */
|
|
/* E-Mail: held@cs.sbg.ac.at */
|
|
/* Fax Mail: (+43 662) 8044-611 */
|
|
/* Voice Mail: (+43 662) 8044-6304 */
|
|
/* Snail Mail: Martin Held */
|
|
/* FB Informatik */
|
|
/* Universitaet Salzburg */
|
|
/* A-5020 Salzburg, Austria */
|
|
/* */
|
|
/*****************************************************************************/
|
|
|
|
#ifndef VRONI_VECTOR_H
|
|
#define VRONI_VECTOR_H
|
|
|
|
#include <stdlib.h>
|
|
#include <new>
|
|
#include <iostream>
|
|
#include <exception>
|
|
|
|
template<typename T>
|
|
class vronivector {
|
|
|
|
private:
|
|
unsigned int arraysize;
|
|
T * array;
|
|
|
|
public:
|
|
inline vronivector() : arraysize(0), array(NULL) {}
|
|
|
|
inline ~vronivector() {
|
|
free(array);
|
|
}
|
|
|
|
T * begin() {
|
|
return array;
|
|
}
|
|
|
|
T * end() {
|
|
return array + arraysize;
|
|
}
|
|
|
|
unsigned int size() {
|
|
return arraysize;
|
|
}
|
|
|
|
void inline reserve(unsigned int ) {}
|
|
|
|
void resize(unsigned int newsize) {
|
|
|
|
/* */
|
|
/* Call destructor of elements no longer needed if the size of the */
|
|
/* array is reduced */
|
|
/* */
|
|
for(unsigned int i = newsize; i < arraysize; ++i) {
|
|
array[i].~T();
|
|
}
|
|
|
|
// Reallocate
|
|
T * newarray = (T*) realloc(array, newsize * sizeof(T));
|
|
if (newarray == NULL) throw std::bad_alloc();
|
|
|
|
//Initialize new elements of array
|
|
for(unsigned int i = arraysize; i < newsize; ++i) {
|
|
new (&newarray[i]) T();
|
|
}
|
|
array = newarray;
|
|
arraysize = newsize;
|
|
}
|
|
|
|
void clear() {
|
|
free(array);
|
|
array = NULL;
|
|
arraysize = 0;
|
|
}
|
|
|
|
|
|
inline T & operator[](unsigned int ind) {
|
|
return array[ind];
|
|
}
|
|
|
|
};
|
|
|
|
template <class vtype>
|
|
inline void gentlyResizeSTLVector(vtype & v, unsigned int newsize,
|
|
const char * vname) {
|
|
try {
|
|
v.reserve(newsize);
|
|
v.resize(newsize);
|
|
}
|
|
catch (std::bad_alloc& c) {
|
|
std::cerr << "\n" << "VRONI exception while trying to resize vector "
|
|
<< vname << " (" << c.what() << ")" << std::endl;
|
|
throw std::runtime_error("VRONI error: gentlyResizeSTLVector() - memory reallocation failed!");
|
|
}
|
|
}
|
|
|
|
|
|
#endif
|