pflib v3.9.0-rc3-11-g2537d8f
Pretty Fine HGCROC Interaction Library
Loading...
Searching...
No Matches
pflib::utility Namespace Reference

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.
 

Detailed Description

Dumping ground for various functions that are used in many places.

Function Documentation

◆ crc32()

uint32_t pflib::utility::crc32 ( std::span< uint32_t > data)

Calculate the CRC checksum for a set of 32bit words.

Parameters
[in]data32-bit words to calculate CRC for
Returns
value of CRC

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.

Note
these CRC parameters are copied from the HGCROC but appear to be correct for the ECOND implementation as well.

◆ econd_crc8()

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.

Parameters
[in]data64-bit word containing both ECOND event packet headers
Returns
value of CRC

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?

◆ efficiency()

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

Parameters
[in]sampleslist of samples to find efficiency of
Returns
efficiency of the samples

find number of non-zero samples divided by total number of samples in vector

◆ ends_with()

bool pflib::utility::ends_with ( const std::string & full,
const std::string & ending )

Check if a given string has a specific ending.

Parameters
[in]fullstring to check
[in]endingstring to look for
Returns
true if full's last characters match ending

◆ get_next_row()

template<typename CellType >
static std::optional< std::vector< CellType > > pflib::utility::get_next_row ( std::istream & ss,
std::function< CellType(std::string)> conversion )
static

get next row from the input stream

Template Parameters
CellTypetype held within cell
Parameters
[in]ssinput stream to read from
[in]conversionfunction to convert the cell string into desired type
Returns
optional vector containing cells

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 ,.

◆ load_integer_csv()

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.

Parameters
[in]file_namepath to CSV to open and read
[in]Actionfunction that receives the row of integer values and does something with them
[in]HeaderActionoptional function that receives a header row of strings, default nothing
Exceptions
Exceptionif 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.

◆ median() [1/2]

double pflib::utility::median ( std::vector< double > samples)

find the median of the input vector of samples

Parameters
[in]sampleslist of samples to find median of
Returns
median of the samples

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

◆ median() [2/2]

int pflib::utility::median ( std::vector< int > samples)

find the median of the input vector of samples

Parameters
[in]sampleslist of samples to find median of
Returns
median of the samples

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

◆ str_to_int()

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 --> binary
  • 0x --> hexidecimal
  • 0 --> octal
  • none of the above --> decimal
Parameters
[in]strstring form of integer
Returns
integer decoded from string

◆ string_format()

template<typename... Args>
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.

Template Parameters
Argstypes of arguments passed into snprintf
Parameters
[in]formatsnprintf format string to use
[in]argsarguments for snprintf
Returns
formatted std::string

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.