[mlpack-git] master: Backport unordered_map serialization from boost 1.56. (413456a)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Wed Dec 23 11:43:54 EST 2015


Repository : https://github.com/mlpack/mlpack

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/de9cc4b05069e1fa4793d9355f2f595af5ff45d2...6070527af14296cd99739de6c62666cc5d2a2125

>---------------------------------------------------------------

commit 413456ab636325eaa4d00ace544d1b02df326e2f
Author: Ryan Curtin <ryan at ratml.org>
Date:   Thu Oct 1 18:42:53 2015 -0700

    Backport unordered_map serialization from boost 1.56.


>---------------------------------------------------------------

413456ab636325eaa4d00ace544d1b02df326e2f
 src/mlpack/core/CMakeLists.txt                     |   1 +
 .../core/{data => boost_backport}/CMakeLists.txt   |  14 +-
 src/mlpack/core/boost_backport/README.md           |   3 +
 .../unordered_collections_load_imp.hpp             |  74 +++++++
 .../unordered_collections_save_imp.hpp             |  86 ++++++++
 src/mlpack/core/boost_backport/unordered_map.hpp   | 231 +++++++++++++++++++++
 src/mlpack/prereqs.hpp                             |   6 +-
 7 files changed, 403 insertions(+), 12 deletions(-)

