g3rc4n@gmail.com
12/15/2008 1:16:00 PM
On Dec 15, 9:17 am, ebony.s...@gmail.com wrote:
> On Dec 14, 6:46 pm, "g3r...@gmail.com" <g3r...@gmail.com> wrote:
>
>
>
> > 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::pair<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::pair<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_;
>
> > };- Hide quoted text -
>
> > - Show quoted text -
>
> Hi
> I suggest to use map family containers (map, multimap or hash_map).
> std::list<std::pair<key, value> >
> is like a map with the main benefit of map, fast retrieval I mean. You
> can wrap one of these container in your
> own table data structure. It is definitely faster than a list.
>
> Good luck,
> Saeed Amrollahi
yeah but i need to order/reorder them, every time one is accesed so
they are ordered from last accesed to one accesed longest ago, i don't
think i can do that with a map?