LFUCachePolicy.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file LFUCachePolicy.cpp
22 
23  \brief This class is used to implement the LFU cache policy.
24 */
25 
26 #include "LFUCachePolicy.h"
27 
28 #include <limits>
29 
31 
33 {
34  m_LFU.clear();
35 }
36 
38 {
39  m_LFU.insert(std::map<int, double>::value_type(value, 1.));
40 }
41 
43 {
44  accessed(value);
45 }
46 
48 {
49  std::map<int, double>::iterator itLFU = m_LFU.begin();
50  std::map<int, double>::iterator itMin = m_LFU.end();
51 
52  double minValue = std::numeric_limits<double>::max();
53 
54  while(itLFU != m_LFU.end())
55  {
56  if(itLFU->second < minValue)
57  {
58  minValue = itLFU->second;
59  itMin = itLFU;
60  }
61  ++itLFU;
62  }
63 
64  if(itMin != m_LFU.end())
65  {
66  value = itMin->first;
67  m_LFU.erase(itMin);
68  }
69 }
70 
72 {
73  std::map<int, double>::iterator itLFU = m_LFU.begin();
74 
75  while(itLFU != m_LFU.end())
76  {
77  if(itLFU->first == value)
78  {
79  itLFU->second += 1.;
80  break;
81  }
82  ++itLFU;
83  }
84 }
85 
86 
virtual void update(int value)
Function used to inform that an index must be updated.
virtual void toRemove(int &value)
Function used to check what index has to be removed from the cache.
std::map< int, double > m_LFU
virtual void accessed(int value)
Function used to inform that an index was accessed.
virtual ~LFUCachePolicy()
Virtual destructor.
virtual void added(int value)
Function used to add a new index to be controlled.
This class is used to implement the LFU cache policy.
LFUCachePolicy()
Default constructor.