mirror of
https://github.com/boostorg/array.git
synced 2025-07-30 20:57:19 +02:00
initial checkin of Nico's array class
[SVN r7677]
This commit is contained in:
@ -1,143 +1,135 @@
|
|||||||
// -*-C++-*- array.hpp
|
/* The following code declares class array,
|
||||||
// <!!---------------------------------------------------------------------->
|
* an STL container (as wrapper) for arrays of constant size.
|
||||||
// <!! Copyright (C) 1998 Dietmar Kuehl, Claas Solutions GmbH >
|
*
|
||||||
// <!!>
|
* See
|
||||||
// <!! Permission to use, copy, modify, distribute and sell this >
|
* http://www.josuttis.com/cppcode
|
||||||
// <!! software for any purpose is hereby granted without fee, provided >
|
* for details and the latest version.
|
||||||
// <!! that the above copyright notice appears in all copies and that >
|
*
|
||||||
// <!! both that copyright notice and this permission notice appear in >
|
* (C) Copyright Nicolai M. Josuttis 1999.
|
||||||
// <!! supporting documentation. Dietmar Kuehl and Claas Solutions make no >
|
* Permission to copy, use, modify, sell and distribute this software
|
||||||
// <!! representations about the suitability of this software for any >
|
* is granted provided this copyright notice appears in all copies.
|
||||||
// <!! purpose. It is provided "as is" without express or implied warranty. >
|
* This software is provided "as is" without express or implied
|
||||||
// <!!---------------------------------------------------------------------->
|
* warranty, and with no claim as to its suitability for any purpose.
|
||||||
|
*
|
||||||
// Author: Dietmar Kuehl dietmar.kuehl@claas-solutions.de
|
* Jul 31, 2000
|
||||||
// Title: STL container support, including support for built-in arrays
|
*/
|
||||||
// Version: $Id$
|
#ifndef BOOST_ARRAY_HPP
|
||||||
|
#define BOOST_ARRAY_HPP
|
||||||
// --------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#if !defined(BOOST_ARRAY_HPP)
|
|
||||||
#define BOOST_ARRAY_HPP 1
|
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <iterator>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
#include <boost/config.hpp> // for std::size_t and std::ptrdiff_t workarounds
|
||||||
|
|
||||||
namespace boost
|
namespace boost {
|
||||||
{
|
|
||||||
|
|
||||||
// --- a general version of container traits ------------------------------
|
template<class T, std::size_t N>
|
||||||
|
class array {
|
||||||
|
public:
|
||||||
|
T elems[N]; // fixed-size array of elements of type T
|
||||||
|
|
||||||
template <typename Cont>
|
public:
|
||||||
struct array_traits
|
// type definitions
|
||||||
{
|
typedef T value_type;
|
||||||
typedef typename Cont::iterator iter_type;
|
typedef T* iterator;
|
||||||
typedef typename Cont::size_type size_type;
|
typedef const T* const_iterator;
|
||||||
static iter_type begin(Cont &cont) { return cont.begin(); }
|
typedef T& reference;
|
||||||
static iter_type end(Cont &cont) { return cont.end(); }
|
typedef const T& const_reference;
|
||||||
static size_type size(Cont &cont) { return cont.size(); }
|
typedef std::size_t size_type;
|
||||||
|
typedef std::ptrdiff_t difference_type;
|
||||||
|
|
||||||
|
// 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
|
||||||
|
typedef std::reverse_iterator<iterator> reverse_iterator;
|
||||||
|
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||||
|
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 elems[i]; }
|
||||||
|
const_reference operator[](size_type i) const { return elems[i]; }
|
||||||
|
|
||||||
|
// at() with range check
|
||||||
|
// note: rangecheck() is public because we have implemented array
|
||||||
|
// as aggregate, which forbids non-public members
|
||||||
|
void rangecheck (size_type i) const {
|
||||||
|
if (i >= size()) { throw std::range_error("array"); }
|
||||||
|
}
|
||||||
|
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()
|
||||||
|
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]; }
|
||||||
|
|
||||||
|
// 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& y) {
|
||||||
|
std::swap_ranges(begin(),end(),y.begin());
|
||||||
|
}
|
||||||
|
|
||||||
|
// direct access to data
|
||||||
|
const T* data() const { return elems; }
|
||||||
|
|
||||||
|
// assignment with type conversion
|
||||||
|
//template <typename T2>
|
||||||
|
//T& operator= (const array<T2,N>& rhs) {
|
||||||
|
// std::copy (begin(),end(),rhs.begin());
|
||||||
|
//}
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- a version of container traits for constant constainer --------------
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
template <typename Cont>
|
// global swap()
|
||||||
struct array_traits<Cont const>
|
template<class T, std::size_t N>
|
||||||
{
|
inline void swap (const array<T,N>& x, const array<T,N>& y) {
|
||||||
typedef typename Cont::const_iterator iter_type;
|
x.swap(y);
|
||||||
typedef typename Cont::size_type size_type;
|
}
|
||||||
static iter_type begin(Cont const &cont) { return cont.begin(); }
|
|
||||||
static iter_type end(Cont const &cont) { return cont.end(); }
|
|
||||||
static size_type size(Cont const &cont) { return cont.size(); }
|
|
||||||
};
|
|
||||||
|
|
||||||
// --- a special version for non-const built-in arrays --------------------
|
} /* namespace boost */
|
||||||
|
|
||||||
template <typename T, size_t sz>
|
#endif /*BOOST_ARRAY_HPP*/
|
||||||
struct array_traits<T[sz]>
|
|
||||||
{
|
|
||||||
typedef T* iter_type;
|
|
||||||
typedef size_t size_type;
|
|
||||||
static iter_type begin(T (&array)[sz]) { return array; }
|
|
||||||
static iter_type end(T (&array)[sz]) { return array + sz; }
|
|
||||||
static size_type size(T (&)[sz]) { return sz; }
|
|
||||||
};
|
|
||||||
|
|
||||||
// --- a special version for const built-in arrays ------------------------
|
|
||||||
|
|
||||||
template <typename T, size_t sz>
|
|
||||||
struct array_traits<T const[sz]>
|
|
||||||
{
|
|
||||||
typedef T const* iter_type;
|
|
||||||
typedef size_t size_type;
|
|
||||||
static iter_type begin(T const (&array)[sz]) { return array; }
|
|
||||||
static iter_type end(T const (&array)[sz]) { return array + sz; }
|
|
||||||
static size_type size(T const (&array)[sz]) { return sz; }
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T, int sz>
|
|
||||||
inline char (&sizer(T (&)[sz]))[sz];
|
|
||||||
|
|
||||||
// --- general version of the global accessor functions ---------------------
|
|
||||||
|
|
||||||
template <typename Cont>
|
|
||||||
inline typename array_traits<Cont>::iter_type
|
|
||||||
begin(Cont &cont) { return array_traits<Cont>::begin(cont); }
|
|
||||||
|
|
||||||
template <typename Cont>
|
|
||||||
inline typename array_traits<Cont>::iter_type
|
|
||||||
end(Cont &cont) { return array_traits<Cont>::end(cont); }
|
|
||||||
|
|
||||||
template <typename Cont>
|
|
||||||
inline typename array_traits<Cont>::size_type
|
|
||||||
size(Cont &cont) { return array_traits<Cont>::size(cont); }
|
|
||||||
|
|
||||||
// --- Actually the above should be sufficient but compilers seem -----------
|
|
||||||
// --- to welcome some help. So here we go:
|
|
||||||
|
|
||||||
template <typename T, size_t sz>
|
|
||||||
inline typename array_traits<T[sz]>::iter_type
|
|
||||||
begin(T (&a)[sz]) { return array_traits<T[sz]>::begin(a); }
|
|
||||||
|
|
||||||
template <typename T, size_t sz>
|
|
||||||
inline typename array_traits<T[sz]>::iter_type
|
|
||||||
end(T (&a)[sz]) { return array_traits<T[sz]>::end(a); }
|
|
||||||
|
|
||||||
template <typename T, size_t sz>
|
|
||||||
inline typename array_traits<T[sz]>::size_type
|
|
||||||
size(T (&a)[sz]) { return array_traits<T[sz]>::size(a); }
|
|
||||||
|
|
||||||
// --- Apparently the compilers also need some specific help, ---------------
|
|
||||||
|
|
||||||
// --- EDG-2.39 wants to pass around pointers in some contexts --------------
|
|
||||||
#ifdef __EDG__
|
|
||||||
template <typename T>
|
|
||||||
struct array_traits<T*>
|
|
||||||
{
|
|
||||||
typedef T* iter_type;
|
|
||||||
typedef size_t size_type;
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// --- egcs-1998-11-22 apparently likes an extra const version: -------------
|
|
||||||
#ifdef __GNUG__
|
|
||||||
template <typename T, size_t sz>
|
|
||||||
inline typename array_traits<T const[sz]>::iter_type
|
|
||||||
begin(T const(&a)[sz]) { return array_traits<T const[sz]>::begin(a); }
|
|
||||||
|
|
||||||
template <typename T, size_t sz>
|
|
||||||
inline typename array_traits<T const[sz]>::iter_type
|
|
||||||
end(T const(&a)[sz]) { return array_traits<T const[sz]>::end(a); }
|
|
||||||
|
|
||||||
template <typename T, size_t sz>
|
|
||||||
inline typename array_traits<T const[sz]>::size_type
|
|
||||||
size(T const (&a)[sz]) { return array_traits<T const[sz]>::size(a); }
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#endif /* BOOST_ARRAY_HPP */
|
|
||||||
|
Reference in New Issue
Block a user