]> git.lyx.org Git - lyx.git/blob - src/ShareContainer.h
gen toc also when NEW_INSETS is defined
[lyx.git] / src / ShareContainer.h
1 // -*- C++ -*-
2
3 #ifndef SHARECONTAINER_H
4 #define SHARECONTAINER_H
5
6 #include <vector>
7 #include <algorithm>
8 #include <functional>
9 #include <boost/utility.hpp>
10 #include <boost/smart_ptr.hpp>
11
12 ///
13 template<class Share>
14 class ShareContainer : public noncopyable {
15 public:
16         ///
17         typedef std::vector<boost::shared_ptr<Share> > Params;
18         ///
19         typedef typename Params::value_type value_type;
20         ///
21         value_type
22         get(Share const & ps) const {
23                 // First see if we already have this ps in the container
24                 Params::iterator it = std::find_if(params.begin(),
25                                                    params.end(),
26                                                    isEqual(ps));
27                 value_type tmp;
28                 if (it == params.end()) {
29                         // ok we don't have it so we should
30                         // insert it.
31                         tmp.reset(new Share(ps));
32                         params.push_back(tmp);
33                         // We clean here. This can cause us to have
34                         // some (one) unique elemements some times
35                         // but we should gain a lot in speed.
36                         clean();
37                         //std::sort(params.rbegin(), params.rend(), comp());
38                 } else {
39                         // yes we have it already
40                         tmp = *it;
41                         // move it forward
42                         if (it != params.begin())
43                                 swap(*it, *(it - 1));
44                 }
45                 return tmp;
46         }
47 private:
48         ///
49         struct isEqual {
50                 isEqual(Share const & s) : p_(s) {}
51                 bool operator()(value_type const & p1) const {
52                         return *p1.get() == p_;
53                 }
54         private:
55                 Share const & p_;
56         };
57         ///
58         //struct comp {
59         //      int operator()(value_type const & p1,
60         //                     value_type const & p2) const {
61         //              return p1.use_count() < p2.use_count();
62         //      }
63         //};
64         ///
65         struct isUnique {
66                 bool operator()(value_type const & p) const {
67                         return p.unique();
68                 }
69         };
70         
71         ///
72         void clean() const {
73                 // Remove all unique items. (i.e. entries that only
74                 // exists in the conatainer and does not have a
75                 // corresponding paragrah.
76                 Params::iterator it = std::remove_if(params.begin(),
77                                                      params.end(),
78                                                      isUnique());
79                 params.erase(it, params.end());
80         }
81         
82         ///
83         mutable Params params;
84 };
85 #endif