vigra - vigra/graph_maps.hxx Source File

[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

graph_maps.hxx |

1 /************************************************************************/
2 /* */
3 /* Copyright 2014 by Ullrich Koethe and Thorsten Beier */
4 /* */
5 /* This file is part of the VIGRA computer vision library. */
6 /* The VIGRA Website is */
7 /* http://hci.iwr.uni-heidelberg.de/vigra/ */
8 /* Please direct questions, bug reports, and contributions to */
9 /* ullrich.koethe@iwr.uni-heidelberg.de or */
10 /* vigra@informatik.uni-hamburg.de */
11 /* */
12 /* Permission is hereby granted, free of charge, to any person */
13 /* obtaining a copy of this software and associated documentation */
14 /* files (the "Software"), to deal in the Software without */
15 /* restriction, including without limitation the rights to use, */
16 /* copy, modify, merge, publish, distribute, sublicense, and/or */
17 /* sell copies of the Software, and to permit persons to whom the */
18 /* Software is furnished to do so, subject to the following */
19 /* conditions: */
20 /* */
21 /* The above copyright notice and this permission notice shall be */
22 /* included in all copies or substantial portions of the */
23 /* Software. */
24 /* */
25 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32 /* OTHER DEALINGS IN THE SOFTWARE. */
33 /* */
34 /************************************************************************/
35
36
37 #ifndef VIGRA_GRAPH_MAPS
38 #define VIGRA_GRAPH_MAPS
39
40 /*vigra*/
41 #include "multi_array.hxx"
42 #include "graph_generalization.hxx"
43 #include "graphs.hxx"
44
45
46 namespace vigra{
47
48 // base class for basic implementation
49 // of a class for node,edge and arc maps.
50 template<class T,class KEY,class REF,class CREF>
51 class DenseReferenceMap
52 : public MultiArray<1,T>
53 {
54 public:
55 typedef KEY Key;
56 typedef T Value;
57 typedef REF Reference;
58 typedef CREF ConstReference;
59
60 typedef Key key_type;
61 typedef Value value_type;
62 typedef Reference reference;
63 typedef ConstReference const_reference;
64 typedef boost::read_write_property_map_tag category;
65
66
67 typedef typename MultiArray<1,T>::difference_type Shape1Type;
68
69 DenseReferenceMap()
70 : MultiArray<1,T>(){
71 }
72 DenseReferenceMap(const size_t maxKey)
73 : MultiArray<1,T>(Shape1Type(maxKey+1)){
74 }
75 DenseReferenceMap(const size_t maxKey,ConstReference value)
76 : MultiArray<1,T>(Shape1Type(maxKey+1),value){
77 }
78
79
80
81
82 ConstReference operator[](const KEY & key)const{
83 //return this->operator[](key.id());
84 return MultiArray<1,T>::operator()(key.id());
85 }
86 Reference operator[](const KEY & key){
87 return MultiArray<1,T>::operator()(key.id());
88 }
89
90 size_t size()const{
91 return this->shape(0);
92 }
93 protected:
94 void assign(const size_t maxKey){
95 this->reshape(Shape1Type(maxKey+1));
96 }
97 private:
98 // NONE
99 };
100
101
102 // basic implementation
103 // of a class for node,edge and arc maps.
104 template<class GRAPH,class ITEM,class T,class REF,class CREF>
105 class DenseGraphItemReferenceMap
106 : public DenseReferenceMap<T,ITEM,REF,CREF>
107 {
108 typedef GRAPH Graph;
109 typedef ITEM Item;
110 typedef DenseReferenceMap<T,ITEM,REF,CREF> DenseReferenceMapType;
111 typedef GraphItemHelper<Graph,ITEM> ItemHelper;
112 typedef typename ItemHelper::ItemIt ItemIt;
113
114 public:
115 DenseGraphItemReferenceMap()
116 : DenseReferenceMapType(){
117
118 }
119 DenseGraphItemReferenceMap(const Graph & g)
120 : DenseReferenceMapType(ItemHelper::itemNum(g)==0 ? 0: ItemHelper::maxItemId(g) ){
121
122 }
123 DenseGraphItemReferenceMap(const Graph & g,typename DenseReferenceMapType::ConstReference value)
124 : DenseReferenceMapType(ItemHelper::itemNum(g)==0 ? 0: ItemHelper::maxItemId(g)){
125
126 }
127 void assign(const Graph & g){
128 DenseReferenceMapType::assign(ItemHelper::itemNum(g)==0 ? 0: ItemHelper::maxItemId(g));
129 }
130 };
131
132 // basic class for node-maps
133 template<class GRAPH,class T,class REF= T & ,class CREF = const T & >
134 class DenseNodeReferenceMap
135 : public DenseGraphItemReferenceMap<GRAPH,typename GRAPH::Node,T,REF,CREF>
136 {
137 typedef typename GRAPH::Node Node;
138 typedef DenseGraphItemReferenceMap<GRAPH,Node,T,REF,CREF> DenseGraphItemReferenceMapType;
139 public:
140 DenseNodeReferenceMap()
141 : DenseGraphItemReferenceMapType(){
142 }
143 DenseNodeReferenceMap(const GRAPH & g)
144 : DenseGraphItemReferenceMapType(g){
145 }
146 DenseNodeReferenceMap(const GRAPH & g,typename DenseGraphItemReferenceMapType::ConstReference value)
147 : DenseGraphItemReferenceMapType(g,value){
148 }
149 };
150
151 // basic class for edge-maps
152 template<class GRAPH,class T,class REF= T & ,class CREF = const T & >
153 class DenseEdgeReferenceMap
154 : public DenseGraphItemReferenceMap<GRAPH,typename GRAPH::Edge,T,REF,CREF>
155 {
156 typedef typename GRAPH::Edge Edge;
157 typedef DenseGraphItemReferenceMap<GRAPH,Edge,T,REF,CREF> DenseGraphItemReferenceMapType;
158 public:
159 DenseEdgeReferenceMap()
160 : DenseGraphItemReferenceMapType(){
161 }
162 DenseEdgeReferenceMap(const GRAPH & g)
163 : DenseGraphItemReferenceMapType(g){
164 }
165 DenseEdgeReferenceMap(const GRAPH & g,typename DenseGraphItemReferenceMapType::ConstReference value)
166 : DenseGraphItemReferenceMapType(g,value){
167 }
168 };
169
170 // basic class for arc-maps
171 template<class GRAPH,class T,class REF= T & ,class CREF = const T & >
172 class DenseArcReferenceMap
173 : public DenseGraphItemReferenceMap<GRAPH,typename GRAPH::Arc,T,REF,CREF>
174 {
175 typedef typename GRAPH::Arc Arc;
176 typedef DenseGraphItemReferenceMap<GRAPH,Arc,T,REF,CREF> DenseGraphItemReferenceMapType;
177 public:
178 DenseArcReferenceMap()
179 : DenseGraphItemReferenceMapType(){
180 }
181 DenseArcReferenceMap(const GRAPH & g)
182 : DenseGraphItemReferenceMapType(g){
183 }
184 DenseArcReferenceMap(const GRAPH & g,typename DenseGraphItemReferenceMapType::ConstReference value)
185 : DenseGraphItemReferenceMapType(g,value){
186 }
187 };
188
189 // implicit edge map:
190 // the values of a node map are converted
191 // to an edge map.
192 // FUNCTOR is used to convert the two
193 // node map values corresponding to an edge to
194 // an edge map value
195 template<class G,class NODE_MAP,class FUNCTOR,class RESULT>
196 class OnTheFlyEdgeMap{
197
198 public:
199 typedef G Graph;
200 typedef typename Graph::Node Node;
201 typedef NODE_MAP NodeMap;
202 typedef typename Graph::Edge Key;
203 typedef RESULT Value;
204 typedef RESULT ConstReference;
205
206 typedef Key key_type;
207 typedef Value value_type;
208 typedef ConstReference const_reference;
209
210 typedef boost::readable_property_map_tag category;
211
212 OnTheFlyEdgeMap(const Graph & graph,const NodeMap & nodeMap,FUNCTOR & f)
213 : graph_(graph),
214 nodeMap_(nodeMap),
215 f_(f){
216 }
217
218 ConstReference operator[](const Key & key){
219 const Node u(graph_.u(key));
220 const Node v(graph_.v(key));
221 return f_(nodeMap_[u],nodeMap_[v]);
222 }
223
224 ConstReference operator[](const Key & key)const{
225 const Node u(graph_.u(key));
226 const Node v(graph_.v(key));
227 return f_(nodeMap_[u],nodeMap_[v]);
228 }
229 private:
230
231 const Graph & graph_;
232 const NodeMap & nodeMap_;
233 FUNCTOR & f_;
234 };
235
236 // convert 2 edge maps with a functor into a single edge map
237 template<class G,class EDGE_MAP_A,class EDGE_MAP_B,class FUNCTOR,class RESULT>
238 class BinaryOpEdgeMap{
239 public:
240 typedef G Graph;
241 typedef typename Graph::Edge Key;
242 typedef RESULT Value;
243 typedef RESULT ConstReference;
244
245 typedef Key key_type;
246 typedef Value value_type;
247 typedef ConstReference const_reference;
248
249 typedef boost::readable_property_map_tag category;
250
251 BinaryOpEdgeMap(const Graph & graph,const EDGE_MAP_A & edgeMapA,const EDGE_MAP_B & edgeMapB,FUNCTOR & f)
252 : graph_(graph),
253 edgeMapA_(edgeMapA),
254 edgeMapB_(edgeMapB),
255 f_(f){
256 }
257 ConstReference operator[](const Key & key){
258 return f_(edgeMapA_[key],edgeMapB_[key]);
259 }
260 ConstReference operator[](const Key & key)const{
261 return f_(edgeMapA_[key],edgeMapB_[key]);
262 }
263 private:
264
265 const Graph & graph_;
266 const EDGE_MAP_A & edgeMapA_;
267 const EDGE_MAP_B & edgeMapB_;
268 FUNCTOR & f_;
269 };
270
271
272
273
274 } // end namespace vigra
275
276 #endif // VIGRA_GRAPH_MAPS

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany | html generated using doxygen and Python vigra 1.10.0 (Thu Jan 8 2015)