1 /* The following code declares class array,
2 * an STL container (as wrapper) for arrays of constant size.
5 * http://www.josuttis.com/cppcode
6 * for details and the latest version.
8 * http://www.boost.org/libs/array for Documentation.
11 * (C) Copyright Nicolai M. Josuttis 2001.
12 * Distributed under the Boost Software License, Version 1.0. (See
13 * accompanying file LICENSE_1_0.txt or copy at
14 * http://www.boost.org/LICENSE_1_0.txt)
16 * 29 Jan 2004 - c_array() added, BOOST_NO_PRIVATE_IN_AGGREGATE removed (Nico Josuttis)
17 * 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries.
18 * 05 Aug 2001 - minor update (Nico Josuttis)
19 * 20 Jan 2001 - STLport fix (Beman Dawes)
20 * 29 Sep 2000 - Initial Revision (Nico Josuttis)
24 #ifndef BOOST_ARRAY_HPP
25 #define BOOST_ARRAY_HPP
29 #include <boost/assert.hpp>
31 // Handles broken standard libraries better than <iterator>
32 #include <boost/detail/iterator.hpp>
35 // FIXES for broken compilers
36 #include <boost/config.hpp>
41 template<class T, std::size_t N>
44 T elems[N]; // fixed-size array of elements of type T
50 typedef const T* const_iterator;
52 typedef const T& const_reference;
53 typedef std::size_t size_type;
54 typedef std::ptrdiff_t difference_type;
57 iterator begin() { return elems; }
58 const_iterator begin() const { return elems; }
59 iterator end() { return elems+N; }
60 const_iterator end() const { return elems+N; }
62 // reverse iterator support
63 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
64 typedef std::reverse_iterator<iterator> reverse_iterator;
65 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
66 #elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
67 // workaround for broken reverse_iterator in VC7
68 typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
69 reference, iterator, reference> > reverse_iterator;
70 typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
71 const_reference, iterator, reference> > const_reverse_iterator;
73 // workaround for broken reverse_iterator implementations
74 typedef std::reverse_iterator<iterator,T> reverse_iterator;
75 typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
78 reverse_iterator rbegin() { return reverse_iterator(end()); }
79 const_reverse_iterator rbegin() const {
80 return const_reverse_iterator(end());
82 reverse_iterator rend() { return reverse_iterator(begin()); }
83 const_reverse_iterator rend() const {
84 return const_reverse_iterator(begin());
88 reference operator[](size_type i)
90 BOOST_ASSERT( i < N && "out of range" );
94 const_reference operator[](size_type i) const
96 BOOST_ASSERT( i < N && "out of range" );
100 // at() with range check
101 reference at(size_type i) { rangecheck(i); return elems[i]; }
102 const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
104 // front() and back()
110 const_reference front() const
120 const_reference back() const
126 static size_type size() { return N; }
127 static bool empty() { return false; }
128 static size_type max_size() { return N; }
129 enum { static_size = N };
131 // swap (note: linear complexity)
132 void swap (array<T,N>& y) {
133 std::swap_ranges(begin(),end(),y.begin());
136 // direct access to data (read-only)
137 const T* data() const { return elems; }
139 // use array as C array (direct read/write access to data)
140 T* c_array() { return elems; }
142 // assignment with type conversion
143 template <typename T2>
144 array<T,N>& operator= (const array<T2,N>& rhs) {
145 std::copy(rhs.begin(),rhs.end(), begin());
149 // assign one value to all elements
150 void assign (const T& value)
152 std::fill_n(begin(),size(),value);
155 // check range (may be private because it is static)
156 static void rangecheck (size_type i) {
158 throw std::range_error("array<>: index out of range");
165 template<class T, std::size_t N>
166 bool operator== (const array<T,N>& x, const array<T,N>& y) {
167 return std::equal(x.begin(), x.end(), y.begin());
169 template<class T, std::size_t N>
170 bool operator< (const array<T,N>& x, const array<T,N>& y) {
171 return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
173 template<class T, std::size_t N>
174 bool operator!= (const array<T,N>& x, const array<T,N>& y) {
177 template<class T, std::size_t N>
178 bool operator> (const array<T,N>& x, const array<T,N>& y) {
181 template<class T, std::size_t N>
182 bool operator<= (const array<T,N>& x, const array<T,N>& y) {
185 template<class T, std::size_t N>
186 bool operator>= (const array<T,N>& x, const array<T,N>& y) {
191 template<class T, std::size_t N>
192 inline void swap (array<T,N>& x, array<T,N>& y) {
196 } /* namespace boost */
198 #endif /*BOOST_ARRAY_HPP*/