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