DGtal 0.3.0

ImageContainerBySTLMap.h

Go to the documentation of this file.
00001 
00017 #pragma once
00018 
00032 #if defined(ImageContainerBySTLMap_RECURSES)
00033 #error Recursive header files inclusion detected in ImageContainerBySTLMap.h
00034 #else // defined(ImageContainerBySTLMap_RECURSES)
00035 
00036 #define ImageContainerBySTLMap_RECURSES
00037 
00038 #if !defined ImageContainerBySTLMap_h
00039 
00040 #define ImageContainerBySTLMap_h
00041 
00043 // Inclusions
00044 #include <iostream>
00045 #include <map>
00046 
00047 #include "DGtal/base/Common.h"
00048 #include "DGtal/kernel/images/CValue.h"
00049 #include "DGtal/kernel/domains/CBoundedDomain.h"
00051 
00052 namespace DGtal
00053 {
00054 
00056   // class ImageContainerBySTLMap
00064   template <typename Domain, typename Value>
00065   class ImageContainerBySTLMap: public map<typename Domain::Point,Value>
00066   {
00067 
00068   public:
00069 
00070     BOOST_CONCEPT_ASSERT(( CValue<Value> ));
00071     BOOST_CONCEPT_ASSERT(( CDomain<Domain> ));          
00072                 
00073     typedef typename Domain::Point Point;
00074     typedef typename Domain::Dimension Dimension;
00075     typedef typename map<Point,Value>::size_type TSize;
00076     typedef typename map<Point,Value>::iterator Iterator;
00077     typedef typename map<Point,Value>::const_iterator ConstIterator;
00078     typedef typename map<Point,Value>::reverse_iterator ReverseIterator;
00079     typedef typename map<Point,Value>::const_reverse_iterator
00080     ConstReverseIterator;
00081 
00082 
00083 
00087     Domain domain() const
00088     {
00089       return Domain(myLowerBound, myUpperBound);
00090     }
00091     
00092 
00094     class SpanIterator: public Iterator
00095     {
00096       friend class ImageContainerBySTLMap<Domain,Value>;
00097 
00098     public:
00099       SpanIterator( const Point & p ,
00100                     const Dimension aDim ,
00101                     ImageContainerBySTLMap<Domain,Value> *aMap ) :   
00102         myStartingPoint( p ),   myDimension ( aDim ),   myMap ( aMap )
00103       {
00104         myPos = myMap->find( p );
00105       }
00106 
00107 
00108       const Value & operator*() const
00109       {
00110         return (*myPos).second;
00111       }
00112       
00117       void next()
00118       {
00119         while ((myPos != myMap->end()) && 
00120                ( (*myPos).first.at(myDimension) != myStartingPoint.at(myDimension)))
00121           {
00122             myPos++;
00123           }
00124       }
00125 
00130       void prev()
00131       {
00132         while ((myPos != myMap->end()) && 
00133                ( (*myPos).first.at(myDimension) != myStartingPoint.at(myDimension)))
00134           {
00135             myPos--;
00136           }
00137       }      
00138 
00143       SpanIterator &operator++()
00144       {
00145         this->next();
00146         return *this;
00147       }
00148 
00153       SpanIterator &operator++ ( int )
00154       {
00155         SpanIterator tmp = *this;
00156         ++*this;
00157         return tmp;
00158       }
00159 
00160 
00165       SpanIterator &operator--()
00166       {
00167         this->prev();
00168         return *this;
00169       }
00170 
00175       SpanIterator &operator-- ( int )
00176       {
00177         SpanIterator tmp = *this;
00178         --*this;
00179         return tmp;
00180       }
00181 
00182     private:
00183       
00185       Point myStartingPoint;
00186       
00188       Iterator myPos;
00189 
00191       ImageContainerBySTLMap<Domain,Value> *myMap;
00192 
00194       Dimension myDimension;
00195 
00196     };
00197 
00198     ImageContainerBySTLMap(const Point &,
00199                            const Point & ) {};
00200 
00201     ~ImageContainerBySTLMap() {};
00202 
00203     Value operator()(const Point &aPoint) throw( std::bad_alloc )
00204     {
00205       Iterator it = this->find( aPoint );
00206       if ( it == this->end() )
00207         throw std::bad_alloc();
00208       else
00209         return (*it).second; // this->operator[]( aPoint );
00210     }
00211 
00212 
00213     Value operator()(const Iterator &it) throw( std::bad_alloc )
00214     {
00215       if ( it == this->end() )
00216         throw std::bad_alloc();
00217       else
00218         return (*it).second;
00219     }
00220     
00221 
00222     void setValue(const Point &aPoint, const Value &aValue)
00223     {
00224       Iterator it  = find( aPoint ) ;
00225       if (it != this->end() )
00226         (*it).second = aValue;
00227     }
00228 
00229 
00230     void setValue(SpanIterator &it, const Value &aValue)
00231     {
00232       ASSERT("NOT-YET-IMPLEMENTED");
00233     }
00234 
00235     void setValue(Iterator &it, const Value &aValue)
00236     {
00237       it->second = aValue;
00238     }
00239 
00240 
00241     SpanIterator span_begin(const Point &aPoint, const Dimension aDimension)
00242     {
00243       return SpanIterator ( aPoint, aDimension, this);
00244     }
00245 
00246     SpanIterator span_end(const Point &aPoint,const Dimension aDimension)
00247     {
00248       Point tmp = aPoint;
00249       tmp.at( aDimension ) = myLowerBound.at( aDimension ) +
00250         myUpperBound.at( aDimension ) - 
00251         myLowerBound.at( aDimension ) + 1;
00252       return SpanIterator( tmp, aDimension, this);
00253     }
00254 
00255     void allocate(const std::size_t aSize) {};
00256 
00257   private:
00258     
00259     Point myLowerBound;
00260     Point myUpperBound;
00261   };
00262 
00263 } // namespace DGtal
00264 
00265 
00266 
00267 //                                                                           //
00269 
00270 #endif // !defined ImageContainerBySTLMap_h
00271 
00272 #undef ImageContainerBySTLMap_RECURSES
00273 #endif // else defined(ImageContainerBySTLMap_RECURSES)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines