]> git.lyx.org Git - lyx.git/blob - boost/boost/array.hpp
LFUN_ESCAPE gets ReadOnly (fix bug 1142)
[lyx.git] / boost / boost / array.hpp
1 /* The following code declares class array,
2  * an STL container (as wrapper) for arrays of constant size.
3  *
4  * See
5  *      http://www.josuttis.com/cppcode
6  * for details and the latest version.
7  *
8  * (C) Copyright Nicolai M. Josuttis 2001.
9  * Permission to copy, use, modify, sell and distribute this software
10  * is granted provided this copyright notice appears in all copies.
11  * This software is provided "as is" without express or implied
12  * warranty, and with no claim as to its suitability for any purpose.
13  *
14  * 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries.
15  * 05 Aug 2001 - minor update (Nico Josuttis)
16  * 20 Jan 2001 - STLport fix (Beman Dawes)
17  * 29 Sep 2000 - Initial Revision (Nico Josuttis)
18  */
19
20 // See http://www.boost.org/libs/array for Documentation.
21
22 #ifndef BOOST_ARRAY_HPP
23 #define BOOST_ARRAY_HPP
24
25 #include <cstddef>
26 #include <stdexcept>
27
28 // Handles broken standard libraries better than <iterator>
29 #include <boost/detail/iterator.hpp>
30 #include <algorithm>
31
32 // FIXES for broken compilers
33 #include <boost/config.hpp>
34
35 namespace boost {
36
37     template<class T, std::size_t N>
38     class array {
39       public:
40         T elems[N];    // fixed-size array of elements of type T
41
42       public:
43         // type definitions
44         typedef T              value_type;
45         typedef T*             iterator;
46         typedef const T*       const_iterator;
47         typedef T&             reference;
48         typedef const T&       const_reference;
49         typedef std::size_t    size_type;
50         typedef std::ptrdiff_t difference_type;
51     
52         // iterator support
53         iterator begin() { return elems; }
54         const_iterator begin() const { return elems; }
55         iterator end() { return elems+N; }
56         const_iterator end() const { return elems+N; }
57
58         // reverse iterator support
59 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
60         typedef std::reverse_iterator<iterator> reverse_iterator;
61         typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
62 #elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
63         // workaround for broken reverse_iterator in VC7
64         typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
65                                       reference, iterator, reference> > reverse_iterator;
66         typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
67                                       const_reference, iterator, reference> > const_reverse_iterator;
68 #else
69         // workaround for broken reverse_iterator implementations
70         typedef std::reverse_iterator<iterator,T> reverse_iterator;
71         typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
72 #endif
73
74         reverse_iterator rbegin() { return reverse_iterator(end()); }
75         const_reverse_iterator rbegin() const {
76             return const_reverse_iterator(end());
77         }
78         reverse_iterator rend() { return reverse_iterator(begin()); }
79         const_reverse_iterator rend() const {
80             return const_reverse_iterator(begin());
81         }
82
83         // operator[]
84         reference operator[](size_type i) { return elems[i]; }
85         const_reference operator[](size_type i) const { return elems[i]; }
86
87         // at() with range check
88         reference at(size_type i) { rangecheck(i); return elems[i]; }
89         const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
90     
91         // front() and back()
92         reference front() { return elems[0]; }
93         const_reference front() const { return elems[0]; }
94         reference back() { return elems[N-1]; }
95         const_reference back() const { return elems[N-1]; }
96
97         // size is constant
98         static size_type size() { return N; }
99         static bool empty() { return false; }
100         static size_type max_size() { return N; }
101         enum { static_size = N };
102
103         // swap (note: linear complexity)
104         void swap (array<T,N>& y) {
105             std::swap_ranges(begin(),end(),y.begin());
106         }
107
108         // direct access to data
109         const T* data() const { return elems; }
110
111         // assignment with type conversion
112         template <typename T2>
113         array<T,N>& operator= (const array<T2,N>& rhs) {
114             std::copy(rhs.begin(),rhs.end(), begin());
115             return *this;
116         }
117
118         // assign one value to all elements
119         void assign (const T& value)
120         {
121             std::fill_n(begin(),size(),value);
122         }
123
124 #ifndef BOOST_NO_PRIVATE_IN_AGGREGATE
125       private:
126 #endif
127         // check range (may be private because it is static)
128         static void rangecheck (size_type i) {
129             if (i >= size()) { throw std::range_error("array"); }
130         }
131
132     };
133
134     // comparisons
135     template<class T, std::size_t N>
136     bool operator== (const array<T,N>& x, const array<T,N>& y) {
137         return std::equal(x.begin(), x.end(), y.begin());
138     }
139     template<class T, std::size_t N>
140     bool operator< (const array<T,N>& x, const array<T,N>& y) {
141         return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
142     }
143     template<class T, std::size_t N>
144     bool operator!= (const array<T,N>& x, const array<T,N>& y) {
145         return !(x==y);
146     }
147     template<class T, std::size_t N>
148     bool operator> (const array<T,N>& x, const array<T,N>& y) {
149         return y<x;
150     }
151     template<class T, std::size_t N>
152     bool operator<= (const array<T,N>& x, const array<T,N>& y) {
153         return !(y<x);
154     }
155     template<class T, std::size_t N>
156     bool operator>= (const array<T,N>& x, const array<T,N>& y) {
157         return !(x<y);
158     }
159
160     // global swap()
161     template<class T, std::size_t N>
162     inline void swap (array<T,N>& x, array<T,N>& y) {
163         x.swap(y);
164     }
165
166 } /* namespace boost */
167
168 #endif /*BOOST_ARRAY_HPP*/
169
170
171
172
173
174
175