Line data Source code
1 : /*
2 : * Copyright (c) 2015: G-CSC, Goethe University Frankfurt
3 : * Author: Sebastian Reiter
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : #ifndef __H__UG_associated_elements_iterator
34 : #define __H__UG_associated_elements_iterator
35 :
36 : #include <iterator>
37 : #include <limits>
38 : #include "lib_grid/callbacks/basic_callbacks.h"
39 :
40 : namespace ug{
41 :
42 : /// Iterator that allows to traverse associated elements of a given element
43 : /** The type of the element whose associated elements shall be traversed
44 : * has to be specified through the template argument TElem. The type of the
45 : * associated elements has to be specified through the TAssocElem template
46 : * argument. Through VRefOrder one can choose whether the traversed associated
47 : * elements shall be sorted in the order in which they appear in the reference
48 : * element of given element (false by default).*/
49 : template <class TElem, class TAssocElem, bool VSorted = false>
50 : class AssocElemIter : public std::iterator<std::input_iterator_tag, TAssocElem*>
51 : {
52 : public:
53 0 : AssocElemIter(typename Grid::traits<TAssocElem>::callback cbConsiderElem =
54 : ConsiderAll()) :
55 0 : m_i(0),
56 : m_cbConsiderElem(cbConsiderElem)
57 0 : {}
58 :
59 : AssocElemIter(Grid& grid, TElem* elem,
60 : typename Grid::traits<TAssocElem>::callback cbConsiderElem =
61 : ConsiderAll()) :
62 : m_i(0),
63 : m_cbConsiderElem(cbConsiderElem)
64 : {
65 : init(grid, elem);
66 : }
67 :
68 : void set_callback(typename Grid::traits<TAssocElem>::callback cbConsiderElem)
69 : {
70 : m_cbConsiderElem(cbConsiderElem);
71 : }
72 :
73 : void reinit(Grid& grid, TElem* elem)
74 : {
75 : m_i = 0;
76 : init(grid, elem);
77 : }
78 :
79 : void reinit(Grid& grid, TElem* elem,
80 : typename Grid::traits<TAssocElem>::callback cb)
81 : {
82 : m_i = 0;
83 : m_cbConsiderElem(cb);
84 : init(grid, elem);
85 : }
86 :
87 : AssocElemIter end() const
88 : {
89 : AssocElemIter e;
90 : e.m_i = std::numeric_limits<size_t>::max();
91 : return e;
92 : }
93 :
94 : bool valid() const {return m_i < m_assElems.size();}
95 : bool invalid() const {return m_i >= m_assElems.size();}
96 :
97 :
98 : AssocElemIter& operator ++() {increment(); return *this;}
99 : AssocElemIter operator ++(int unused) {AssocElemIter i = *this; increment(); return i;}
100 :
101 : /// returns true if both iterators are invalid or if both point to the same elemnt.
102 : bool operator ==(const AssocElemIter& iter) const {return equal(iter);}
103 : /// returns true if exactly one iterator is invalid or if the iterators point to different elements.
104 : bool operator !=(const AssocElemIter& iter) const {return !equal(iter);}
105 :
106 : TAssocElem* operator *() {return dereference();}
107 :
108 : private:
109 : inline void init(Grid& grid, TElem* elem){
110 : if(VSorted)
111 : grid.associated_elements_sorted(m_assElems, elem);
112 : else
113 : grid.associated_elements(m_assElems, elem);
114 :
115 : if(valid() && (!m_cbConsiderElem(dereference())))
116 : increment();
117 : }
118 :
119 : /// returns true if both iterators are invalid or if both point to the same elemnt.
120 : inline bool equal(AssocElemIter const& other) const{
121 : if(valid()){
122 : if(other.valid()){
123 : if(dereference() == *other)
124 : return true;
125 : }
126 : }
127 : else{
128 : if(!other.valid())
129 : return true;
130 : }
131 : return false;
132 : }
133 :
134 : /// returns next iterator
135 : void increment(){
136 : while(1){
137 : ++m_i;
138 : if(valid()){
139 : if(m_cbConsiderElem(dereference()))
140 : break;
141 : }
142 : else
143 : break;
144 : }
145 : }
146 :
147 : /// dereference
148 : inline TAssocElem* dereference() const{
149 : return m_assElems[m_i];
150 : }
151 :
152 : private:
153 : size_t m_i;
154 : typename Grid::traits<TAssocElem>::secure_container m_assElems;
155 : typename Grid::traits<TAssocElem>::callback m_cbConsiderElem;
156 : };
157 :
158 : }// end of namespace
159 :
160 : #endif //__H__UG_associated_elements_iterator
|