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. c# 字符串操作

    一.字符串操作 //字符串转数组 string mystring="this is a string" char[] mychars=mystring.ToCharArray(); ...

  2. centos 6 安装 gitlib

    安装gitlab-----------1. 下载 gitlabcurl -O https://downloads-packages.s3.amazonaws.com/centos-6.5/gitlab ...

  3. Android中的onActivityResult和setResult方法的使用

    如果你想在Activity中得到新打开Activity关闭后返回的数据,你需要使用系统提供的startActivityForResult(Intent intent,int requestCode)方 ...

  4. c# System.Data.OracleClient需要Oracle客户端软件8.1.7或更高版本

    前几天遇到了这个问题,情景是与oracle数据库连接的时候出现的.本机已经安装了客户端,使用toad数据库工具能够与数据库相连进行相关的操作.但是在使用代码进行连接的时候出现了这样的问题.找了好久,都 ...

  5. Myeclipse+AJAX+Servlet

    最近刚开始学AJAX的知识,这里介绍一个简单的Myeclipse+AJAX+Servlet项目. 此项目包含3个文件:index.jsp.check.java.还有一个需要配置的文件是:web.xml ...

  6. NSOperation实现线程间通信

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //1.创建队列 NSOperat ...

  7. NSOperation操作依赖和监听

    1.操作依赖 NSOperation之间可以设置依赖来保证执行顺序 比如一定要让操作A执行完后,才能执行操作B,可以这么写 [operationB addDependency:operationA]; ...

  8. ArcGIS二次开发实践— — 遍历ToolBox中的工具!

    在AO中,打开“文件A”的基本流程是: 1.创建对应“文件A”类型的WorkspaceFactory: 2.用WorkspaceFactory创建“文件A”的Workspace,Workspace可以 ...

  9. jieba中文分词(python)

    问题小结 1.安装 需要用到python,根据python2.7选择适当的安装包.先下载http://pypi.python.org/pypi/jieba/ ,解压后运行python setup.py ...

  10. DotNetBar中collapsibleSplitContainer的问题

    如果有两个collapsibleSplitContainer,并且将splitwidth都设置为1,则只有第一个起作用,如,窗体设计中的图是: 而执行的图是: 解决办法:在窗体load里手动将第二个c ...