1 // SwiftShader Software Renderer
\r
3 // Copyright(c) 2005-2011 TransGaming Inc.
\r
5 // All rights reserved. No part of this software may be copied, distributed, transmitted,
\r
6 // transcribed, stored in a retrieval system, translated into any human or computer
\r
7 // language by any means, or disclosed to third parties without the explicit written
\r
8 // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
\r
9 // or implied, including but not limited to any patent rights, are granted to you.
\r
12 #ifndef sw_LRUCache_hpp
\r
13 #define sw_LRUCache_hpp
\r
15 #include "Common/Math.hpp"
\r
19 template<class Key, class Data>
\r
27 Data *query(const Key &key) const;
\r
28 Data *add(const Key &key, Data *data);
\r
30 int getSize() {return size;}
\r
31 Key &getKey(int i) {return key[i];}
\r
47 template<class Key, class Data>
\r
48 LRUCache<Key, Data>::LRUCache(int n)
\r
55 key = new Key[size];
\r
56 ref = new Key*[size];
\r
57 data = new Data*[size];
\r
59 for(int i = 0; i < size; i++)
\r
67 template<class Key, class Data>
\r
68 LRUCache<Key, Data>::~LRUCache()
\r
76 for(int i = 0; i < size; i++)
\r
89 template<class Key, class Data>
\r
90 Data *LRUCache<Key, Data>::query(const Key &key) const
\r
92 for(int i = top; i > top - fill; i--)
\r
98 Data *hit = data[j];
\r
103 int k = (j + 1) & mask;
\r
105 Data *swapD = data[k];
\r
109 Key *swapK = ref[k];
\r
118 return 0; // Not found
\r
121 template<class Key, class Data>
\r
122 Data *LRUCache<Key, Data>::add(const Key &key, Data *data)
\r
124 top = (top + 1) & mask;
\r
125 fill = fill + 1 < size ? fill + 1 : size;
\r
131 if(this->data[top])
\r
133 this->data[top]->unbind();
\r
136 this->data[top] = data;
\r
142 #endif // sw_LRUCache_hpp
\r