mirror of
https://github.com/boostorg/array.git
synced 2025-06-28 21:41:00 +02:00
Compare commits
9 Commits
svn-branch
...
svn-branch
Author | SHA1 | Date | |
---|---|---|---|
9c4de218f9 | |||
e875287d55 | |||
100b5d687b | |||
7fb9412ea8 | |||
471bc9bf06 | |||
9cf5e0c9a1 | |||
d2910e195a | |||
b3ace9fb6e | |||
a6b531b5b1 |
107
array0.cpp
107
array0.cpp
@ -1,107 +0,0 @@
|
|||||||
/* tests for using class array<> specialization for size 0
|
|
||||||
* (C) Copyright Alisdair Meredith 2006.
|
|
||||||
* 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)
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <iostream>
|
|
||||||
#include <boost/array.hpp>
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
unsigned int failed_tests = 0;
|
|
||||||
|
|
||||||
void fail_test( const char * reason ) {
|
|
||||||
++failed_tests;
|
|
||||||
std::cerr << "Test failure " << failed_tests << ": " << reason << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
template< class T >
|
|
||||||
void BadValue( const T & )
|
|
||||||
{
|
|
||||||
fail_test( "Unexpected value" );
|
|
||||||
}
|
|
||||||
|
|
||||||
template< class T >
|
|
||||||
void RunTests()
|
|
||||||
{
|
|
||||||
typedef boost::array< T, 0 > test_type;
|
|
||||||
|
|
||||||
// Test value and aggegrate initialization
|
|
||||||
test_type test_case = {};
|
|
||||||
const boost::array< T, 0 > const_test_case = test_type();
|
|
||||||
|
|
||||||
test_case.assign( T() );
|
|
||||||
|
|
||||||
// front/back and operator[] must compile, but calling them is undefined
|
|
||||||
// Likewise, all tests below should evaluate to false, avoiding undefined behaviour
|
|
||||||
if( !test_case.empty() ) {
|
|
||||||
BadValue( test_case.front() );
|
|
||||||
}
|
|
||||||
|
|
||||||
if( !const_test_case.empty() ) {
|
|
||||||
BadValue( const_test_case.back() );
|
|
||||||
}
|
|
||||||
|
|
||||||
if( test_case.size() > 0 ) {
|
|
||||||
BadValue( test_case[ 0 ] );
|
|
||||||
}
|
|
||||||
|
|
||||||
if( const_test_case.max_size() > 0 ) {
|
|
||||||
BadValue( const_test_case[ 0 ] );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assert requirements of TR1 6.2.2.4
|
|
||||||
if( test_case.begin() != test_case.end() ) {
|
|
||||||
fail_test( "Not an empty range" );
|
|
||||||
}
|
|
||||||
if( const_test_case.begin() != const_test_case.end() ) {
|
|
||||||
fail_test( "Not an empty range" );
|
|
||||||
}
|
|
||||||
|
|
||||||
if( test_case.begin() == const_test_case.begin() ) {
|
|
||||||
fail_test( "iterators for different containers are not distinct" );
|
|
||||||
}
|
|
||||||
|
|
||||||
if( test_case.data() == const_test_case.data() ) {
|
|
||||||
// Value of data is unspecified in TR1, so no requirement this test pass or fail
|
|
||||||
// However, it must compile!
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Check can safely use all iterator types with std algorithms
|
|
||||||
std::for_each( test_case.begin(), test_case.end(), BadValue< T > );
|
|
||||||
std::for_each( test_case.rbegin(), test_case.rend(), BadValue< T > );
|
|
||||||
std::for_each( const_test_case.begin(), const_test_case.end(), BadValue< T > );
|
|
||||||
std::for_each( const_test_case.rbegin(), const_test_case.rend(), BadValue< T > );
|
|
||||||
|
|
||||||
// Check swap is well formed
|
|
||||||
std::swap( test_case, test_case );
|
|
||||||
|
|
||||||
// Check assigment operator and overloads are well formed
|
|
||||||
test_case = const_test_case;
|
|
||||||
|
|
||||||
// Confirm at() throws the std lib defined exception
|
|
||||||
try {
|
|
||||||
BadValue( test_case.at( 0 ) );
|
|
||||||
} catch ( const std::out_of_range & ) {
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
BadValue( const_test_case.at( 0 ) );
|
|
||||||
} catch ( const std::out_of_range & ) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
RunTests< bool >();
|
|
||||||
RunTests< void * >();
|
|
||||||
RunTests< long double >();
|
|
||||||
RunTests< std::string >();
|
|
||||||
return failed_tests;
|
|
||||||
}
|
|
||||||
|
|
58
array1.cpp
58
array1.cpp
@ -1,58 +0,0 @@
|
|||||||
/* simple example for using class array<>
|
|
||||||
*
|
|
||||||
* (C) Copyright Nicolai M. Josuttis 2001.
|
|
||||||
* 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)
|
|
||||||
*
|
|
||||||
* Changelog:
|
|
||||||
* 20 Jan 2001 - Removed boolalpha use since stock GCC doesn't support it
|
|
||||||
* (David Abrahams)
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <boost/array.hpp>
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
// define special type name
|
|
||||||
typedef boost::array<float,6> Array;
|
|
||||||
|
|
||||||
// create and initialize an array
|
|
||||||
Array a = { { 42 } };
|
|
||||||
|
|
||||||
// access elements
|
|
||||||
for (unsigned i=1; i<a.size(); ++i) {
|
|
||||||
a[i] = a[i-1]+1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// use some common STL container operations
|
|
||||||
std::cout << "size: " << a.size() << std::endl;
|
|
||||||
std::cout << "empty: " << (a.empty() ? "true" : "false") << std::endl;
|
|
||||||
std::cout << "max_size: " << a.max_size() << std::endl;
|
|
||||||
std::cout << "front: " << a.front() << std::endl;
|
|
||||||
std::cout << "back: " << a.back() << std::endl;
|
|
||||||
std::cout << "elems: ";
|
|
||||||
|
|
||||||
// iterate through all elements
|
|
||||||
for (Array::const_iterator pos=a.begin(); pos<a.end(); ++pos) {
|
|
||||||
std::cout << *pos << ' ';
|
|
||||||
}
|
|
||||||
std::cout << std::endl;
|
|
||||||
|
|
||||||
// check copy constructor and assignment operator
|
|
||||||
Array b(a);
|
|
||||||
Array c;
|
|
||||||
c = a;
|
|
||||||
if (a==b && a==c) {
|
|
||||||
std::cout << "copy construction and copy assignment are OK"
|
|
||||||
<< std::endl;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
std::cout << "copy construction and copy assignment FAILED"
|
|
||||||
<< std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0; // makes Visual-C++ compiler happy
|
|
||||||
}
|
|
||||||
|
|
40
array2.cpp
40
array2.cpp
@ -1,40 +0,0 @@
|
|||||||
/* example for using class array<>
|
|
||||||
* (C) Copyright Nicolai M. Josuttis 2001.
|
|
||||||
* 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)
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <functional>
|
|
||||||
#include <boost/array.hpp>
|
|
||||||
#include "print.hpp"
|
|
||||||
using namespace std;
|
|
||||||
using namespace boost;
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
// create and initialize array
|
|
||||||
array<int,10> a = { { 1, 2, 3, 4, 5 } };
|
|
||||||
|
|
||||||
print_elements(a);
|
|
||||||
|
|
||||||
// modify elements directly
|
|
||||||
for (unsigned i=0; i<a.size(); ++i) {
|
|
||||||
++a[i];
|
|
||||||
}
|
|
||||||
print_elements(a);
|
|
||||||
|
|
||||||
// change order using an STL algorithm
|
|
||||||
reverse(a.begin(),a.end());
|
|
||||||
print_elements(a);
|
|
||||||
|
|
||||||
// negate elements using STL framework
|
|
||||||
transform(a.begin(),a.end(), // source
|
|
||||||
a.begin(), // destination
|
|
||||||
negate<int>()); // operation
|
|
||||||
print_elements(a);
|
|
||||||
|
|
||||||
return 0; // makes Visual-C++ compiler happy
|
|
||||||
}
|
|
||||||
|
|
55
array3.cpp
55
array3.cpp
@ -1,55 +0,0 @@
|
|||||||
/* example for using class array<>
|
|
||||||
* (C) Copyright Nicolai M. Josuttis 2001.
|
|
||||||
* 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)
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <iostream>
|
|
||||||
#include <boost/array.hpp>
|
|
||||||
|
|
||||||
template <class T>
|
|
||||||
void print_elements (const T& x);
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
// create array of four seasons
|
|
||||||
boost::array<std::string,4> seasons = {
|
|
||||||
{ "spring", "summer", "autumn", "winter" }
|
|
||||||
};
|
|
||||||
|
|
||||||
// copy and change order
|
|
||||||
boost::array<std::string,4> seasons_orig = seasons;
|
|
||||||
for (unsigned i=seasons.size()-1; i>0; --i) {
|
|
||||||
std::swap(seasons.at(i),seasons.at((i+1)%seasons.size()));
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << "one way: ";
|
|
||||||
print_elements(seasons);
|
|
||||||
|
|
||||||
// try swap()
|
|
||||||
std::cout << "other way: ";
|
|
||||||
std::swap(seasons,seasons_orig);
|
|
||||||
print_elements(seasons);
|
|
||||||
|
|
||||||
// try reverse iterators
|
|
||||||
std::cout << "reverse: ";
|
|
||||||
for (boost::array<std::string,4>::reverse_iterator pos
|
|
||||||
=seasons.rbegin(); pos<seasons.rend(); ++pos) {
|
|
||||||
std::cout << " " << *pos;
|
|
||||||
}
|
|
||||||
std::cout << std::endl;
|
|
||||||
|
|
||||||
return 0; // makes Visual-C++ compiler happy
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class T>
|
|
||||||
void print_elements (const T& x)
|
|
||||||
{
|
|
||||||
for (unsigned i=0; i<x.size(); ++i) {
|
|
||||||
std::cout << " " << x[i];
|
|
||||||
}
|
|
||||||
std::cout << std::endl;
|
|
||||||
}
|
|
||||||
|
|
43
array4.cpp
43
array4.cpp
@ -1,43 +0,0 @@
|
|||||||
/* example for using class array<>
|
|
||||||
* (C) Copyright Nicolai M. Josuttis 2001.
|
|
||||||
* 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)
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <functional>
|
|
||||||
#include <string>
|
|
||||||
#include <iostream>
|
|
||||||
#include <boost/array.hpp>
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
// array of arrays of seasons
|
|
||||||
boost::array<boost::array<std::string,4>,2> seasons_i18n = {
|
|
||||||
{ { { "spring", "summer", "autumn", "winter", } },
|
|
||||||
{ { "Fruehling", "Sommer", "Herbst", "Winter" } }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// for any array of seasons print seasons
|
|
||||||
for (unsigned i=0; i<seasons_i18n.size(); ++i) {
|
|
||||||
boost::array<std::string,4> seasons = seasons_i18n[i];
|
|
||||||
for (unsigned j=0; j<seasons.size(); ++j) {
|
|
||||||
std::cout << seasons[j] << " ";
|
|
||||||
}
|
|
||||||
std::cout << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
// print first element of first array
|
|
||||||
std::cout << "first element of first array: "
|
|
||||||
<< seasons_i18n[0][0] << std::endl;
|
|
||||||
|
|
||||||
// print last element of last array
|
|
||||||
std::cout << "last element of last array: "
|
|
||||||
<< seasons_i18n[seasons_i18n.size()-1][seasons_i18n[0].size()-1]
|
|
||||||
<< std::endl;
|
|
||||||
|
|
||||||
return 0; // makes Visual-C++ compiler happy
|
|
||||||
}
|
|
||||||
|
|
72
array5.cpp
72
array5.cpp
@ -1,72 +0,0 @@
|
|||||||
/* simple example for using class array<>
|
|
||||||
* (C) Copyright Nicolai M. Josuttis 2001.
|
|
||||||
* 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)
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <boost/array.hpp>
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void test_static_size (const T& cont)
|
|
||||||
{
|
|
||||||
int tmp[T::static_size];
|
|
||||||
for (unsigned i=0; i<T::static_size; ++i) {
|
|
||||||
tmp[i] = int(cont[i]);
|
|
||||||
}
|
|
||||||
for (unsigned j=0; j<T::static_size; ++j) {
|
|
||||||
std::cout << tmp[j] << ' ';
|
|
||||||
}
|
|
||||||
std::cout << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
// define special type name
|
|
||||||
typedef boost::array<float,6> Array;
|
|
||||||
|
|
||||||
// create and initialize an array
|
|
||||||
const Array a = { { 42.42 } };
|
|
||||||
|
|
||||||
// use some common STL container operations
|
|
||||||
std::cout << "static_size: " << a.size() << std::endl;
|
|
||||||
std::cout << "size: " << a.size() << std::endl;
|
|
||||||
// Can't use std::boolalpha because it isn't portable
|
|
||||||
std::cout << "empty: " << (a.empty()? "true" : "false") << std::endl;
|
|
||||||
std::cout << "max_size: " << a.max_size() << std::endl;
|
|
||||||
std::cout << "front: " << a.front() << std::endl;
|
|
||||||
std::cout << "back: " << a.back() << std::endl;
|
|
||||||
std::cout << "[0]: " << a[0] << std::endl;
|
|
||||||
std::cout << "elems: ";
|
|
||||||
|
|
||||||
// iterate through all elements
|
|
||||||
for (Array::const_iterator pos=a.begin(); pos<a.end(); ++pos) {
|
|
||||||
std::cout << *pos << ' ';
|
|
||||||
}
|
|
||||||
std::cout << std::endl;
|
|
||||||
test_static_size(a);
|
|
||||||
|
|
||||||
// check copy constructor and assignment operator
|
|
||||||
Array b(a);
|
|
||||||
Array c;
|
|
||||||
c = a;
|
|
||||||
if (a==b && a==c) {
|
|
||||||
std::cout << "copy construction and copy assignment are OK"
|
|
||||||
<< std::endl;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
std::cout << "copy construction and copy assignment are BROKEN"
|
|
||||||
<< std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef boost::array<double,6> DArray;
|
|
||||||
typedef boost::array<int,6> IArray;
|
|
||||||
IArray ia = { { 1, 2, 3, 4, 5, 6 } } ; // extra braces silence GCC warning
|
|
||||||
DArray da;
|
|
||||||
da = ia;
|
|
||||||
da.assign(42);
|
|
||||||
|
|
||||||
return 0; // makes Visual-C++ compiler happy
|
|
||||||
}
|
|
||||||
|
|
@ -24,9 +24,17 @@
|
|||||||
#ifndef BOOST_ARRAY_HPP
|
#ifndef BOOST_ARRAY_HPP
|
||||||
#define BOOST_ARRAY_HPP
|
#define BOOST_ARRAY_HPP
|
||||||
|
|
||||||
|
#include <boost/detail/workaround.hpp>
|
||||||
|
|
||||||
|
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
|
||||||
|
# pragma warning(push)
|
||||||
|
# pragma warning(disable:4996) // 'std::equal': Function call with parameters that may be unsafe
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <boost/assert.hpp>
|
#include <boost/assert.hpp>
|
||||||
|
#include <boost/swap.hpp>
|
||||||
|
|
||||||
// Handles broken standard libraries better than <iterator>
|
// Handles broken standard libraries better than <iterator>
|
||||||
#include <boost/detail/iterator.hpp>
|
#include <boost/detail/iterator.hpp>
|
||||||
@ -131,7 +139,8 @@ namespace boost {
|
|||||||
|
|
||||||
// swap (note: linear complexity)
|
// swap (note: linear complexity)
|
||||||
void swap (array<T,N>& y) {
|
void swap (array<T,N>& y) {
|
||||||
std::swap_ranges(begin(),end(),y.begin());
|
for (size_type i = 0; i < N; ++i)
|
||||||
|
boost::swap(elems[i],y.elems[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// direct access to data (read-only)
|
// direct access to data (read-only)
|
||||||
@ -209,19 +218,19 @@ namespace boost {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// operator[]
|
// operator[]
|
||||||
reference operator[](size_type i)
|
reference operator[](size_type /*i*/)
|
||||||
{
|
{
|
||||||
return failed_rangecheck();
|
return failed_rangecheck();
|
||||||
}
|
}
|
||||||
|
|
||||||
const_reference operator[](size_type i) const
|
const_reference operator[](size_type /*i*/) const
|
||||||
{
|
{
|
||||||
return failed_rangecheck();
|
return failed_rangecheck();
|
||||||
}
|
}
|
||||||
|
|
||||||
// at() with range check
|
// at() with range check
|
||||||
reference at(size_type i) { return failed_rangecheck(); }
|
reference at(size_type /*i*/) { return failed_rangecheck(); }
|
||||||
const_reference at(size_type i) const { return failed_rangecheck(); }
|
const_reference at(size_type /*i*/) const { return failed_rangecheck(); }
|
||||||
|
|
||||||
// front() and back()
|
// front() and back()
|
||||||
reference front()
|
reference front()
|
||||||
@ -250,7 +259,7 @@ namespace boost {
|
|||||||
static size_type max_size() { return 0; }
|
static size_type max_size() { return 0; }
|
||||||
enum { static_size = 0 };
|
enum { static_size = 0 };
|
||||||
|
|
||||||
void swap (array<T,0>& y) {
|
void swap (array<T,0>& /*y*/) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// direct access to data (read-only)
|
// direct access to data (read-only)
|
||||||
@ -318,4 +327,9 @@ namespace boost {
|
|||||||
|
|
||||||
} /* namespace boost */
|
} /* namespace boost */
|
||||||
|
|
||||||
|
|
||||||
|
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
|
||||||
|
# pragma warning(pop)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif /*BOOST_ARRAY_HPP*/
|
#endif /*BOOST_ARRAY_HPP*/
|
||||||
|
27
print.hpp
27
print.hpp
@ -1,27 +0,0 @@
|
|||||||
/* The following code example is taken from the book
|
|
||||||
* "The C++ Standard Library - A Tutorial and Reference"
|
|
||||||
* by Nicolai M. Josuttis, Addison-Wesley, 1999
|
|
||||||
*
|
|
||||||
* (C) Copyright Nicolai M. Josuttis 1999.
|
|
||||||
* 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)
|
|
||||||
*/
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
/* print_elements()
|
|
||||||
* - prints optional C-string optcstr followed by
|
|
||||||
* - all elements of the collection coll
|
|
||||||
* - separated by spaces
|
|
||||||
*/
|
|
||||||
template <class T>
|
|
||||||
inline void print_elements (const T& coll, const char* optcstr="")
|
|
||||||
{
|
|
||||||
typename T::const_iterator pos;
|
|
||||||
|
|
||||||
std::cout << optcstr;
|
|
||||||
for (pos=coll.begin(); pos!=coll.end(); ++pos) {
|
|
||||||
std::cout << *pos << ' ';
|
|
||||||
}
|
|
||||||
std::cout << std::endl;
|
|
||||||
}
|
|
@ -21,7 +21,7 @@ int main()
|
|||||||
|
|
||||||
// copy and change order
|
// copy and change order
|
||||||
boost::array<std::string,4> seasons_orig = seasons;
|
boost::array<std::string,4> seasons_orig = seasons;
|
||||||
for (unsigned i=seasons.size()-1; i>0; --i) {
|
for (std::size_t i=seasons.size()-1; i>0; --i) {
|
||||||
std::swap(seasons.at(i),seasons.at((i+1)%seasons.size()));
|
std::swap(seasons.at(i),seasons.at((i+1)%seasons.size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ int main()
|
|||||||
typedef boost::array<float,6> Array;
|
typedef boost::array<float,6> Array;
|
||||||
|
|
||||||
// create and initialize an array
|
// create and initialize an array
|
||||||
const Array a = { { 42.42 } };
|
const Array a = { { 42.42f } };
|
||||||
|
|
||||||
// use some common STL container operations
|
// use some common STL container operations
|
||||||
std::cout << "static_size: " << a.size() << std::endl;
|
std::cout << "static_size: " << a.size() << std::endl;
|
||||||
|
Reference in New Issue
Block a user