Theoretica
A C++ numerical and automatic mathematical library
Loading...
Searching...
No Matches
utility.h
Go to the documentation of this file.
1
5
6#ifndef THEORETICA_UTILITY_H
7#define THEORETICA_UTILITY_H
8
9#include <iostream>
10#include <string>
11#include <algorithm>
12#include "./algebra/vec.h"
13
14
15namespace theoretica {
16
17
19 template<typename Type>
20 inline void print(const Type& curr) {
21 std::cout << curr;
22 }
23
26 template<typename Type, typename ...Args>
27 inline void print(const Type& curr, Args... args) {
28 std::cout << curr << " ";
29 print(args...);
30 }
31
33 template<typename Type>
34 inline void fprint(std::ostream& out, const Type& last) {
35 out << last;
36 }
37
40 template<typename Type, typename ...Args>
41 inline void fprint(std::ostream& out, const Type& curr, Args... args) {
42 out << curr << " ";
43 fprint(out, args...);
44 }
45
46
48 inline void println() {
49 std::cout << "\n";
50 }
51
54 template<typename Type>
55 inline void println(const Type& curr) {
56 std::cout << curr << "\n";
57 }
58
61 template<typename Type, typename ...Args>
62 inline void println(const Type& curr, Args... args) {
63 std::cout << curr << " ";
64 println(args...);
65 }
66
69 template<typename Type>
70 inline void fprintln(std::ostream& out, const Type& curr) {
71 out << curr << "\n";
72 }
73
76 template<typename Type, typename ...Args>
77 inline void fprintln(std::ostream& out, const Type& curr, Args... args) {
78 out << curr << " ";
79 fprintln(out, args...);
80 }
81
82
87 template<typename Type = real>
89 std::istream& in,
90 const std::string& terminator,
91 std::function<Type(std::string)> parse) {
92
93 vec<Type> data;
94 std::string line;
95 Type value;
96
97 while(true) {
98
99 std::getline(in, line);
100
101 // Stop reading when the terminator is reached.
102 if(line == terminator)
103 break;
104
105 // Skip empty lines.
106 if(line == "")
107 continue;
108
109 // Try to parse the line and add it to the vector.
110 try {
111 value = parse(line);
112 } catch(...) {
113 std::cout << "Input conversion error" << std::endl;
114 continue;
115 }
116
117 data.push(value);
118 }
119
120 return data;
121 }
122
123
129 std::istream& in, const std::string& terminator = "") {
130
131 return readln<real>(in, terminator, [](std::string line) {
132 std::replace(line.begin(), line.end(), ',', '.');
133 return std::stod(line);
134 });
135 }
136
137
142 inline vec<real> readln(const std::string& terminator = "") {
143 return readln(std::cin, terminator);
144 }
145
146}
147
148#endif
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
void fprint(std::ostream &out, const Type &last)
Print the given argument to a stream.
Definition utility.h:34
std::remove_reference_t< decltype(std::declval< Structure >()[0])> vector_element_t
Extract the type of a vector (or any indexable container) from its operator[].
Definition core_traits.h:134
void println()
Print a newline to standard output.
Definition utility.h:48
void fprintln(std::ostream &out, const Type &curr)
Print the given argument to an output stream followed by a newline.
Definition utility.h:70
void print(const Type &curr)
Print the given argument to standard output.
Definition utility.h:20
vec< Type > readln(std::istream &in, const std::string &terminator, std::function< Type(std::string)> parse)
Insert a data set of the given type from a stream, reading line by line until a line is equal to the ...
Definition utility.h:88
Vector class and operations.