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 )
26 {
27  if( i >= size() )
28  {
30  "our of range access to list element. \n";
31  fatalExit;
32  }
33  auto iter = listType::begin();
34  std::advance(iter, i);
35  return iter;
36 }
37 
38 template<typename T>
39 const auto pFlow::List<T>::pos
40 (
41  size_t i
42 )const
43 {
44  if( i >= size() )
45  {
47  "our of range access to list element. \n";
48  fatalExit;
49  }
50  auto iter = listType::cbegin();
51  std::advance(iter, i);
52  return iter;
53 }
54 
55 
56 template<typename T>
58 (
59  const T& elm
60 ) const
61 {
62  return std::count( listType::begin(), listType::end(), elm);
63 }
64 
65 template<typename T>
66 inline size_t pFlow::List<T>::size()const
67 {
68  return listType::size();
69 }
70 
71 template<typename T>
72 inline T& pFlow::List<T>::operator[]
73 (
74  size_t i
75 )
76 {
77  return *pos(i);
78 }
79 
80 template<typename T>
81 inline const T& pFlow::List<T>::operator[]
82 (
83  size_t i
84 )const
85 {
86 
87  return *pos(i);
88 }
89 
90 template<typename T>
92 (
93  const T& val
94 ) const
95 {
96  return std::find(this->begin(),this->end(), val);
97 }
98 
99 template<typename T>
101 (
102  const T& val
103 )
104 {
105  return std::find(this->begin(),this->end(), val);
106 }
107 
108 template<typename T>
110 {
111  auto pos = find(val);
112  if( pos == this->end() )return -1;
113  return static_cast<int32> (std::distance(this->begin(), pos));
114 }
115 
116 template<typename T>
118 (
119  const T& val
120 ) const
121 {
122  if( find(val) == this->end())return false;
123  return true;
124 }
125 
126 
127 template<typename T>
128 inline void pFlow::List<T>::set(size_t i, const T& val)
129 {
130  auto p = pos(i);
131  *p = val;
132 }
133 
134 template<typename T>
135 inline void pFlow::List<T>::set(size_t i, T&& val)
136 {
137  auto p = pos(i);
138  *p = std::move(val);
139 }
140 
141 
142 template<typename T>
143 inline bool pFlow::List<T>::writeList
144 (
145  iOstream& os
146 ) const
147 {
148 
149  size_t len = size();
150  size_t stride = getListStride(len);
151 
152  // start of List
153 
154  os << beginListToken();
155 
156  for(auto elm = listType::begin(); elm!=listType::end(); )
157  {
158  os<< *elm++;
159 
160  for(size_t j=0; j<stride && elm!=listType::end(); j++)
161  {
162  os<<spaceToken()<< *elm++;
163  }
164 
165  if( elm!=listType::end() )
166  os<<newLineToken();
167  }
168 
169  os<< endListToken();
170 
171  //os.check(FUNCTION_NAME);
172 
173  return os.check(FUNCTION_NAME);
174 }
175 
176 template<typename T>
177 inline bool pFlow::List<T>::readList
178 (
179  iIstream& is
180 )
181 {
182  // first clear the list
183  listType::clear();
184 
186 
187  token firstToken(is);
188 
189  if( firstToken.isPunctuation() ) // start of list
190  {
191  if(firstToken != beginListToken() )
192  {
193  ioErrorInFile(is.name(), is.lineNumber())
194  << "expected token "<< token::BEGIN_LIST
195  << " but found "<< firstToken ;
196  return false;
197 
198  }
199 
200  token lastToken(is);
201 
203 
204  while
205  ( !(
206  lastToken.isPunctuation()
207  && lastToken == token::END_LIST
208 
209  )
210  )
211  {
212 
213  is.putBack(lastToken);
214 
215  T val;
216  is >> val;
217 
218  listType::push_back(val);
219 
220  is >> lastToken;
222 
223  }
224 
225  }
226  else
227  {
228  ioErrorInFile(is.name(), is.lineNumber())
229  << "expected token "<< beginListToken()
230  << " but found "<< firstToken ;
231  return false;
232 
233  }
234 
235  return is.fatalCheck(FUNCTION_NAME);
236 
237 }
238 
239 template<typename T>
240 inline pFlow::iOstream& pFlow::operator << (iOstream& os, const List<T>& lst )
241 {
242  if(!lst.writeList(os))
243  {
244  fatalExit;
245  }
246  return os;
247 }
248 
249 template<typename T>
250 inline pFlow::iIstream& pFlow::operator >>(iIstream& is, List<T>& lst)
251 {
252  if( !lst.readList(is) )
253  {
254  fatalExit;
255  }
256  return is;
257 }
count
auto count(const Vector< T, Allocator > &vec, const T &val)
pFlow::List
Definition: List.hpp:39
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::token
Definition: token.hpp:42
pFlow::List::set
void set(size_t i, const T &val)
Definition: ListI.hpp:128
pFlow::find
int64 find(Vector< T, Allocator > &vec, const T &val)
Definition: VectorAlgorithm.hpp:69
pFlow::token::isPunctuation
bool isPunctuation() const
Definition: tokenI.hpp:426
pFlow::List::search
bool search(const T &val) const
Definition: ListI.hpp:118
pFlow::List::find
constIterator find(const T &val) const
Definition: ListI.hpp:92
FUNCTION_NAME
#define FUNCTION_NAME
Definition: pFlowMacros.hpp:29
pFlow::newLineToken
token newLineToken()
Definition: token.hpp:524
pFlow::List::countElement
int32 countElement(const T &elm) const
Definition: ListI.hpp:58
pFlow::IOstream::check
virtual bool check(const char *operation) const
Definition: IOstream.cpp:42
pFlow::List::findi
int32 findi(const T &val) const
Definition: ListI.hpp:109
pFlow::iIstream
Definition: iIstream.hpp:33
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::iIstream::putBack
void putBack(const token &tok)
Definition: iIstream.cpp:5
pFlow::operator>>
INLINE_FUNCTION iIstream & operator>>(iIstream &str, AB3History &ab3)
Definition: AdamsBashforth3.hpp:41
pFlow::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Definition: IOstream.cpp:48
pFlow::List::pos
auto pos(size_t i)
Definition: ListI.hpp:23
pFlow::List::iterator
typename listType::iterator iterator
Definition: List.hpp:50
pFlow::beginListToken
token beginListToken()
Definition: token.hpp:499
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
pFlow::spaceToken
token spaceToken()
Definition: token.hpp:519
pFlow::IOstream::name
virtual const word & name() const
Definition: IOstream.cpp:31
pFlow::List::writeList
bool writeList(iOstream &os) const
Definition: ListI.hpp:144
pFlow::endListToken
token endListToken()
Definition: token.hpp:494
pFlow::List::size
size_t size() const
Definition: ListI.hpp:66
pFlow::List::readList
bool readList(iIstream &is)
Definition: ListI.hpp:178
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Definition: error.hpp:49
pFlow::IOstream::lineNumber
int32 lineNumber() const
Definition: IOstream.hpp:187
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::List::constIterator
typename listType::const_iterator constIterator
Definition: List.hpp:52