首页 > 代码库 > Serializable unordered set
Serializable unordered set
Serializable unordered set
可序列化的哈希表
/*‘‘‘[[[[[[[[[[[[[[[ ]]]]]]]]]]]]]]][:::::::::::::: ::::::::::::::][:::::::::::::: ::::::::::::::][::::::[[[[[[[: :]]]]]]]::::::][:::::[ ]:::::][:::::[ ]:::::][:::::[ ]:::::][:::::[ Serializable unordered set ]:::::][:::::[ Created on Jan 25, 2015 ]:::::][:::::[ @author: ScottGu<150316990@qq.com, ]:::::][:::::[ gu.kai.66@gmail.com> ]:::::][:::::[ performance tested: ]:::::][:::::[ environment: 64bit win7, i7-4800MQ, 8GB ]:::::][:::::[ ]:::::][:::::[ ]:::::][:::::[ ]:::::][:::::[ ]:::::][::::::[[[[[[[: :]]]]]]]::::::][:::::::::::::: ::::::::::::::][:::::::::::::: ::::::::::::::][[[[[[[[[[[[[[[ ]]]]]]]]]]]]]]]]‘‘‘*/#pragma once#include "StdAfx.h"#include <cstdlib>#include <iostream>#include <string>#include <fstream>#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() { } //Serializable_unordered_set(const Serializable_unordered_set& newset) { // m_unordered_set = newset.m_unordered_set; // m_mtx_protect = newset.m_mtx_protect; //} 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。