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. iOS 7.1耗电严重解决办法

    自从iOS 7.1正式版发布以来,三天后的升级率就已经达到17.9%,预计一周后升级率能突破40%.但是也有不少用户在苹果官方支持论坛上抱怨iOS 7.1系统耗电严重. 名为PJS2006的iPhon ...

  2. VM虚拟主机怎么设置网络

    VMware是很受欢迎的虚拟机,在我们平时的工作中需要经常用到,此文简单总结了平时使用的三种网络配置方式,具体的原理没有去深究.我估计咱也研究不懂! 虚拟主机安装很简单,网上教程有很多,但是有很多新手 ...

  3. Python的平凡之路(15)

    一.CSS补充: 1. 上节课讲述 a.css重用               <style>            如果整个页面的宽度 > 900px时:            { ...

  4. 构造一个简单的Linux系统MenuOS

    陈智威20135125 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验指导 ...

  5. Spring MVC学习笔记--认识SpringMVC

    Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块.使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还是 Struts 这样的 Web 框 ...

  6. RABBITMQ(小总结 持续更新...

    (一)理解消息通信 1.消息通信概念---消费者.生产者和代理 生产者(producer)创建消息,然后发送到代理服务器(RaabitMQ). 其中消息包括两部分内容:有效载荷(payload)和标签 ...

  7. 炫酷的Linux终端命令大全-1

    1. 命令行日常快捷键. CTRL + U            ------------------------------- 剪切光标前的内容 CTRL + K             ----- ...

  8. web api 初体验 解决js调用跨域问题

    跨域界定 常见跨域: 同IP不同端口: http:IP:8001/api/user     http:IP:8002/api/user 不同IP不同端口: http://172.28.20.100:8 ...

  9. dis进行反编译

    摘录自官方文档: https://docs.python.org/2/library/dis.html 回头自己也脑补下. 可以使用dis查看自己代码的复杂度之类的东西. 比如while 1 和whi ...

  10. track by

    ng-repeat指令中使用track by子语句解决重复数据遍历的错误 <li ng-repat="x in [2, 2]" ng-bind="x"&g ...