rsvs3D  0.0.0
Codes for the c++ implementation of the 3D RSVS
snakstruct_incl.cpp
Go to the documentation of this file.
1 
10 #ifndef SNAKSTRUCT_INCL_H_INCLUDED
11 #define SNAKSTRUCT_INCL_H_INCLUDED
12 
13 #include "arraystructures.hpp" // never does anything but useful for linter
14 
15 template <class T> bool SnakStruct<T>::checkready()
16 {
17  readyforuse = ArrayStruct<T>::checkready();
18 
19  readyforuse = (readyforuse && isHashParent);
20 
21  return (readyforuse);
22 }
23 
24 template <class T> void SnakStruct<T>::HashParent()
25 {
26  // Generates a valid unordered_map for the current ArrayStruct
27  // Function should not be called repeatedly
28  if (!hashParent.empty())
29  {
30  hashParent.clear();
31  }
32  hashParent.reserve(elems.size());
33  for (int i = 0; i < int(elems.size()); ++i)
34  {
35  hashParent.emplace(elems[i].KeyParent(), i);
36  }
37  isHashParent = 1;
38 
39  // std::cout << "Array Struct Succesfully Hashed" << std::endl;
40 }
41 
42 template <class T> void SnakStruct<T>::DeHashParent(const int pos)
43 {
44  int key = elems[pos].KeyParent();
45  auto it = hashParent.find(key);
46  while (it->second != pos && it->first == key)
47  {
48  ++it;
49  }
50 
51  if (it->second == pos && it->first == key)
52  {
53  hashParent.erase(it);
54  }
55  else
56  {
57  std::cerr << "Error: Key value std::pair not found and could not be removed " << std::endl;
58  std::cerr << " key " << key << " pos " << pos << std::endl;
59  std::cerr << " in function:" << __PRETTY_FUNCTION__ << std::endl;
60  RSVS3D_ERROR_ARGUMENT("Key value std::pair not found and could not be removed");
61  }
62 }
63 template <class T> bool SnakStruct<T>::memberIsHashParent(const int pos) const
64 {
65  int key = elems[pos].KeyParent();
66  auto it = hashParent.find(key);
67  if (it != hashParent.end())
68  {
69  while (it != hashParent.end() && it->second != pos && it->first == key)
70  {
71  ++it;
72  }
73 
74  if (it != hashParent.end() && it->second == pos && it->first == key)
75  {
76  return (true);
77  }
78  else
79  {
80  return (false);
81  }
82  }
83  else
84  {
85  return (false);
86  }
87 }
88 
89 template <class T> void SnakStruct<T>::PrepareForUse()
90 {
92 
93  if (isHashParent == 0)
94  {
95  this->HashParent();
96  }
97 
98  readyforuse = true;
99 }
100 
101 template <class T> void SnakStruct<T>::Concatenate(const SnakStruct<T> &other)
102 {
104  isHashParent = 0;
105 }
106 
107 template <class T> void SnakStruct<T>::remove(const std::vector<int> &sub)
108 {
110  isHashParent = 0;
111 }
112 
113 template <class T> void SnakStruct<T>::ForceArrayReady()
114 {
116  isHashParent = 1;
117 }
118 template <class T> inline void SnakStruct<T>::clear()
119 {
121  hashParent.clear();
122  // isHashParent=0;
123 }
124 template <class T> inline void SnakStruct<T>::Init(int n)
125 {
126  this->clear();
128 }
129 template <class T> int SnakStruct<T>::findparent(int key) const
130 {
131  if (isHashParent == 0)
132  {
133  std::cerr << "Warning: reading from potentially obsolete unordered_map " << std::endl;
134  std::cerr << " in snaxarray::findedge(int key)" << std::endl;
135  }
136  auto search = hashParent.find(key);
137 
138  if (search == hashParent.end())
139  {
140  return (-1);
141  }
142 #ifdef SAFE_ACCESS
143  int key2;
144  key2 = elems[search->second].KeyParent();
145  if (key2 != key)
146  {
147  RSVS3D_ERROR_ARGUMENT("FINDPARENT returned an invalid output ");
148  }
149 #endif // SAFE_ACCESS
150  return (search->second);
151 }
152 
153 template <class T> void SnakStruct<T>::findsiblings(int key, std::vector<int> &siblings) const
154 {
155  if (isHashParent == 0)
156  {
157  std::cerr << "Warning: reading from potentially obsolete std::unordered_multimap " << std::endl;
158  std::cerr << " in " << __PRETTY_FUNCTION__ << std::endl;
159  std::cerr << " To avoid this message perform read operations on "
160  "ArrayStruct<T> using the () operator"
161  << std::endl;
162  }
163  ReturnDataEqualRange(key, hashParent, siblings);
164 }
165 template <class T> inline void SnakStruct<T>::push_back(T &newelem)
166 {
167  ArrayStruct<T>::push_back(newelem);
168 
169  hashParent.emplace(newelem.KeyParent(), elems.size() - 1);
170  // isHashParent=0;
171 }
172 
173 // Surfarray methods
174 template <class T> void ModiftrackArray<T>::SetNoModif()
175 {
176  int ii, n;
177  n = ArrayStruct<T>::size();
178  for (ii = 0; ii < n; ++ii)
179  {
180  elems[ii].isModif = false;
181  }
182 }
183 
184 template <class T> void ModiftrackArray<T>::ReturnModifInd(std::vector<int> &vecind)
185 {
186  int ii, n;
187  n = ArrayStruct<T>::size();
188  vecind.clear();
189  for (ii = 0; ii < n; ++ii)
190  {
191  if (elems[ii].isModif)
192  {
193  vecind.push_back(elems[ii].index);
194  }
195  }
196 }
197 
198 template <class T> void ModiftrackArray<T>::ReturnModifLog(std::vector<bool> &modiflog)
199 {
200  int ii, n;
201  n = ArrayStruct<T>::size();
202  modiflog.assign(n, false);
203  for (ii = 0; ii < n; ++ii)
204  {
205  modiflog[ii] = (elems[ii].index);
206  }
207 }
208 
209 #endif // ARRAYSTRUCTS_INCL_H_INCLUDED
Provide std::vector container with hashed index mapping.
#define RSVS3D_ERROR_ARGUMENT(M)
Throw a invalid_argument.
Definition: warning.hpp:148