treelite
Classes | Macros | Functions
common.h File Reference

Some useful utilities. More...

#include <treelite/base.h>
#include <dmlc/logging.h>
#include <dmlc/json.h>
#include <dmlc/data.h>
#include <memory>
#include <iterator>
#include <functional>
#include <iomanip>
#include <cerrno>
#include <climits>
Include dependency graph for common.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Cloneable
 abstract interface for classes that can be cloned More...
 
class  DeepCopyUniquePtr< T >
 a wrapper around std::unique_ptr that supports deep copying and moving. More...
 

Macros

#define CLONEABLE_BOILERPLATE(className)
 macro to define boilerplate code for Cloneable classes More...
 

Functions

template<typename T , typename... Args>
std::unique_ptr< T > make_unique (Args &&...args)
 construct a new object of type T and wraps it with a std::unique_ptr. This is support legacy compiles (e.g. g++ 4.8.x) that do not yet support std::make_unique<T>. More...
 
template<typename T >
T && MoveUniquePtr (const std::unique_ptr< T > &ptr)
 utility function to move an object pointed by a std::unique_ptr. After the move operation, the unique_ptr will hold an invalid object. Usage example: More...
 
void WrapText (std::ostringstream *p_stm, size_t *p_length, const std::string &str, size_t indent, size_t textwidth)
 insert a string into a string stream, adding a newline character (
) so that the string stream has no line longer than the given width. More...
 
template<class Iter , class T >
Iter binary_search (Iter begin, Iter end, const T &val)
 perform binary search on the range [begin, end). More...
 
template<typename T >
std::string ToString (T value)
 obtain a string representation of primitive type using ostringstream More...
 
void WriteToFile (const std::string &filename, const std::string &content)
 write a sequence of strings to a text file, with newline character (
) inserted between strings. This function is suitable for creating multi-line text files. More...
 
void TransformPushBack (std::vector< std::string > *p_dest, const std::vector< std::string > &lines, std::function< std::string(std::string)> func)
 apply a given transformation to a sequence of strings and append them to another sequence. More...
 
template<typename T >
TextToNumber (const std::string &str)
 convert text to number More...
 
template<>
float TextToNumber (const std::string &str)
 
template<>
double TextToNumber (const std::string &str)
 
template<>
int TextToNumber (const std::string &str)
 
template<>
int8_t TextToNumber (const std::string &str)
 
template<>
uint32_t TextToNumber (const std::string &str)
 
template<typename T >
std::vector< T > TextToArray (const std::string &text, int num_entry)
 convert text to number array More...
 
std::vector< std::string > Split (const std::string &text, char delim)
 split text using a delimiter More...
 

Detailed Description

Some useful utilities.

Some useful math utilities.

Copyright by 2017 Contributors

Author
Philip Cho

Definition in file common.h.

Macro Definition Documentation

#define CLONEABLE_BOILERPLATE (   className)
Value:
explicit className(const className& other) = default; \
explicit className(className&& other) = default; \
Cloneable* clone() const override { \
return new className(*this); \
} \
Cloneable* move_clone() override { \
return new className(std::move(*this)); \
}

macro to define boilerplate code for Cloneable classes

Definition at line 33 of file common.h.

Function Documentation

Iter treelite::common::binary_search ( Iter  begin,
Iter  end,
const T &  val 
)

perform binary search on the range [begin, end).

Parameters
beginbeginning of the search range
endend of the search range
valvalue being searched
Returns
iterator pointing to the value if found; end if value not found.
Template Parameters
Itertype of iterator
Ttype of elements

Definition at line 148 of file common.h.

std::unique_ptr<T> treelite::common::make_unique ( Args &&...  args)

construct a new object of type T and wraps it with a std::unique_ptr. This is support legacy compiles (e.g. g++ 4.8.x) that do not yet support std::make_unique<T>.

Parameters
argslist of arguments with which an instance of T will be constructed
Returns
unique_ptr wrapping the newly created object
Template Parameters
Ttype of object to be constructed
Argsvariadic template for forwarded arguments

Definition at line 91 of file common.h.

T&& treelite::common::MoveUniquePtr ( const std::unique_ptr< T > &  ptr)
inline

utility function to move an object pointed by a std::unique_ptr. After the move operation, the unique_ptr will hold an invalid object. Usage example:

// Suppose class Foo has a move constructor with signature Foo(Bar&& bar)
std::unique_ptr<Bar> bar;
Foo foo(MoveUniquePtr(bar));
Parameters
ptrunique_ptr
Returns
rvalue reference to the object being moved
Template Parameters
typeof object being moved

Definition at line 109 of file common.h.

std::vector<std::string> treelite::common::Split ( const std::string &  text,
char  delim 
)
inline

split text using a delimiter

Parameters
strtext
delimdelimiter
Returns
std::vector of strings, split by a delimiter

Definition at line 331 of file common.h.

std::vector<T> treelite::common::TextToArray ( const std::string &  text,
int  num_entry 
)
inline

convert text to number array

Parameters
strstring containing numbers, separated by spaces in between
Returns
std::vector of numbers, converted to type T
Template Parameters
Ttype of value (should be a floating-point or integer type)

Definition at line 314 of file common.h.

T treelite::common::TextToNumber ( const std::string &  str)
inline

convert text to number

Parameters
strstring containing number
Returns
number converted to type T
Template Parameters
Ttype of value (should be a floating-point or integer type)

Definition at line 210 of file common.h.

std::string treelite::common::ToString ( value)
inline

obtain a string representation of primitive type using ostringstream

Parameters
valuea value of primitive type
Returns
string representation

Definition at line 164 of file common.h.

void treelite::common::TransformPushBack ( std::vector< std::string > *  p_dest,
const std::vector< std::string > &  lines,
std::function< std::string(std::string)>  func 
)
inline

apply a given transformation to a sequence of strings and append them to another sequence.

Parameters
p_destpointer to the destination sequence
linesa list of string lines to be transformed and then appended.
functransformation function.

Definition at line 196 of file common.h.

void treelite::common::WrapText ( std::ostringstream *  p_stm,
size_t *  p_length,
const std::string &  str,
size_t  indent,
size_t  textwidth 
)
inline

insert a string into a string stream, adding a newline character (
) so that the string stream has no line longer than the given width.

Parameters
p_stmpointer to string stream
p_lengthused to keep track of the lenght of current line. It will always be the case that [length] <= [textwidth]
indentindent level inside array
strstring to be inserted
textwidthmaximum width of each line

Definition at line 124 of file common.h.

void treelite::common::WriteToFile ( const std::string &  filename,
const std::string &  content 
)
inline

write a sequence of strings to a text file, with newline character (
) inserted between strings. This function is suitable for creating multi-line text files.

Parameters
filenamename of text file
linesa sequence of strings to be written.

Definition at line 180 of file common.h.