DGtal  1.5.beta
ImageCachePolicies.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 ImageCachePolicies.ih
19  * @author Martial Tola (\c martial.tola@liris.cnrs.fr )
20  * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21  *
22  * @date 2013/02/06
23  *
24  * Implementation of inline methods defined in ImageCachePolicies.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 
33 //////////////////////////////////////////////////////////////////////////////
34 
35 ///////////////////////////////////////////////////////////////////////////////
36 // IMPLEMENTATION of inline methods.
37 ///////////////////////////////////////////////////////////////////////////////
38 
39 ///////////////////////////////////////////////////////////////////////////////
40 // ----------------------- Standard services ------------------------------
41 
42 // ----------------------- Specialization DGtal::CACHE_READ_POLICY_LAST ------------------------------
43 
44 template <typename TImageContainer, typename TImageFactory>
45 inline
46 TImageContainer *
47 DGtal::ImageCacheReadPolicyLAST<TImageContainer, TImageFactory>::getPage(const Point & aPoint)
48 {
49  if (myCacheImagesPtr==NULL)
50  return NULL;
51 
52  if (myCacheImagesPtr->domain().isInside(aPoint))
53  return myCacheImagesPtr;
54 
55  return NULL;
56 }
57 
58 template <typename TImageContainer, typename TImageFactory>
59 inline
60 TImageContainer *
61 DGtal::ImageCacheReadPolicyLAST<TImageContainer, TImageFactory>::getPage(const Domain & aDomain)
62 {
63  if (myCacheImagesPtr==NULL)
64  return NULL;
65 
66  if ( (myCacheImagesPtr->domain().lowerBound() == aDomain.lowerBound()) && (myCacheImagesPtr->domain().upperBound() == aDomain.upperBound()) )
67  return myCacheImagesPtr;
68 
69  return NULL;
70 }
71 
72 template <typename TImageContainer, typename TImageFactory>
73 inline
74 TImageContainer *
75 DGtal::ImageCacheReadPolicyLAST<TImageContainer, TImageFactory>::getPageToDetach()
76 {
77  return myCacheImagesPtr;
78 }
79 
80 template <typename TImageContainer, typename TImageFactory>
81 inline
82 void
83 DGtal::ImageCacheReadPolicyLAST<TImageContainer, TImageFactory>::updateCache(const Domain &aDomain)
84 {
85  myCacheImagesPtr = myImageFactory->requestImage(aDomain);
86 }
87 
88 template <typename TImageContainer, typename TImageFactory>
89 inline
90 void
91 DGtal::ImageCacheReadPolicyLAST<TImageContainer, TImageFactory>::clearCache()
92 {
93  myCacheImagesPtr = NULL;
94 }
95 
96 // ----------------------- Specialization DGtal::CACHE_READ_POLICY_FIFO ------------------------------
97 
98 template <typename TImageContainer, typename TImageFactory>
99 inline
100 TImageContainer *
101 DGtal::ImageCacheReadPolicyFIFO<TImageContainer, TImageFactory>::getPage(const Point & aPoint)
102 {
103  if (myFIFOCacheImages.empty())
104  return NULL;
105 
106  for (unsigned int i=0; i<myFIFOCacheImages.size(); i++)
107  if (myFIFOCacheImages[i]->domain().isInside(aPoint))
108  return myFIFOCacheImages[i];
109 
110  return NULL;
111 }
112 
113 template <typename TImageContainer, typename TImageFactory>
114 inline
115 TImageContainer *
116 DGtal::ImageCacheReadPolicyFIFO<TImageContainer, TImageFactory>::getPage(const Domain & aDomain)
117 {
118  if (myFIFOCacheImages.empty())
119  return NULL;
120 
121  for (unsigned int i=0; i<myFIFOCacheImages.size(); i++)
122  if ( (myFIFOCacheImages[i]->domain().lowerBound() == aDomain.lowerBound()) && (myFIFOCacheImages[i]->domain().upperBound() == aDomain.upperBound()) )
123  return myFIFOCacheImages[i];
124 
125  return NULL;
126 }
127 
128 template <typename TImageContainer, typename TImageFactory>
129 inline
130 TImageContainer *
131 DGtal::ImageCacheReadPolicyFIFO<TImageContainer, TImageFactory>::getPageToDetach()
132 {
133  TImageContainer *pageToDetach = NULL;
134 
135  if (myFIFOCacheImages.size() >= myFIFOSizeMax)
136  {
137  pageToDetach = myFIFOCacheImages.front();
138  myFIFOCacheImages.pop_front();
139  }
140 
141  return pageToDetach;
142 }
143 
144 template <typename TImageContainer, typename TImageFactory>
145 inline
146 void
147 DGtal::ImageCacheReadPolicyFIFO<TImageContainer, TImageFactory>::updateCache(const Domain &aDomain)
148 {
149  myFIFOCacheImages.push_back(myImageFactory->requestImage(aDomain));
150 }
151 
152 template <typename TImageContainer, typename TImageFactory>
153 inline
154 void
155 DGtal::ImageCacheReadPolicyFIFO<TImageContainer, TImageFactory>::clearCache()
156 {
157  myFIFOCacheImages.clear();
158 }
159 
160 // ----------------------- Specialization DGtal::CACHE_WRITE_POLICY_WT ------------------------------
161 
162 template <typename TImageContainer, typename TImageFactory>
163 inline
164 void
165 DGtal::ImageCacheWritePolicyWT<TImageContainer, TImageFactory>::writeInPage(TImageContainer * anImageContainer, const Point & aPoint, const Value &aValue)
166 {
167  anImageContainer->setValue(aPoint, aValue);
168 
169  myImageFactory->flushImage(anImageContainer); // DGtal::CACHE_WRITE_POLICY_WT
170 }
171 
172 template <typename TImageContainer, typename TImageFactory>
173 inline
174 void
175 DGtal::ImageCacheWritePolicyWT<TImageContainer, TImageFactory>::flushPage(TImageContainer * anImageContainer)
176 {
177  boost::ignore_unused_variable_warning(anImageContainer);
178 }
179 
180 // ----------------------- Specialization DGtal::CACHE_WRITE_POLICY_WB ------------------------------
181 
182 template <typename TImageContainer, typename TImageFactory>
183 inline
184 void
185 DGtal::ImageCacheWritePolicyWB<TImageContainer, TImageFactory>::writeInPage(TImageContainer * anImageContainer, const Point & aPoint, const Value &aValue)
186 {
187  anImageContainer->setValue(aPoint, aValue);
188 }
189 
190 template <typename TImageContainer, typename TImageFactory>
191 inline
192 void
193 DGtal::ImageCacheWritePolicyWB<TImageContainer, TImageFactory>::flushPage(TImageContainer * anImageContainer)
194 {
195  myImageFactory->flushImage(anImageContainer); // DGtal::CACHE_WRITE_POLICY_WB
196 }
197 
198 // //
199 ///////////////////////////////////////////////////////////////////////////////
200 
201