DGtal  1.2.0
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>
51 ::ImageContainerBySTLMap( Clone<const Domain> aDomain, const Value& aValue)
52  : myDomainPtr( aDomain ), myDefaultValue( aValue )
53 {
54 }
55 
56 //------------------------------------------------------------------------------
57 template <typename TDomain, typename TValue>
58 inline
59 DGtal::ImageContainerBySTLMap<TDomain,TValue>
60 ::ImageContainerBySTLMap(const ImageContainerBySTLMap& other)
61  : Parent(other),
62  myDomainPtr(other.myDomainPtr), myDefaultValue(other.myDefaultValue)
63 {
64 }
65 //------------------------------------------------------------------------------
66 template <typename TDomain, typename TValue>
67 inline
68 DGtal::ImageContainerBySTLMap<TDomain,TValue>&
69 DGtal::ImageContainerBySTLMap<TDomain,TValue>
70 ::operator=(const ImageContainerBySTLMap& other)
71 {
72  if (this != &other)
73  {
74  Parent::operator=(other);
75  myDomainPtr = other.myDomainPtr;
76  myDefaultValue = other.myDefaultValue;
77  }
78  return *this;
79 }
80 //------------------------------------------------------------------------------
81 template <typename TDomain, typename TValue>
82 inline
83 DGtal::ImageContainerBySTLMap<TDomain,TValue>::~ImageContainerBySTLMap( )
84 {
85 }
86 
87 //------------------------------------------------------------------------------
88 template <typename TDomain, typename TValue>
89 inline
90 typename DGtal::ImageContainerBySTLMap<TDomain,TValue>::Value
91 DGtal::ImageContainerBySTLMap<TDomain,TValue>::operator()(const Point &aPoint) const
92 {
93  ASSERT( this->domain().isInside( aPoint ) );
94  ConstIterator it = this->find( aPoint );
95  if ( it == this->end() )
96  return myDefaultValue;
97  else
98  return it->second;
99 }
100 
101 //------------------------------------------------------------------------------
102 template <typename TDomain, typename TValue>
103 inline
104 void
105 DGtal::ImageContainerBySTLMap<TDomain,TValue>::setValue(const Point &aPoint, const Value &aValue)
106 {
107  ASSERT( this->domain().isInside( aPoint ) );
108  std::pair<typename std::map<Point,Value>::iterator, bool>
109  res = this->insert( std::pair<Point,Value>(aPoint, aValue) );
110  if (res.second == false)
111  res.first->second = aValue;
112 }
113 
114 //------------------------------------------------------------------------------
115 template <typename TDomain, typename TValue>
116 inline
117 const typename DGtal::ImageContainerBySTLMap<TDomain,TValue>::Domain&
118 DGtal::ImageContainerBySTLMap<TDomain,TValue>::domain() const
119 {
120  return *myDomainPtr;
121 }
122 
123 //------------------------------------------------------------------------------
124 template <typename TDomain, typename TValue>
125 inline
126 typename DGtal::ImageContainerBySTLMap<TDomain,TValue>::ConstRange
127 DGtal::ImageContainerBySTLMap<TDomain,TValue>::constRange() const
128 {
129  return ConstRange( *this );
130 }
131 
132 //------------------------------------------------------------------------------
133 template <typename TDomain, typename TValue>
134 inline
135 typename DGtal::ImageContainerBySTLMap<TDomain,TValue>::Range
136 DGtal::ImageContainerBySTLMap<TDomain,TValue>::range()
137 {
138  return Range( *this );
139 }
140 //------------------------------------------------------------------------------
141 template <typename TDomain, typename TValue>
142 inline
143 typename DGtal::ImageContainerBySTLMap<TDomain,TValue>::OutputIterator
144 DGtal::ImageContainerBySTLMap<TDomain,TValue>::outputIterator()
145 {
146  return OutputIterator( *this );
147 }
148 
149 
150 //------------------------------------------------------------------------------
151 template <typename TDomain, typename TValue>
152 inline
153 bool
154 DGtal::ImageContainerBySTLMap<TDomain,TValue>::isValid() const
155 {
156  return true;
157 }
158 
159 //------------------------------------------------------------------------------
160 template <typename TDomain, typename TValue>
161 inline
162 void
163 DGtal::ImageContainerBySTLMap<TDomain,TValue>::selfDisplay ( std::ostream & out ) const
164 {
165  out << "[Image - STLMap] size=" << this->size() << " valuetype="
166  << sizeof(TValue) << "bytes Domain=" << *myDomainPtr;
167 }
168 
169 //------------------------------------------------------------------------------
170 template <typename TDomain, typename TValue>
171 inline
172 std::string
173 DGtal::ImageContainerBySTLMap<TDomain,TValue>::className() const
174 {
175  return "ImageContainerBySTLMap";
176 }