List.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 
22 #ifndef __List_hpp__
23 #define __List_hpp__
24 
25 #include <list>
26 
27 #include "types.hpp"
28 #include "typeInfo.hpp"
29 #include "uniquePtr.hpp"
30 #include "iOstream.hpp"
31 #include "iIstream.hpp"
32 
33 namespace pFlow
34 {
35 
36 
37 template<
38  typename T
39 > class List
40 :
41  public std::list<T, std::allocator<T> >
42 {
43 
44 public:
45 
46  using ListType = List<T>;
47 
48  using listType = std::list<T,std::allocator<T>>;
49 
50  using iterator = typename listType::iterator;
51 
52  using constIterator = typename listType::const_iterator;
53 
54  using reference = typename listType::reference;
55 
56  using constReference= typename listType::const_reference;
57 
58  using initList = typename std::initializer_list<T>;
59 
60  using valueType = T;
61 
62 
63 protected:
64 
65  // position of ith element
66  auto pos(size_t i);
67 
68  // position of ith element
69  const auto pos(size_t i)const;
70 
71  static inline size_t getListStride(const size_t& len)
72  {
73  size_t stride = 1;
74  if( len < 6 )
75  stride = len;
76  else if( len <16 )
77  stride = 3;
78  else if( len < 31)
79  stride = 2;
80  else
81  stride = 1;
82 
83  return stride;
84  }
85 
86 public:
87 
88  // - Type info
89  TypeInfoTemplateNV("List", T);
90 
92 
93  // - empty list
94  List()
95  {}
96 
97  // - list with length len
98  List(size_t len)
99  :
100  listType(len)
101  {}
102 
103  // - list with length len and value
104  List(size_t len, const T& value)
105  :
106  listType(len, value)
107  {}
108 
109  // - construct from initList
111  :
112  listType(lst)
113  {}
114 
115 
116  // - copy construct
117  List(const List& src):
118  listType(src)
119  {}
120 
121  // - move construct
122  List( List && mv)
123  :
124  listType(std::move(mv))
125  {}
126 
127  // - copy assignment
129  {
130  listType::operator=(rhs);
131  return *this;
132  }
133 
134  // - move assignment
136  {
137  listType::operator=(std::move(rhs));
138  return *this;
139  }
140 
141 
143  return makeUnique<ListType>(*this);
144  }
145 
147  return new ListType(*this);
148  }
149 
150 
151  // - destructor
153  {
154  listType::clear();
155  }
156 
158 
159  // - counts elements based on a given value
160  int32 countElement(const T& elm) const;
161 
162  // - size of container
163  size_t size()const;
164 
165  // - access to ith element
166  // fatal exit if out of range
167  T& operator[](size_t i);
168 
169  // - const access to ith element
170  // fatal exit if our of range
171  const T& operator[](size_t i)const;
172 
173  // - find the position of the first element with value val
174  // cend() if not found
175  constIterator find(const T& val) const;
176 
177  // - find the position of the first element with value val
178  // end() if not found
179  iterator find(const T& val);
180 
181  // - find the index of the first element with value val
182  // -1 if not found
183  int32 findi(const T& val) const;
184 
185 
186  // - search for element, if finds it,
187  // return true, otherwise false
188  bool search(const T& val) const;
189 
190  // - set ith element with copy
191  // fatal error if out of range
192  void set(size_t i, const T& val);
193 
194  // - set ith element with move operation
195  // fatal error if out of range
196  void set(size_t i, T&& val);
197 
199 
200  // - write to oStream
201  bool writeList(iOstream& os) const;
202 
203  // - read from iStream
204  bool readList(iIstream& is);
205 
206  bool read(iIstream& is)
207  {
208  return readList(is);
209  }
210 
211  bool write(iOstream& os)const
212  {
213  return writeList(os);
214  }
215 
216 };
217 
218 
219 template<typename T>
220 iOstream& operator << (iOstream& os, const List<T>& lst );
221 
222 
223 template<typename T>
224 iIstream& operator >>(iIstream& is, List<T>& lst);
225 
226 
227 
234 
238 
239 
242 
243 
244 
245 } // pFlow
246 
247 
248 #include "ListI.hpp"
249 
250 #endif //__List_hpp__
pFlow::List::clonePtr
ListType * clonePtr() const
Definition: List.hpp:146
pFlow::List
Definition: List.hpp:39
pFlow::List< word >::initList
typename std::initializer_list< word > initList
Definition: List.hpp:58
pFlow::List::List
List(size_t len, const T &value)
Definition: List.hpp:104
iIstream.hpp
pFlow::List::set
void set(size_t i, const T &val)
Definition: ListI.hpp:128
types.hpp
pFlow::List::List
List(const List &src)
Definition: List.hpp:117
pFlow::List::search
bool search(const T &val) const
Definition: ListI.hpp:118
pFlow::List::operator=
ListType & operator=(const ListType &rhs)
Definition: List.hpp:128
pFlow::List::find
constIterator find(const T &val) const
Definition: ListI.hpp:92
pFlow::List::operator=
ListType & operator=(ListType &&rhs)
Definition: List.hpp:135
pFlow::List::List
List(initList lst)
Definition: List.hpp:110
pFlow::List::read
bool read(iIstream &is)
Definition: List.hpp:206
pFlow::List::countElement
int32 countElement(const T &elm) const
Definition: ListI.hpp:58
pFlow::List< word >::valueType
word valueType
Definition: List.hpp:60
pFlow
Definition: demComponent.hpp:28
ListI.hpp
pFlow::List::clone
uniquePtr< ListType > clone() const
Definition: List.hpp:142
pFlow::List::findi
int32 findi(const T &val) const
Definition: ListI.hpp:109
uniquePtr.hpp
pFlow::List::TypeInfoTemplateNV
TypeInfoTemplateNV("List", T)
pFlow::iIstream
Definition: iIstream.hpp:33
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::List::ListType
List< T > ListType
Definition: List.hpp:46
pFlow::operator>>
INLINE_FUNCTION iIstream & operator>>(iIstream &str, AB3History &ab3)
Definition: AdamsBashforth3.hpp:41
pFlow::List::write
bool write(iOstream &os) const
Definition: List.hpp:211
pFlow::List::pos
auto pos(size_t i)
Definition: ListI.hpp:23
pFlow::List::List
List(List &&mv)
Definition: List.hpp:122
pFlow::List< word >::iterator
typename listType::iterator iterator
Definition: List.hpp:50
pFlow::List::getListStride
static size_t getListStride(const size_t &len)
Definition: List.hpp:71
pFlow::List< word >::reference
typename listType::reference reference
Definition: List.hpp:54
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
pFlow::List< word >::constReference
typename listType::const_reference constReference
Definition: List.hpp:56
pFlow::List::writeList
bool writeList(iOstream &os) const
Definition: ListI.hpp:144
pFlow::uniquePtr
Definition: uniquePtr.hpp:44
pFlow::List::size
size_t size() const
Definition: ListI.hpp:66
pFlow::List::readList
bool readList(iIstream &is)
Definition: ListI.hpp:178
pFlow::List::operator[]
T & operator[](size_t i)
Definition: ListI.hpp:73
typeInfo.hpp
pFlow::List::List
List(size_t len)
Definition: List.hpp:98
pFlow::List< word >::listType
std::list< word, std::allocator< word > > listType
Definition: List.hpp:48
iOstream.hpp
pFlow::List::~List
~List()
Definition: List.hpp:152
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::List< word >::constIterator
typename listType::const_iterator constIterator
Definition: List.hpp:52
pFlow::List::List
List()
Definition: List.hpp:94