From c143aafaea246c564464736aee843499f83f9566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20Sch=C3=B6berl?= Date: Fri, 24 Feb 2017 22:29:57 +0100 Subject: [PATCH] Array resize checks for trivially copyable --- libsrc/general/array.hpp | 11 ++++++- libsrc/meshing/meshtype.hpp | 16 +++++++++ libsrc/meshing/topology.hpp | 66 ++++++++++++++++++++++--------------- 3 files changed, 66 insertions(+), 27 deletions(-) diff --git a/libsrc/general/array.hpp b/libsrc/general/array.hpp index 9c6bcbe5..568a6d30 100644 --- a/libsrc/general/array.hpp +++ b/libsrc/general/array.hpp @@ -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::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; diff --git a/libsrc/meshing/meshtype.hpp b/libsrc/meshing/meshtype.hpp index f3446192..92acaaf7 100644 --- a/libsrc/meshing/meshtype.hpp +++ b/libsrc/meshing/meshtype.hpp @@ -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 diff --git a/libsrc/meshing/topology.hpp b/libsrc/meshing/topology.hpp index 5acbcdbb..fdef9ba8 100644 --- a/libsrc/meshing/topology.hpp +++ b/libsrc/meshing/topology.hpp @@ -27,6 +27,14 @@ struct T_FACE int fnr; // 0-based }; + template + 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 edge2vert; Array face2vert; + /* Array edges; Array faces; Array surfedges; + */ + Array> edges; + Array> faces; + Array> surfedges; + Array segedges; Array surffaces; Array 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 },