线程安全的查找表

template<typename Key, typename Value, typename Hash = std::hash<Key>>
class ThreadsafeLookupTable
{
private:
    class BucketType
    {
    private:
        typedef std::pair<Key, Value> bucketValue;
        typedef std::list<bucketValue> bucketData;
        typedef typename bucketData::iterator bucketIterator;

        bucketData                   data;
        mutable boost::shared_mutex mutex;

        bucketIterator findEntryFor(Key const& key) const
        {
            return std::find_if(data.cbegin (), data.cend (),
                                [&](bucketValue const& item)
                                {return item.first == key;});
        }
    public:
        Value valueFor(Key const& key, Value const& defaultValue) const
        {
            boost::shared_lock<boost::shared_mutex> lock(mutex);
            auto const foundEntry = findEntryFor (key);
            return (foundEntry == data.cend ()? defaultValue : foundEntry->second);
        }

        void addOrUpdate(Key const& key, Value const& value)
        {
            std::unique_lock<boost::shared_mutex> lock(mutex);
            auto const foundEntry = findEntryFor (key);
            if (foundEntry == data.cend ()){
                data.push_back ({key, value});
            }
            else{
                foundEntry->second = value;
            }
        }

        void removeMapping(Key const& key)
        {
            std::unique_lock<boost::shared_mutex> lock(mutex);
            auto const foundEntry = findEntryFor (key);
            if (foundEntry != data.cend ()){
                data.erase (foundEntry);
            }
        }
    };

    std::vector<std::unique_ptr<BucketType>> buckets;
    Hash hasher;

    BucketType& getBucket(Key const& key) const
    {
        auto const bucketIdnex = hasher(key) % buckets.size ();
        return *buckets[bucketIdnex];
    }

public:
    ThreadsafeLookupTable(const ThreadsafeLookupTable&) = delete;
    ThreadsafeLookupTable& operator=(const ThreadsafeLookupTable&) = delete;

    typedef Key   KeyType;
    typedef Value MappedValue;

    ThreadsafeLookupTable(size_t numBuckets = 19, Hash const& hasher_ = Hash()):
        buckets(std::vector<std::unique_ptr<BucketType>>(numBuckets)),
        hasher(hasher_)
    {
        for (size_t i = 0; i < numBuckets; ++i){
            buckets[i].reset(std::make_unique<BucketType>());
        }
    }

    Value valueFor(Key const& key, Value const& defaultValue)
    {
        getBucket (key).valueFor (key, defaultValue);
    }

    void addOrUpdateMapping(Key const& key, Value const& value)
    {
        getBucket (key).addOrUpdate (key, value);
    }

    void removeMapping(Key const& key)
    {
        getBucket (key).removeMapping (key);
    }
};

 

 

转载于:https://www.cnblogs.com/wuOverflow/p/4841903.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值