当C++遇到IOS应用开发---LRUCache缓存(3)

来源:未知 责任编辑:责任编辑 发表时间:2014-01-20 07:51 点击:

template <> 
struct hashkey_compare<HashKey> 

    bool operator()(HashKey __x, HashKey __y) const{ 
        string x = [__x.key UTF8String]; 
        string y = [__y.key UTF8String]; 
        return x < y; 
    } 
}; 
        
//自定义map类型 
template <typename K, typename V, typename _Compare = hashkey_compare<K>, 
typename _Alloc = std::allocator<std::pair<const K, V> > > 
class  lru_map: public map<K, V, _Compare, _Alloc>{}; 
            
class CLRUCache 

public: 
    
    CLRUCache() : _now(0){ 
        _lru_list = shared_ptr<lru_map<virtual_time, HashKey> >(new lru_map<virtual_time, HashKey>); 
        _hash_table = shared_ptr<lru_map<HashKey, HashValue> > (new lru_map<HashKey, HashValue>); 
    } 
    
    ~CLRUCache(){ 
        _lru_list->clear(); 
        _hash_table->clear(); 
    } 
    
    int set( const HashKey& key, const id &value ) 
    { 
        HashValue hash_value; 
        hash_value.value_ = value; 
        hash_value.access_ = get_virtual_time(); 
        pair< map<HashKey, HashValue>::iterator, bool > ret = _hash_table->insert(make_pair(key, hash_value)); 
        if ( !ret.second ){ 
            // key already exist 
            virtual_time old_access = (*_hash_table)[key].access_; 
            map<virtual_time, HashKey>::iterator iter = _lru_list->find(old_access); 
            if(iter != _lru_list->end()) 
            { 
                _lru_list->erase(iter); 
            } 
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • Lexical or Preprocessor Issue 'xxx.h
  • ios学习笔记(二)xcode 4.3.2下实现基本交互
  • ios版本的helloworld
  • iphone(object-c) 内存管理(3) 有效的内存管理 前半部分
  • ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
  • IOS类似iphone通讯录TableView的完整demo【附源码】
  • UITableView一些方法
  • [iPhone中级]iPhone团购信息客户端的开发 (二)
  • 如何为Iphone应用创建启动界面
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1