]> git.lyx.org Git - lyx.git/blob - src/ShareContainer.h
no log...
[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 #if 0
25                 Params::iterator it = params.begin();
26                 Params::iterator end = params.end();
27                 for (; it != end; ++it) {
28                         if (ps == *(*it).get())
29                                 break;
30                 }
31 #else
32                 Params::iterator it = find_if(params.begin(),
33                                               params.end(),
34                                               isEqual(ps));
35 #endif
36                 value_type tmp;
37                 if (it == params.end()) {
38                         // ok we don't have it so we should
39                         // insert it.
40                         tmp.reset(new Share(ps));
41                         params.push_back(tmp);
42                         // We clean here. This can cause us to have
43                         // some (one) unique elemements some times
44                         // but we should gain a lot in speed.
45                         clean();
46                         std::sort(params.rbegin(), params.rend(), comp());
47                 } else {
48                         // yes we have it already
49                         tmp = *it;
50                 }
51                 return tmp;
52         }
53 private:
54         ///
55         struct isEqual {
56                 isEqual(Share const & s) : p_(s) {}
57                 bool operator()(value_type const & p1) const {
58                         return *p1.get() == p_;
59                 }
60         private:
61                 Share const & p_;
62         };
63         ///
64         struct comp {
65                 int operator()(value_type const & p1,
66                                value_type const & p2) const {
67                         return p1.use_count() < p2.use_count();
68                 }
69         };
70         ///
71         struct isUnique {
72                 bool operator()(value_type const & p) const {
73                         return p.unique();
74                 }
75         };
76
77         ///
78         void clean() const {
79                 // Remove all unique items. (i.e. entries that only
80                 // exists in the conatainer and does not have a
81                 // corresponding paragrah.
82                 Params::iterator it = std::remove_if(params.begin(),
83                                                      params.end(),
84                                                      isUnique());
85                 params.erase(it, params.end());
86         }
87         
88         ///
89         mutable Params params;
90 };
91 #endif