array_view2d is a two-file header-only library to provide a two-dimensional view on a one-dimensional array or vector. The library is inspired on array_view [1] and can be used with C++98 and later and with Visual C++ 6 (VC6). With C++11 array_view2d provides a unified way to reference an array or vector (see example).
Contents
- Example usage
- In a nutshell
- License
- Dependencies
- Installation
- Synopsis
- Reported to work with
- C++ standard proposals on array views
- Notes and references
#include "array_view2d.hpp"
#include "array_view2d_output.hpp"
#include <iostream>
struct packet
{
std::size_t dim;
std::vector<int> data;
};
packet produce()
{
return { 3, { 0,0, 1,1, 2,2 } };
}
int main()
{
auto pack = produce();
auto view = nonstd::make_view2d( pack.data, pack.dim );
for ( auto pos : view.as_rows() )
{
std::cout << pos << "\n";
}
}
prompt>g++ -Wall -std=c++11 -o example.exe example.cpp && example
{ 0, 0 }
{ 1, 1 }
{ 2, 2 }
Here is the same example expressed in C++98.
array_view2d ...
Features and properties of array_view2d are ease of installation (single header), ...
Not provided are ...
array_view2d uses the Boost Software License.
array_view2d has no other dependencies than the C++ standard library.
array_view2d is a two-file header-only library. Put array_view2d.hpp
and array_view2d_output.hpp
directly into the project source tree or somewhere reachable from your project.
Contents
Types in namespace nonstd
Interface of array_view2d
Algorithms for array_view2d
Macros to control error reporting
Purpose | Type | Object |
---|---|---|
View | template< typename T > class array_view2d; |
|
Bound checking | struct check_bound_t; | check_bound_t check_bound; |
Error reporting | see this section |
Kind | Method | Result |
---|---|---|
Construction | array_view2d() | A blind eye |
array_view2d(...) C-array, std::array, std::vector, std::initializer_list |
A view | |
Iteration | begin(), end() | An iterator |
cbegin(), cend() | ||
Iteration,reverse | rbegin(), rend() | |
crbegin(), crend() | ||
Iteration, row | as_rows() | A forward-iteratable |
Access | empty() | True if no elements |
size(), length() | Number of elements | |
max_size() | Number of elements | |
row_size() | Number of elements in a row | |
rows() | Number of rows | |
Access, content | operator[]( n ) | Element |
at( n ) | Element, bound checked | |
data() | Pointer to first element | |
front() | First element | |
back() | Last element | |
Slicing | slice( index, count ) | A subview |
slice_before( index ) | ||
slice_from( index ) | ||
Slicing, iterator | slice( first, last ) | |
slice_before( pos ) | ||
slice_from( pos ) | ||
Slicing, checked | slice( check_bound, index, count ) | |
slice_before( check_bound, index ) | ||
slice_from( check_bound, index ) | ||
Slicing, iterator | slice( check_bound, first, last ) | |
slice_before( check_bound, pos ) | ||
slice_from( check_bound, pos ) | ||
Row access | row( n ) | A view on row elements |
Row, checked | row( check_bound, n ) |
Kind | Function | Result |
---|---|---|
Equality | ||
== | operator==( view1, view2 ) | True if equal |
operator==( view, container ) | ||
operator==( container, view ) | ||
!= | operator!=( view1, view2 ) | True if not equal |
operator!=( view, container ) | ||
operator!=( container, view ) | ||
Other algorithms | ||
Create | make_view2d(...) C-array, pointer and count, iterator pair, std::array, std::vector, std::initializer_list |
A view |
Convert | to_vector(...) view and optional allocator |
A std::vector |
array_view2d can perform sanity checks on construction and perform bound checking on methods such as row()
. Sanity checks in constructors use the same mechanism as implicit bound checks explained below.
array_view2d provides two approaches to bound checking:
- explicitly when calling
row(check_bound, n)
and - implicitly when using
row(n)
.
The explicit variants with the check_bound_t
parameter throw a std::out_of_range
exception when a parameter is out of bound.
The implicit variants without the check_bound_t
parameter, behave in the following way:
- Assert that a parameter is within bounds; this is the default for debug builds.
- Throw an exception when a parameter violates its bounds, see
av_FEATURE_EXPECT_THROWS
. - Do not check for bound violation; this is the default for release build, see
av_FEATURE_EXPECT_NOP
. - Perform a user-specified action, see
av_EXPECT
.
The following macros control boundary checking behaviour for methods that don't use check_bound_t.
-Dav_FEATURE_EXPECT_NOP=0
Define this to 1 to turn off boundary checking assertions. Default is 0.
-Dav_FEATURE_EXPECT_ASSERTS=1
At default, boundary checks assert. Default is 1.
Turn it off by defining av_FEATURE_EXPECT_NOP.
Use exceptions instead by defining av_FEATURE_EXPECT_THROWS.
Note that the assertion mechanism is active in debug builds, not in release builds.
-Dav_FEATURE_EXPECT_THROWS=0
Define this to 1 to activate boundary checking exceptions. Default is 0.
When an out of bounds error occurs, a std::out_of_range
exception is thrown.
When a sanity check fails, a std::runtime_error
exception is thrown.
-Dav_EXPECT( expr, excpt, note )
Define this macro to specify boundary checking behaviour, e.g.:
#define av_EXPECT( expr, excpt, note ) assert( ( expr ) && note )
Defining av_EXPECT deactivates macros av_FEATURE_EXPECT_NOP, av_FEATURE_EXPECT_ASSERTS and av_FEATURE_EXPECT_THROWS.
array_view2d is reported to work with the following compilers:
- Visual C++ 6 SP6 (VS6), VC10, (VS2010), VC11 (VS2012), VC12 (VS2013)
- GNUC 4.8.1 with -std=c++98, -std=c++03, -std=c++11, -std=c++1y
- clang 3.4 with -std=c++03, -std=c++11 (on Travis)
At the time of writing there are two proposals for the C++ standard on providing multidimensional views on arrays [2-4]. These proposals take a general approach to dimensionality, whereas array_view2d restricts itself to unstrided rows and slices.
[1] array_view.
[2] N3851: Multidimensional bounds, index and array_view (PDF).
[3] N4346: Multidimensional bounds, index and array_view, revision 6.
[4] N4355: Shared Multidimensional Array with Polymorphic Layout (PDF).