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)
19 #ifndef BOOST_ARRAY_HPP
20 #define BOOST_ARRAY_HPP
25 // Handles broken standard libraries better than <iterator>
26 #include <boost/detail/iterator.hpp>
29 // FIXES for broken compilers
30 #include <boost/config.hpp>
34 template<class T, std::size_t N>
37 T elems[N]; // fixed-size array of elements of type T
43 typedef const T* const_iterator;
45 typedef const T& const_reference;
46 typedef std::size_t size_type;
47 typedef std::ptrdiff_t difference_type;
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; }
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;
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;
71 reverse_iterator rbegin() { return reverse_iterator(end()); }
72 const_reverse_iterator rbegin() const {
73 return const_reverse_iterator(end());
75 reverse_iterator rend() { return reverse_iterator(begin()); }
76 const_reverse_iterator rend() const {
77 return const_reverse_iterator(begin());
81 reference operator[](size_type i) { return elems[i]; }
82 const_reference operator[](size_type i) const { return elems[i]; }
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]; }
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]; }
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 };
100 // swap (note: linear complexity)
101 void swap (array<T,N>& y) {
102 std::swap_ranges(begin(),end(),y.begin());
105 // direct access to data
106 const T* data() const { return elems; }
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());
115 // assign one value to all elements
116 void assign (const T& value)
118 std::fill_n(begin(),size(),value);
121 #ifndef BOOST_NO_PRIVATE_IN_AGGREGATE
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"); }
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());
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());
140 template<class T, std::size_t N>
141 bool operator!= (const array<T,N>& x, const array<T,N>& y) {
144 template<class T, std::size_t N>
145 bool operator> (const array<T,N>& x, const array<T,N>& y) {
148 template<class T, std::size_t N>
149 bool operator<= (const array<T,N>& x, const array<T,N>& y) {
152 template<class T, std::size_t N>
153 bool operator>= (const array<T,N>& x, const array<T,N>& y) {
158 template<class T, std::size_t N>
159 inline void swap (array<T,N>& x, array<T,N>& y) {
163 } /* namespace boost */
165 #endif /*BOOST_ARRAY_HPP*/