2000-10-02 00:55:02 +00:00
|
|
|
/* The following code declares class array,
|
|
|
|
* an STL container (as wrapper) for arrays of constant size.
|
|
|
|
*
|
|
|
|
* See
|
2006-09-16 16:39:04 +00:00
|
|
|
* http://www.boost.org/libs/array/
|
2004-11-20 09:08:45 +00:00
|
|
|
* for documentation.
|
2000-10-02 00:55:02 +00:00
|
|
|
*
|
2006-09-16 16:39:04 +00:00
|
|
|
* The original author site is at: http://www.josuttis.com/
|
|
|
|
*
|
2001-08-29 22:33:34 +00:00
|
|
|
* (C) Copyright Nicolai M. Josuttis 2001.
|
2006-09-16 16:39:04 +00:00
|
|
|
*
|
2004-11-20 09:08:45 +00:00
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See
|
|
|
|
* accompanying file LICENSE_1_0.txt or copy at
|
|
|
|
* http://www.boost.org/LICENSE_1_0.txt)
|
2000-10-02 00:55:02 +00:00
|
|
|
*
|
2004-11-20 09:08:45 +00:00
|
|
|
* 29 Jan 2004 - c_array() added, BOOST_NO_PRIVATE_IN_AGGREGATE removed (Nico Josuttis)
|
2002-10-15 17:51:41 +00:00
|
|
|
* 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries.
|
2001-08-29 22:33:34 +00:00
|
|
|
* 05 Aug 2001 - minor update (Nico Josuttis)
|
2001-02-12 14:09:09 +00:00
|
|
|
* 20 Jan 2001 - STLport fix (Beman Dawes)
|
|
|
|
* 29 Sep 2000 - Initial Revision (Nico Josuttis)
|
2004-11-20 09:08:45 +00:00
|
|
|
*
|
|
|
|
* Jan 29, 2004
|
2000-10-02 00:55:02 +00:00
|
|
|
*/
|
|
|
|
#ifndef BOOST_ARRAY_HPP
|
|
|
|
#define BOOST_ARRAY_HPP
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <stdexcept>
|
2004-11-20 09:08:45 +00:00
|
|
|
#include <boost/assert.hpp>
|
2002-10-15 17:51:41 +00:00
|
|
|
|
|
|
|
// Handles broken standard libraries better than <iterator>
|
|
|
|
#include <boost/detail/iterator.hpp>
|
2006-09-05 07:18:36 +00:00
|
|
|
#include <boost/throw_exception.hpp>
|
2000-10-02 00:55:02 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2001-02-12 14:09:09 +00:00
|
|
|
// FIXES for broken compilers
|
2000-10-02 00:55:02 +00:00
|
|
|
#include <boost/config.hpp>
|
|
|
|
|
2004-11-20 09:08:45 +00:00
|
|
|
|
2001-04-17 13:40:18 +00:00
|
|
|
namespace boost {
|
2000-10-02 00:55:02 +00:00
|
|
|
|
|
|
|
template<class T, std::size_t N>
|
|
|
|
class array {
|
|
|
|
public:
|
|
|
|
T elems[N]; // fixed-size array of elements of type T
|
|
|
|
|
|
|
|
public:
|
|
|
|
// type definitions
|
|
|
|
typedef T value_type;
|
|
|
|
typedef T* iterator;
|
|
|
|
typedef const T* const_iterator;
|
|
|
|
typedef T& reference;
|
|
|
|
typedef const T& const_reference;
|
|
|
|
typedef std::size_t size_type;
|
|
|
|
typedef std::ptrdiff_t difference_type;
|
2006-09-05 07:18:36 +00:00
|
|
|
|
2000-10-02 00:55:02 +00:00
|
|
|
// iterator support
|
|
|
|
iterator begin() { return elems; }
|
|
|
|
const_iterator begin() const { return elems; }
|
|
|
|
iterator end() { return elems+N; }
|
|
|
|
const_iterator end() const { return elems+N; }
|
|
|
|
|
|
|
|
// reverse iterator support
|
2002-10-15 17:51:41 +00:00
|
|
|
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
|
2000-10-02 00:55:02 +00:00
|
|
|
typedef std::reverse_iterator<iterator> reverse_iterator;
|
|
|
|
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
2003-03-03 15:53:48 +00:00
|
|
|
#elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
|
2002-05-21 23:39:58 +00:00
|
|
|
// workaround for broken reverse_iterator in VC7
|
|
|
|
typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
|
|
|
|
reference, iterator, reference> > reverse_iterator;
|
|
|
|
typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
|
2002-10-15 17:51:41 +00:00
|
|
|
const_reference, iterator, reference> > const_reverse_iterator;
|
2001-02-12 14:09:09 +00:00
|
|
|
#else
|
|
|
|
// workaround for broken reverse_iterator implementations
|
2000-10-02 00:55:02 +00:00
|
|
|
typedef std::reverse_iterator<iterator,T> reverse_iterator;
|
|
|
|
typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
|
2001-02-12 14:09:09 +00:00
|
|
|
#endif
|
2000-10-02 00:55:02 +00:00
|
|
|
|
|
|
|
reverse_iterator rbegin() { return reverse_iterator(end()); }
|
|
|
|
const_reverse_iterator rbegin() const {
|
|
|
|
return const_reverse_iterator(end());
|
|
|
|
}
|
|
|
|
reverse_iterator rend() { return reverse_iterator(begin()); }
|
|
|
|
const_reverse_iterator rend() const {
|
|
|
|
return const_reverse_iterator(begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
// operator[]
|
2004-11-20 09:08:45 +00:00
|
|
|
reference operator[](size_type i)
|
|
|
|
{
|
|
|
|
BOOST_ASSERT( i < N && "out of range" );
|
|
|
|
return elems[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
const_reference operator[](size_type i) const
|
|
|
|
{
|
|
|
|
BOOST_ASSERT( i < N && "out of range" );
|
|
|
|
return elems[i];
|
|
|
|
}
|
2000-10-02 00:55:02 +00:00
|
|
|
|
|
|
|
// at() with range check
|
|
|
|
reference at(size_type i) { rangecheck(i); return elems[i]; }
|
|
|
|
const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
|
|
|
|
|
|
|
|
// front() and back()
|
2004-11-20 09:08:45 +00:00
|
|
|
reference front()
|
|
|
|
{
|
|
|
|
return elems[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
const_reference front() const
|
|
|
|
{
|
|
|
|
return elems[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
reference back()
|
|
|
|
{
|
|
|
|
return elems[N-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
const_reference back() const
|
|
|
|
{
|
|
|
|
return elems[N-1];
|
|
|
|
}
|
2000-10-02 00:55:02 +00:00
|
|
|
|
|
|
|
// size is constant
|
|
|
|
static size_type size() { return N; }
|
|
|
|
static bool empty() { return false; }
|
|
|
|
static size_type max_size() { return N; }
|
|
|
|
enum { static_size = N };
|
|
|
|
|
|
|
|
// swap (note: linear complexity)
|
|
|
|
void swap (array<T,N>& y) {
|
|
|
|
std::swap_ranges(begin(),end(),y.begin());
|
|
|
|
}
|
|
|
|
|
2004-11-20 09:08:45 +00:00
|
|
|
// direct access to data (read-only)
|
2000-10-02 00:55:02 +00:00
|
|
|
const T* data() const { return elems; }
|
2006-09-05 07:18:36 +00:00
|
|
|
T* data() { return elems; }
|
2000-10-02 00:55:02 +00:00
|
|
|
|
2004-11-20 09:08:45 +00:00
|
|
|
// use array as C array (direct read/write access to data)
|
|
|
|
T* c_array() { return elems; }
|
|
|
|
|
2000-10-02 00:55:02 +00:00
|
|
|
// assignment with type conversion
|
|
|
|
template <typename T2>
|
|
|
|
array<T,N>& operator= (const array<T2,N>& rhs) {
|
|
|
|
std::copy(rhs.begin(),rhs.end(), begin());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign one value to all elements
|
|
|
|
void assign (const T& value)
|
|
|
|
{
|
|
|
|
std::fill_n(begin(),size(),value);
|
|
|
|
}
|
|
|
|
|
2001-02-12 14:09:09 +00:00
|
|
|
// check range (may be private because it is static)
|
2000-10-02 00:55:02 +00:00
|
|
|
static void rangecheck (size_type i) {
|
2006-09-05 07:18:36 +00:00
|
|
|
if (i >= size()) {
|
2004-11-20 09:08:45 +00:00
|
|
|
throw std::range_error("array<>: index out of range");
|
|
|
|
}
|
2000-10-02 00:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2006-09-05 07:18:36 +00:00
|
|
|
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
|
|
|
|
template< class T >
|
|
|
|
class array< T, 0 > {
|
|
|
|
|
|
|
|
public:
|
|
|
|
// type definitions
|
|
|
|
typedef T value_type;
|
|
|
|
typedef T* iterator;
|
|
|
|
typedef const T* const_iterator;
|
|
|
|
typedef T& reference;
|
|
|
|
typedef const T& const_reference;
|
|
|
|
typedef std::size_t size_type;
|
|
|
|
typedef std::ptrdiff_t difference_type;
|
|
|
|
|
|
|
|
// iterator support
|
|
|
|
iterator begin() { return iterator( reinterpret_cast< T * >( this ) ); }
|
|
|
|
const_iterator begin() const { return const_iterator( reinterpret_cast< const T * >( this ) ); }
|
|
|
|
iterator end() { return begin(); }
|
|
|
|
const_iterator end() const { return begin(); }
|
|
|
|
|
|
|
|
// reverse iterator support
|
|
|
|
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
|
|
|
|
typedef std::reverse_iterator<iterator> reverse_iterator;
|
|
|
|
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
|
|
|
#elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
|
|
|
|
// workaround for broken reverse_iterator in VC7
|
|
|
|
typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
|
|
|
|
reference, iterator, reference> > reverse_iterator;
|
|
|
|
typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
|
|
|
|
const_reference, iterator, reference> > const_reverse_iterator;
|
|
|
|
#else
|
|
|
|
// workaround for broken reverse_iterator implementations
|
|
|
|
typedef std::reverse_iterator<iterator,T> reverse_iterator;
|
|
|
|
typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
reverse_iterator rbegin() { return reverse_iterator(end()); }
|
|
|
|
const_reverse_iterator rbegin() const {
|
|
|
|
return const_reverse_iterator(end());
|
|
|
|
}
|
|
|
|
reverse_iterator rend() { return reverse_iterator(begin()); }
|
|
|
|
const_reverse_iterator rend() const {
|
|
|
|
return const_reverse_iterator(begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
// operator[]
|
|
|
|
reference operator[](size_type i)
|
|
|
|
{
|
|
|
|
return failed_rangecheck();
|
|
|
|
}
|
|
|
|
|
|
|
|
const_reference operator[](size_type i) const
|
|
|
|
{
|
|
|
|
return failed_rangecheck();
|
|
|
|
}
|
|
|
|
|
|
|
|
// at() with range check
|
|
|
|
reference at(size_type i) { return failed_rangecheck(); }
|
|
|
|
const_reference at(size_type i) const { return failed_rangecheck(); }
|
|
|
|
|
|
|
|
// front() and back()
|
|
|
|
reference front()
|
|
|
|
{
|
|
|
|
return failed_rangecheck();
|
|
|
|
}
|
|
|
|
|
|
|
|
const_reference front() const
|
|
|
|
{
|
|
|
|
return failed_rangecheck();
|
|
|
|
}
|
|
|
|
|
|
|
|
reference back()
|
|
|
|
{
|
|
|
|
return failed_rangecheck();
|
|
|
|
}
|
|
|
|
|
|
|
|
const_reference back() const
|
|
|
|
{
|
|
|
|
return failed_rangecheck();
|
|
|
|
}
|
|
|
|
|
|
|
|
// size is constant
|
|
|
|
static size_type size() { return 0; }
|
|
|
|
static bool empty() { return true; }
|
|
|
|
static size_type max_size() { return 0; }
|
|
|
|
enum { static_size = 0 };
|
|
|
|
|
|
|
|
void swap (array<T,0>& y) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// direct access to data (read-only)
|
|
|
|
const T* data() const { return 0; }
|
|
|
|
T* data() { return 0; }
|
|
|
|
|
|
|
|
// use array as C array (direct read/write access to data)
|
|
|
|
T* c_array() { return 0; }
|
|
|
|
|
|
|
|
// assignment with type conversion
|
|
|
|
template <typename T2>
|
|
|
|
array<T,0>& operator= (const array<T2,0>& ) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign one value to all elements
|
|
|
|
void assign (const T& ) { }
|
|
|
|
|
|
|
|
// check range (may be private because it is static)
|
|
|
|
static reference failed_rangecheck () {
|
|
|
|
std::range_error e("attempt to access element of an empty array");
|
|
|
|
boost::throw_exception(e);
|
|
|
|
//
|
|
|
|
// We need to return something here to keep
|
|
|
|
// some compilers happy: however we will never
|
|
|
|
// actually get here....
|
|
|
|
//
|
|
|
|
static T placeholder;
|
|
|
|
return placeholder;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2000-10-02 00:55:02 +00:00
|
|
|
// comparisons
|
|
|
|
template<class T, std::size_t N>
|
|
|
|
bool operator== (const array<T,N>& x, const array<T,N>& y) {
|
|
|
|
return std::equal(x.begin(), x.end(), y.begin());
|
|
|
|
}
|
|
|
|
template<class T, std::size_t N>
|
|
|
|
bool operator< (const array<T,N>& x, const array<T,N>& y) {
|
|
|
|
return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
|
|
|
|
}
|
|
|
|
template<class T, std::size_t N>
|
|
|
|
bool operator!= (const array<T,N>& x, const array<T,N>& y) {
|
|
|
|
return !(x==y);
|
|
|
|
}
|
|
|
|
template<class T, std::size_t N>
|
|
|
|
bool operator> (const array<T,N>& x, const array<T,N>& y) {
|
|
|
|
return y<x;
|
|
|
|
}
|
|
|
|
template<class T, std::size_t N>
|
|
|
|
bool operator<= (const array<T,N>& x, const array<T,N>& y) {
|
|
|
|
return !(y<x);
|
|
|
|
}
|
|
|
|
template<class T, std::size_t N>
|
|
|
|
bool operator>= (const array<T,N>& x, const array<T,N>& y) {
|
|
|
|
return !(x<y);
|
|
|
|
}
|
|
|
|
|
|
|
|
// global swap()
|
|
|
|
template<class T, std::size_t N>
|
|
|
|
inline void swap (array<T,N>& x, array<T,N>& y) {
|
|
|
|
x.swap(y);
|
|
|
|
}
|
|
|
|
|
2001-04-17 13:40:18 +00:00
|
|
|
} /* namespace boost */
|
2000-10-02 00:55:02 +00:00
|
|
|
|
|
|
|
#endif /*BOOST_ARRAY_HPP*/
|