www.cemf.ir
ListI.hpp
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 template<typename T>
23 (
24  size_t i,
25  bool noError
26 )
27 {
28  if( i >= size() )
29  {
30  if(noError)
31  {
32  return std::end(*this);
33  }
34  else
35  {
37  "out of range access to list element. \n";
38  fatalExit;
39  }
40  }
41  auto iter = listType::begin();
42  std::advance(iter, i);
43  return iter;
44 }
45 
46 template<typename T>
47 const auto pFlow::List<T>::pos
48 (
49  size_t i,
50  bool noError
51 )const
52 {
53  if( i >= size() )
54  {
55  if(noError)
56  {
57  return std::end(*this);
58  }
59  else
60  {
62  "out of range access to list element. \n";
63  fatalExit;
64  }
65  }
66  auto iter = listType::cbegin();
67  std::advance(iter, i);
68  return iter;
69 }
70 
71 
72 template<typename T>
74 (
75  const T& elm
76 ) const
77 {
78  return std::count( listType::begin(), listType::end(), elm);
79 }
80 
81 template<typename T>
82 inline size_t pFlow::List<T>::size()const
83 {
84  return listType::size();
85 }
86 
87 template<typename T>
88 inline T& pFlow::List<T>::operator[]
89 (
90  size_t i
91 )
92 {
93  return *pos(i);
94 }
95 
96 template<typename T>
97 inline const T& pFlow::List<T>::operator[]
98 (
99  size_t i
100 )const
101 {
102 
103  return *pos(i);
104 }
105 
106 template<typename T>
108 (
109  const T& val
110 ) const
111 {
112  return std::find(this->begin(),this->end(), val);
113 }
114 
115 template<typename T>
117 (
118  const T& val
119 )
120 {
121  return std::find(this->begin(),this->end(), val);
122 }
123 
124 template<typename T>
126 {
127  auto pos = find(val);
128  if( pos == this->end() )return -1;
129  return static_cast<int32> (std::distance(this->begin(), pos));
130 }
131 
132 template<typename T>
134 (
135  const T& val
136 ) const
137 {
138  if( find(val) == this->end())return false;
139  return true;
140 }
141 
142 
143 template<typename T>
144 inline void pFlow::List<T>::set(size_t i, const T& val)
145 {
146  auto p = pos(i);
147  *p = val;
148 }
149 
150 template<typename T>
151 inline void pFlow::List<T>::set(size_t i, T&& val)
152 {
153  auto p = pos(i);
154  *p = std::move(val);
155 }
156 
157 
158 template<typename T>
159 inline bool pFlow::List<T>::writeList
160 (
161  iOstream& os
162 ) const
163 {
164 
165  size_t len = size();
166  size_t stride = getListStride(len);
167 
168  // start of List
169 
170  os << beginListToken();
171 
172  for(auto elm = listType::begin(); elm!=listType::end(); )
173  {
174  os<< *elm++;
175 
176  for(size_t j=0; j<stride && elm!=listType::end(); j++)
177  {
178  os<<spaceToken()<< *elm++;
179  }
180 
181  if( elm!=listType::end() )
182  os<<newLineToken();
183  }
184 
185  os<< endListToken();
186 
187  //os.check(FUNCTION_NAME);
188 
189  return os.check(FUNCTION_NAME);
190 }
191 
192 template<typename T>
193 inline bool pFlow::List<T>::readList
194 (
195  iIstream& is
196 )
197 {
198  // first clear the list
199  listType::clear();
200 
202 
203  token firstToken(is);
204 
205  if( firstToken.isPunctuation() ) // start of list
206  {
207  if(firstToken != beginListToken() )
208  {
209  ioErrorInFile(is.name(), is.lineNumber())
210  << "expected token "<< token::BEGIN_LIST
211  << " but found "<< firstToken ;
212  return false;
213 
214  }
215 
216  token lastToken(is);
217 
219 
220  while
221  ( !(
222  lastToken.isPunctuation()
223  && lastToken == token::END_LIST
224 
225  )
226  )
227  {
228 
229  is.putBack(lastToken);
230 
231  T val;
232  is >> val;
233 
234  listType::push_back(val);
235 
236  is >> lastToken;
238 
239  }
240 
241  }
242  else
243  {
244  ioErrorInFile(is.name(), is.lineNumber())
245  << "expected token "<< beginListToken()
246  << " but found "<< firstToken ;
247  return false;
248 
249  }
250 
251  return is.fatalCheck(FUNCTION_NAME);
252 
253 }
254 
255 template<typename T>
256 inline pFlow::iOstream& pFlow::operator << (iOstream& os, const List<T>& lst )
257 {
258  if(!lst.writeList(os))
259  {
260  fatalExit;
261  }
262  return os;
263 }
264 
265 template<typename T>
266 inline pFlow::iIstream& pFlow::operator >>(iIstream& is, List<T>& lst)
267 {
268  if( !lst.readList(is) )
269  {
270  fatalExit;
271  }
272  return is;
273 }
count
auto count(const Vector< T, Allocator > &vec, const T &val)
pFlow::List
Definition: List.hpp:39
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::token
Token class based on OpenFOAM stream, with some modifications/simplifications to be tailored to our n...
Definition: token.hpp:44
pFlow::List::set
void set(size_t i, const T &val)
Definition: ListI.hpp:144
pFlow::find
int64 find(Vector< T, Allocator > &vec, const T &val)
Definition: VectorAlgorithm.hpp:69
pFlow::token::isPunctuation
bool isPunctuation() const
Token is PUNCTUATION.
Definition: tokenI.hpp:444
pFlow::List::search
bool search(const T &val) const
Definition: ListI.hpp:134
pFlow::List::const_iterator
typename listType::const_iterator const_iterator
Definition: List.hpp:52
FUNCTION_NAME
#define FUNCTION_NAME
Definition: pFlowMacros.hpp:29
pFlow::List::find
const_iterator find(const T &val) const
Definition: ListI.hpp:108
pFlow::newLineToken
token newLineToken()
Definition: token.hpp:533
pFlow::List::countElement
int32 countElement(const T &elm) const
Definition: ListI.hpp:74
pFlow::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.cpp:42
pFlow::List::findi
int32 findi(const T &val) const
Definition: ListI.hpp:125
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::int32
int int32
Definition: builtinTypes.hpp:50
pFlow::iIstream::putBack
void putBack(const token &tok)
Put back token Only a single put back is permitted.
Definition: iIstream.cpp:23
pFlow::operator>>
INLINE_FUNCTION iIstream & operator>>(iIstream &str, AB3History &ab3)
Definition: AdamsBashforth3.hpp:41
pFlow::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.cpp:48
pFlow::List::iterator
typename listType::iterator iterator
Definition: List.hpp:50
pFlow::beginListToken
token beginListToken()
Definition: token.hpp:508
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
pFlow::spaceToken
token spaceToken()
Definition: token.hpp:528
pFlow::IOstream::name
virtual const word & name() const
Return the name of the stream.
Definition: IOstream.cpp:31
pFlow::List::writeList
bool writeList(iOstream &os) const
Definition: ListI.hpp:160
pFlow::endListToken
token endListToken()
Definition: token.hpp:503
pFlow::List::size
size_t size() const
Definition: ListI.hpp:82
pFlow::List::readList
bool readList(iIstream &is)
Definition: ListI.hpp:194
pFlow::List::pos
auto pos(size_t i, bool noError=false)
Definition: ListI.hpp:23
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Report an error in file operation with supplied fileName and lineNumber.
Definition: error.hpp:87
pFlow::IOstream::lineNumber
int32 lineNumber() const
Const access to the current stream line number.
Definition: IOstream.hpp:223
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59