]> git.lyx.org Git - lyx.git/blob - src/support/RandomAccessList.h
* output_latex.h:
[lyx.git] / src / support / RandomAccessList.h
1 // -*- C++ -*-
2 /**
3  * \file RandomAccessList.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  */
12
13 #ifndef RANDOM_ACESS_LIST_H
14 #define RANDOM_ACESS_LIST_H
15
16 //#include "debug.h"
17
18 #include <boost/utility.hpp>
19
20 #include <vector>
21 #include <list>
22 #include <algorithm>
23
24 #define USE_OLD_ITERATOR 1
25
26 /// Random Access List.
27 /**
28 This templatized class provide a std::vector like interface to a
29 standard std::list underneath. An important property is that it
30 keeps the std::list::iterator interface. A typical use would be:
31
32         typedef RandomAccessList<some_class> MyContainer;
33
34 Then you can use MyContainer as if it was a standard 
35 std::vector<some_class> for operator[] access and as if it was a
36 standard std::list for iterator access. The main difference with
37 std::vector is that insertion of elements is much less costly. Compared
38 to a standard list alone, there is of course a small overhead because
39 the class always keeps its internal vector of iterator (it_vector_) up
40 to date.
41 */
42 template <class T>
43 class RandomAccessList {
44 public:
45         // types
46         typedef std::list<T> Container;
47         typedef typename Container::reference reference;
48         typedef typename Container::const_reference const_reference;
49 #if USE_OLD_ITERATOR
50         // iterator (below)
51         typedef typename Container::iterator iterator;
52         // const_iterator (below)
53         typedef typename Container::const_iterator const_iterator;
54 #else
55         // wip
56 #endif
57         typedef typename Container::size_type size_type;
58         typedef typename Container::difference_type difference_type;
59         typedef typename Container::value_type value_type;
60         typedef typename Container::allocator_type allocator_type;
61         typedef typename Container::pointer pointer;
62         typedef typename Container::const_pointer const_pointer;
63         // reverse_iterator
64         // const_reverse_iterator
65
66         typedef std::vector<typename Container::iterator> IterCont;
67
68         // construct/copy/destroy
69
70         RandomAccessList()
71         {}
72
73         // RandomAccessList(size_type n T const & value = T())
74
75         template<class InputIterator>
76         RandomAccessList(InputIterator first, InputIterator last)
77         {
78                 assign(first, last);
79         }
80
81
82
83         RandomAccessList(RandomAccessList const & x)
84         {
85                 assign(x.begin(), x.end());
86         }
87
88         // ~RandomAccessList()
89
90         ///
91         RandomAccessList & operator=(RandomAccessList const & x)
92         {
93                 assign(x.begin(), x.end());
94                 return *this;
95         }
96
97         template<class InputIterator>
98         void assign(InputIterator first, InputIterator last)
99         {
100                 container_.assign(first, last);
101                 recreateVector();
102         }
103
104
105         // void assign(size_type n, T const & u);
106
107         // iterators
108
109         iterator begin()
110         {
111                 return container_.begin();
112         }
113
114         const_iterator begin() const
115         {
116                 return container_.begin();
117         }
118
119         iterator end()
120         {
121                 return container_.end();
122         }
123
124         const_iterator end() const
125         {
126                 return container_.end();
127         }
128
129         // reverse_iterator rbegin();
130         // const_reverse_iterator rbegin() const;
131         // reverse_iterator rend();
132         // const_reverse_iterator rend() const;
133
134         // capacity
135         size_type size() const
136         {
137                 return iterCont_.size();
138         }
139
140         size_type max_size() const
141         {
142                 return iterCont_.max_size();
143         }
144
145         // void resize(size_type sz,  T c = T());
146
147         size_type capacity() const
148         {
149                 return iterCont_.capacity();
150         }
151
152         bool empty() const
153         {
154                 return container_.empty();
155         }
156
157         // void reserve(size_type n);
158
159         // element access
160
161         reference operator[](size_type pos)
162         {
163                 return *iterCont_[pos];
164         }
165
166         ///
167         const_reference operator[](size_type pos) const
168         {
169                 return *iterCont_[pos];
170         }
171
172         reference at(size_type pos)
173         {
174                 return *iterCont_.at(pos);
175         }
176
177         const_reference at(size_type pos) const
178         {
179                 return *iterCont_.at(pos);
180         }
181
182         reference front()
183         {
184                 return container_.front();
185         }
186
187         const_reference front() const
188         {
189                 return container_.front();
190         }
191
192         reference back()
193         {
194                 return container_.back();
195         }
196
197         const_reference back() const
198         {
199                 return container_.back();
200         }
201
202         // modifiers
203
204         void push_back(T const & x)
205         {
206                 typename Container::iterator it =
207                         container_.insert(container_.end(), x);
208                 iterCont_.push_back(it);
209         }
210
211         void pop_back()
212         {
213                 container_.pop_back();
214                 iterCont_.pop_back();
215         }
216
217         iterator insert(iterator position, T const & x)
218         {
219                 typename Container::iterator it =
220                         container_.insert(position, x);
221                 recreateVector();
222                 return it;
223         }
224
225         // void insert(iterator position, size_type n, T const & x);
226
227         template<class InputIterator>
228         void insert(iterator position,
229                     InputIterator first, InputIterator last)
230         {
231                 container_.insert(position, first, last);
232                 recreateVector();
233         }
234
235         iterator erase(iterator position)
236         {
237                 typename Container::iterator it =
238                         container_.erase(position);
239                 recreateVector();
240                 return it;
241         }
242
243         iterator erase(iterator first, iterator last)
244         {
245                 typename Container::iterator it =
246                         container_.erase(first, last);
247                 recreateVector();
248                 return it;
249         }
250
251         void swap(RandomAccessList & x)
252         {
253                 std::swap(container_, x.container_);
254                 std::swap(iterCont_, x.iterCont_);
255         }
256
257         void clear()
258         {
259                 container_.clear();
260                 iterCont_.clear();
261         }
262
263 private:
264         void recreateVector()
265         {
266                 iterCont_.clear();
267                 typename Container::iterator beg = container_.begin();
268                 typename Container::iterator end = container_.end();
269                 for (; beg != end; ++beg)
270                         iterCont_.push_back(beg);
271         }
272
273         /// Our container.
274         Container container_;
275         /// Our container of iterators.
276         IterCont iterCont_;
277 };
278
279 #endif