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