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