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 * (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.
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)
20 // See http://www.boost.org/libs/array for Documentation.
22 #ifndef BOOST_ARRAY_HPP
23 #define BOOST_ARRAY_HPP
28 // Handles broken standard libraries better than <iterator>
29 #include <boost/detail/iterator.hpp>
32 // FIXES for broken compilers
33 #include <boost/config.hpp>
37 template<class T, std::size_t N>
40 T elems[N]; // fixed-size array of elements of type T
46 typedef const T* const_iterator;
48 typedef const T& const_reference;
49 typedef std::size_t size_type;
50 typedef std::ptrdiff_t difference_type;
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; }
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;
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;
74 reverse_iterator rbegin() { return reverse_iterator(end()); }
75 const_reverse_iterator rbegin() const {
76 return const_reverse_iterator(end());
78 reverse_iterator rend() { return reverse_iterator(begin()); }
79 const_reverse_iterator rend() const {
80 return const_reverse_iterator(begin());
84 reference operator[](size_type i) { return elems[i]; }
85 const_reference operator[](size_type i) const { return elems[i]; }
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]; }
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]; }
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 };
103 // swap (note: linear complexity)
104 void swap (array<T,N>& y) {
105 std::swap_ranges(begin(),end(),y.begin());
108 // direct access to data
109 const T* data() const { return elems; }
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());
118 // assign one value to all elements
119 void assign (const T& value)
121 std::fill_n(begin(),size(),value);
124 #ifndef BOOST_NO_PRIVATE_IN_AGGREGATE
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"); }
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());
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());
143 template<class T, std::size_t N>
144 bool operator!= (const array<T,N>& x, const array<T,N>& y) {
147 template<class T, std::size_t N>
148 bool operator> (const array<T,N>& x, const array<T,N>& y) {
151 template<class T, std::size_t N>
152 bool operator<= (const array<T,N>& x, const array<T,N>& y) {
155 template<class T, std::size_t N>
156 bool operator>= (const array<T,N>& x, const array<T,N>& y) {
161 template<class T, std::size_t N>
162 inline void swap (array<T,N>& x, array<T,N>& y) {
166 } /* namespace boost */
168 #endif /*BOOST_ARRAY_HPP*/