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