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;

Serializable unordered set的更多相关文章

  1. 对 Serializable和Parcelable理解

    1.首先他们两个接口都是为了实现对象的序列化,使之可以传递,所谓序列化就是将对象信息装换成可以存储的介质的过程. 2.Serializable是jdk所提供的序列化接口,该接口存在于io包下,可想用于 ...

  2. java.io.Serializable 序列化接口

    什么是序列化.反序列化? Serialization(序列化)是一种将对象以一连串的字节描述的过程: 反序列化deserialization是一种将这些字节重建成一个对象的过程. 序列化通俗一点说就是 ...

  3. serialVersionUID, ObjectInputStream与ObjectOutputStream类,Serializable接口,serialVersionUID的作用和用法

    ObjectInputStream与ObjectOutputStream类所读写的对象必须实现Serializable接口,对象中的transient和static类型成员变量不会被读取和写入 Ser ...

  4. Bitset<>用于unordered container时的默认hash函数

    自从c++11起,bitset用于unordered container,将会提供默认的hash函数. 在gcc中,相关代码如下: // DR 1182. /// std::hash speciali ...

  5. Java Serializable系列化与反系列化

    [引言] 将 Java 对象序列化为二进制文件的 Java 序列化技术是 Java 系列技术中一个较为重要的技术点,在大部分情况下,开发人员只需要了解被序列化的类需要实现 Serializable 接 ...

  6. 在Activity之间传递参数(三)——serializable和parcelable的区别

    传递值对象: 一.serializable实现:简单易用 serializable的迷人之处在于你只需要对某个类以及它的属性实现Serializable 接口即可.Serializable 接口是一种 ...

  7. Java中的Serializable接口transient关键字,及字节、字符、对象IO

    1.什么是序列化和反序列化Serialization是一种将对象转为为字节流的过程:deserialization是将字节流恢复为对象的过程. 2.什么情况下需要序列化a)当你想把的内存中的对象保存到 ...

  8. Hibernate的实体类为什么要实现Serializable序列化接口?

    Hibernate的实体类中为什么要继承Serializable?   hibernate有二级缓存,缓存会将对象写进硬盘,就必须序列化,以及兼容对象在网络中的传输 等等. java中常见的几个类(如 ...

  9. Java 序列化Serializable详解

    Java 序列化Serializable详解(附详细例子) Java 序列化Serializable详解(附详细例子) 1.什么是序列化和反序列化Serialization(序列化)是一种将对象以一连 ...

随机推荐

  1. Win7下Hyenae的安装

    (1)下载 链接:http://sourceforge.net/projects/hyenae/   资源:hyenae-0.36-1_fe_0.1-1-win32.exe (2)README --- ...

  2. 重置了下系统好多关于mysql密码的文章都很渣拷分好的备用

    方法1: 用SET PASSWORD命令 mysql -u root mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass ...

  3. nim也玩一行流,nim版的list comprehension

    nim 是一门风格类似python的静态编译型语言,官方网站:http://nim-lang.org 如果你想折腾nim的编辑环境,可以用sublime text3 +插件nimlime,notepa ...

  4. 如何在静态博客hexo中只显示摘要信息

    默认情况下hexo博客(如本站)的首页显示的是完整的文章 – 而文章比较长的时候这无疑会带来诸多不遍. 那怎么样才能只显示个摘要呢? 方法说白了,其实很简单 – 只要加入一个<!-- more ...

  5. 1.6jdk + eclipse + pydev搭建Python开发环境

    直接在1.6jdk的eclipse上用install new software的方法安装插件,会找不到安装好的插件.pydev官网还提供一种zip直接解压插件到eclipse文件夹下的dropins文 ...

  6. ansible安装httpd

    --- - hosts: web   tasks:       - name: "INSTALL"         yum: name={{ item  }} state=pres ...

  7. Reverse-Daily(3)-DotNetCrackMe1

    链接:http://pan.baidu.com/s/1cuYQhK    密码:zjx6 这是一个用c#编写的程序 用dotpeek或者ILSPY反编译可以看到程序结构,主体比较简单,是一个des加密 ...

  8. iOS开发,用了ARC,引入非ARC的第三方,报错

    ARC forbids explicit message sendof'dealloc' 在xcode中,打开“Build Setting”,找到“Objective-C Automatic Refe ...

  9. 测试markdown

    总结 写内容:博客重要的是内容,如果要讲形式与技术,应该深入学习html,这不是我的重点. 专注:就是利用MD这个好机会,忘记格式,用最简约的美观,来反衬出思考的清晰,内容的价值性. 今天 既要美国式 ...

  10. WAMPserver配置(允许外部访问、phpmyadmin设置为输入用户名密码才可登录等)

    对于很多不熟悉PHP环境安装的朋友来说,用集成环境可以更快的上手,更方便的搭建PHP的运行环境,但是,WAMP的集成环境仅仅是将底层基础工作做好 了,有些个别关键的配置操作并没有集成到环境安装中,所以 ...