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 * 05 Aug 2001 - minor update (Nico Josuttis)
15 * 20 Jan 2001 - STLport fix (Beman Dawes)
16 * 29 Sep 2000 - Initial Revision (Nico Josuttis)
18 #ifndef BOOST_ARRAY_HPP
19 #define BOOST_ARRAY_HPP
26 // FIXES for broken compilers
27 #include <boost/config.hpp>
31 template<class T, std::size_t N>
34 T elems[N]; // fixed-size array of elements of type T
40 typedef const T* const_iterator;
42 typedef const T& const_reference;
43 typedef std::size_t size_type;
44 typedef std::ptrdiff_t difference_type;
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; }
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 #elif defined(BOOST_MSVC) && (BOOST_MSVC == 1300)
57 // workaround for broken reverse_iterator in VC7
58 typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
59 reference, iterator, reference> > reverse_iterator;
60 typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
61 const_reference, iterator, reference> > const_reverse_iterator;
63 // workaround for broken reverse_iterator implementations
64 typedef std::reverse_iterator<iterator,T> reverse_iterator;
65 typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
68 reverse_iterator rbegin() { return reverse_iterator(end()); }
69 const_reverse_iterator rbegin() const {
70 return const_reverse_iterator(end());
72 reverse_iterator rend() { return reverse_iterator(begin()); }
73 const_reverse_iterator rend() const {
74 return const_reverse_iterator(begin());
78 reference operator[](size_type i) { return elems[i]; }
79 const_reference operator[](size_type i) const { return elems[i]; }
81 // at() with range check
82 reference at(size_type i) { rangecheck(i); return elems[i]; }
83 const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
86 reference front() { return elems[0]; }
87 const_reference front() const { return elems[0]; }
88 reference back() { return elems[N-1]; }
89 const_reference back() const { return elems[N-1]; }
92 static size_type size() { return N; }
93 static bool empty() { return false; }
94 static size_type max_size() { return N; }
95 enum { static_size = N };
97 // swap (note: linear complexity)
98 void swap (array<T,N>& y) {
99 std::swap_ranges(begin(),end(),y.begin());
102 // direct access to data
103 const T* data() const { return elems; }
105 // assignment with type conversion
106 template <typename T2>
107 array<T,N>& operator= (const array<T2,N>& rhs) {
108 std::copy(rhs.begin(),rhs.end(), begin());
112 // assign one value to all elements
113 void assign (const T& value)
115 std::fill_n(begin(),size(),value);
118 #ifndef BOOST_NO_PRIVATE_IN_AGGREGATE
121 // check range (may be private because it is static)
122 static void rangecheck (size_type i) {
123 if (i >= size()) { throw std::range_error("array"); }
129 template<class T, std::size_t N>
130 bool operator== (const array<T,N>& x, const array<T,N>& y) {
131 return std::equal(x.begin(), x.end(), y.begin());
133 template<class T, std::size_t N>
134 bool operator< (const array<T,N>& x, const array<T,N>& y) {
135 return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
137 template<class T, std::size_t N>
138 bool operator!= (const array<T,N>& x, const array<T,N>& y) {
141 template<class T, std::size_t N>
142 bool operator> (const array<T,N>& x, const array<T,N>& y) {
145 template<class T, std::size_t N>
146 bool operator<= (const array<T,N>& x, const array<T,N>& y) {
149 template<class T, std::size_t N>
150 bool operator>= (const array<T,N>& x, const array<T,N>& y) {
155 template<class T, std::size_t N>
156 inline void swap (array<T,N>& x, array<T,N>& y) {
160 } /* namespace boost */
162 #endif /*BOOST_ARRAY_HPP*/