G
g3rc4n
i'm writing a class for caching lines of a text editor as accessing
the actually lines is expensive
now i know that caching works by whenever cache is accsed it's put the
the front of the list, then when the cahce is full it's either the one
at the end of the list of removed or one at the begging, for me it's
the one at the end
anyway i'm wondering whats the best std libary container for this? atm
i'm using a std::list<std:
air<key,T> >
template<typename E>
void assert_(const bool assertion, E e = E()){
if(!assertion)
throw e;
}
template<typename KEY, typename T>
class cache_table{
public:
typedef std::size_t size_type;
typedef KEY key_type;
typedef T cache_type;
typedef cache_type& cache_ref;
private:
typedef std:
air<key_type,cache_type> element_type;
typedef std::list<element_type> list_type;
typedef typename list_type::iterator LI;
typedef typename list_type::const_iterator CLI;
public:
struct invalid_cache : public std::exception{};
public:
cache_table(const size_type max_cache_size):
max_cache_size_((max_cache_size>1?max_cache_size:1)){
}
const bool is_cached(const key_type k){
return (find(k)!=cache_list_.end());
}
cache_ref get_cache(const key_type k){
LI iter(find(k));
assert_<invalid_cache>(iter!=cache_list_.end());
move_to_front(iter);
return cache_list_.begin()->second;
}
void put_cache(const key_type k, const cache_type& c){
add(element_type(k,c));
}
void remove_cache(const key_type k){
LI iter(find(k));
assert_<invalid_cache>(iter!=cache_list_.end());
cache_list_.erase(iter);
}
void clear(){
cache_list_.clear();
}
private:
LI find(const key_type k){
LI iter(cache_list_.begin());
for(;iter!=cache_list_.end();++iter)
if(iter->first == k)
break;
return iter;
}
void move_to_front(LI i){
element_type temp(*i);
cache_list_.erase(i);
cache_list_.push_front(temp);
}
void add(const element_type& e){
cache_list_.push_front(e);
if(cache_list_.size() > max_cache_size_)
cache_list_.pop_back();
}
const size_type max_cache_size_;
list_type cache_list_;
};
the actually lines is expensive
now i know that caching works by whenever cache is accsed it's put the
the front of the list, then when the cahce is full it's either the one
at the end of the list of removed or one at the begging, for me it's
the one at the end
anyway i'm wondering whats the best std libary container for this? atm
i'm using a std::list<std:
template<typename E>
void assert_(const bool assertion, E e = E()){
if(!assertion)
throw e;
}
template<typename KEY, typename T>
class cache_table{
public:
typedef std::size_t size_type;
typedef KEY key_type;
typedef T cache_type;
typedef cache_type& cache_ref;
private:
typedef std:
typedef std::list<element_type> list_type;
typedef typename list_type::iterator LI;
typedef typename list_type::const_iterator CLI;
public:
struct invalid_cache : public std::exception{};
public:
cache_table(const size_type max_cache_size):
max_cache_size_((max_cache_size>1?max_cache_size:1)){
}
const bool is_cached(const key_type k){
return (find(k)!=cache_list_.end());
}
cache_ref get_cache(const key_type k){
LI iter(find(k));
assert_<invalid_cache>(iter!=cache_list_.end());
move_to_front(iter);
return cache_list_.begin()->second;
}
void put_cache(const key_type k, const cache_type& c){
add(element_type(k,c));
}
void remove_cache(const key_type k){
LI iter(find(k));
assert_<invalid_cache>(iter!=cache_list_.end());
cache_list_.erase(iter);
}
void clear(){
cache_list_.clear();
}
private:
LI find(const key_type k){
LI iter(cache_list_.begin());
for(;iter!=cache_list_.end();++iter)
if(iter->first == k)
break;
return iter;
}
void move_to_front(LI i){
element_type temp(*i);
cache_list_.erase(i);
cache_list_.push_front(temp);
}
void add(const element_type& e){
cache_list_.push_front(e);
if(cache_list_.size() > max_cache_size_)
cache_list_.pop_back();
}
const size_type max_cache_size_;
list_type cache_list_;
};