Chi-Tech
chi_utils.h
Go to the documentation of this file.
1#ifndef CHITECH_CHI_UTILS_H
2#define CHITECH_CHI_UTILS_H
3
4#include <cstddef>
5#include <string>
6#include <vector>
7#include <algorithm>
8#include <fstream>
9
10/**Miscellaneous utilities. These utilities should have no dependencies.*/
11namespace chi
12{
13std::string PrintIterationProgress(size_t current_iteration,
14 size_t total_num_iterations,
15 unsigned int num_intvls = 10);
16
17// #include <iostream>
18// #include <string>
19
20const std::string WHITESPACE = " \n\r\t\f\v";
21
22/**Trims whitespace from the front of a string.*/
23std::string StringLTrim(const std::string& s);
24
25/**Trims whitespace from the back of a string.*/
26std::string StringRTrim(const std::string& s);
27
28/**Trims whitespace from the front and back of a string.*/
29std::string StringTrim(const std::string& s);
30
31/**Splits a string using the given delimiter. Consecutive delimiters
32 * are treated as one.*/
33std::vector<std::string> StringSplit(const std::string& input,
34 const std::string& delim = " ");
35
36/**The string portion, from the rear of the input string, up to
37 * encountering the search_string.*/
38std::string StringUpToFirstReverse(const std::string& input,
39 const std::string& search_string);
40
41void AssertReadibleFile(const std::string& file_name);
42
43template <typename T, typename B>
44bool VectorListHas(const std::vector<T>& list, const B& val)
45{
46 return std::find(list.begin(), list.end(), val) != list.end();
47}
48
50{
51 size_t ss_begin;
52 size_t ss_end;
53 size_t ss_size;
54};
55/**Subdivides a number of items (X) into a desired number of sub sets (Y). The
56 * remainder of X/Y, i.e. r=X/Y obeys the indentity r < Y. These items will be
57 * distributed to the first Y sub-sets. Example:
58 * MakeSubSets(6659, 8) generates subsets of sizes
59 * {833,833,833,832,832,832,832,832}.*/
60std::vector<SubSetInfo> MakeSubSets(size_t num_items,
61 size_t desired_num_subsets);
62
63/**Popular and fast djb2a hashing algorithm.*/
64inline constexpr uint32_t hash_djb2a(const std::string_view sv)
65{
66 uint32_t hash{5381};
67 for (unsigned char c : sv)
68 hash = ((hash << 5) + hash) ^ c;
69
70 return hash;
71}
72
73inline constexpr uint32_t operator"" _hash(const char* str, size_t len)
74{
75 return hash_djb2a(std::string_view{str, len});
76}
77
78template<typename T>
79void WriteBinaryValue(std::ofstream& output_file, T value)
80{
81 output_file.write((char*)&value, sizeof(T));
82}
83template<typename T>
84T ReadBinaryValue(std::ifstream& input_file)
85{
86 T value;
87 input_file.read((char*)&value, sizeof(T));
88
89 return value;
90}
91} // namespace chi
92
93#endif // CHITECH_CHI_UTILS_H
std::string StringUpToFirstReverse(const std::string &input, const std::string &search_string)
constexpr uint32_t hash_djb2a(const std::string_view sv)
Definition: chi_utils.h:64
std::string PrintIterationProgress(size_t current_iteration, size_t total_num_iterations, unsigned int num_intvls=10)
const std::string WHITESPACE
Definition: chi_utils.h:20
std::vector< SubSetInfo > MakeSubSets(size_t num_items, size_t desired_num_subsets)
Definition: subsets.cc:10
T ReadBinaryValue(std::ifstream &input_file)
Definition: chi_utils.h:84
void AssertReadibleFile(const std::string &file_name)
std::string StringLTrim(const std::string &s)
std::vector< std::string > StringSplit(const std::string &input, const std::string &delim)
bool VectorListHas(const std::vector< T > &list, const B &val)
Definition: chi_utils.h:44
void WriteBinaryValue(std::ofstream &output_file, T value)
Definition: chi_utils.h:79
std::string StringRTrim(const std::string &s)
std::string StringTrim(const std::string &s)
size_t ss_size
Definition: chi_utils.h:53
size_t ss_begin
Definition: chi_utils.h:51
size_t ss_end
Definition: chi_utils.h:52