array2D: use std::vector

This commit is contained in:
Ingo Weyrich
2020-08-01 15:29:05 +02:00
parent 03acf7680a
commit 04854518a3

View File

@@ -55,6 +55,7 @@
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
#include <sys/types.h> #include <sys/types.h>
#include <vector>
#include "noncopyable.h" #include "noncopyable.h"
// flags for use // flags for use
@@ -68,75 +69,59 @@ class array2D :
{ {
private: private:
ssize_t width, height; ssize_t width;
T** rows; std::vector<T*> rows;
T* data; std::vector<T> buffer;
void initRows(ssize_t h, int offset = 0)
{
rows.resize(h);
T* start = buffer.data();
for (ssize_t i = 0; i < h; i++) {
rows[i] = start + offset + width * i;
}
}
void ar_realloc(ssize_t w, ssize_t h, int offset = 0) void ar_realloc(ssize_t w, ssize_t h, int offset = 0)
{ {
if (rows && (h > height || 4 * h < height)) {
delete[] rows;
rows = nullptr;
}
if (!rows) {
rows = new T*[h];
}
if (data && ((h * w > width * height) || (h * w < (width * height / 4)))) {
delete[] data;
data = nullptr;
}
width = w; width = w;
height = h; buffer.resize(h * width + offset);
initRows(h, offset);
if (!data) {
data = new T[height * width + offset];
}
for (ssize_t i = 0; i < height; i++) {
rows[i] = data + offset + width * i;
}
} }
public: public:
// use as empty declaration, resize before use! // use as empty declaration, resize before use!
// very useful as a member object // very useful as a member object
array2D() : array2D() : width(0) {}
width(0), height(0), rows(nullptr), data(nullptr)
{ }
// creator type1 // creator type1
array2D(int w, int h, unsigned int flags = 0) : array2D(int w, int h, unsigned int flags = 0) : width(w)
width(w), height(h)
{ {
data = new T[height * width];
rows = new T*[height];
for (ssize_t i = 0; i < height; ++i) {
rows[i] = data + i * width;
}
if (flags & ARRAY2D_CLEAR_DATA) { if (flags & ARRAY2D_CLEAR_DATA) {
memset(data, 0, width * height * sizeof(T)); buffer.resize(h * width, 0);
} else {
buffer.resize(h * width);
} }
initRows(h);
} }
// creator type 2 // creator type 2
array2D(int w, int h, T ** source, unsigned int flags = 0) : array2D(int w, int h, T ** source, unsigned int flags = 0) :
width(w), height(h) width(w)
{ {
const bool owner = !(flags & ARRAY2D_BYREFERENCE); const bool owner = !(flags & ARRAY2D_BYREFERENCE);
if (owner) { if (owner) {
data = new T[height * width]; buffer.resize(h * width);
} else { } else {
data = nullptr; buffer.clear();
} }
rows = new T*[height]; rows.resize(h);
for (ssize_t i = 0; i < height; ++i) { T* start = buffer.data();
for (ssize_t i = 0; i < h; ++i) {
if (owner) { if (owner) {
rows[i] = data + i * width; rows[i] = start + i * width;
for (ssize_t j = 0; j < width; ++j) { for (ssize_t j = 0; j < width; ++j) {
rows[i][j] = source[i][j]; rows[i][j] = source[i][j];
} }
@@ -146,68 +131,59 @@ public:
} }
} }
// destructor
~array2D()
{
delete[] data;
delete[] rows;
}
void fill(const T val, bool multiThread = false) void fill(const T val, bool multiThread = false)
{ {
const ssize_t height = rows.size();
#ifdef _OPENMP #ifdef _OPENMP
#pragma omp parallel for if(multiThread) #pragma omp parallel for if(multiThread)
#endif #endif
for (ssize_t i = 0; i < width * height; ++i) { for (ssize_t i = 0; i < width * height; ++i) {
data[i] = val; buffer[i] = val;
} }
} }
void free() void free()
{ {
delete[] data; buffer.clear();
data = nullptr; rows.clear();
delete [] rows;
rows = nullptr;
} }
// use with indices // use with indices
T * operator[](int index) T * operator[](int index)
{ {
assert((index >= 0) && (index < height)); assert((index >= 0) && (index < rows.size()));
return rows[index]; return rows[index];
} }
const T * operator[](int index) const const T * operator[](int index) const
{ {
assert((index >= 0) && (index < height)); assert((index >= 0) && (index < rows.size()));
return rows[index]; return rows[index];
} }
// use as pointer to T** // use as pointer to T**
operator T**() operator T**()
{ {
return rows; return rows.data();
} }
// use as pointer to T** // use as pointer to T**
operator const T* const *() const operator const T* const *() const
{ {
return rows; return rows.data();
} }
// use as pointer to data // use as pointer to buffer
operator T*() operator T*()
{ {
// only if owner this will return a valid pointer // only if owner this will return a valid pointer
return data; return buffer.data();
} }
operator const T*() const operator const T*() const
{ {
// only if owner this will return a valid pointer // only if owner this will return a valid pointer
return data; return buffer.data();
} }
@@ -218,7 +194,7 @@ public:
ar_realloc(w, h, offset); ar_realloc(w, h, offset);
if (flags & ARRAY2D_CLEAR_DATA) { if (flags & ARRAY2D_CLEAR_DATA) {
memset(data + offset, 0, width * height * sizeof(T)); fill(0);
} }
} }
@@ -228,12 +204,12 @@ public:
} }
int getHeight() const int getHeight() const
{ {
return height; return rows.size();
} }
operator bool() operator bool()
{ {
return (width > 0 && height > 0); return (width > 0 && !rows.empty());
} }
}; };