Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
142 lines (128 loc) · 4.08 KB

File metadata and controls

142 lines (128 loc) · 4.08 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// -*- c++ -*-
/*
* Copyright (c) 2010-2012, Jim Bosch
* All rights reserved.
*
* ndarray is distributed under a simple BSD-like license;
* see the LICENSE file that should be present in the root
* of the source distribution, or alternately available at:
* https://github.com/ndarray/ndarray
*/
#ifndef NDARRAY_formatting_h_INCLUDED
#define NDARRAY_formatting_h_INCLUDED
/**
* @file ndarray/formatting.h
*
* @brief iostream output support for Expression.
*/
#include "ndarray/ExpressionBase.h"
#include <iostream>
namespace ndarray {
namespace detail {
template <typename Derived, int N = Derived::ND::value> class Formatter;
} // namespace detail
/**
* @class FormatOptions
* @ingroup MainGroup
* @brief Options for controlling stream output of ExpressionBase.
*/
class FormatOptions {
int _width;
int _precision;
std::ios_base::fmtflags _flags;
std::string _delimiter;
std::string _open;
std::string _close;
public:
/// @brief Standard constructor.
explicit 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 = "]"
) :
_width(width),
_precision(precision),
_flags(flags),
_delimiter(delimiter),
_open(open),
_close(close)
{}
/// @brief Format the given expression into the given output stream.
template <typename Derived>
void apply(std::ostream & os, ExpressionBase<Derived> const & expr) {
detail::Formatter<Derived>::apply(*this,os,expr,0);
}
template <typename Derived, int N> friend class detail::Formatter;
};
/// @brief Stream output for ExpressionBase using default-constructed FormatOptions.
template <typename Derived>
std::ostream & operator<<(std::ostream & os, ExpressionBase<Derived> const & expr) {
FormatOptions options;
options.apply(os,expr);
return os;
}
namespace detail {
/**
* @internal @ingroup ndarrayInternalGroup
* @brief Recursive metafunction used in stream output.
*/
template <typename Derived, int N>
class Formatter {
public:
static void apply(
FormatOptions const & options,
std::ostream & os,
ExpressionBase<Derived> const & expr,
int level
) {
os << options._open;
if (!expr.empty()) {
typename ExpressionBase<Derived>::Iterator const end = expr.end();
typename ExpressionBase<Derived>::Iterator iter = expr.begin();
Formatter<typename ExpressionBase<Derived>::Reference>::apply(options,os,*iter,level+1);
for (++iter; iter != end; ++iter) {
os << options._delimiter;
os << std::endl << std::string(level,' ');
Formatter<typename ExpressionBase<Derived>::Reference>::apply(options,os,*iter,level+1);
}
}
os << options._close;
}
};
/**
* @internal @ingroup ndarrayInternalGroup
* @brief Recursive metafunction used in stream output (1d specialization).
*/
template <typename Derived>
class Formatter<Derived,1> {
public:
static void apply(
FormatOptions const & options,
std::ostream & os,
ExpressionBase<Derived> const & expr,
int level
) {
os << options._open;
if (!expr.empty()) {
typename ExpressionBase<Derived>::Iterator const end = expr.end();
typename ExpressionBase<Derived>::Iterator iter = expr.begin();
int precision = os.precision(options._precision);
int width = os.width(options._width);
std::ios_base::fmtflags flags = os.setf(options._flags,std::ios_base::floatfield);
os << (*iter);
for (++iter; iter != end; ++iter) {
os << options._delimiter << (*iter);
}
os.precision(precision);
os.width(width);
os.setf(flags);
}
os << options._close;
}
};
} // namespace detail
} // namespace ndarray
#endif // !NDARRAY_formatting_h_INCLUDED
Morty Proxy This is a proxified and sanitized view of the page, visit original site.