|
pflib v3.9.0-rc3-11-g2537d8f
Pretty Fine HGCROC Interaction Library
|
Dumping ground for various functions that are used in many places. More...
Functions | |
| uint32_t | crc32 (std::span< uint32_t > data) |
| Calculate the CRC checksum for a set of 32bit words. | |
| uint8_t | econd_crc8 (uint64_t data) |
| Calculate the 8-bit CRC checksum as it is done for the event header on the ECOND. | |
| double | efficiency (std::vector< int > samples) |
| find the efficiency of the input vector of samples this is the fraction of samples that are above 0 | |
| bool | ends_with (const std::string &full, const std::string &ending) |
| Check if a given string has a specific ending. | |
| template<typename CellType > | |
| static std::optional< std::vector< CellType > > | get_next_row (std::istream &ss, std::function< CellType(std::string)> conversion) |
| get next row from the input stream | |
| void | load_integer_csv (const std::string &file_name, std::function< void(const std::vector< int > &)> Action, std::optional< std::function< void(const std::vector< std::string > &)> > HeaderAction=std::nullopt) |
| Load an integer CSV file, doing some Action on each row. | |
| int | median (std::vector< int > samples) |
| find the median of the input vector of samples | |
| double | median (std::vector< double > samples) |
| find the median of the input vector of samples | |
| int | str_to_int (std::string str) |
| Get an integer from the input string. | |
| template<typename... Args> | |
| std::string | string_format (const std::string &format, Args... args) |
| Backport of C++20 std::format-like function. | |
Dumping ground for various functions that are used in many places.
| uint32_t pflib::utility::crc32 | ( | std::span< uint32_t > | data | ) |
Calculate the CRC checksum for a set of 32bit words.
| [in] | data | 32-bit words to calculate CRC for |
We flip the endian-ness so that the bytes are processed MSB to LSB after casting our array of words into an array of bytes.
| uint8_t pflib::utility::econd_crc8 | ( | uint64_t | data | ) |
Calculate the 8-bit CRC checksum as it is done for the event header on the ECOND.
| [in] | data | 64-bit word containing both ECOND event packet headers |
Reverse endian-ness so when we cast to an array of bytes, we read the bytes from most-significant to least-significant.
These CRC parameters are what I can find online for 8bit Bluetooth CRC, except the last two booleans RefIn and RefOut which are false instead of the Bluetooth values of true (according to https://www.crccalc.com/). I'm guessing we aren't having the CRC calculator reflecting because we are already reflecting when constructing the byte stream?
| double pflib::utility::efficiency | ( | std::vector< int > | samples | ) |
find the efficiency of the input vector of samples this is the fraction of samples that are above 0
| [in] | samples | list of samples to find efficiency of |
find number of non-zero samples divided by total number of samples in vector
| bool pflib::utility::ends_with | ( | const std::string & | full, |
| const std::string & | ending ) |
Check if a given string has a specific ending.
| [in] | full | string to check |
| [in] | ending | string to look for |
|
static |
get next row from the input stream
| CellType | type held within cell |
| [in] | ss | input stream to read from |
| [in] | conversion | function to convert the cell string into desired type |
if the line we read is empty or starts with #, then return std::nullopt to signal that there wasn't a row
when the line is not empty or a comment, fill the vector with cells separated by ,.
| void pflib::utility::load_integer_csv | ( | const std::string & | file_name, |
| std::function< void(const std::vector< int > &)> | Action, | ||
| std::optional< std::function< void(const std::vector< std::string > &)> | , | ||
| HeaderAction | = std::nullopt ) |
Load an integer CSV file, doing some Action on each row.
This function skips blank lines and comment lines that start with the # character.
Header rows are not skipped. If you want to skip the header, prepend it with the # character so that it is treated as a comment line. You can also use the HeaderAction argument to handle the header row yourself.
| [in] | file_name | path to CSV to open and read |
| [in] | Action | function that receives the row of integer values and does something with them |
| [in] | HeaderAction | optional function that receives a header row of strings, default nothing |
| Exception | if no file is provided or file cannot be opened |
If the user provides a HeaderAction and we have not seen a header yet, then get the next line and apply the HeaderAction.
If the user did not provide a HeaderAction or we already handled the header, get the next line and apply the user's action if the line is not a comment or empty.
| double pflib::utility::median | ( | std::vector< double > | samples | ) |
find the median of the input vector of samples
| [in] | samples | list of samples to find median of |
we partially sort the vector in order to find the median
for even-number sets, median is halfway between the two central values
for odd-number sets, there is a central value
| int pflib::utility::median | ( | std::vector< int > | samples | ) |
find the median of the input vector of samples
| [in] | samples | list of samples to find median of |
we partially sort the vector in order to find the median
we don't bother to see if the median should be between two numbers
| int pflib::utility::str_to_int | ( | std::string | str | ) |
Get an integer from the input string.
The normal stoi (and similar) tools don't support binary inputs which are helpful in our case where sometimes the value is set in binary but each bit has a non-base-2 scale.
Supported prefixes:
0b --> binary0x --> hexidecimal0 --> octal| [in] | str | string form of integer |
| std::string pflib::utility::string_format | ( | const std::string & | format, |
| Args... | args ) |
Backport of C++20 std::format-like function.
I say "std::format-like" because instead of using curly-braces {} for inserting the arguments like std::format does, this function uses the C-style (printf-style) percent-characters (e.g. s to insert a string).
(Supported in GCC-13 and we have GCC-11)
We basically write to a char* and then conver that to std::string.
| Args | types of arguments passed into snprintf |
| [in] | format | snprintf format string to use |
| [in] | args | arguments for snprintf |
Shamelessly taken from https://stackoverflow.com/a/26221725 which has a line-by-line explanation for those who wish to learn more C++! I've modified it slightly to use our exceptions and longer variable names.