]> git.lyx.org Git - lyx.git/blob - boost/boost/array.hpp
add include
[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 #else
57         // workaround for broken reverse_iterator implementations
58         typedef std::reverse_iterator<iterator,T> reverse_iterator;
59         typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
60 #endif
61
62         reverse_iterator rbegin() { return reverse_iterator(end()); }
63         const_reverse_iterator rbegin() const {
64             return const_reverse_iterator(end());
65         }
66         reverse_iterator rend() { return reverse_iterator(begin()); }
67         const_reverse_iterator rend() const {
68             return const_reverse_iterator(begin());
69         }
70
71         // operator[]
72         reference operator[](size_type i) { return elems[i]; }
73         const_reference operator[](size_type i) const { return elems[i]; }
74
75         // at() with range check
76         reference at(size_type i) { rangecheck(i); return elems[i]; }
77         const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
78     
79         // front() and back()
80         reference front() { return elems[0]; }
81         const_reference front() const { return elems[0]; }
82         reference back() { return elems[N-1]; }
83         const_reference back() const { return elems[N-1]; }
84
85         // size is constant
86         static size_type size() { return N; }
87         static bool empty() { return false; }
88         static size_type max_size() { return N; }
89         enum { static_size = N };
90
91         // swap (note: linear complexity)
92         void swap (array<T,N>& y) {
93             std::swap_ranges(begin(),end(),y.begin());
94         }
95
96         // direct access to data
97         const T* data() const { return elems; }
98
99         // assignment with type conversion
100         template <typename T2>
101         array<T,N>& operator= (const array<T2,N>& rhs) {
102             std::copy(rhs.begin(),rhs.end(), begin());
103             return *this;
104         }
105
106         // assign one value to all elements
107         void assign (const T& value)
108         {
109             std::fill_n(begin(),size(),value);
110         }
111
112 #ifndef BOOST_NO_PRIVATE_IN_AGGREGATE
113       private:
114 #endif
115         // check range (may be private because it is static)
116         static void rangecheck (size_type i) {
117             if (i >= size()) { throw std::range_error("array"); }
118         }
119
120     };
121
122     // comparisons
123     template<class T, std::size_t N>
124     bool operator== (const array<T,N>& x, const array<T,N>& y) {
125         return std::equal(x.begin(), x.end(), y.begin());
126     }
127     template<class T, std::size_t N>
128     bool operator< (const array<T,N>& x, const array<T,N>& y) {
129         return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
130     }
131     template<class T, std::size_t N>
132     bool operator!= (const array<T,N>& x, const array<T,N>& y) {
133         return !(x==y);
134     }
135     template<class T, std::size_t N>
136     bool operator> (const array<T,N>& x, const array<T,N>& y) {
137         return y<x;
138     }
139     template<class T, std::size_t N>
140     bool operator<= (const array<T,N>& x, const array<T,N>& y) {
141         return !(y<x);
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
148     // global swap()
149     template<class T, std::size_t N>
150     inline void swap (array<T,N>& x, array<T,N>& y) {
151         x.swap(y);
152     }
153
154 } /* namespace boost */
155
156 #endif /*BOOST_ARRAY_HPP*/