stridedRange.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 #ifndef __stridedRange_hpp__
22 #define __stridedRange_hpp__
23 
24 #include "types.hpp"
25 #include "typeInfo.hpp"
26 #include "dictionary.hpp"
27 
28 
29 namespace pFlow
30 {
31 
32 template<typename T>
33 class
35 {
36 protected:
37 
38  T begin_;
39 
40  T end_;
41 
43 
44  static inline const T maxVal = largestPositive<T>();
45  static inline const T minVal = largestNegative<T>();
46 
47 public:
48 
49  TypeInfoTemplateNV("stridedRange",T);
50 
51  stridedRange(T begin, T end, T stride)
52  :
53  begin_(begin),
54  end_(end),
55  stride_(stride)
56  {}
57 
58  stridedRange(T begin, T stride)
59  :
60  begin_(begin),
61  end_(maxVal),
62  stride_(stride)
63  {}
64 
65 
66  // it should be in the form of begin:stride:end
67  stridedRange(const word& rangeString)
68  {
69  if(!parseRange(rangeString,begin_,end_, stride_))
70  {
72  "bad input for the range. It should have the form of begin:stride:end \n";
73  fatalExit;
74  }
75  }
76 
77  stridedRange(const dictionary& dict)
78  :
79  begin_(dict.getVal<label>("begin")),
80  end_(dict.getVal<label>("end")),
81  stride_(dict.getVal<label>("stride"))
82  {}
83 
84  inline
85  T begin()const
86  {
87  return begin_;
88  }
89 
90  T end()const
91  {
92  return end_;
93  }
94 
95  T stride()const
96  {
97  return stride_;
98  }
99  inline
100  bool isMember(T val, T epsilon = 0)const
101  {
102  if(equal(val,begin_))return true;
103  if(equal(val,end_))return true;
104  if(abs(mod(val-begin_,stride_))<= epsilon) return true;
105  return false;
106  }
107 
108  static
109  bool parseRange(const word& rangeString, T& begin, T& end, T& stride)
110  {
111  if(std::count(
112  rangeString.begin(),
113  rangeString.end(),
114  ':') != 2)
115  return false;
116 
117  auto col1 = rangeString.find_first_of(":");
118 
119  if(col1 == 0 || col1==std::string::npos)return false;
120 
121  auto beginCh = rangeString.substr(0,col1);
122 
123  auto col2 = rangeString.find_first_of(":",col1+1);
124 
125  if(col1 == col2 || col2==std::string::npos)return false;
126 
127  auto strideCh = rangeString.substr(col1+1,col2-col1-1);
128 
129  auto endCh = rangeString.substr(col2+1);
130 
131  if(!readValue(beginCh,begin))return false;
132  if(!readValue(strideCh, stride))return false;
133  if(!readValue(endCh, end))return false;
134 
135  return true;
136 
137  }
138 };
139 
140 }
141 
142 #endif //__stridedRange_hpp__
count
auto count(const Vector< T, Allocator > &vec, const T &val)
pFlow::stridedRange::begin_
T begin_
Definition: stridedRange.hpp:38
pFlow::readValue
bool readValue(const word &w, real &val)
Definition: bTypesFunctions.hpp:140
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::stridedRange::end_
T end_
Definition: stridedRange.hpp:40
pFlow::stridedRange::stridedRange
stridedRange(T begin, T end, T stride)
Definition: stridedRange.hpp:51
types.hpp
pFlow::stridedRange::stride_
T stride_
Definition: stridedRange.hpp:42
pFlow::stridedRange::begin
T begin() const
Definition: stridedRange.hpp:85
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::stridedRange
Definition: stridedRange.hpp:33
pFlow::stridedRange::stridedRange
stridedRange(T begin, T stride)
Definition: stridedRange.hpp:58
pFlow
Definition: demComponent.hpp:28
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
dictionary.hpp
pFlow::abs
INLINE_FUNCTION_HD real abs(real x)
Definition: math.hpp:43
pFlow::stridedRange::end
T end() const
Definition: stridedRange.hpp:90
pFlow::stridedRange::stride
T stride() const
Definition: stridedRange.hpp:95
pFlow::mod
INLINE_FUNCTION_HD real mod(real x, real y)
Definition: math.hpp:72
TypeInfoTemplateNV
#define TypeInfoTemplateNV(tName, Type)
Definition: typeInfo.hpp:89
pFlow::stridedRange::stridedRange
stridedRange(const dictionary &dict)
Definition: stridedRange.hpp:77
pFlow::stridedRange::isMember
bool isMember(T val, T epsilon=0) const
Definition: stridedRange.hpp:100
typeInfo.hpp
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::stridedRange::stridedRange
stridedRange(const word &rangeString)
Definition: stridedRange.hpp:67
pFlow::equal
INLINE_FUNCTION_HD bool equal(const real &s1, const real &s2)
Definition: bTypesFunctions.hpp:188
pFlow::dictionary
Definition: dictionary.hpp:38
pFlow::stridedRange::parseRange
static bool parseRange(const word &rangeString, T &begin, T &end, T &stride)
Definition: stridedRange.hpp:109