Reference documentation for deal.II version 9.0.0
block_indices.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 2018 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_block_indices_h
17 #define dealii_block_indices_h
18 
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/subscriptor.h>
22 #include <deal.II/base/exceptions.h>
23 #include <deal.II/base/logstream.h>
24 #include <deal.II/base/utilities.h>
25 #include <cstddef>
26 #include <vector>
27 
28 DEAL_II_NAMESPACE_OPEN
29 
30 
58 class BlockIndices : public Subscriptor
59 {
60 public:
65 
69  BlockIndices ();
70 
76  BlockIndices (const std::vector<size_type> &block_sizes);
77 
82  BlockIndices (BlockIndices &&b) noexcept;
83 
87  BlockIndices (const BlockIndices &) = default;
88 
92  explicit BlockIndices(const unsigned int n_blocks,
93  const size_type block_size = 0);
94 
99  void reinit (const unsigned int n_blocks,
100  const size_type n_elements_per_block);
101 
108  void reinit (const std::vector<size_type> &block_sizes);
109 
113  void push_back(const size_type size);
114 
119 
123  unsigned int size () const;
124 
129  size_type total_size () const;
130 
134  size_type block_size (const unsigned int i) const;
135 
141  std::string to_string () const;
142 
144 
154 
160  std::pair<unsigned int,size_type>
161  global_to_local (const size_type i) const;
162 
166  size_type local_to_global (const unsigned int block,
167  const size_type index) const;
168 
172  size_type block_start (const unsigned int i) const;
174 
179 
184  BlockIndices &operator = (BlockIndices &&) noexcept;
185 
190  bool operator == (const BlockIndices &b) const;
191 
195  void swap (BlockIndices &b);
196 
201  std::size_t memory_consumption () const;
202 
203 private:
208  unsigned int n_blocks;
209 
215 };
216 
217 
226 inline
227 LogStream &
228 operator << (LogStream &s, const BlockIndices &bi)
229 {
230  const unsigned int n = bi.size();
231  s << n << ":[";
232  // Write first size without leading space
233  if (n>0)
234  s << bi.block_size(0);
235  // Write all other sizes
236  for (unsigned int i=1; i<n; ++i)
237  s << ' ' << bi.block_size(i);
238  s << "]->" << bi.total_size();
239  return s;
240 }
241 
242 
243 
244 /* ---------------------- template and inline functions ------------------- */
245 
246 inline
247 void
248 BlockIndices::reinit (const unsigned int nb,
249  const size_type block_size)
250 {
251  n_blocks = nb;
252  start_indices.resize(n_blocks+1);
253  for (size_type i=0; i<=n_blocks; ++i)
254  start_indices[i] = i * block_size;
255 }
256 
257 
258 
259 inline
260 void
261 BlockIndices::reinit (const std::vector<size_type> &block_sizes)
262 {
263  if (start_indices.size() != block_sizes.size()+1)
264  {
265  n_blocks = static_cast<unsigned int>(block_sizes.size());
266  start_indices.resize(n_blocks+1);
267  }
268  start_indices[0] = 0;
269  for (size_type i=1; i<=n_blocks; ++i)
270  start_indices[i] = start_indices[i-1] + block_sizes[i-1];
271 }
272 
273 
274 inline
276  :
277  n_blocks(0),
278  start_indices(1, 0)
279 {}
280 
281 
282 
283 inline
284 BlockIndices::BlockIndices (const unsigned int n_blocks,
285  const size_type block_size)
286  :
287  n_blocks(n_blocks),
288  start_indices(n_blocks+1)
289 {
290  for (size_type i=0; i<=n_blocks; ++i)
291  start_indices[i] = i * block_size;
292 }
293 
294 
295 
296 inline
297 BlockIndices::BlockIndices (const std::vector<size_type> &block_sizes)
298  :
299  n_blocks(static_cast<unsigned int>(block_sizes.size())),
300  start_indices(block_sizes.size()+1)
301 {
302  reinit (block_sizes);
303 }
304 
305 
306 
307 inline
309 :
310 n_blocks(b.n_blocks),
311  start_indices(std::move(b.start_indices))
312 {
313  b.n_blocks = 0;
314  b.start_indices = std::vector<size_type>(1, 0);
315 }
316 
317 
318 
319 inline
320 void
322 {
323  start_indices.push_back(start_indices[n_blocks]+sz);
324  ++n_blocks;
326 }
327 
328 
329 inline
330 std::pair<unsigned int,BlockIndices::size_type>
332 {
333  Assert (i<total_size(), ExcIndexRangeType<size_type>(i, 0, total_size()));
334  Assert (n_blocks > 0, ExcLowerRangeType<size_type>(i, size_type(1)));
335 
336  unsigned int block = n_blocks-1;
337  while (i < start_indices[block])
338  --block;
339 
340  return std::pair<unsigned int,size_type>(block,
341  i-start_indices[block]);
342 }
343 
344 
345 inline
347 BlockIndices::local_to_global (const unsigned int block,
348  const size_type index) const
349 {
350  Assert (block < n_blocks, ExcIndexRange(block, 0, n_blocks));
351  Assert (index < start_indices[block+1]-start_indices[block],
352  ExcIndexRangeType<size_type> (index, 0, start_indices[block+1]-start_indices[block]));
353 
354  return start_indices[block]+index;
355 }
356 
357 
358 inline
359 unsigned int
361 {
362  return n_blocks;
363 }
364 
365 
366 
367 inline
370 {
371  if (n_blocks == 0) return 0;
372  return start_indices[n_blocks];
373 }
374 
375 
376 
377 inline
379 BlockIndices::block_size (const unsigned int block) const
380 {
381  Assert (block < n_blocks, ExcIndexRange(block, 0, n_blocks));
382  return start_indices[block+1]-start_indices[block];
383 }
384 
385 
386 
387 inline
388 std::string
390 {
391  std::string result = "[" + Utilities::int_to_string(n_blocks) + "->";
392  for (unsigned int i=0; i<n_blocks; ++i)
393  {
394  if (i>0)
395  result += ',';
396  result += Utilities::to_string(block_size(i));
397  }
398  result += "|" + Utilities::to_string(total_size()) + ']';
399  return result;
400 }
401 
402 
403 
404 inline
406 BlockIndices::block_start (const unsigned int block) const
407 {
408  Assert (block < n_blocks, ExcIndexRange(block, 0, n_blocks));
409  return start_indices[block];
410 }
411 
412 
413 
414 inline
415 BlockIndices &
417 {
418  start_indices = b.start_indices;
419  n_blocks = b.n_blocks;
420  return *this;
421 }
422 
423 
424 
425 inline
426 BlockIndices &
428 {
429  start_indices = std::move(b.start_indices);
430  n_blocks = b.n_blocks;
431 
432  b.start_indices = std::vector<size_type>(1, 0);
433  b.n_blocks = 0;
434 
435  return *this;
436 }
437 
438 
439 
440 inline
441 bool
443 {
444  if (n_blocks != b.n_blocks)
445  return false;
446 
447  for (size_type i=0; i<=n_blocks; ++i)
448  if (start_indices[i] != b.start_indices[i])
449  return false;
450 
451  return true;
452 }
453 
454 
455 
456 inline
457 void
459 {
460  std::swap(n_blocks, b.n_blocks);
461  std::swap(start_indices, b.start_indices);
462 }
463 
464 
465 
466 inline
467 std::size_t
469 {
470  return (sizeof(*this) +
471  start_indices.size() * sizeof(start_indices[0]));
472 }
473 
474 
475 
476 /* ----------------- global functions ---------------------------- */
477 
478 
487 inline
489 {
490  u.swap (v);
491 }
492 
493 
494 
495 
496 DEAL_II_NAMESPACE_CLOSE
497 
498 #endif
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1248
STL namespace.
size_type block_size(const unsigned int i) const
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
std::string to_string(const number value, const unsigned int digits=numbers::invalid_unsigned_int)
Definition: utilities.cc:107
size_type total_size() const
unsigned int n_blocks
void swap(BlockIndices &u, BlockIndices &v)
bool operator==(const BlockIndices &b) const
unsigned int global_dof_index
Definition: types.h:88
#define Assert(cond, exc)
Definition: exceptions.h:1142
void reinit(const unsigned int n_blocks, const size_type n_elements_per_block)
types::global_dof_index size_type
Definition: block_indices.h:64
std::string to_string() const
size_type local_to_global(const unsigned int block, const size_type index) const
std::string int_to_string(const unsigned int value, const unsigned int digits=numbers::invalid_unsigned_int)
Definition: utilities.cc:98
std::vector< size_type > start_indices
void swap(Vector< Number > &u, Vector< Number > &v)
Definition: vector.h:1312
BlockIndices & operator=(const BlockIndices &b)
std::size_t memory_consumption() const
size_type block_start(const unsigned int i) const
void push_back(const size_type size)
void swap(BlockIndices &b)
unsigned int size() const
std::pair< unsigned int, size_type > global_to_local(const size_type i) const