Project revived from Feb2017
[EroSomnia.git] / deps / boost_1_63_0 / boost / multi_index / detail / index_saver.hpp
blobae09d4eba4f28568f16d83a05227e47c1be57cc6
1 /* Copyright 2003-2013 Joaquin M Lopez Munoz.
2 * Distributed under the Boost Software License, Version 1.0.
3 * (See accompanying file LICENSE_1_0.txt or copy at
4 * http://www.boost.org/LICENSE_1_0.txt)
6 * See http://www.boost.org/libs/multi_index for library home page.
7 */
9 #ifndef BOOST_MULTI_INDEX_DETAIL_INDEX_SAVER_HPP
10 #define BOOST_MULTI_INDEX_DETAIL_INDEX_SAVER_HPP
12 #if defined(_MSC_VER)
13 #pragma once
14 #endif
16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
17 #include <boost/multi_index/detail/index_matcher.hpp>
18 #include <boost/noncopyable.hpp>
19 #include <boost/serialization/nvp.hpp>
20 #include <cstddef>
22 namespace boost{
24 namespace multi_index{
26 namespace detail{
28 /* index_saver accepts a base sequence of previously saved elements
29 * and saves a possibly reordered subsequence in an efficient manner,
30 * serializing only the information needed to rearrange the subsequence
31 * based on the original order of the base.
32 * multi_index_container is in charge of supplying the info about the
33 * base sequence, and each index can subsequently save itself using the
34 * const interface of index_saver.
37 template<typename Node,typename Allocator>
38 class index_saver:private noncopyable
40 public:
41 index_saver(const Allocator& al,std::size_t size):alg(al,size){}
43 template<class Archive>
44 void add(Node* node,Archive& ar,const unsigned int)
46 ar<<serialization::make_nvp("position",*node);
47 alg.add(node);
50 template<class Archive>
51 void add_track(Node* node,Archive& ar,const unsigned int)
53 ar<<serialization::make_nvp("position",*node);
56 template<typename IndexIterator,class Archive>
57 void save(
58 IndexIterator first,IndexIterator last,Archive& ar,
59 const unsigned int)const
61 /* calculate ordered positions */
63 alg.execute(first,last);
65 /* Given a consecutive subsequence of displaced elements
66 * x1,...,xn, the following information is serialized:
68 * p0,p1,...,pn,0
70 * where pi is a pointer to xi and p0 is a pointer to the element
71 * preceding x1. Crealy, from this information is possible to
72 * restore the original order on loading time. If x1 is the first
73 * element in the sequence, the following is serialized instead:
75 * p1,p1,...,pn,0
77 * For each subsequence of n elements, n+2 pointers are serialized.
78 * An optimization policy is applied: consider for instance the
79 * sequence
81 * a,B,c,D
83 * where B and D are displaced, but c is in its correct position.
84 * Applying the schema described above we would serialize 6 pointers:
86 * p(a),p(B),0
87 * p(c),p(D),0
89 * but this can be reduced to 5 pointers by treating c as a displaced
90 * element:
92 * p(a),p(B),p(c),p(D),0
95 std::size_t last_saved=3; /* distance to last pointer saved */
96 for(IndexIterator it=first,prev=first;it!=last;prev=it++,++last_saved){
97 if(!alg.is_ordered(get_node(it))){
98 if(last_saved>1)save_node(get_node(prev),ar);
99 save_node(get_node(it),ar);
100 last_saved=0;
102 else if(last_saved==2)save_node(null_node(),ar);
104 if(last_saved<=2)save_node(null_node(),ar);
106 /* marks the end of the serialization info for [first,last) */
108 save_node(null_node(),ar);
111 private:
112 template<typename IndexIterator>
113 static Node* get_node(IndexIterator it)
115 return it.get_node();
118 static Node* null_node(){return 0;}
120 template<typename Archive>
121 static void save_node(Node* node,Archive& ar)
123 ar<<serialization::make_nvp("pointer",node);
126 index_matcher::algorithm<Node,Allocator> alg;
129 } /* namespace multi_index::detail */
131 } /* namespace multi_index */
133 } /* namespace boost */
135 #endif