1 #ifndef DATAFORMATTERTOOLS_HH
2 #define DATAFORMATTERTOOLS_HH
24 template<
typename ValueType,
typename ArgType,
typename DataObject>
33 using Evaluator = std::function<ValueType(const std::vector<ArgType> &)>;
38 const std::string &_init_name,
39 const std::string &_desc,
43 std::unique_ptr<DataFormatterOperator>
clone()
const {
44 return std::unique_ptr<DataFormatterOperator>(this->
_clone());
57 bool parse_args(
const std::string &_args)
override;
69 _stream.flags(std::ios::showpoint | std::ios::fixed | std::ios::right);
105 template<
typename DataObject>
108 [](
const std::vector<double> &vec)->
double {
109 return std::accumulate(vec.cbegin(),
120 template<
typename DataObject>
123 [](
const std::vector<double> &vec)->
double {
125 throw std::runtime_error(
"Subtraction operator must receive exactly two values!");
126 return vec[0] - vec[1];
135 template<
typename DataObject>
138 [](
const std::vector<double> &vec)->
double {
139 return std::accumulate(vec.cbegin(),
142 [](
double a,
double b)->
double{
153 template<
typename DataObject>
156 [](
const std::vector<double> &vec)->
double {
158 throw std::runtime_error(
"Division operator must receive exactly two values!");
159 return vec[0] / vec[1];
168 template<
typename DataObject>
171 [](
const std::vector<double> &vec)->
double {
172 return (*std::max_element(vec.cbegin(),
181 template<
typename DataObject>
184 [](
const std::vector<double> &vec)->
double {
185 return (*std::min_element(vec.cbegin(),
194 template<
typename DataObject>
197 [](
const std::vector<double> &vec)->
long {
198 auto it = std::max_element(vec.cbegin(), vec.cend());
199 return std::distance(vec.cbegin(), it);
207 template<
typename DataObject>
210 [](
const std::vector<double> &vec)->
long {
211 auto it = std::min_element(vec.cbegin(), vec.cend());
212 return std::distance(vec.cbegin(), it);
220 template<
typename DataObject>
223 [](
const std::vector<double> &vec)->
double {
225 throw std::runtime_error(
"Exponent operator must receive exactly one value!");
238 template<
typename DataObject>
241 [](
const std::vector<std::string> &vec)->
bool {
243 throw std::runtime_error(
"Operator re('input_string','regex_pattern') must receive exactly 2 values!");
244 boost::regex e(boost::trim_copy_if(vec[1], boost::is_any_of(
" '")));
245 return boost::regex_match(vec[0], e);
255 template<
typename DataObject>
258 [](
const std::vector<std::string> &vec)->
bool {
260 throw std::runtime_error(
"Operator re('input_string','regex_pattern') must receive exactly 2 values!");
261 boost::regex e(boost::trim_copy_if(vec[1], boost::is_any_of(
" '")));
262 return boost::regex_search(vec[0], e);
270 template<
typename DataObject>
273 [](
const std::vector<double> &vec)->
double {
275 throw std::runtime_error(
"Square operator must receive exactly one value!");
276 return vec[0] * vec[0];
285 template<
typename DataObject>
288 [](
const std::vector<double> &vec)->
double {
290 throw std::runtime_error(
"Square-root operator must receive exactly one value!");
300 template<
typename DataObject>
303 [](
const std::vector<double> &vec)->
double {
305 throw std::runtime_error(
"Negation operator must receive exactly one value!");
315 template<
typename DataObject>
318 [](
const std::vector<bool> &vec)->
bool {
319 return std::accumulate(vec.cbegin(),
322 [](
bool a,
bool b)->
bool{
333 template<
typename DataObject>
336 [](
const std::vector<bool> &vec)->
bool {
337 return std::accumulate(vec.cbegin(),
340 [](
bool a,
bool b)->
bool{
351 template<
typename DataObject>
354 [](
const std::vector<bool> &vec)->
bool {
356 throw std::runtime_error(
"Boolean XOR operator expects exactly two values!");
357 return (vec[0] && !vec[1]) || (!vec[0] && vec[1]);
361 template<
typename DataObject>
364 [](
const std::vector<bool> &vec)->
bool {
366 throw std::runtime_error(
"Boolean NOT operator must receive exactly one value!");
376 template<
typename DataObject>
379 [](
const std::vector<double> &vec)->
bool {
381 throw std::runtime_error(
"Equality operator must receive exactly two values!");
391 template<
typename DataObject>
394 [](
const std::vector<double> &vec)->
bool {
396 throw std::runtime_error(
"Less-than operator must receive exactly two values!");
397 return vec[0] < vec[1];
406 template<
typename DataObject>
409 [](
const std::vector<double> &vec)->
bool {
411 throw std::runtime_error(
"Less-than-or-equal operator must receive exactly two values!");
412 return vec[0] <= vec[1];
421 template<
typename DataObject>
424 [](
const std::vector<double> &vec)->
bool {
426 throw std::runtime_error(
"Greater-than operator must receive exactly two values!");
427 return vec[0] > vec[1];
436 template<
typename DataObject>
439 [](
const std::vector<double> &vec)->
bool {
441 throw std::runtime_error(
"Greater-than-or-equal operator must receive exactly two values!");
442 return vec[0] >= vec[1];
448 template<
typename DataObject>
453 format_operator_add<DataObject>(),
454 format_operator_sub<DataObject>(),
455 format_operator_mult<DataObject>(),
456 format_operator_div<DataObject>(),
457 format_operator_exp<DataObject>(),
458 format_operator_sq<DataObject>(),
459 format_operator_sqrt<DataObject>(),
460 format_operator_neg<DataObject>(),
461 format_operator_and<DataObject>(),
462 format_operator_or<DataObject>(),
463 format_operator_not<DataObject>(),
464 format_operator_min<DataObject>(),
465 format_operator_max<DataObject>(),
466 format_operator_imin<DataObject>(),
467 format_operator_imax<DataObject>(),
468 format_operator_eq<DataObject>(),
469 format_operator_lt<DataObject>(),
470 format_operator_le<DataObject>(),
471 format_operator_gt<DataObject>(),
472 format_operator_ge<DataObject>(),
473 format_operator_re<DataObject>(),
474 format_operator_rs<DataObject>()
488 template<
typename DataObject>
494 const std::string &_command,
496 const std::string &
_help =
"") :
501 throw std::runtime_error(
"Expression '" + _command +
"' is either empty or consists of multiple expressions.\n");
513 std::unique_ptr<DatumFormatterAlias>
clone()
const {
514 return std::unique_ptr<DatumFormatterAlias>(this->
_clone());
530 std::vector<std::string> _col;
533 if(tcount.
count() == 1) {
534 _col.push_back(
name());
539 std::stringstream t_ss;
540 t_ss <<
name() <<
'(' << i <<
')';
541 _col.push_back(t_ss.str());
564 m_formatter->print(_data_obj, _stream, pass_index);
570 m_formatter->inject(_data_obj, _stream, pass_index);
591 throw std::runtime_error(
"ERROR: DataFormatterAlias has no formatter");
596 throw std::runtime_error(
"Invalid arguments passed to '" +
m_format_tags[0] +
"'. Cannot accept expression '" +
m_subexprs[0] +
"'\n");
599 return args.size() == 0;
619 template<
typename DataObject>
621 const std::string &_name,
622 const std::string &_command,
624 const std::string &
_help =
"") {
632 template<
typename DataObject>
634 const std::string &_name,
636 const std::string &
_help =
"") {
647 template<
typename ValueType,
typename DataObject>
655 std::unique_ptr<ConstantValueFormatter>
clone()
const {
656 return std::unique_ptr<ConstantValueFormatter>(this->
_clone());
711 template<
typename ValueType,
typename DataObject>
724 std::unique_ptr<BaseValueFormatter>
clone()
const {
725 return std::unique_ptr<BaseValueFormatter>(this->
_clone());
731 throw std::runtime_error(std::string(
"Invalid DataObject in ") + this->
name());
751 _stream << this->
evaluate(_data_obj);
758 _stream.flags(std::ios::showpoint | std::ios::fixed | std::ios::right);
759 _stream.precision(8);
761 _stream << this->
evaluate(_data_obj);
763 _stream <<
"unknown";
791 template<
typename ValueType,
typename DataObject>
796 using Evaluator = std::function<ValueType(const DataObject &)>;
797 using Validator = std::function<bool(const DataObject &)>;
807 const std::string &_desc,
809 Validator _validator = always_true<DataObject>) :
825 std::unique_ptr<GenericDatumFormatter>
clone()
const {
826 return std::unique_ptr<GenericDatumFormatter>(this->
_clone());
892 template<
typename Container,
typename DataObject>
912 std::unique_ptr<Base1DDatumFormatter>
clone()
const {
913 return std::unique_ptr<Base1DDatumFormatter>(this->
_clone());
926 for(
Index i = 0; i < size; i++) {
935 std::vector<std::string> _col;
938 for(; it != end_it; ++it) {
939 std::stringstream t_ss;
940 t_ss << std::string(s,
' ') << this->
name() <<
'(' << (*it)[0] <<
')';
941 _col.push_back(t_ss.str());
962 Container val = this->
evaluate(_data_obj);
966 for(; it != end_it; ++it) {
971 for(; it != end_it; ++it) {
972 _stream << Access::at(val, (*it)[0]);
987 _stream.flags(std::ios::showpoint | std::ios::fixed | std::ios::right);
988 _stream.precision(8);
989 bool known = this->
validate(_data_obj);
994 for(; it != end_it; ++it) {
996 _stream <<
" " << Access::at(val, (*it)[0]);
998 _stream <<
" unknown";
1022 template<
typename Container,
typename DataObject>
1027 typedef std::function<Container(const DataObject &)>
Evaluator;
1038 const std::string &_desc,
1039 Evaluator _evaluator,
1040 Validator _validator = always_true<DataObject>) :
1054 std::unique_ptr<Generic1DDatumFormatter>
clone()
const {
1055 return std::unique_ptr<Generic1DDatumFormatter>(this->
_clone());
1082 template<
typename DataObject>
1089 template<
typename DataObject>
1096 template<
typename DataObject>
1104 template<
typename DataObject>
1111 template<
typename DataObject>
1118 template<
typename DataObject>
1126 template<
typename DataObject>
1133 template<
typename DataObject>
1140 template<
typename DataObject>
1148 template<
typename DataObject>
1155 template<
typename DataObject>
1162 template<
typename DataObject>
1170 template<
typename DataObject>
1177 template<
typename DataObject>
1184 template<
typename DataObject>
1191 template<
typename DataObject>
1196 make_string_dictionary<DataObject>(),
1197 make_boolean_dictionary<DataObject>(),
1198 make_integer_dictionary<DataObject>(),
1199 make_scalar_dictionary<DataObject>(),
1200 make_vectorxd_dictionary<DataObject>()
Specialize container traits Expects:
DataFormatterDictionary< DataObject > make_attribute_dictionary()
Dictionary of all AttributeFormatter (i.e. BaseValueFormatter)
void split_formatter_expression(const std::string &input_expr, std::vector< std::string > &tag_names, std::vector< std::string > &sub_exprs)
std::pair< iterator, bool > insert(const value_type &value)
Insert single value.
DataFormatterOperator< bool, bool, DataObject > format_operator_not()
EigenIndex Index
For long integer indexing:
T max(const T &A, const T &B)
A 'cloneable_ptr' can be used in place of 'unique_ptr'.
const std::vector< T > & vector() const
bool almost_equal(const GenericCluster< CoordType > &LHS, const GenericCluster< CoordType > &RHS, double tol)
A Configuration represents the values of all degrees of freedom in a Supercell.
DataFormatterDictionary< DataObject > make_operator_dictionary()
Dictionary of all DatumFormatterOperator.