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. Java泛型学习笔记 - (六)泛型的继承

    在学习继承的时候, 我们已经知道可以将一个子类的对象赋值给其父类的对象, 也就是父类引用指向子类对象, 如: Object obj = new Integer(10); 这其实就是面向对象编程中的is ...

  2. JQuery基础总结上

    最近在慕课网学习JQuery基础课程,发现只是跟随网站的课程学习而不去自己总结扩展的话,很难达到真正学会理解的地步. 于是先在网站上草草过了一遍课程,然后借着今天的这个时间边记录边重新整理学习一下. ...

  3. CSS中box-sizing属性的理解与部分用法

    今天看了一些关于box-sizing的一些资料,在这里整理一下,希望也能对大家有所帮助. box-sizing是CSS的一个属性,很好的解决了盒模型的相关问题.CSS中的盒模型(Box model)分 ...

  4. odi 12.2.1中访问excel文件

    由于在odi 12.2.1中,必须使用jdk1.8,而jdk1.8中jdbc-odbc bridge已经不再支持,因此,可以使用Progress DataDirect SequeLink来充当jdbc ...

  5. 一步一步搭框架(asp.netmvc+easyui+sqlserver)-03

    一步一步搭框架(asp.netmvc+easyui+sqlserver)-03 我们期望简洁的后台代码,如下: using System; using System.Collections.Gener ...

  6. 最后一次PSP

    PSP: 一.计划 完成这个任务需要五天左右. 二.开发 1.需求分析: 作为一个排球比赛的现场工作人员,我需要统计每一名球员的得分以及技术统计(如:发球,拦网,一传等等),以便于颁发每场比赛的MVP ...

  7. 411. Minimum Unique Word Abbreviation

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  8. Java Swing 第02记 标签和按钮

    JLable的常用构造器 Public JLabel() 创建无图像且其标题为空字符串的JLabel对象 Public JLabel(String text) 使用指定的字符串text(也就是标签显示 ...

  9. js版扫雷(可直接运行试玩)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 关于AE

    1.下方时间轴左边上角时间,按住ctrl点击,可切换时间与帧数 2.新建comp,ctrl+n 3.特效控制面板,F3 4.时间轴左边图层各个属性快捷键:移动,p:旋转,r:缩放,s:轴心,a:不透明 ...