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 1999.
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 * 20 Jan 2001 - STLport fix (Beman Dawes)
15 * 29 Sep 2000 - Initial Revision (Nico Josuttis)
17 #ifndef BOOST_ARRAY_HPP
18 #define BOOST_ARRAY_HPP
25 // FIXES for broken compilers
26 #include <boost/config.hpp>
30 template<class T, std::size_t N>
33 T elems[N]; // fixed-size array of elements of type T
39 typedef const T* const_iterator;
41 typedef const T& const_reference;
42 typedef std::size_t size_type;
43 typedef std::ptrdiff_t difference_type;
46 iterator begin() { return elems; }
47 const_iterator begin() const { return elems; }
48 iterator end() { return elems+N; }
49 const_iterator end() const { return elems+N; }
51 // reverse iterator support
52 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR)
53 typedef std::reverse_iterator<iterator> reverse_iterator;
54 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
56 // workaround for broken reverse_iterator implementations
57 typedef std::reverse_iterator<iterator,T> reverse_iterator;
58 typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
61 reverse_iterator rbegin() { return reverse_iterator(end()); }
62 const_reverse_iterator rbegin() const {
63 return const_reverse_iterator(end());
65 reverse_iterator rend() { return reverse_iterator(begin()); }
66 const_reverse_iterator rend() const {
67 return const_reverse_iterator(begin());
71 reference operator[](size_type i) { return elems[i]; }
72 const_reference operator[](size_type i) const { return elems[i]; }
74 // at() with range check
75 reference at(size_type i) { rangecheck(i); return elems[i]; }
76 const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
79 reference front() { return elems[0]; }
80 const_reference front() const { return elems[0]; }
81 reference back() { return elems[N-1]; }
82 const_reference back() const { return elems[N-1]; }
85 static size_type size() { return N; }
86 static bool empty() { return false; }
87 static size_type max_size() { return N; }
88 enum { static_size = N };
90 // swap (note: linear complexity)
91 void swap (array<T,N>& y) {
92 std::swap_ranges(begin(),end(),y.begin());
95 // direct access to data
96 const T* data() const { return elems; }
98 // assignment with type conversion
99 template <typename T2>
100 array<T,N>& operator= (const array<T2,N>& rhs) {
101 std::copy(rhs.begin(),rhs.end(), begin());
105 // assign one value to all elements
106 void assign (const T& value)
108 std::fill_n(begin(),size(),value);
111 #ifndef BOOST_NO_PRIVATE_IN_AGGREGATE
114 // check range (may be private because it is static)
115 static void rangecheck (size_type i) {
116 if (i >= size()) { throw std::range_error("array"); }
122 template<class T, std::size_t N>
123 bool operator== (const array<T,N>& x, const array<T,N>& y) {
124 return std::equal(x.begin(), x.end(), y.begin());
126 template<class T, std::size_t N>
127 bool operator< (const array<T,N>& x, const array<T,N>& y) {
128 return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
130 template<class T, std::size_t N>
131 bool operator!= (const array<T,N>& x, const array<T,N>& y) {
134 template<class T, std::size_t N>
135 bool operator> (const array<T,N>& x, const array<T,N>& y) {
138 template<class T, std::size_t N>
139 bool operator<= (const array<T,N>& x, const array<T,N>& y) {
142 template<class T, std::size_t N>
143 bool operator>= (const array<T,N>& x, const array<T,N>& y) {
148 template<class T, std::size_t N>
149 inline void swap (array<T,N>& x, array<T,N>& y) {
153 } /* namespace boost */
155 #endif /*BOOST_ARRAY_HPP*/