Serializable unordered set

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)