首页 技术 正文
技术 2022年11月16日
0 收藏 978 点赞 4,620 浏览 2935 个字
Serializable unordered set 可序列化哈希set
#include <boost/algorithm/string/predicate.hpp>#include <boost/exception/diagnostic_information.hpp>#include <boost/exception_ptr.hpp>#include <boost/archive/text_oarchive.hpp>#include <boost/archive/text_iarchive.hpp>#include <boost/config.hpp>#include <boost/unordered_set.hpp>#include <boost/shared_ptr.hpp>#include <boost/serialization/string.hpp>#include <boost/serialization/version.hpp>#include <boost/serialization/split_member.hpp>#include <boost/serialization/collections_save_imp.hpp>#include <boost/serialization/collections_load_imp.hpp>#include <boost/serialization/utility.hpp>#include <boost/serialization/split_free.hpp>#include <boost/thread/shared_mutex.hpp>#include <boost/thread/locks.hpp>namespace boost {    namespace serialization {        template<class Archive, typename TArgs >        inline void save(Archive & ar, boost::unordered_set<TArgs> const&t, unsigned) {            boost::serialization::stl::save_collection<Archive, boost::unordered_set<TArgs> >(ar, t);        };        template<class Archive, typename TArgs >        inline void load(Archive & ar, boost::unordered_set<TArgs> &t, unsigned) {            boost::serialization::stl::load_collection<Archive,                boost::unordered_set<TArgs>,                boost::serialization::stl::archive_input_set<Archive, boost::unordered_set<TArgs> >,                boost::serialization::stl::no_reserve_imp<boost::unordered_set<TArgs> >            >(ar, t);        };        // split non-intrusive serialization function member into separate        // non intrusive save/load member functions        template <class Archive, typename TArgs>        inline void serialize(Archive & ar, boost::unordered_set<TArgs> &t, unsigned file_version) {            boost::serialization::split_free(ar, t, file_version);        };    }};template<typename DataType>class Serializable_unordered_set : boost::noncopyable{    friend class boost::serialization::access;    // When the class Archive corresponds to an output archive, the    // & operator is defined similar to <<.  Likewise, when the class Archive    // is a type of input archive the & operator is defined similar to >>.    template<class Archive>    void serialize(Archive & ar, const unsigned int version) {        ar & m_unordered_set;    }private:        boost::unordered_set<DataType > m_unordered_set;        mutable boost::shared_mutex m_mtx_protect;public:    Serializable_unordered_set()    {    }    void SetMap(boost::unordered_set<DataType>& newset)    {        m_unordered_set = newset;    }    void insert(DataType t) {        boost::unique_lock< boost::shared_mutex > lock(m_mtx_protect);        m_unordered_set.insert(t);    }    bool exist(DataType ky)    {        boost::shared_lock< boost::shared_mutex > lock(m_mtx_protect);        if (m_unordered_set.find(ky) != m_unordered_set.end())            return true;        return false;    }    void serialize_to(string file_path) {        boost::shared_lock< boost::shared_mutex > lock(m_mtx_protect);        std::ofstream fout(file_path.c_str());        boost::archive::text_oarchive oa(fout);        oa << m_unordered_set;        fout.close();    }    void deserialize_from(std::string file_path) {        boost::unique_lock< boost::shared_mutex > lock(m_mtx_protect);        std::ifstream file_in(file_path.c_str());        boost::archive::text_iarchive ia(file_in);        ia >> m_unordered_set; //recover from file        file_in.close();    }};typedef std::shared_ptr<Serializable_unordered_set<int>> hashset_int_SPtr;
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905