Array resize checks for trivially copyable

This commit is contained in:
Joachim Schöberl 2017-02-24 22:29:57 +01:00
parent a698b57596
commit c143aafaea
3 changed files with 66 additions and 27 deletions

View File

@ -391,7 +391,16 @@ namespace netgen
T * p = new T[nsize];
int mins = (nsize < size) ? nsize : size;
memcpy (p, data, mins * sizeof(T));
// memcpy (p, data, mins * sizeof(T));
#if defined(__GNUG__) && __GNUC__ < 5 && !defined(__clang__)
for (size_t i = 0; i < mins; i++) p[i] = move(data[i]);
#else
if (std::is_trivially_copyable<T>::value)
memcpy (p, data, sizeof(T)*mins);
else
for (size_t i = 0; i < mins; i++) p[i] = move(data[i]);
#endif
if (ownmem)
delete [] data;

View File

@ -25,9 +25,25 @@ namespace netgen
HEX = 25
};
/*
typedef int ELEMENT_EDGE[2]; // initial point, end point
typedef int ELEMENT_FACE[4]; // points, last one is -1 for trig
*/
struct ELEMENT_EDGE
{
int vals[2];
int & operator[] (size_t i) { return vals[i]; }
int operator[] (size_t i) const { return vals[i]; }
};
struct ELEMENT_FACE
{
int vals[4];
int & operator[] (size_t i) { return vals[i]; }
int operator[] (size_t i) const { return vals[i]; }
};
#define ELEMENT_MAXPOINTS 12
#define ELEMENT2D_MAXPOINTS 8

View File