diff --git a/src/mlpack/core/CMakeLists.txt b/src/mlpack/core/CMakeLists.txt
index f34e910..d8a49bb 100644
--- a/src/mlpack/core/CMakeLists.txt
+++ b/src/mlpack/core/CMakeLists.txt
@@ -1,6 +1,7 @@
 # All we have to do is recurse into the subdirectories.
 set(DIRS
   arma_extend
+  boost_backport
   data
   dists
   kernels
diff --git a/src/mlpack/core/data/CMakeLists.txt b/src/mlpack/core/boost_backport/CMakeLists.txt
similarity index 67%
copy from src/mlpack/core/data/CMakeLists.txt
copy to src/mlpack/core/boost_backport/CMakeLists.txt
index 6fd0aef..d388e07 100644
--- a/src/mlpack/core/data/CMakeLists.txt
+++ b/src/mlpack/core/boost_backport/CMakeLists.txt
@@ -1,17 +1,9 @@
 # Define the files that we need to compile.
 # Anything not in this list will not be compiled into MLPACK.
 set(SOURCES
-  dataset_info.hpp
-  dataset_info_impl.hpp
-  extension.hpp
-  format.hpp
-  load.hpp
-  load_impl.hpp
-  normalize_labels.hpp
-  normalize_labels_impl.hpp
-  save.hpp
-  save_impl.hpp
-  serialization_shim.hpp
+  unordered_collections_load_imp.hpp
+  unordered_collections_save_imp.hpp
+  unordered_map.hpp
 )
 
 # add directory name to sources
diff --git a/src/mlpack/core/boost_backport/README.md b/src/mlpack/core/boost_backport/README.md
new file mode 100644
index 0000000..8c35682
--- /dev/null
+++ b/src/mlpack/core/boost_backport/README.md
@@ -0,0 +1,3 @@
+The files in this directory are taken from Boost 1.56.0 in order to backport
+serialization support for unordered_map.  Therefore these files are licensed
+under the Boost Software License.
diff --git a/src/mlpack/core/boost_backport/unordered_collections_load_imp.hpp b/src/mlpack/core/boost_backport/unordered_collections_load_imp.hpp
new file mode 100644
index 0000000..bf56741
--- /dev/null
+++ b/src/mlpack/core/boost_backport/unordered_collections_load_imp.hpp
@@ -0,0 +1,74 @@
+#ifndef BOOST_SERIALIZATION_UNORDERED_COLLECTIONS_LOAD_IMP_HPP
+#define BOOST_SERIALIZATION_UNORDERED_COLLECTIONS_LOAD_IMP_HPP
+
+// MS compatible compilers support #pragma once
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+# pragma warning (disable : 4786) // too long name, harmless warning
+#endif
+
+/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
+// unordered_collections_load_imp.hpp: serialization for loading stl collections
+
+// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . 
+// (C) Copyright 2014 Jim Bell
+// Use, modification and distribution is subject to the Boost Software
+// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+//  See http://www.boost.org for updates, documentation, and revision history.
+
+// helper function templates for serialization of collections
+
+#include <boost/assert.hpp>
+#include <cstddef> // size_t
+#include <boost/config.hpp> // msvc 6.0 needs this for warning suppression
+#if defined(BOOST_NO_STDC_NAMESPACE)
+namespace std{ 
+    using ::size_t; 
+} // namespace std
+#endif
+#include <boost/detail/workaround.hpp>
+
+#include <boost/archive/detail/basic_iarchive.hpp>
+#include <boost/serialization/access.hpp>
+#include <boost/serialization/nvp.hpp>
+#include <boost/serialization/detail/stack_constructor.hpp>
+#include <boost/serialization/collection_size_type.hpp>
+#include <boost/serialization/item_version_type.hpp>
+
+namespace boost{
+namespace serialization {
+namespace stl {
+
+//////////////////////////////////////////////////////////////////////
+// implementation of serialization for STL containers
+//
+template<class Archive, class Container, class InputFunction>
+inline void load_unordered_collection(Archive & ar, Container &s)
+{
+    s.clear();
+    collection_size_type count;
+    collection_size_type bucket_count;
+    boost::serialization::item_version_type item_version(0);
+    boost::archive::library_version_type library_version(
+        ar.get_library_version()
+    );
+    // retrieve number of elements
+    ar >> BOOST_SERIALIZATION_NVP(count);
+    ar >> BOOST_SERIALIZATION_NVP(bucket_count);
+    if(boost::archive::library_version_type(3) < library_version){
+        ar >> BOOST_SERIALIZATION_NVP(item_version);
+    }
+    s.rehash(bucket_count);
+    InputFunction ifunc;
+    while(count-- > 0){
+        ifunc(ar, s, item_version);
+    }
+}
+
+} // namespace stl 
+} // namespace serialization
+} // namespace boost
+
+#endif //BOOST_SERIALIZATION_UNORDERED_COLLECTIONS_LOAD_IMP_HPP
diff --git a/src/mlpack/core/boost_backport/unordered_collections_save_imp.hpp b/src/mlpack/core/boost_backport/unordered_collections_save_imp.hpp
new file mode 100644
index 0000000..56746eb
--- /dev/null
+++ b/src/mlpack/core/boost_backport/unordered_collections_save_imp.hpp
@@ -0,0 +1,86 @@
+#ifndef BOOST_SERIALIZATION_UNORDERED_COLLECTIONS_SAVE_IMP_HPP
+#define BOOST_SERIALIZATION_UNORDERED_COLLECTIONS_SAVE_IMP_HPP
+
+// MS compatible compilers support #pragma once
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
+// hash_collections_save_imp.hpp: serialization for stl collections
+
+// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . 
+// (C) Copyright 2014 Jim Bell
+// Use, modification and distribution is subject to the Boost Software
+// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+//  See http://www.boost.org for updates, documentation, and revision history.
+
+// helper function templates for serialization of collections
+
+#include <boost/config.hpp>
+#include <boost/serialization/nvp.hpp>
+#include <boost/serialization/serialization.hpp>
+#include <boost/serialization/version.hpp>
+#include <boost/serialization/collection_size_type.hpp>
+#include <boost/serialization/item_version_type.hpp>
+
+namespace boost{
+namespace serialization {
+namespace stl {
+
+//////////////////////////////////////////////////////////////////////
+// implementation of serialization for STL containers
+//
+
+template<class Archive, class Container>
+inline void save_unordered_collection(Archive & ar, const Container &s)
+{
+    collection_size_type count(s.size());
+    const collection_size_type bucket_count(s.bucket_count());
+    const item_version_type item_version(
+        version<typename Container::value_type>::value
+    );
+
+    #if 0
+    /* should only be necessary to create archives of previous versions
+     * which is not currently supported.  So for now comment this out
+     */
+    boost::archive::library_version_type library_version(
+        ar.get_library_version()
+    );
+    // retrieve number of elements
+	ar << BOOST_SERIALIZATION_NVP(count);
+	ar << BOOST_SERIALIZATION_NVP(bucket_count);
+    if(boost::archive::library_version_type(3) < library_version){
+        // record number of elements
+        // make sure the target type is registered so we can retrieve
+        // the version when we load
+        ar << BOOST_SERIALIZATION_NVP(item_version);
+    }
+    #else
+        ar << BOOST_SERIALIZATION_NVP(count);
+        ar << BOOST_SERIALIZATION_NVP(bucket_count);
+        ar << BOOST_SERIALIZATION_NVP(item_version);
+    #endif
+
+    typename Container::const_iterator it = s.begin();
+    while(count-- > 0){
+        // note borland emits a no-op without the explicit namespace
+        boost::serialization::save_construct_data_adl(
+            ar, 
+            &(*it), 
+            boost::serialization::version<
+                typename Container::value_type
+            >::value
+        );
+        ar << boost::serialization::make_nvp("item", *it++);
+    }
+}
+
+} // namespace stl 
+} // namespace serialization
+} // namespace boost
+
+#endif //BOOST_SERIALIZATION_UNORDERED_COLLECTIONS_SAVE_IMP_HPP
diff --git a/src/mlpack/core/boost_backport/unordered_map.hpp b/src/mlpack/core/boost_backport/unordered_map.hpp
new file mode 100644
index 0000000..597f4a0
--- /dev/null
+++ b/src/mlpack/core/boost_backport/unordered_map.hpp
@@ -0,0 +1,231 @@
+#ifndef  BOOST_SERIALIZATION_UNORDERED_MAP_HPP
+#define BOOST_SERIALIZATION_UNORDERED_MAP_HPP
+
+// MS compatible compilers support #pragma once
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
+// serialization/unordered_map.hpp:
+// serialization for stl unordered_map templates
+
+// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . 
+// (C) Copyright 2014 Jim Bell
+// Use, modification and distribution is subject to the Boost Software
+// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+//  See http://www.boost.org for updates, documentation, and revision history.
+
+#include <boost/config.hpp>
+
+#include <unordered_map>
+
+#include <boost/serialization/utility.hpp>
+#include "unordered_collections_save_imp.hpp"
+#include "unordered_collections_load_imp.hpp"
+#include <boost/serialization/split_free.hpp>
+
+namespace boost { 
+namespace serialization {
+
+namespace stl {
+
+// map input
+template<class Archive, class Container>
+struct archive_input_unordered_map
+{
+    inline void operator()(
+        Archive &ar, 
+        Container &s, 
+        const unsigned int v
+    ){
+        typedef typename Container::value_type type;
+        detail::stack_construct<Archive, type> t(ar, v);
+        // borland fails silently w/o full namespace
+        ar >> boost::serialization::make_nvp("item", t.reference());
+        std::pair<typename Container::const_iterator, bool> result = 
+            s.insert(t.reference());
+        // note: the following presumes that the map::value_type was NOT tracked
+        // in the archive.  This is the usual case, but here there is no way
+        // to determine that.  
+        if(result.second){
+            ar.reset_object_address(
+                & (result.first->second),
+                & t.reference().second
+            );
+        }
+    }
+};
+
+// multimap input
+template<class Archive, class Container>
+struct archive_input_unordered_multimap
+{
+    inline void operator()(
+        Archive &ar, 
+        Container &s, 
+        const unsigned int v
+    ){
+        typedef typename Container::value_type type;
+        detail::stack_construct<Archive, type> t(ar, v);
+        // borland fails silently w/o full namespace
+        ar >> boost::serialization::make_nvp("item", t.reference());
+        typename Container::const_iterator result 
+            = s.insert(t.reference());
+        // note: the following presumes that the map::value_type was NOT tracked
+        // in the archive.  This is the usual case, but here there is no way
+        // to determine that.  
+        ar.reset_object_address(
+            & result->second,
+            & t.reference()
+        );
+    }
+};
+
+} // stl
+
+template<
+    class Archive, 
+    class Key, 
+    class HashFcn, 
+    class EqualKey,
+    class Allocator
+>
+inline void save(
+    Archive & ar,
+    const std::unordered_map<
+        Key, HashFcn, EqualKey, Allocator
+    > &t,
+    const unsigned int /*file_version*/
+){
+    boost::serialization::stl::save_unordered_collection<
+        Archive, 
+        std::unordered_map<
+            Key, HashFcn, EqualKey, Allocator
+        >
+    >(ar, t);
+}
+
+template<
+    class Archive, 
+    class Key, 
+    class HashFcn, 
+    class EqualKey,
+    class Allocator
+>
+inline void load(
+    Archive & ar,
+    std::unordered_map<
+        Key, HashFcn, EqualKey, Allocator
+    > &t,
+    const unsigned int /*file_version*/
+){
+    boost::serialization::stl::load_unordered_collection<
+        Archive,
+        std::unordered_map<
+            Key, HashFcn, EqualKey, Allocator
+        >,
+        boost::serialization::stl::archive_input_unordered_map<
+            Archive, 
+            std::unordered_map<
+                Key, HashFcn, EqualKey, Allocator
+            >
+        >
+    >(ar, t);
+}
+
+// split non-intrusive serialization function member into separate
+// non intrusive save/load member functions
+template<
+    class Archive, 
+    class Key, 
+    class HashFcn, 
+    class EqualKey,
+    class Allocator
+>
+inline void serialize(
+    Archive & ar,
+    std::unordered_map<
+        Key, HashFcn, EqualKey, Allocator
+    > &t,
+    const unsigned int file_version
+){
+    boost::serialization::split_free(ar, t, file_version);
+}
+
+// unordered_multimap
+template<
+    class Archive, 
+    class Key, 
+    class HashFcn, 
+    class EqualKey,
+    class Allocator
+>
+inline void save(
+    Archive & ar,
+    const std::unordered_multimap<
+        Key, HashFcn, EqualKey, Allocator
+    > &t,
+    const unsigned int /*file_version*/
+){
+    boost::serialization::stl::save_unordered_collection<
+        Archive, 
+        std::unordered_multimap<
+            Key, HashFcn, EqualKey, Allocator
+        >
+    >(ar, t);
+}
+
+template<
+    class Archive, 
+    class Key, 
+    class HashFcn, 
+    class EqualKey,
+    class Allocator
+>
+inline void load(
+    Archive & ar,
+    std::unordered_multimap<
+        Key, HashFcn, EqualKey, Allocator
+    > &t,
+    const unsigned int /*file_version*/
+){
+    boost::serialization::stl::load_unordered_collection<
+        Archive,
+        std::unordered_multimap<
+            Key, HashFcn, EqualKey, Allocator
+        >,
+        boost::serialization::stl::archive_input_unordered_multimap<
+            Archive, 
+            std::unordered_multimap<
+                Key, HashFcn, EqualKey, Allocator
+            >
+        >
+    >(ar, t);
+}
+
+// split non-intrusive serialization function member into separate
+// non intrusive save/load member functions
+template<
+    class Archive, 
+    class Key, 
+    class HashFcn, 
+    class EqualKey,
+    class Allocator
+>
+inline void serialize(
+    Archive & ar,
+    std::unordered_multimap<
+        Key, HashFcn, EqualKey, Allocator
+    > &t,
+    const unsigned int file_version
+){
+    boost::serialization::split_free(ar, t, file_version);
+}
+
+} // namespace serialization
+} // namespace boost
+
+#endif // BOOST_SERIALIZATION_UNORDERED_MAP_HPP
diff --git a/src/mlpack/prereqs.hpp b/src/mlpack/prereqs.hpp
index 000f90b..7c74e1f 100644
--- a/src/mlpack/prereqs.hpp
+++ b/src/mlpack/prereqs.hpp
@@ -53,7 +53,11 @@
 #include <boost/serialization/serialization.hpp>
 #include <boost/serialization/vector.hpp>
 #include <boost/serialization/map.hpp>
-#include <boost/serialization/unordered_map.hpp>
+#if BOOST_VERSION < 105500 // Old versions don't have unordered_map support.
+  #include "core/boost_backport/unordered_map.hpp"
+#else
+  #include <boost/serialization/unordered_map.hpp>
+#endif
 #ifndef BOOST_PFTO
   #define BOOST_PFTO
 #endif



More information about the mlpack-git mailing list