mirror of
https://github.com/boostorg/array.git
synced 2025-07-13 04:36:32 +02:00
Changes reflecting formal review comments
[SVN r7689]
This commit is contained in:
325
array.hpp.html
325
array.hpp.html
@ -1,162 +1,169 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>array.hpp</title>
|
||||
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
|
||||
<meta name="ProgId" content="FrontPage.Editor.Document">
|
||||
</head>
|
||||
|
||||
<body text="#000000" bgcolor="#FFFFFF">
|
||||
<HTML>
|
||||
<HEAD>
|
||||
<TITLE>array.hpp</TITLE>
|
||||
</HEAD>
|
||||
|
||||
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
|
||||
|
||||
<table height="40" width="100%">
|
||||
<tr>
|
||||
<td align="LEFT" width="100%" bgcolor="#DDDDDD"><font face="Arial,Helvetica" size="+2"><b> array.hpp</b></font></td>
|
||||
</tr>
|
||||
</table>
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
<tt><span class="Source"><font color="0000FF">/</font><i><font face="Arial,Helvetica,sans-serif" color="0000FF">* The following code declares class array,</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">* an STL container (as wrapper) for arrays of constant size.</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">*</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">* See</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">* http://www.josuttis.com/cppcode</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">* for details and the latest version.</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">*</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">* (C) Copyright Nicolai M. Josuttis 1999.</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">* Permission to copy, use, modify, sell and distribute this software</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">* is granted provided this copyright notice appears in all copies.</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">* This software is provided "as is" without express or implied</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">* warranty, and with no claim as to its suitability for any purpose.</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">*</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">* Jul 31, 2000</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">*/</font></i><br>
|
||||
#ifndef BOOST_ARRAY_HPP<br>
|
||||
#define BOOST_ARRAY_HPP<br>
|
||||
<br>
|
||||
#include <cstddef><br>
|
||||
#include <stdexcept><br>
|
||||
#include <iterator><br>
|
||||
#include <algorithm><br>
|
||||
<br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">// BUG-FIX for compilers that don't support</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">// std::size_t and std::ptrdiff_t yet</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">// (such as gcc)</font></i><br>
|
||||
#include <<a href="../../boost/config.hpp">boost/config.hpp</a>><br>
|
||||
<br>
|
||||
namespace boost {<br>
|
||||
<br>
|
||||
template<class T, std::size_t N><br>
|
||||
class array {<br>
|
||||
public:<br>
|
||||
T elems[N]; <i><font face="Arial,Helvetica,sans-serif" color="0000FF">// fixed-size array of elements of type T</font></i><br>
|
||||
<br>
|
||||
public:<br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">// type definitions</font></i><br>
|
||||
typedef T value_type;<br>
|
||||
typedef T* iterator;<br>
|
||||
typedef const T* const_iterator;<br>
|
||||
typedef T& reference;<br>
|
||||
typedef const T& const_reference;<br>
|
||||
typedef std::size_t size_type;<br>
|
||||
typedef std::ptrdiff_t difference_type;<br>
|
||||
<br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">// iterator support</font></i><br>
|
||||
iterator begin() { return elems; }<br>
|
||||
const_iterator begin() const { return elems; }<br>
|
||||
iterator end() { return elems+N; }<br>
|
||||
const_iterator end() const { return elems+N; }<br>
|
||||
<br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">// reverse iterator support</font></i><br>
|
||||
typedef std::reverse_iterator<iterator> reverse_iterator;<br>
|
||||
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;<br>
|
||||
reverse_iterator rbegin() { return reverse_iterator(end()); }<br>
|
||||
const_reverse_iterator rbegin() const {<br>
|
||||
return const_reverse_iterator(end());<br>
|
||||
}<br>
|
||||
reverse_iterator rend() { return reverse_iterator(begin()); }<br>
|
||||
const_reverse_iterator rend() const {<br>
|
||||
return const_reverse_iterator(begin());<br>
|
||||
}<br>
|
||||
<br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">// operator[]</font></i><br>
|
||||
reference operator[](size_type i) { return elems[i]; }<br>
|
||||
const_reference operator[](size_type i) const { return elems[i]; }<br>
|
||||
<br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">// at() with range check</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">// note: rangecheck() is public because we have implemented array</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">// as aggregate, which forbids non-public members</font></i><br>
|
||||
void rangecheck (size_type i) const {<br>
|
||||
if (i >= size()) { throw std::range_error("array"); }<br>
|
||||
}<br>
|
||||
reference at(size_type i) { rangecheck(i); return elems[i]; }<br>
|
||||
const_reference at(size_type i) const { rangecheck(i); return elems[i]; }<br>
|
||||
<br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">// front() and back()</font></i><br>
|
||||
reference front() { return elems[0]; }<br>
|
||||
const_reference front() const { return elems[0]; }<br>
|
||||
reference back() { return elems[N-1]; }<br>
|
||||
const_reference back() const { return elems[N-1]; }<br>
|
||||
<br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">// size is constant</font></i><br>
|
||||
static size_type size() { return N; }<br>
|
||||
static bool empty() { return false; }<br>
|
||||
static size_type max_size() { return N; }<br>
|
||||
enum { static_size = N };<br>
|
||||
<br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">// swap (note: linear complexity)</font></i><br>
|
||||
void swap (array& y) {<br>
|
||||
std::swap_ranges(begin(),end(),y.begin());<br>
|
||||
}<br>
|
||||
<br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">// direct access to data</font></i><br>
|
||||
const T* data() const { return elems; }<br>
|
||||
<br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">// assignment with type conversion</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">//template <typename T2></font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">//T& operator= (const array<T2,N>& rhs) {</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">// std::copy (begin(),end(),rhs.begin());</font></i><br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">//}</font></i><br>
|
||||
};<br>
|
||||
<br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">// comparisons</font></i><br>
|
||||
template<class T, std::size_t N><br>
|
||||
bool operator== (const array<T,N>& x, const array<T,N>& y) {<br>
|
||||
return std::equal(x.begin(), x.end(), y.begin());<br>
|
||||
}<br>
|
||||
template<class T, std::size_t N><br>
|
||||
bool operator< (const array<T,N>& x, const array<T,N>& y) {<br>
|
||||
return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());<br>
|
||||
}<br>
|
||||
template<class T, std::size_t N><br>
|
||||
bool operator!= (const array<T,N>& x, const array<T,N>& y) {<br>
|
||||
return !(x==y);<br>
|
||||
}<br>
|
||||
template<class T, std::size_t N><br>
|
||||
bool operator> (const array<T,N>& x, const array<T,N>& y) {<br>
|
||||
return y<x;<br>
|
||||
}<br>
|
||||
template<class T, std::size_t N><br>
|
||||
bool operator<= (const array<T,N>& x, const array<T,N>& y) {<br>
|
||||
return !(y<x);<br>
|
||||
}<br>
|
||||
template<class T, std::size_t N><br>
|
||||
bool operator>= (const array<T,N>& x, const array<T,N>& y) {<br>
|
||||
return !(x<y);<br>
|
||||
}<br>
|
||||
<br>
|
||||
<i><font face="Arial,Helvetica,sans-serif" color="0000FF">// global swap()</font></i><br>
|
||||
template<class T, std::size_t N><br>
|
||||
inline void swap (const array<T,N>& x, const array<T,N>& y) {<br>
|
||||
x.swap(y);<br>
|
||||
}<br>
|
||||
<br>
|
||||
} <i><font face="Arial,Helvetica,sans-serif" color="0000FF">/* namespace boost */</font></i><br>
|
||||
<br>
|
||||
#endif <i><font face="Arial,Helvetica,sans-serif" color="0000FF">/*BOOST_ARRAY_HPP*/</font></i><br>
|
||||
</span></tt>
|
||||
<TABLE HEIGHT=40 WIDTH="100%">
|
||||
<TR> <TD ALIGN=LEFT WIDTH="100%" BGCOLOR="#DDDDDD">
|
||||
<FONT face="Arial,Helvetica" size=+2><B>
|
||||
array.hpp
|
||||
</B></FONT>
|
||||
</TD></TR></TABLE><BR>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
<BR><BR>
|
||||
<TT>
|
||||
<SPAN class="Source">
|
||||
<Font color="0000FF" >/</FONT><I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >* The following code declares class array,</FONT></I><BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >* an STL container (as wrapper) for arrays of constant size.</FONT></I><BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >*</FONT></I><BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >* See</FONT></I><BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >* http://www.josuttis.com/cppcode</FONT></I><BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >* for details and the latest version.</FONT></I><BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >*</FONT></I><BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >* (C) Copyright Nicolai M. Josuttis 1999.</FONT></I><BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >* Permission to copy, use, modify, sell and distribute this software</FONT></I><BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >* is granted provided this copyright notice appears in all copies.</FONT></I><BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >* This software is provided "as is" without express or implied</FONT></I><BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >* warranty, and with no claim as to its suitability for any purpose.</FONT></I><BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >*</FONT></I><BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >* Jul 31, 2000</FONT></I><BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >*/</FONT></I><BR>
|
||||
#ifndef BOOST_ARRAY_HPP<BR>
|
||||
#define BOOST_ARRAY_HPP<BR>
|
||||
<BR>
|
||||
#include <cstddef><BR>
|
||||
#include <stdexcept><BR>
|
||||
#include <iterator><BR>
|
||||
#include <algorithm><BR>
|
||||
<BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// BUG-FIX for compilers that don't support</FONT></I><BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// std::size_t and std::ptrdiff_t yet</FONT></I><BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// (such as gcc)</FONT></I><BR>
|
||||
#include <<A href="./config.hpp.html">boost/config.hpp</A>><BR>
|
||||
<BR>
|
||||
namespace boost {<BR>
|
||||
<BR>
|
||||
template<class T, std::size_t N><BR>
|
||||
class array {<BR>
|
||||
public:<BR>
|
||||
T elems[N]; <I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// fixed-size array of elements of type T</FONT></I><BR>
|
||||
<BR>
|
||||
public:<BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// type definitions</FONT></I><BR>
|
||||
typedef T value_type;<BR>
|
||||
typedef T* iterator;<BR>
|
||||
typedef const T* const_iterator;<BR>
|
||||
typedef T& reference;<BR>
|
||||
typedef const T& const_reference;<BR>
|
||||
typedef std::size_t size_type;<BR>
|
||||
typedef std::ptrdiff_t difference_type;<BR>
|
||||
<BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// iterator support</FONT></I><BR>
|
||||
iterator begin() { return elems; }<BR>
|
||||
const_iterator begin() const { return elems; }<BR>
|
||||
iterator end() { return elems+N; }<BR>
|
||||
const_iterator end() const { return elems+N; }<BR>
|
||||
<BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// reverse iterator support</FONT></I><BR>
|
||||
typedef std::reverse_iterator<iterator> reverse_iterator;<BR>
|
||||
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;<BR>
|
||||
reverse_iterator rbegin() { return reverse_iterator(end()); }<BR>
|
||||
const_reverse_iterator rbegin() const {<BR>
|
||||
return const_reverse_iterator(end());<BR>
|
||||
}<BR>
|
||||
reverse_iterator rend() { return reverse_iterator(begin()); }<BR>
|
||||
const_reverse_iterator rend() const {<BR>
|
||||
return const_reverse_iterator(begin());<BR>
|
||||
}<BR>
|
||||
<BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// operator[]</FONT></I><BR>
|
||||
reference operator[](size_type i) { return elems[i]; }<BR>
|
||||
const_reference operator[](size_type i) const { return elems[i]; }<BR>
|
||||
<BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// at() with range check</FONT></I><BR>
|
||||
reference at(size_type i) { rangecheck(i); return elems[i]; }<BR>
|
||||
const_reference at(size_type i) const { rangecheck(i); return elems[i]; }<BR>
|
||||
<BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// front() and back()</FONT></I><BR>
|
||||
reference front() { return elems[0]; }<BR>
|
||||
const_reference front() const { return elems[0]; }<BR>
|
||||
reference back() { return elems[N-1]; }<BR>
|
||||
const_reference back() const { return elems[N-1]; }<BR>
|
||||
<BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// size is constant</FONT></I><BR>
|
||||
static size_type size() { return N; }<BR>
|
||||
static bool empty() { return false; }<BR>
|
||||
static size_type max_size() { return N; }<BR>
|
||||
enum { static_size = N };<BR>
|
||||
<BR>
|
||||
public:<BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// swap (note: linear complexity)</FONT></I><BR>
|
||||
void swap (array<T,N>& y) {<BR>
|
||||
std::swap_ranges(begin(),end(),y.begin());<BR>
|
||||
}<BR>
|
||||
<BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// direct access to data</FONT></I><BR>
|
||||
const T* data() const { return elems; }<BR>
|
||||
<BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// assignment with type conversion</FONT></I><BR>
|
||||
template <typename T2><BR>
|
||||
array<T,N>& operator= (const array<T2,N>& rhs) {<BR>
|
||||
std::copy(rhs.begin(),rhs.end(), begin());<BR>
|
||||
return *this;<BR>
|
||||
}<BR>
|
||||
<BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// assign one value to all elements</FONT></I><BR>
|
||||
void assign (const T& value)<BR>
|
||||
{<BR>
|
||||
std::fill_n(begin(),size(),value);<BR>
|
||||
}<BR>
|
||||
<BR>
|
||||
private:<BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// check range (may be private because it is static)</FONT></I><BR>
|
||||
static void rangecheck (size_type i) {<BR>
|
||||
if (i >= size()) { throw std::range_error("array"); }<BR>
|
||||
}<BR>
|
||||
<BR>
|
||||
};<BR>
|
||||
<BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// comparisons</FONT></I><BR>
|
||||
template<class T, std::size_t N><BR>
|
||||
bool operator== (const array<T,N>& x, const array<T,N>& y) {<BR>
|
||||
return std::equal(x.begin(), x.end(), y.begin());<BR>
|
||||
}<BR>
|
||||
template<class T, std::size_t N><BR>
|
||||
bool operator< (const array<T,N>& x, const array<T,N>& y) {<BR>
|
||||
return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());<BR>
|
||||
}<BR>
|
||||
template<class T, std::size_t N><BR>
|
||||
bool operator!= (const array<T,N>& x, const array<T,N>& y) {<BR>
|
||||
return !(x==y);<BR>
|
||||
}<BR>
|
||||
template<class T, std::size_t N><BR>
|
||||
bool operator> (const array<T,N>& x, const array<T,N>& y) {<BR>
|
||||
return y<x;<BR>
|
||||
}<BR>
|
||||
template<class T, std::size_t N><BR>
|
||||
bool operator<= (const array<T,N>& x, const array<T,N>& y) {<BR>
|
||||
return !(y<x);<BR>
|
||||
}<BR>
|
||||
template<class T, std::size_t N><BR>
|
||||
bool operator>= (const array<T,N>& x, const array<T,N>& y) {<BR>
|
||||
return !(x<y);<BR>
|
||||
}<BR>
|
||||
<BR>
|
||||
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// global swap()</FONT></I><BR>
|
||||
template<class T, std::size_t N><BR>
|
||||
inline void swap (array<T,N>& x, array<T,N>& y) {<BR>
|
||||
x.swap(y);<BR>
|
||||
}<BR>
|
||||
<BR>
|
||||
} <I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >/* namespace boost */</FONT></I><BR>
|
||||
<BR>
|
||||
#endif <I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >/*BOOST_ARRAY_HPP*/</FONT></I><BR>
|
||||
</SPAN>
|
||||
</TT>
|
||||
</BODY>
|
||||
</HTML>
|
||||
|
Reference in New Issue
Block a user