Conquer Chess
Chess and Starcraft combined
Loading...
Searching...
No Matches
helper.h
Go to the documentation of this file.
1#ifndef HELPER_H
2#define HELPER_H
3
5
6#include <algorithm>
7#include <cassert>
8#include <cmath>
9#include <string>
10#include <vector>
11
13std::string bool_to_str(const bool b) noexcept;
14
25double calc_angle_degrees(const double dx, const double dy);
26
37double calc_angle_radians(const double dx, const double dy);
38
42double calc_distance(const double dx, const double dy) noexcept;
43
46template <class T> bool is_close(const T& lhs, const T& rhs, const T& max)
47{
48 return std::abs(lhs - rhs) < max;
49}
50
52template <class T> bool is_present_in(
53 const T& element,
54 const std::vector<T>& collection
55)
56{
57 return std::find(
58 std::begin(collection),
59 std::end(collection),
60 element
61 ) != std::end(collection);
62}
63
70std::vector<int> make_sequence(
71 const int from,
72 const int to,
73 const int increment = 1
74);
75
78std::vector<std::string> read_lines(const std::string& filename);
79
83template <class T>
84void remove_first(T& v)
85{
86 assert(!v.empty());
87 v.erase(v.begin());
88}
89
93std::vector<std::string> split_str(
94 const std::string& s,
95 const char seperator = ' '
96);
97
101void test_helper();
102
104std::wstring to_wstring(const std::string s);
105
106#endif // HELPER_H
bool is_present_in(const T &element, const std::vector< T > &collection)
Determine if a value if part of a collection.
Definition helper.h:52
void remove_first(T &v)
Remove the first element from a collection.
Definition helper.h:84
std::vector< int > make_sequence(const int from, const int to, const int increment=1)
Make a sequence in an inclusive way.
Definition helper.cpp:30
double calc_angle_degrees(const double dx, const double dy)
Calculate the angle in degrees.
Definition helper.cpp:14
bool is_close(const T &lhs, const T &rhs, const T &max)
Determine if the difference between two values is less than the maximum tolerated value.
Definition helper.h:46
std::string bool_to_str(const bool b) noexcept
Convert 'true' to 'true' and 'false' to 'false'.
Definition helper.cpp:9
double calc_distance(const double dx, const double dy) noexcept
Calculate the Euclidean distance between two points.
Definition helper.cpp:24
std::wstring to_wstring(const std::string s)
Convert a string to a wide string.
Definition helper.cpp:192
std::vector< std::string > split_str(const std::string &s, const char seperator=' ')
Split a string.
Definition helper.cpp:72
std::vector< std::string > read_lines(const std::string &filename)
Convert a file's content to a collection of strings.
Definition helper.cpp:59
double calc_angle_radians(const double dx, const double dy)
Calculate the angle in radians.
Definition helper.cpp:19
void test_helper()
Test the help functions.
Definition helper.cpp:89