MEPP2 Project
AIFCellContainer.h
Go to the documentation of this file.
1 // Copyright (c) 2012-2019 University of Lyon and CNRS (France).
2 // All rights reserved.
3 //
4 // This file is part of MEPP2; you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as
6 // published by the Free Software Foundation; either version 3 of
7 // the License, or (at your option) any later version.
8 //
9 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
10 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 #pragma once
12 
13 namespace FEVV {
14 namespace DataStructures {
15 namespace AIF {
16 
17 /*
18  * A class to store pointers to cells.
19  * The index of a cell may vary in time according to the
20  * remove operations that occur on the container.
21  * When a cell is removed from the container, it is
22  * not destroyed.
23  */
24 template< typename T >
26 {
27 public:
28  typedef typename std::vector< T >::iterator iterator;
29  typedef typename std::vector< T >::const_iterator const_iterator;
30 
34  void add(T c)
35  {
36  c->SetIndex(container.size());
37  container.push_back(c);
38  }
43  std::size_t remove(std::size_t i)
44  {
45  std::size_t size = container.size();
46  std::size_t lastId = size - 1;
47 
48  if(size == 0 || i > lastId)
49  throw std::runtime_error(
50  "In CellContainer::remove(): index out of range.");
51 
52  // Remove element #i.
53  // If #i is not the last element, then replace #i by the last
54  // element, then free the last slot.
55  std::size_t tmp = container[i]->GetIndex();
56  bool ascending_order = (tmp==i) && (container[lastId]->GetIndex() == lastId); // not exact, but avoid linear access for meshes without vertex reordering
57  //for(std::size_t n=0; (n < lastId) && ascending_order; ++n)
58  // if (container[n]->GetIndex() > container[n + 1]->GetIndex())
59  // {
60  // ascending_order = false;
61  // break;
62  // }
63  if (ascending_order)
64  {
65  container[i] = container[lastId];
66  container[i]->SetIndex(i);
67  }
68  else
69  {
70  if (tmp == lastId)
71  {
72  container[i] = container[lastId];
73  }
74  else
75  {
76  std::size_t index_max = 0;
77  for(std::size_t n=1; n <= lastId; ++n)
78  if (container[n]->GetIndex() > container[index_max]->GetIndex())
79  {
80  index_max = n;
81  }
82 
83  container[i] = container[lastId];
84 
85  container[index_max]->SetIndex(tmp);
86  }
87  }
88 
89  container.pop_back();
90 
91  return lastId;
92  }
93 
98  T &operator[](std::size_t idx) { return container[idx]; }
103  const T &operator[](std::size_t idx) const { return container[idx]; }
108  iterator begin(void) { return container.begin(); }
112  iterator end(void) { return container.end(); }
117  const_iterator begin(void) const { return container.begin(); }
122  const_iterator end(void) const { return container.end(); }
127  const_iterator cbegin(void) const { return container.cbegin(); }
132  const_iterator cend(void) const { return container.cend(); }
136  std::size_t size(void) const { return container.size(); }
140  void reserve(std::size_t n) { container.reserve(n); }
141 
146  void erase(const iterator iter) { container.erase(iter); }
150  void displayDebugInfos(const char *title)
151  {
152  std::cout << "Debug, " << title << ":\n";
153  std::cout << " size=" << container.size() << '\n';
154  for(std::size_t i = 0; i < container.size(); i++)
155  std::cout << " [" << i << "]: " << container[i]->GetIndex() << ", "
156  << container[i]->GetDegree() << '\n';
157  }
158 
159 private:
161  std::vector< T > container;
162 };
163 
164 } // namespace AIF
165 } // namespace DataStructures
166 } // namespace FEVV
167 
FEVV::DataStructures::AIF::AIFCellContainer::cend
const_iterator cend(void) const
Definition: AIFCellContainer.h:132
FEVV::DataStructures::AIF::AIFCellContainer::cbegin
const_iterator cbegin(void) const
Definition: AIFCellContainer.h:127
FEVV::DataStructures::AIF::AIFCellContainer::add
void add(T c)
Definition: AIFCellContainer.h:34
FEVV::DataStructures::AIF::AIFCellContainer::reserve
void reserve(std::size_t n)
Definition: AIFCellContainer.h:140
FEVV::DataStructures::AIF::AIFCellContainer::iterator
std::vector< T >::iterator iterator
Definition: AIFCellContainer.h:28
FEVV::DataStructures::AIF::AIFCellContainer::begin
iterator begin(void)
Definition: AIFCellContainer.h:108
FEVV
Interfaces for plugins These interfaces will be used for different plugins.
Definition: Assert.h:16
FEVV::DataStructures::AIF::AIFCellContainer::remove
std::size_t remove(std::size_t i)
Definition: AIFCellContainer.h:43
FEVV::DataStructures::AIF::AIFCellContainer::displayDebugInfos
void displayDebugInfos(const char *title)
Definition: AIFCellContainer.h:150
FEVV::DataStructures::AIF::AIFCellContainer::operator[]
T & operator[](std::size_t idx)
Definition: AIFCellContainer.h:98
FEVV::DataStructures::AIF::AIFCellContainer::container
std::vector< T > container
the real cell container
Definition: AIFCellContainer.h:161
FEVV::DataStructures::AIF::AIFCellContainer
Definition: AIFCellContainer.h:26
FEVV::DataStructures::AIF::AIFCellContainer::const_iterator
std::vector< T >::const_iterator const_iterator
Definition: AIFCellContainer.h:29
FEVV::DataStructures::AIF::AIFCellContainer::end
const_iterator end(void) const
Definition: AIFCellContainer.h:122
FEVV::DataStructures::AIF::AIFCellContainer::end
iterator end(void)
Definition: AIFCellContainer.h:112
FEVV::DataStructures::AIF::AIFCellContainer::erase
void erase(const iterator iter)
Definition: AIFCellContainer.h:146
FEVV::DataStructures::AIF::AIFCellContainer::operator[]
const T & operator[](std::size_t idx) const
Definition: AIFCellContainer.h:103
FEVV::DataStructures::AIF::AIFCellContainer::size
std::size_t size(void) const
Definition: AIFCellContainer.h:136
FEVV::DataStructures::AIF::AIFCellContainer::begin
const_iterator begin(void) const
Definition: AIFCellContainer.h:117