DGtal  1.5.beta
ImageContainerBySTLMap.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file ImageContainerBySTLMap.ih
19  * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20  * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21  *
22  * @date 2010/06/15
23  *
24  * @author Tristan Roussillon (\c tristan.roussillon@liris.cnrs.fr )
25  * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
26  *
27  * @date 2012/02/13
28  *
29  * Implementation of inline methods defined in ImageContainerBySTLMap.h
30  *
31  * This file is part of the DGtal library.
32  */
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
37 
38 //////////////////////////////////////////////////////////////////////////////
39 #include <cstdlib>
40 //////////////////////////////////////////////////////////////////////////////
41 
42 template <typename TDomain, typename TValue>
43 const typename TDomain::Dimension DGtal::ImageContainerBySTLMap<TDomain,
44  TValue>::dimension = TDomain::Space::dimension;
45 
46 //------------------------------------------------------------------------------
47 
48 template <typename TDomain, typename TValue>
49 inline
50 DGtal::ImageContainerBySTLMap<TDomain,TValue>::ImageContainerBySTLMap( DGtal::Clone<const TDomain> aDomain, const TValue& aValue): myDomainPtr( aDomain ), myDefaultValue( aValue )
51 {
52 }
53 
54 //------------------------------------------------------------------------------
55 template <typename TDomain, typename TValue>
56 inline
57 DGtal::ImageContainerBySTLMap<TDomain,TValue>::ImageContainerBySTLMap(const ImageContainerBySTLMap& other): Parent(other),
58  myDomainPtr(other.myDomainPtr), myDefaultValue(other.myDefaultValue)
59 {
60 }
61 //------------------------------------------------------------------------------
62 template <typename TDomain, typename TValue>
63 inline
64 DGtal::ImageContainerBySTLMap<TDomain,TValue>&
65 DGtal::ImageContainerBySTLMap<TDomain,TValue>
66 ::operator=(const ImageContainerBySTLMap& other)
67 {
68  if (this != &other)
69  {
70  Parent::operator=(other);
71  myDomainPtr = other.myDomainPtr;
72  myDefaultValue = other.myDefaultValue;
73  }
74  return *this;
75 }
76 //------------------------------------------------------------------------------
77 template <typename TDomain, typename TValue>
78 inline
79 DGtal::ImageContainerBySTLMap<TDomain,TValue>::~ImageContainerBySTLMap( )
80 {
81 }
82 
83 //------------------------------------------------------------------------------
84 template <typename TDomain, typename TValue>
85 inline
86 typename DGtal::ImageContainerBySTLMap<TDomain,TValue>::Value
87 DGtal::ImageContainerBySTLMap<TDomain,TValue>::operator()(const Point &aPoint) const
88 {
89  ASSERT( this->domain().isInside( aPoint ) );
90  ConstIterator it = this->find( aPoint );
91  if ( it == this->end() )
92  return myDefaultValue;
93  else
94  return it->second;
95 }
96 
97 //------------------------------------------------------------------------------
98 template <typename TDomain, typename TValue>
99 inline
100 void
101 DGtal::ImageContainerBySTLMap<TDomain,TValue>::setValue(const Point &aPoint, const Value &aValue)
102 {
103  ASSERT( this->domain().isInside( aPoint ) );
104  std::pair<typename std::map<Point,Value>::iterator, bool>
105  res = this->insert( std::pair<Point,Value>(aPoint, aValue) );
106  if (res.second == false)
107  res.first->second = aValue;
108 }
109 
110 //------------------------------------------------------------------------------
111 template <typename TDomain, typename TValue>
112 inline
113 const typename DGtal::ImageContainerBySTLMap<TDomain,TValue>::Domain&
114 DGtal::ImageContainerBySTLMap<TDomain,TValue>::domain() const
115 {
116  return *myDomainPtr;
117 }
118 
119 //------------------------------------------------------------------------------
120 template <typename TDomain, typename TValue>
121 inline
122 typename DGtal::ImageContainerBySTLMap<TDomain,TValue>::ConstRange
123 DGtal::ImageContainerBySTLMap<TDomain,TValue>::constRange() const
124 {
125  return ConstRange( *this );
126 }
127 
128 //------------------------------------------------------------------------------
129 template <typename TDomain, typename TValue>
130 inline
131 typename DGtal::ImageContainerBySTLMap<TDomain,TValue>::Range
132 DGtal::ImageContainerBySTLMap<TDomain,TValue>::range()
133 {
134  return Range( *this );
135 }
136 //------------------------------------------------------------------------------
137 template <typename TDomain, typename TValue>
138 inline
139 typename DGtal::ImageContainerBySTLMap<TDomain,TValue>::OutputIterator
140 DGtal::ImageContainerBySTLMap<TDomain,TValue>::outputIterator()
141 {
142  return OutputIterator( *this );
143 }
144 
145 
146 //------------------------------------------------------------------------------
147 template <typename TDomain, typename TValue>
148 inline
149 bool
150 DGtal::ImageContainerBySTLMap<TDomain,TValue>::isValid() const
151 {
152  return true;
153 }
154 
155 //------------------------------------------------------------------------------
156 template <typename TDomain, typename TValue>
157 inline
158 void
159 DGtal::ImageContainerBySTLMap<TDomain,TValue>::selfDisplay ( std::ostream & out ) const
160 {
161  out << "[Image - STLMap] size=" << this->size() << " valuetype="
162  << sizeof(TValue) << "bytes Domain=" << *myDomainPtr;
163 }
164 
165 //------------------------------------------------------------------------------
166 template <typename TDomain, typename TValue>
167 inline
168 std::string
169 DGtal::ImageContainerBySTLMap<TDomain,TValue>::className() const
170 {
171  return "ImageContainerBySTLMap";
172 }