]> git.lyx.org Git - lyx.git/blob - boost/boost/array.hpp
leave formula when pressing end at the end of a formula (and similarly
[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  * 05 Aug 2001 - minor update (Nico Josuttis)
15  * 20 Jan 2001 - STLport fix (Beman Dawes)
16  * 29 Sep 2000 - Initial Revision (Nico Josuttis)
17  */
18 #ifndef BOOST_ARRAY_HPP
19 #define BOOST_ARRAY_HPP
20
21 #include <cstddef>
22 #include <stdexcept>
23 #include <iterator>
24 #include <algorithm>
25
26 // FIXES for broken compilers
27 #include <boost/config.hpp>
28
29 namespace boost {
30
31     template<class T, std::size_t N>
32     class array {
33       public:
34         T elems[N];    // fixed-size array of elements of type T
35
36       public:
37         // type definitions
38         typedef T              value_type;
39         typedef T*             iterator;
40         typedef const T*       const_iterator;
41         typedef T&             reference;
42         typedef const T&       const_reference;
43         typedef std::size_t    size_type;
44         typedef std::ptrdiff_t difference_type;
45     
46         // iterator support
47         iterator begin() { return elems; }
48         const_iterator begin() const { return elems; }
49         iterator end() { return elems+N; }
50         const_iterator end() const { return elems+N; }
51
52         // reverse iterator support
53 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR)
54         typedef std::reverse_iterator<iterator> reverse_iterator;
55         typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
56 #elif defined(BOOST_MSVC) && (BOOST_MSVC == 1300)
57         // workaround for broken reverse_iterator in VC7
58         typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
59                                       reference, iterator, reference> > reverse_iterator;
60         typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
61                                                 const_reference, iterator, reference> > const_reverse_iterator;
62 #else
63         // workaround for broken reverse_iterator implementations
64         typedef std::reverse_iterator<iterator,T> reverse_iterator;
65         typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
66 #endif
67
68         reverse_iterator rbegin() { return reverse_iterator(end()); }
69         const_reverse_iterator rbegin() const {
70             return const_reverse_iterator(end());
71         }
72         reverse_iterator rend() { return reverse_iterator(begin()); }
73         const_reverse_iterator rend() const {
74             return const_reverse_iterator(begin());
75         }
76
77         // operator[]
78         reference operator[](size_type i) { return elems[i]; }
79         const_reference operator[](size_type i) const { return elems[i]; }
80
81         // at() with range check
82         reference at(size_type i) { rangecheck(i); return elems[i]; }
83         const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
84     
85         // front() and back()
86         reference front() { return elems[0]; }
87         const_reference front() const { return elems[0]; }
88         reference back() { return elems[N-1]; }
89         const_reference back() const { return elems[N-1]; }
90
91         // size is constant
92         static size_type size() { return N; }
93         static bool empty() { return false; }
94         static size_type max_size() { return N; }
95         enum { static_size = N };
96
97         // swap (note: linear complexity)
98         void swap (array<T,N>& y) {
99             std::swap_ranges(begin(),end(),y.begin());
100         }
101
102         // direct access to data
103         const T* data() const { return elems; }
104
105         // assignment with type conversion
106         template <typename T2>
107         array<T,N>& operator= (const array<T2,N>& rhs) {
108             std::copy(rhs.begin(),rhs.end(), begin());
109             return *this;
110         }
111
112         // assign one value to all elements
113         void assign (const T& value)
114         {
115             std::fill_n(begin(),size(),value);
116         }
117
118 #ifndef BOOST_NO_PRIVATE_IN_AGGREGATE
119       private:
120 #endif
121         // check range (may be private because it is static)
122         static void rangecheck (size_type i) {
123             if (i >= size()) { throw std::range_error("array"); }
124         }
125
126     };
127
128     // comparisons
129     template<class T, std::size_t N>
130     bool operator== (const array<T,N>& x, const array<T,N>& y) {
131         return std::equal(x.begin(), x.end(), y.begin());
132     }
133     template<class T, std::size_t N>
134     bool operator< (const array<T,N>& x, const array<T,N>& y) {
135         return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
136     }
137     template<class T, std::size_t N>
138     bool operator!= (const array<T,N>& x, const array<T,N>& y) {
139         return !(x==y);
140     }
141     template<class T, std::size_t N>
142     bool operator> (const array<T,N>& x, const array<T,N>& y) {
143         return y<x;
144     }
145     template<class T, std::size_t N>
146     bool operator<= (const array<T,N>& x, const array<T,N>& y) {
147         return !(y<x);
148     }
149     template<class T, std::size_t N>
150     bool operator>= (const array<T,N>& x, const array<T,N>& y) {
151         return !(x<y);
152     }
153
154     // global swap()
155     template<class T, std::size_t N>
156     inline void swap (array<T,N>& x, array<T,N>& y) {
157         x.swap(y);
158     }
159
160 } /* namespace boost */
161
162 #endif /*BOOST_ARRAY_HPP*/
163