Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
EqualIteratorRange.h
Go to the documentation of this file.
1/* This file is part of MoDELib, the Mechanics Of Defects Evolution Library.
2 *
3 *
4 * MoDELib is distributed without any warranty under the
5 * GNU General Public License (GPL) v2 <http://www.gnu.org/licenses/>.
6 */
7
8#ifndef model_EqualIteratorRange_H_
9#define model_EqualIteratorRange_H_
10
11#include <deque>
12
13#include <utility> // std::pair
14#include <iterator> // std::distance, std::advance
15#include <assert.h> // std::distance, std::advance
16
17namespace model
18{
19
20 template <typename IterType>
21 struct EqualIteratorRange : public std::deque<std::pair<IterType,IterType>>
22 {
23
24 /**********************************************************************/
25 EqualIteratorRange(const IterType& first,
26 const IterType& last,
27 const size_t& nRanges,
28 const bool& checkRanges=false)
29 {
30
31 if(nRanges==0)
32 {
33 throw std::runtime_error("NUMBER OF RANGES MUST BE >0");
34 }
35// assert(nRanges>0 && "NUMBER OF RANGES MUST BE >0");
36
37 const auto nElments(std::distance(first,last));
38 //assert( nElments > 0 && "RANGE MUST HAVE AT LEAST ONE ELEMENT");
39
40 const size_t quotient = nElments / nRanges;
41 const size_t remainder= nElments % nRanges;
42
43 // Fill ranges
44 IterType currentBegin(first);
45 IterType currentEnd(first);
46
47 for (size_t i=0;i<nRanges;++i)
48 {
49 std::advance(currentEnd,(i<remainder)? quotient+1 : quotient);
50 this->emplace_back(currentBegin,currentEnd);
51 currentBegin=currentEnd;
52 }
53
54 //check
55 if(checkRanges)
56 {
57 auto temp(nElments*0);
58 for (size_t i=0;i<nRanges;++i)
59 {
60 temp+=std::distance(this->operator[](i).first,this->operator[](i).second);
61 }
62 assert(temp==nElments);
63 }
64
65 }
66
67 };
68
69} // namespace model
70#endif
EqualIteratorRange(const IterType &first, const IterType &last, const size_t &nRanges, const bool &checkRanges=false)
int i
vertex temp
vertex first