ndarray
NumPy-friendly multidimensional arrays in C++
All Classes Files Functions Variables Typedefs Enumerations Friends Modules Pages
formatting.h
Go to the documentation of this file.
1// -*- c++ -*-
2/*
3 * Copyright (c) 2010-2012, Jim Bosch
4 * All rights reserved.
5 *
6 * ndarray is distributed under a simple BSD-like license;
7 * see the LICENSE file that should be present in the root
8 * of the source distribution, or alternately available at:
9 * https://github.com/ndarray/ndarray
10 */
11#ifndef NDARRAY_formatting_h_INCLUDED
12#define NDARRAY_formatting_h_INCLUDED
13
21
22#include <cstdint>
23#include <iostream>
24
25namespace ndarray {
26namespace detail {
27template <typename Derived, int N = Derived::ND::value> class Formatter;
28} // namespace detail
29
36 int _width;
37 int _precision;
38 std::ios_base::fmtflags _flags;
39 std::string _delimiter;
40 std::string _open;
41 std::string _close;
42public:
43
45 explicit FormatOptions(
46 int width = 8,
47 int precision = 6,
48 std::ios_base::fmtflags flags = std::ios_base::fmtflags(0),
49 std::string const & delimiter = ", ",
50 std::string const & open = "[",
51 std::string const & close = "]"
52 ) :
53 _width(width),
54 _precision(precision),
55 _flags(flags),
56 _delimiter(delimiter),
57 _open(open),
58 _close(close)
59 {}
60
62 template <typename Derived>
63 void apply(std::ostream & os, ExpressionBase<Derived> const & expr) {
64 detail::Formatter<Derived>::apply(*this,os,expr,0);
65 }
66
67 template <typename Derived, int N> friend class detail::Formatter;
68};
69
71template <typename Derived>
72std::ostream & operator<<(std::ostream & os, ExpressionBase<Derived> const & expr) {
73 FormatOptions options;
74 options.apply(os,expr);
75 return os;
76}
77
78namespace detail {
79
81inline std::ostream &operator<<(std::ostream &os, std::int8_t value) {
82 return os << static_cast<int>(value);
83}
84
86inline std::ostream &operator<<(std::ostream &os, std::uint8_t value) {
87 return os << static_cast<int>(value);
88}
89
94template <typename Derived, int N>
95class Formatter {
96public:
97 static void apply(
98 FormatOptions const & options,
99 std::ostream & os,
100 ExpressionBase<Derived> const & expr,
101 int level
102 ) {
103 os << options._open;
104 if (!expr.empty()) {
105 typename ExpressionBase<Derived>::Iterator const end = expr.end();
106 typename ExpressionBase<Derived>::Iterator iter = expr.begin();
107 Formatter<typename ExpressionBase<Derived>::Reference>::apply(options,os,*iter,level+1);
108 for (++iter; iter != end; ++iter) {
109 os << options._delimiter;
110 os << std::endl << std::string(level,' ');
111 Formatter<typename ExpressionBase<Derived>::Reference>::apply(options,os,*iter,level+1);
112 }
113 }
114 os << options._close;
115 }
116};
117
122template <typename Derived>
123class Formatter<Derived,1> {
124public:
125 static void apply(
126 FormatOptions const & options,
127 std::ostream & os,
128 ExpressionBase<Derived> const & expr,
129 int level
130 ) {
131 os << options._open;
132 if (!expr.empty()) {
133 typename ExpressionBase<Derived>::Iterator const end = expr.end();
134 typename ExpressionBase<Derived>::Iterator iter = expr.begin();
135 int precision = os.precision(options._precision);
136 int width = os.width(options._width);
137 std::ios_base::fmtflags flags = os.setf(options._flags,std::ios_base::floatfield);
138 os << (*iter);
139 for (++iter; iter != end; ++iter) {
140 os << options._delimiter << (*iter);
141 }
142 os.precision(precision);
143 os.width(width);
144 os.setf(flags);
145 }
146 os << options._close;
147 }
148};
149
150} // namespace detail
151} // namespace ndarray
152
153#endif // !NDARRAY_formatting_h_INCLUDED
Definitions for ExpressionBase.
CRTP base class for all multidimensional expressions.
Definition ExpressionBase.h:40
Iterator end() const
Return an Iterator to one past the end of the expression.
Definition ExpressionBase.h:70
Iterator begin() const
Return an Iterator to the beginning of the expression.
Definition ExpressionBase.h:67
bool empty() const
Return true if the first dimension has no elements.
Definition ExpressionBase.h:96
ExpressionTraits< Derived >::Iterator Iterator
Nested expression or element iterator.
Definition ExpressionBase.h:47
Options for controlling stream output of ExpressionBase.
Definition formatting.h:35
FormatOptions(int width=8, int precision=6, std::ios_base::fmtflags flags=std::ios_base::fmtflags(0), std::string const &delimiter=", ", std::string const &open="[", std::string const &close="]")
Standard constructor.
Definition formatting.h:45
void apply(std::ostream &os, ExpressionBase< Derived > const &expr)
Format the given expression into the given output stream.
Definition formatting.h:63
Definition formatting.h:95
std::ostream & operator<<(std::ostream &os, ExpressionBase< Derived > const &expr)
Stream output for ExpressionBase using default-constructed FormatOptions.
Definition formatting.h:72