DOLFIN
DOLFIN C++ interface
MeshConnectivity.h
1// Copyright (C) 2006-2007 Anders Logg
2//
3// This file is part of DOLFIN.
4//
5// DOLFIN is free software: you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// DOLFIN is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17//
18// First added: 2006-05-09
19// Last changed: 2010-11-28
20
21#ifndef __MESH_CONNECTIVITY_H
22#define __MESH_CONNECTIVITY_H
23
24#include <vector>
25#include <dolfin/log/log.h>
26
27#include <cstdint>
28
29namespace dolfin
30{
31
40
42 {
43 public:
44
46 MeshConnectivity(std::size_t d0, std::size_t d1);
47
49 MeshConnectivity(const MeshConnectivity& connectivity);
50
53
55 const MeshConnectivity& operator= (const MeshConnectivity& connectivity);
56
58 bool empty() const
59 { return _connections.empty(); }
60
62 std::size_t size() const
63 { return _connections.size(); }
64
66 std::size_t size(std::size_t entity) const
67 {
68 return (entity + 1) < index_to_position.size()
69 ? index_to_position[entity + 1] - index_to_position[entity] : 0;
70 }
71
73 std::size_t size_global(std::size_t entity) const
74 {
75 if (_num_global_connections.empty())
76 return size(entity);
77 else
78 {
79 dolfin_assert(entity < _num_global_connections.size());
80 return _num_global_connections[entity];
81 }
82 }
83
85 const unsigned int* operator() (std::size_t entity) const
86 {
87 return (entity + 1) < index_to_position.size()
88 ? &_connections[index_to_position[entity]] : 0;
89 }
90
92 const std::vector<unsigned int>& operator() () const
93 { return _connections; }
94
96 void clear();
97
100 void init(std::size_t num_entities, std::size_t num_connections);
101
104 void init(std::vector<std::size_t>& num_connections);
105
107 void set(std::size_t entity, std::size_t connection, std::size_t pos);
108
111 template<typename T>
112 void set(std::size_t entity, const T& connections)
113 {
114 dolfin_assert((entity + 1) < index_to_position.size());
115 dolfin_assert(connections.size()
116 == index_to_position[entity + 1]-index_to_position[entity]);
117
118 // Copy data
119 std::copy(connections.begin(), connections.end(),
120 _connections.begin() + index_to_position[entity]);
121 }
122
124 void set(std::size_t entity, std::size_t* connections);
125
129 template <typename T>
130 void set(const T& connections)
131 {
132 // Clear old data if any
133 clear();
134
135 // Initialize offsets and compute total size
136 index_to_position.resize(connections.size() + 1);
137 std::int32_t size = 0;
138 for (std::size_t e = 0; e < connections.size(); e++)
139 {
140 index_to_position[e] = size;
141 size += connections[e].size();
142 }
143 index_to_position[connections.size()] = size;
144
145 // Initialize connections
146 _connections.reserve(size);
147 for (auto e = connections.begin(); e != connections.end(); ++e)
148 _connections.insert(_connections.end(), e->begin(), e->end());
149
150 _connections.shrink_to_fit();
151 }
152
154 void
155 set_global_size(const std::vector<unsigned int>& num_global_connections)
156 {
157 dolfin_assert(num_global_connections.size()
158 == index_to_position.size() - 1);
159 _num_global_connections = num_global_connections;
160 }
161
163 std::size_t hash() const;
164
166 std::string str(bool verbose) const;
167
168 private:
169
170 // Dimensions (only used for pretty-printing)
171 std::size_t _d0, _d1;
172
173 // Connections for all entities stored as a contiguous array
174 std::vector<unsigned int> _connections;
175
176 // Global number of connections for all entities (possibly not
177 // computed)
178 std::vector<unsigned int> _num_global_connections;
179
180 // Position of first connection for each entity (using local index)
181 std::vector<unsigned int> index_to_position;
182
183 };
184
185}
186
187#endif
Definition: MeshConnectivity.h:42
std::size_t size() const
Return total number of connections.
Definition: MeshConnectivity.h:62
const std::vector< unsigned int > & operator()() const
Return contiguous array of connections for all entities.
Definition: MeshConnectivity.h:92
std::size_t hash() const
Hash of connections.
Definition: MeshConnectivity.cpp:128
void set(std::size_t entity, const T &connections)
Definition: MeshConnectivity.h:112
bool empty() const
Return true if the total number of connections is equal to zero.
Definition: MeshConnectivity.h:58
void set(const T &connections)
Definition: MeshConnectivity.h:130
void clear()
Clear all data.
Definition: MeshConnectivity.cpp:61
MeshConnectivity(std::size_t d0, std::size_t d1)
Create empty connectivity between given dimensions (d0 – d1)
Definition: MeshConnectivity.cpp:31
std::string str(bool verbose) const
Return informal string representation (pretty-print)
Definition: MeshConnectivity.cpp:135
void set_global_size(const std::vector< unsigned int > &num_global_connections)
Set global number of connections for all local entities.
Definition: MeshConnectivity.h:155
std::size_t size(std::size_t entity) const
Return number of connections for given entity.
Definition: MeshConnectivity.h:66
void set(std::size_t entity, std::size_t connection, std::size_t pos)
Set given connection for given entity.
Definition: MeshConnectivity.cpp:107
const MeshConnectivity & operator=(const MeshConnectivity &connectivity)
Assignment.
Definition: MeshConnectivity.cpp:49
~MeshConnectivity()
Destructor.
Definition: MeshConnectivity.cpp:43
void init(std::size_t num_entities, std::size_t num_connections)
Definition: MeshConnectivity.cpp:67
std::size_t size_global(std::size_t entity) const
Return global number of connections for given entity.
Definition: MeshConnectivity.h:73
Definition: adapt.h:30