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>
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;
57 // workaround for broken reverse_iterator implementations
58 typedef std::reverse_iterator<iterator,T> reverse_iterator;
59 typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
62 reverse_iterator rbegin() { return reverse_iterator(end()); }
63 const_reverse_iterator rbegin() const {
64 return const_reverse_iterator(end());
66 reverse_iterator rend() { return reverse_iterator(begin()); }
67 const_reverse_iterator rend() const {
68 return const_reverse_iterator(begin());
72 reference operator[](size_type i) { return elems[i]; }
73 const_reference operator[](size_type i) const { return elems[i]; }
75 // at() with range check
76 reference at(size_type i) { rangecheck(i); return elems[i]; }
77 const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
80 reference front() { return elems[0]; }
81 const_reference front() const { return elems[0]; }
82 reference back() { return elems[N-1]; }
83 const_reference back() const { return elems[N-1]; }
86 static size_type size() { return N; }
87 static bool empty() { return false; }
88 static size_type max_size() { return N; }
89 enum { static_size = N };
91 // swap (note: linear complexity)
92 void swap (array<T,N>& y) {
93 std::swap_ranges(begin(),end(),y.begin());
96 // direct access to data
97 const T* data() const { return elems; }
99 // assignment with type conversion
100 template <typename T2>
101 array<T,N>& operator= (const array<T2,N>& rhs) {
102 std::copy(rhs.begin(),rhs.end(), begin());
106 // assign one value to all elements
107 void assign (const T& value)
109 std::fill_n(begin(),size(),value);
112 #ifndef BOOST_NO_PRIVATE_IN_AGGREGATE
115 // check range (may be private because it is static)
116 static void rangecheck (size_type i) {
117 if (i >= size()) { throw std::range_error("array"); }
123 template<class T, std::size_t N>
124 bool operator== (const array<T,N>& x, const array<T,N>& y) {
125 return std::equal(x.begin(), x.end(), y.begin());
127 template<class T, std::size_t N>
128 bool operator< (const array<T,N>& x, const array<T,N>& y) {
129 return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
131 template<class T, std::size_t N>
132 bool operator!= (const array<T,N>& x, const array<T,N>& y) {
135 template<class T, std::size_t N>
136 bool operator> (const array<T,N>& x, const array<T,N>& y) {
139 template<class T, std::size_t N>
140 bool operator<= (const array<T,N>& x, const array<T,N>& y) {
143 template<class T, std::size_t N>
144 bool operator>= (const array<T,N>& x, const array<T,N>& y) {
149 template<class T, std::size_t N>
150 inline void swap (array<T,N>& x, array<T,N>& y) {
155 // } /* namespace boost */
157 #endif /*BOOST_ARRAY_HPP*/