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  size_t len
33 )
34 {
35 
36 
37  if(is.isBinary() && !std::is_same_v<T,word>)
38  {
39  this->resize(len);
40  is.read(reinterpret_cast<char*>(this->data()), this->size()*sizeof(T));
41  }
42  else
43  {
44  this->clear();
46 
47  token firstToken(is);
48 
49  T val{};
50  if( firstToken.isPunctuation() ) // start of vector
51  {
52  if(firstToken != token::BEGIN_LIST)
53  {
55  << "expected token "<< token::BEGIN_LIST
56  << " but found "<< firstToken ;
57  return false;
58 
59  }
60 
61  token lastToken(is);
62 
63 
65 
66  while
67  ( !(
68  lastToken.isPunctuation()
69  && lastToken == token::END_LIST
70 
71  )
72  )
73  {
74 
75  is.putBack(lastToken);
76 
77  is >> val;
78  this->push_back(val);
79 
80  is >> lastToken;
82  }
83 
84  } else
85  {
87  << "expected token "<< token::BEGIN_LIST
88  << " but found "<< firstToken ;
89  return false;
90  }
91  }
92 
93 
94 
95  return true;
96 }
97 
98 
99 template<typename T, typename Allocator>
101 (
102  iOstream& os
103 ) const
104 {
105  // start of
106  if( os.isBinary() && !std::is_same_v<T,word>)
107  {
108  os.write(reinterpret_cast<const char*>(this->data()), this->size()*sizeof(T));
109  }
110  else
111  {
112 
113  auto len = size();
114  auto stride = getVectorStride(len);
115  os << token::BEGIN_LIST;
116  label i = 0;
117  while( i<len )
118  {
119 
120  os << this->operator[](i++);
121  for(label j=0; j<stride-1 && i<len; j++ )
122  {
123  os << token::SPACE << this->operator[](i++);
124  }
125 
126  if(i<len)
127  os<< token::NL;
128  }
129 
130  os << token::END_LIST;
131 
132  os.check(FUNCTION_NAME);
133  }
134 
135  return true;
136 }
137 
138 template<typename T, typename Allocator>
140 (
141  const Vector<label>& indices
142 )
143 {
144  if( indices.size() == 0 )return true;
145 
146  if( indices.size() == 1 )
147  {
148  return deleteElement(indices[0]);
149  }
150 
151  if( *(indices.end()-1) >= size() ) return false;
152 
153 
154  VectorType tmp(capacity(), RESERVE());
155  tmp.clear();
156 
157  label lindex = 0;
158  for(auto& delem: indices)
159  {
160  for(label i=lindex; i<delem; i++)
161  {
162  tmp.push_back( vectorType::operator[](i) );
163  }
164  lindex = delem+1;
165  }
166 
167  // copy after the last delete element
168  for(label i=lindex; i<size(); i++)
169  {
170  tmp.push_back( vectorType::operator[](i) );
171  }
172 
173 
174  vectorType::swap(tmp);
175 
176  return true;
177 }
178 
179 
180 template<typename T, typename Allocator>
182 (
183  const Vector<label>& indices
184 )
185 {
186  if( indices.size() == 0 )return true;
187 
188  if( indices.size() == 1)
189  {
190  return deleteElement(indices[0]);
191  }
192 
193  // sorts
194  auto sorted = indices;
195  sort(sorted);
196  return deleteElement_sorted(sorted);
197 }
198 
199 template<typename T, typename Allocator>
201 (
202  label index
203 )
204 {
205  if(index < size())
206  {
207  auto iter = vectorType::begin();
208  advance(iter, index);
209  vectorType::erase(iter);
210  return true;
211  }
212  else
213  return false;
214 }
215 
216 template<typename T, typename Allocator>
218  const int32IndexContainer& indices)
219 {
220  if(indices.size() == 0)
221  {
222  this->resize(0);
223  return;
224  }
225  size_t newSize = indices.size();
226  auto hIndices = indices.hostView();
227  VectorType sortedVec(name(), capacity(), newSize, RESERVE());
228 
229  ForAll(i, hIndices)
230  {
231  sortedVec[i] = vectorType::operator[](i);
232  }
233  *this = std::move(sortedVec);
234 }
235 
236 template<typename T, typename Allocator>
238  const int32IndexContainer& indices,
239  const T& val)
240 {
241 
242  if(indices.size() == 0)return true;
243 
244  auto hIndices = indices.hostView();
245 
246  ForAll(i, indices)
247  {
248  auto s = size();
249  auto idx = hIndices[i];
250 
251  if( idx < s )
252  {
253  this->operator[](idx) = val;
254  }
255  else if(idx == s )
256  {
257  this->push_back(val);
258  }
259  else
260  {
261  this->resize(idx+1);
262  this->operator[](idx) = val;
263  }
264  }
265  return true;
266 }
267 
268 template<typename T, typename Allocator>
270  const int32IndexContainer& indices,
271  const Vector<T>& vals)
272 {
273  if(indices.size() == 0)return true;
274  if(indices.size() != vals.size())return false;
275 
276  auto hIndices = indices.hostView();
277 
278  ForAll(i, indices)
279  {
280  auto s = size();
281  auto idx = hIndices[i];
282  if( idx < s )
283  {
284  this->operator[](idx) = vals[i];
285  }
286  else if(idx == s )
287  {
288  this->push_back(vals[i]);
289  }
290  else
291  {
292  this->resize(idx+1);
293  this->operator[](idx) = vals[i];
294  }
295  }
296  return true;
297 
298 }
299 
300 template<typename T, typename Allocator>
302 (
303  const Vector<int32>& indices,
304  const T& val
305 )
306 {
307  if(indices.size() == 0)return true;
308 
309  ForAll(i, indices)
310  {
311  auto s = size();
312  auto idx = indices[i];
313  if( idx < s )
314  {
315  this->operator[](idx) = val;
316  }
317  else if(idx == s )
318  {
319  this->push_back(val);
320  }
321  else
322  {
323  this->resize(idx+1);
324  this->operator[](idx) = val;
325  }
326  }
327  return true;
328 }
329 
330 
331 template<typename T, typename Allocator>
333 (
334  const Vector<int32>& indices,
335  const Vector<T>& vals
336 )
337 {
338  if(indices.size() == 0)return true;
339  if(indices.size() != vals.size())return false;
340 
341  ForAll(i, indices)
342  {
343  auto s = size();
344  auto idx = indices[i];
345  if( idx < s )
346  {
347  this->operator[](idx) = vals[i];
348  }
349  else if(idx == s )
350  {
351  this->push_back(vals[i]);
352  }
353  else
354  {
355  this->resize(idx+1);
356  this->operator[](idx) = vals[i];
357  }
358  }
359  return true;
360 }
361 
362 template<typename T, typename Allocator>
364 (
365  int32 idx,
366  const T & val
367 )
368 {
369 
370  auto s = size();
371 
372  if( idx < s )
373  {
374  this->operator[](idx) = val;
375  }
376  else if(idx == s)
377  {
378  this->push_back(val);
379  }
380  else
381  {
382  this->resize(idx+1);
383  this->operator[](idx) = val;
384  }
385 
386  return true;
387 }
pFlow::iOstream::write
virtual bool write(const token &tok)=0
Write Functions.
pFlow::Vector::readVector
bool readVector(iIstream &is, size_t len=0)
Definition: Vector.cpp:30
pFlow::iIstream::read
virtual iIstream & read(token &)=0
pFlow::token
Definition: token.hpp:42
pFlow::Vector::sortItems
void sortItems(const int32IndexContainer &indices)
Sort elements based on the indices.
Definition: Vector.cpp:217
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:115
pFlow::Vector::size
auto size() const
Definition: Vector.hpp:301
pFlow::Vector::deleteElement
bool deleteElement(const Vector< label > &indices)
Definition: Vector.cpp:182
pFlow::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.cpp:42
pFlow::Vector::writeVector
bool writeVector(iOstream &os) const
Definition: Vector.cpp:101
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:140
pFlow::indexContainer::hostView
const HostViewType & hostView() const
Definition: indexContainer.hpp:151
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
Check IOstream status for given operation.
Definition: IOstream.cpp:48
ForAll
#define ForAll(i, container)
Definition: pFlowMacros.hpp:71
sort
void sort(Vector< T, Allocator > &vec)
pFlow::Vector::clear
auto clear()
Definition: Vector.hpp:248
pFlow::IOstream::isBinary
bool isBinary() const
Return true if stream format is binray.
Definition: IOstream.hpp:178
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:237