Vector.cpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 
22 template<typename T, typename Allocator>
24 {
25  readVector(is);
26 }
27 
28 template<typename T, typename Allocator>
30 (
31  iIstream& is
32 )
33 {
34  this->clear();
35 
37 
38  token firstToken(is);
39 
40  T val{};
41  if( firstToken.isPunctuation() ) // start of vector
42  {
43  if(firstToken != token::BEGIN_LIST)
44  {
46  << "expected token "<< token::BEGIN_LIST
47  << " but found "<< firstToken ;
48  return false;
49 
50  }
51 
52  token lastToken(is);
53 
54 
56 
57  while
58  ( !(
59  lastToken.isPunctuation()
60  && lastToken == token::END_LIST
61 
62  )
63  )
64  {
65 
66  is.putBack(lastToken);
67 
68  is >> val;
69  this->push_back(val);
70 
71  is >> lastToken;
73  }
74 
75  } else
76  {
78  << "expected token "<< token::BEGIN_LIST
79  << " but found "<< firstToken ;
80  return false;
81 
82  }
83 
84  return true;
85 }
86 
87 
88 template<typename T, typename Allocator>
90 (
91  iOstream& os
92 ) const
93 {
94 
95 
96  auto len = size();
97  auto stride = getVectorStride(len);
98 
99  // start of
100  os << token::BEGIN_LIST;
101  label i = 0;
102  while( i<len )
103  {
104 
105  os << this->operator[](i++);
106  for(label j=0; j<stride-1 && i<len; j++ )
107  {
108  os << token::SPACE << this->operator[](i++);
109  }
110 
111  if(i<len)
112  os<< token::NL;
113  }
114 
115  os << token::END_LIST;
116 
117  os.check(FUNCTION_NAME);
118 
119  return true;
120 }
121 
122 template<typename T, typename Allocator>
124 (
125  const Vector<label>& indices
126 )
127 {
128  if( indices.size() == 0 )return true;
129 
130  if( indices.size() == 1 )
131  {
132  return deleteElement(indices[0]);
133  }
134 
135  if( *(indices.end()-1) >= size() ) return false;
136 
137 
138  VectorType tmp(capacity(), RESERVE());
139  tmp.clear();
140 
141  label lindex = 0;
142  for(auto& delem: indices)
143  {
144  for(label i=lindex; i<delem; i++)
145  {
146  tmp.push_back( vectorType::operator[](i) );
147  }
148  lindex = delem+1;
149  }
150 
151  // copy after the last delete element
152  for(label i=lindex; i<size(); i++)
153  {
154  tmp.push_back( vectorType::operator[](i) );
155  }
156 
157 
158  vectorType::swap(tmp);
159 
160  return true;
161 }
162 
163 
164 template<typename T, typename Allocator>
166 (
167  const Vector<label>& indices
168 )
169 {
170  if( indices.size() == 0 )return true;
171 
172  if( indices.size() == 1)
173  {
174  return deleteElement(indices[0]);
175  }
176 
177  // sorts
178  auto sorted = indices;
179  sort(sorted);
180  return deleteElement_sorted(sorted);
181 }
182 
183 template<typename T, typename Allocator>
185 (
186  label index
187 )
188 {
189  if(index < size())
190  {
191  auto iter = vectorType::begin();
192  advance(iter, index);
193  vectorType::erase(iter);
194  return true;
195  }
196  else
197  return false;
198 }
199 
200 template<typename T, typename Allocator>
202  const int32IndexContainer& indices,
203  const T& val)
204 {
205 
206  if(indices.size() == 0)return true;
207 
208  auto hIndices = indices.hostView();
209 
210  ForAll(i, indices)
211  {
212  auto s = size();
213  auto idx = hIndices[i];
214 
215  if( idx < s )
216  {
217  this->operator[](idx) = val;
218  }
219  else if(idx == s )
220  {
221  this->push_back(val);
222  }
223  else
224  {
225  this->resize(idx+1);
226  this->operator[](idx) = val;
227  }
228  }
229  return true;
230 }
231 
232 template<typename T, typename Allocator>
234  const int32IndexContainer& indices,
235  const Vector<T>& vals)
236 {
237  if(indices.size() == 0)return true;
238  if(indices.size() != vals.size())return false;
239 
240  auto hIndices = indices.hostView();
241 
242  ForAll(i, indices)
243  {
244  auto s = size();
245  auto idx = hIndices[i];
246  if( idx < s )
247  {
248  this->operator[](idx) = vals[i];
249  }
250  else if(idx == s )
251  {
252  this->push_back(vals[i]);
253  }
254  else
255  {
256  this->resize(idx+1);
257  this->operator[](idx) = vals[i];
258  }
259  }
260  return true;
261 
262 }
263 
264 template<typename T, typename Allocator>
266 (
267  const Vector<int32>& indices,
268  const T& val
269 )
270 {
271  if(indices.size() == 0)return true;
272 
273  ForAll(i, indices)
274  {
275  auto s = size();
276  auto idx = indices[i];
277  if( idx < s )
278  {
279  this->operator[](idx) = val;
280  }
281  else if(idx == s )
282  {
283  this->push_back(val);
284  }
285  else
286  {
287  this->resize(idx+1);
288  this->operator[](idx) = val;
289  }
290  }
291  return true;
292 }
293 
294 
295 template<typename T, typename Allocator>
297 (
298  const Vector<int32>& indices,
299  const Vector<T>& vals
300 )
301 {
302  if(indices.size() == 0)return true;
303  if(indices.size() != vals.size())return false;
304 
305  ForAll(i, indices)
306  {
307  auto s = size();
308  auto idx = indices[i];
309  if( idx < s )
310  {
311  this->operator[](idx) = vals[i];
312  }
313  else if(idx == s )
314  {
315  this->push_back(vals[i]);
316  }
317  else
318  {
319  this->resize(idx+1);
320  this->operator[](idx) = vals[i];
321  }
322  }
323  return true;
324 }
325 
326 template<typename T, typename Allocator>
328 (
329  int32 idx,
330  const T & val
331 )
332 {
333 
334  auto s = size();
335 
336  if( idx < s )
337  {
338  this->operator[](idx) = val;
339  }
340  else if(idx == s)
341  {
342  this->push_back(val);
343  }
344  else
345  {
346  this->resize(idx+1);
347  this->operator[](idx) = val;
348  }
349 
350  return true;
351 }
pFlow::token
Definition: token.hpp:42
warningInFunction
#define warningInFunction
Definition: error.hpp:55
pFlow::token::isPunctuation
bool isPunctuation() const
Definition: tokenI.hpp:426
pFlow::Vector::Vector
Vector()
Definition: Vector.hpp:123
FUNCTION_NAME
#define FUNCTION_NAME
Definition: pFlowMacros.hpp:29
pFlow::indexContainer::size
INLINE_FUNCTION_HD size_t size() const
Definition: indexContainer.hpp:107
pFlow::Vector::size
auto size() const
Definition: Vector.hpp:299
pFlow::Vector::deleteElement
bool deleteElement(const Vector< label > &indices)
Definition: Vector.cpp:166
pFlow::IOstream::check
virtual bool check(const char *operation) const
Definition: IOstream.cpp:42
pFlow::Vector::writeVector
bool writeVector(iOstream &os) const
Definition: Vector.cpp:90
RESERVE
Definition: Vector.hpp:38
pFlow::iIstream
Definition: iIstream.hpp:33
pFlow::Vector::deleteElement_sorted
bool deleteElement_sorted(const Vector< label > &indices)
Definition: Vector.cpp:124
pFlow::indexContainer::hostView
const HostViewType & hostView() const
Definition: indexContainer.hpp:143
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::iIstream::putBack
void putBack(const token &tok)
Definition: iIstream.cpp:5
pFlow::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Definition: IOstream.cpp:48
ForAll
#define ForAll(i, container)
Definition: pFlowMacros.hpp:71
pFlow::Vector::readVector
bool readVector(iIstream &is)
Definition: Vector.cpp:30
sort
void sort(Vector< T, Allocator > &vec)
pFlow::Vector::clear
auto clear()
Definition: Vector.hpp:248
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::Vector
Definition: Vector.hpp:46
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::indexContainer< int32 >
pFlow::Vector::insertSetElement
bool insertSetElement(const int32IndexContainer &indices, const T &val)
Definition: Vector.cpp:201