1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #ifndef sw_LRUCache_hpp
16 #define sw_LRUCache_hpp
18 #include "Common/Math.hpp"
22 template<class Key, class Data>
30 Data *query(const Key &key) const;
31 Data *add(const Key &key, Data *data);
33 int getSize() {return size;}
34 Key &getKey(int i) {return key[i];}
50 template<class Key, class Data>
51 LRUCache<Key, Data>::LRUCache(int n)
60 data = new Data*[size];
62 for(int i = 0; i < size; i++)
70 template<class Key, class Data>
71 LRUCache<Key, Data>::~LRUCache()
79 for(int i = 0; i < size; i++)
92 template<class Key, class Data>
93 Data *LRUCache<Key, Data>::query(const Key &key) const
95 for(int i = top; i > top - fill; i--)
106 int k = (j + 1) & mask;
108 Data *swapD = data[k];
121 return 0; // Not found
124 template<class Key, class Data>
125 Data *LRUCache<Key, Data>::add(const Key &key, Data *data)
127 top = (top + 1) & mask;
128 fill = fill + 1 < size ? fill + 1 : size;
136 this->data[top]->unbind();
139 this->data[top] = data;
145 #endif // sw_LRUCache_hpp