libcommonc++  0.7
Cache.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_Cache_hxx
24 #define __ccxx_Cache_hxx
25 
26 #include <map>
27 #include <utility>
28 
29 #include <commonc++/Common.h++>
30 
31 namespace ccxx {
32 
46 template<typename K, typename T> class Cache
47 {
48  public:
49 
56  Cache(uint_t maxSize);
57 
61  virtual ~Cache();
62 
69  void put(K key, T* item);
70 
78  T* get(K key) const;
79 
83  inline T* operator[](K key) const
84  { return(this->get(key)); }
85 
94  T* take(K key);
95 
103  bool remove(K key);
104 
112  bool contains(K key) const;
113 
117  void clear();
118 
123  inline uint_t getMaxSize() const
124  { return(_maxSize); }
125 
130  inline uint_t getSize() const
131  { return(_size); }
132 
136  inline bool isEmpty() const
137  { return(_size == 0); }
138 
139  protected:
140 
151  virtual void onRemove(K key, T* item);
152 
153  private:
154 
155  uint_t _maxSize;
156  uint_t _size;
157 
159  struct Link
160  {
161  Link(K key)
162  : key(key),
163  prev(NULL),
164  next(NULL)
165  { }
166 
167  K key;
168  Link *prev;
169  Link *next;
170  };
171 
172  struct KeyCompare
173  {
174  bool operator()(const Link* key1, const Link* key2) const
175  { return(key1->key < key2->key); }
176  };
179  void _unlink(Link* link) const;
180  void _link(Link* link) const;
181 
182  typedef std::map<Link*, T*, KeyCompare> MapType;
183  mutable MapType _map;
184  mutable Link* _head;
185  mutable Link* _tail;
186 };
187 
188 #include <commonc++/CacheImpl.h++>
189 
190 } // namespace ccxx
191 
192 #endif // __ccxx_Cache_hxx
virtual ~Cache()
Destructor.
void put(K key, T *item)
Put an item in the cache.
T * operator[](K key) const
Index operator.
Definition: Cache.h++:83
uint_t getSize() const
Return the current size of the cache, i.e., the number of items in the cache.
Definition: Cache.h++:130
T * take(K key)
Remove an item from the cache, and return the item.
bool isEmpty() const
Test if the cache is empty.
Definition: Cache.h++:136
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
uint_t getMaxSize() const
Return the maximum size of the cache, i.e., the maximum number of items that the cache can hold...
Definition: Cache.h++:123
virtual void onRemove(K key, T *item)
A method that is called whenever an item is being removed from the cache, but just before it has been...
Cache(uint_t maxSize)
Construct a new cache with the given maximum size.
bool contains(K key) const
Test if the cache contains an item with the specified key.
A general-purpose LRU-cache with a maximum capacity.
Definition: Cache.h++:46
void clear()
Remove all items from the cache.
Definition: AllocationMap.c++:25