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 namespace dolfin
28 {
29 
38 
40  {
41  public:
42 
44  MeshConnectivity(std::size_t d0, std::size_t d1);
45 
47  MeshConnectivity(const MeshConnectivity& connectivity);
48 
51 
53  const MeshConnectivity& operator= (const MeshConnectivity& connectivity);
54 
56  bool empty() const
57  { return _connections.empty(); }
58 
60  std::size_t size() const
61  { return _connections.size(); }
62 
64  std::size_t size(std::size_t entity) const
65  {
66  return (entity + 1) < index_to_position.size()
67  ? index_to_position[entity + 1] - index_to_position[entity] : 0;
68  }
69 
71  std::size_t size_global(std::size_t entity) const
72  {
73  if (_num_global_connections.empty())
74  return size(entity);
75  else
76  {
77  dolfin_assert(entity < _num_global_connections.size());
78  return _num_global_connections[entity];
79  }
80  }
81 
83  const unsigned int* operator() (std::size_t entity) const
84  {
85  return (entity + 1) < index_to_position.size()
86  ? &_connections[index_to_position[entity]] : 0;
87  }
88 
90  const std::vector<unsigned int>& operator() () const
91  { return _connections; }
92 
94  void clear();
95 
98  void init(std::size_t num_entities, std::size_t num_connections);
99 
102  void init(std::vector<std::size_t>& num_connections);
103 
105  void set(std::size_t entity, std::size_t connection, std::size_t pos);
106 
109  template<typename T>
110  void set(std::size_t entity, const T& connections)
111  {
112  dolfin_assert((entity + 1) < index_to_position.size());
113  dolfin_assert(connections.size()
114  == index_to_position[entity + 1]-index_to_position[entity]);
115 
116  // Copy data
117  std::copy(connections.begin(), connections.end(),
118  _connections.begin() + index_to_position[entity]);
119  }
120 
122  void set(std::size_t entity, std::size_t* connections);
123 
127  template <typename T>
128  void set(const T& connections)
129  {
130  // Clear old data if any
131  clear();
132 
133  // Initialize offsets and compute total size
134  index_to_position.resize(connections.size() + 1);
135  std::int32_t size = 0;
136  for (std::size_t e = 0; e < connections.size(); e++)
137  {
138  index_to_position[e] = size;
139  size += connections[e].size();
140  }
141  index_to_position[connections.size()] = size;
142 
143  // Initialize connections
144  _connections.reserve(size);
145  for (auto e = connections.begin(); e != connections.end(); ++e)
146  _connections.insert(_connections.end(), e->begin(), e->end());
147 
148  _connections.shrink_to_fit();
149  }
150 
152  void
153  set_global_size(const std::vector<unsigned int>& num_global_connections)
154  {
155  dolfin_assert(num_global_connections.size()
156  == index_to_position.size() - 1);
157  _num_global_connections = num_global_connections;
158  }
159 
161  std::size_t hash() const;
162 
164  std::string str(bool verbose) const;
165 
166  private:
167 
168  // Dimensions (only used for pretty-printing)
169  std::size_t _d0, _d1;
170 
171  // Connections for all entities stored as a contiguous array
172  std::vector<unsigned int> _connections;
173 
174  // Global number of connections for all entities (possibly not
175  // computed)
176  std::vector<unsigned int> _num_global_connections;
177 
178  // Position of first connection for each entity (using local index)
179  std::vector<unsigned int> index_to_position;
180 
181  };
182 
183 }
184 
185 #endif
const MeshConnectivity & operator=(const MeshConnectivity &connectivity)
Assignment.
Definition: MeshConnectivity.cpp:49
Definition: adapt.h:29
void set_global_size(const std::vector< unsigned int > &num_global_connections)
Set global number of connections for all local entities.
Definition: MeshConnectivity.h:153
void clear()
Clear all data.
Definition: MeshConnectivity.cpp:61
Definition: MeshConnectivity.h:39
std::size_t size() const
Return total number of connections.
Definition: MeshConnectivity.h:60
const std::vector< unsigned int > & operator()() const
Return contiguous array of connections for all entities.
Definition: MeshConnectivity.h:90
std::string str(bool verbose) const
Return informal string representation (pretty-print)
Definition: MeshConnectivity.cpp:135
MeshConnectivity(std::size_t d0, std::size_t d1)
Create empty connectivity between given dimensions (d0 – d1)
Definition: MeshConnectivity.cpp:31
std::size_t size(std::size_t entity) const
Return number of connections for given entity.
Definition: MeshConnectivity.h:64
~MeshConnectivity()
Destructor.
Definition: MeshConnectivity.cpp:43
std::size_t hash() const
Hash of connections.
Definition: MeshConnectivity.cpp:128
std::size_t size_global(std::size_t entity) const
Return global number of connections for given entity.
Definition: MeshConnectivity.h:71
void init(std::size_t num_entities, std::size_t num_connections)
Definition: MeshConnectivity.cpp:67
bool empty() const
Return true if the total number of connections is equal to zero.
Definition: MeshConnectivity.h:56