@ -27,6 +27,14 @@ struct T_FACE
int fnr; // 0-based
};
template <typename T, int S>
struct FixArray
{
T vals[S];
T & operator[] (size_t i) { return vals[i]; }
T operator[] (size_t i) const { return vals[i]; }
};
class MeshTopology
{
@ -36,9 +44,15 @@ class MeshTopology
Array<INDEX_2> edge2vert;
Array<INDEX_4> face2vert;
/*
Array<T_EDGE[12]> edges;
Array<T_FACE[6]> faces;
Array<T_EDGE[4]> surfedges;
*/
Array<FixArray<T_EDGE,12>> edges;
Array<FixArray<T_FACE,6>> faces;
Array<FixArray<T_EDGE,4>> surfedges;
Array<T_EDGE> segedges;
Array<T_FACE> surffaces;
Array<INDEX_2> surf2volelement;
@ -337,22 +351,22 @@ inline short int MeshTopology :: GetNFaces (ELEMENT_TYPE et)
const ELEMENT_EDGE * MeshTopology :: GetEdges1 (ELEMENT_TYPE et)
{
static int segm_edges[1][2] =
static ELEMENT_EDGE segm_edges[1] =
{ { 1, 2 }};
static int trig_edges[3][2] =
static ELEMENT_EDGE trig_edges[3] =
{ { 3, 1 },
{ 2, 3 },
{ 1, 2 }};
static int quad_edges[4][2] =
static ELEMENT_EDGE quad_edges[4] =
{ { 1, 2 },
{ 3, 4 },
{ 4, 1 },
{ 2, 3 }};
static int tet_edges[6][2] =
static ELEMENT_EDGE tet_edges[6] =
{ { 4, 1 },
{ 4, 2 },
{ 4, 3 },
@ -360,7 +374,7 @@ const ELEMENT_EDGE * MeshTopology :: GetEdges1 (ELEMENT_TYPE et)
{ 1, 3 },
{ 2, 3 }};
static int prism_edges[9][2] =
static ELEMENT_EDGE prism_edges[9] =
{ { 3, 1 },
{ 1, 2 },
{ 3, 2 },
@ -371,7 +385,7 @@ const ELEMENT_EDGE * MeshTopology :: GetEdges1 (ELEMENT_TYPE et)
{ 1, 4 },
{ 2, 5 }};
static int pyramid_edges[8][2] =
static ELEMENT_EDGE pyramid_edges[8] =
{ { 1, 2 },
{ 2, 3 },
{ 1, 4 },
@ -381,7 +395,7 @@ const ELEMENT_EDGE * MeshTopology :: GetEdges1 (ELEMENT_TYPE et)
{ 3, 5 },
{ 4, 5 }};
static int hex_edges[12][2] =
static ELEMENT_EDGE hex_edges[12] =
{
{ 1, 2 },
{ 3, 4 },
@ -435,22 +449,22 @@ const ELEMENT_EDGE * MeshTopology :: GetEdges1 (ELEMENT_TYPE et)
const ELEMENT_EDGE * MeshTopology :: GetEdges0 (ELEMENT_TYPE et)
{
static int segm_edges[1][2] =
static ELEMENT_EDGE segm_edges[1] =
{ { 0, 1 }};
static int trig_edges[3][2] =
static ELEMENT_EDGE trig_edges[3] =
{ { 2, 0 },
{ 1, 2 },
{ 0, 1 }};
static int quad_edges[4][2] =
static ELEMENT_EDGE quad_edges[4] =
{ { 0, 1 },
{ 2, 3 },
{ 3, 0 },
{ 1, 2 }};
static int tet_edges[6][2] =
static ELEMENT_EDGE tet_edges[6] =
{ { 3, 0 },
{ 3, 1 },
{ 3, 2 },
@ -458,7 +472,7 @@ const ELEMENT_EDGE * MeshTopology :: GetEdges0 (ELEMENT_TYPE et)
{ 0, 2 },
{ 1, 2 }};
static int prism_edges[9][2] =
static ELEMENT_EDGE prism_edges[9] =
{ { 2, 0 },
{ 0, 1 },
{ 2, 1 },
@ -469,7 +483,7 @@ const ELEMENT_EDGE * MeshTopology :: GetEdges0 (ELEMENT_TYPE et)
{ 0, 3 },
{ 1, 4 }};
static int pyramid_edges[8][2] =
static ELEMENT_EDGE pyramid_edges[8] =
{ { 0, 1 },
{ 1, 2 },
{ 0, 3 },
@ -479,7 +493,7 @@ const ELEMENT_EDGE * MeshTopology :: GetEdges0 (ELEMENT_TYPE et)
{ 2, 4 },
{ 3, 4 }};
static int hex_edges[12][2] =
static ELEMENT_EDGE hex_edges[12] =
{
{ 0, 1 },
{ 2, 3 },
@ -540,18 +554,18 @@ const ELEMENT_EDGE * MeshTopology :: GetEdges0 (ELEMENT_TYPE et)
inline const ELEMENT_FACE * MeshTopology :: GetFaces1 (ELEMENT_TYPE et)
{
static const int trig_faces[1][4] =
static const ELEMENT_FACE trig_faces[1] =
{ { 1, 2, 3, 0 } };
static const int quad_faces[1][4] =
static const ELEMENT_FACE quad_faces[1] =
{ { 1, 2, 3, 4 } };
static const int tet_faces[4][4] =
static const ELEMENT_FACE tet_faces[4] =
{ { 4, 2, 3, 0 },
{ 4, 3, 1, 0 },
{ 4, 1, 2, 0 },
{ 1, 3, 2, 0 } };
static const int prism_faces[5][4] =
static const ELEMENT_FACE prism_faces[5] =
{
{ 1, 3, 2, 0 },
{ 4, 5, 6, 0 },
@ -560,7 +574,7 @@ inline const ELEMENT_FACE * MeshTopology :: GetFaces1 (ELEMENT_TYPE et)
{ 2, 3, 6, 5 }
};
static const int pyramid_faces[5][4] =
static const ELEMENT_FACE pyramid_faces[5] =
{
{ 1, 2, 5, 0 },
{ 2, 3, 5, 0 },
@ -569,7 +583,7 @@ inline const ELEMENT_FACE * MeshTopology :: GetFaces1 (ELEMENT_TYPE et)
{ 1, 4, 3, 2 }
};
static const int hex_faces[6][4] =
static const ELEMENT_FACE hex_faces[6] =
{
{ 1, 4, 3, 2 },
{ 5, 6, 7, 8 },
@ -622,18 +636,18 @@ inline const ELEMENT_FACE * MeshTopology :: GetFaces1 (ELEMENT_TYPE et)
inline const ELEMENT_FACE * MeshTopology :: GetFaces0 (ELEMENT_TYPE et)
{
static const int trig_faces[1][4] =
static const ELEMENT_FACE trig_faces[1] =
{ { 0, 1, 2, -1 } };
static const int quad_faces[1][4] =
static const ELEMENT_FACE quad_faces[1] =
{ { 0, 1, 2, 3 } };
static const int tet_faces[4][4] =
static const ELEMENT_FACE tet_faces[4] =
{ { 3, 1, 2, -1 },
{ 3, 2, 0, -1 },
{ 3, 0, 1, -1 },
{ 0, 2, 1, -1 } };
static const int prism_faces[5][4] =
static const ELEMENT_FACE prism_faces[5] =
{
{ 0, 2, 1, -1 },
{ 3, 4, 5, -1 },
@ -642,7 +656,7 @@ inline const ELEMENT_FACE * MeshTopology :: GetFaces0 (ELEMENT_TYPE et)
{ 1, 2, 5, 4 }
};
static const int pyramid_faces[5][4] =
static const ELEMENT_FACE pyramid_faces[5] =
{
{ 0, 1, 4, -1 },
{ 1, 2, 4, -1 },
@ -651,7 +665,7 @@ inline const ELEMENT_FACE * MeshTopology :: GetFaces0 (ELEMENT_TYPE et)
{ 0, 3, 2, 1 }
};
static const int hex_faces[6][4] =
static const ELEMENT_FACE hex_faces[6] =
{
{ 0, 3, 2, 1 },
{ 4, 5, 6, 7 },