Templates and utility-routines for strings and numbers. More...
#include "global.hpp"
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <math.h>
#include <vector>
#include <sstream>
#include <algorithm>
Go to the source code of this file.
Classes | |
struct | bad_lexical_cast |
Thrown when a lexical_cast fails. More... | |
class | util::unique_ptr< T > |
struct | util::detail::contains_impl< Container, Value > |
A struct that exists to implement a generic wrapper for std::find. More... | |
struct | util::detail::contains_impl< Container, typename Container::key_type > |
A struct that exists to implement a generic wrapper for the find() member of associative containers. More... | |
Namespaces | |
util | |
util::detail | |
Macros | |
#define | LIKELY(a) a |
#define | UNLIKELY(a) a |
#define | fxp_shift 8 |
#define | fxp_base (1 << fxp_shift) |
#define | ftofxp(x) (fixed_t((x) * fxp_base)) |
IN: float or int - OUT: fixed_t. More... | |
#define | fxpmult(x, y) (((x)*(y)) >> fxp_shift) |
IN: unsigned and fixed_t - OUT: unsigned. More... | |
#define | fxpdiv(x, y) (((x) << fxp_shift) / (y)) |
IN: unsigned and int - OUT: fixed_t. More... | |
#define | fxptoi(x) ( ((x)>0) ? ((x) >> fxp_shift) : (-((-(x)) >> fxp_shift)) ) |
IN: fixed_t - OUT: int. More... | |
Typedefs | |
typedef int32_t | fixed_t |
Functions | |
template<typename T > | |
bool | is_even (T num) |
template<typename T > | |
bool | is_odd (T num) |
int | bounded_add (int base, int increment, int max_sum, int min_sum=0) |
Returns base + increment, but will not increase base above max_sum, nor decrease it below min_sum. More... | |
int | div100rounded (int num) |
Guarantees portable results for division by 100; round towards 0. More... | |
int | round_damage (int base_damage, int bonus, int divisor) |
round (base_damage * bonus / divisor) to the closest integer, but up or down towards base_damage More... | |
int | round_double (double d) |
double | round_portable (double d) |
template<typename To , typename From > | |
To | lexical_cast (From a) |
template<typename To , typename From > | |
To | lexical_cast_default (From a, To def=To()) |
template<> | |
size_t | lexical_cast< size_t, const std::string & > (const std::string &a) |
template<> | |
size_t | lexical_cast< size_t, const char * > (const char *a) |
template<> | |
size_t | lexical_cast_default< size_t, const std::string & > (const std::string &a, size_t def) |
template<> | |
size_t | lexical_cast_default< size_t, const char * > (const char *a, size_t def) |
template<> | |
long | lexical_cast< long, const std::string & > (const std::string &a) |
template<> | |
long | lexical_cast< long, const char * > (const char *a) |
template<> | |
long | lexical_cast_default< long, const std::string & > (const std::string &a, long def) |
template<> | |
long | lexical_cast_default< long, const char * > (const char *a, long def) |
template<> | |
int | lexical_cast< int, const std::string & > (const std::string &a) |
template<> | |
int | lexical_cast< int, const char * > (const char *a) |
template<> | |
int | lexical_cast_default< int, const std::string & > (const std::string &a, int def) |
template<> | |
int | lexical_cast_default< int, const char * > (const char *a, int def) |
template<> | |
double | lexical_cast< double, const std::string & > (const std::string &a) |
template<> | |
double | lexical_cast< double, const char * > (const char *a) |
template<> | |
double | lexical_cast_default< double, const std::string & > (const std::string &a, double def) |
template<> | |
double | lexical_cast_default< double, const char * > (const char *a, double def) |
template<> | |
float | lexical_cast< float, const std::string & > (const std::string &a) |
template<> | |
float | lexical_cast< float, const char * > (const char *a) |
template<> | |
float | lexical_cast_default< float, const std::string & > (const std::string &a, float def) |
template<> | |
float | lexical_cast_default< float, const char * > (const char *a, float def) |
template<typename To , typename From > | |
To | lexical_cast_in_range (From a, To def, To min, To max) |
template<typename Cmp > | |
bool | in_ranges (const Cmp c, const std::vector< std::pair< Cmp, Cmp > > &ranges) |
bool | chars_equal_insensitive (char a, char b) |
bool | chars_less_insensitive (char a, char b) |
template<typename T > | |
std::size_t | bit_width () |
Returns the size, in bits, of an instance of type T , providing a convenient and self-documenting name for the underlying expression: More... | |
template<typename T > | |
std::size_t | bit_width (const T &) |
Returns the size, in bits, of x , providing a convenient and self-documenting name for the underlying expression: More... | |
template<typename N > | |
unsigned int | count_ones (N n) |
Returns the quantity of 1 bits in n — i.e., n ’s population count. More... | |
template<typename N > | |
unsigned int | count_leading_zeros_impl (N n, std::size_t w) |
template<typename N > | |
unsigned int | count_leading_zeros (N n) |
Returns the quantity of leading 0 bits in n — i.e., the quantity of bits in n , minus the 1-based bit index of the most significant 1 bit in n , or minus 0 if n is 0. More... | |
template<typename N > | |
unsigned int | count_leading_ones (N n) |
Returns the quantity of leading 1 bits in n — i.e., the quantity of bits in n , minus the 1-based bit index of the most significant 0 bit in n , or minus 0 if n contains no 0 bits. More... | |
template<typename Container , typename Value > | |
bool | util::contains (const Container &container, const Value &value) |
Returns true iff value is found in container. More... | |
Templates and utility-routines for strings and numbers.
Definition in file util.hpp.
IN: float or int - OUT: fixed_t.
Definition at line 503 of file util.hpp.
Referenced by unit_type::build_full(), font::floating_label::create_surface(), unit_drawer::draw_bar(), gui::textbox::draw_contents(), image::get_brightened(), game_display::new_turn(), image::o_modification::operator()(), unit_frame::redraw(), unit_drawer::redraw_unit(), display::render_image(), scale_surface(), image::simplify_type(), and submerge_alpha().
IN: unsigned and int - OUT: fixed_t.
Definition at line 509 of file util.hpp.
Referenced by unit_drawer::draw_bar(), game_display::new_turn(), and scale_surface().
IN: unsigned and fixed_t - OUT: unsigned.
Definition at line 506 of file util.hpp.
Referenced by adjust_surface_alpha(), brighten_image(), unit_drawer::draw_bar(), and submerge_alpha().
IN: fixed_t - OUT: int.
Definition at line 512 of file util.hpp.
Referenced by unit_drawer::draw_bar(), and scale_surface().
Definition at line 425 of file util.hpp.
Referenced by floating_point_emulation::detail::tidiv< T, 8 >::idiv(), and tokenizer::next_char_fast().
Definition at line 426 of file util.hpp.
Referenced by buffered_istream::fill_buffer(), buffered_istream::get(), floating_point_emulation::detail::tidiv< T, 8 >::idiv(), tokenizer::next_char(), tokenizer::next_char_fast(), and buffered_istream::peek().
|
inline |
Returns the size, in bits, of an instance of type T
, providing a convenient and self-documenting name for the underlying expression:
sizeof(T) * std::numeric_limits<unsigned char>::digits
T | The return value is the size, in bits, of an instance of this type. |
T
. Definition at line 221 of file util.hpp.
Referenced by BOOST_AUTO_TEST_CASE(), and count_leading_zeros().
|
inline |
Returns the size, in bits, of x
, providing a convenient and self-documenting name for the underlying expression:
sizeof(x) * std::numeric_limits<unsigned char>::digits
T | The type of x . |
x | The return value is the size, in bits, of this object. |
T
. Returns base + increment, but will not increase base above max_sum, nor decrease it below min_sum.
(If base is already beyond the applicable limit, base will be returned.)
Definition at line 44 of file util.hpp.
Referenced by tod_manager::get_illuminated_time_of_day(), and terrain_type::light_bonus().
|
inline |
Definition at line 206 of file util.hpp.
Referenced by events::menu_handler::do_search(), gui2::tgame_load::filter_text_changed(), gui2::tunit_create::filter_text_changed(), mp::gamebrowser::game_matches_filter(), gui::filter_textbox::handle_text_changed(), game_filter_general_string_part::match(), and utils::word_completion().
|
inline |
Definition at line 207 of file util.hpp.
Referenced by gui::menu::basic_sorter::less().
|
inline |
Returns the quantity of leading 1
bits in n
— i.e., the quantity of bits in n
, minus the 1-based bit index of the most significant 0
bit in n
, or minus 0 if n
contains no 0
bits.
N | The type of n . This should be a fundamental integer type that (a) is not wider than unsigned long long int , and (b) is no greater than INT_MAX bits in width; if N does not satisfy these constraints, the return value is undefined. |
n | An integer upon which to operate. |
1
bits in n
, if N
satisfies the above constraints.Definition at line 415 of file util.hpp.
Referenced by BOOST_AUTO_TEST_CASE(), utf8::byte_size_from_utf8_first(), and ucs4_convert_impl::utf8_impl::byte_size_from_utf8_first().
|
inline |
Returns the quantity of leading 0
bits in n
— i.e., the quantity of bits in n
, minus the 1-based bit index of the most significant 1
bit in n
, or minus 0 if n
is 0.
N | The type of n . This should be a fundamental integer type that (a) is not wider than unsigned long long int (the GCC count-leading-zeros built-in functions are defined for unsigned int , unsigned long int , and unsigned long long int ), and (b) is no greater than INT_MAX bits in width (the GCC built-in functions return instances of type int ); if N does not satisfy these constraints, the return value is undefined. |
n | An integer upon which to operate. |
0
bits in n
, if N
satisfies the above constraints.Definition at line 369 of file util.hpp.
References bit_width(), and count_leading_zeros_impl().
Referenced by BOOST_AUTO_TEST_CASE().
|
inline |
Definition at line 338 of file util.hpp.
References count_ones().
Referenced by count_leading_zeros().
|
inline |
Returns the quantity of 1
bits in n
— i.e., n
’s population count.
Algorithm adapted from: https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
This algorithm was chosen for relative simplicity, not for speed.
N | The type of n . This should be a fundamental integer type no greater than UINT_MAX bits in width; if it is not, the return value is undefined. |
n | An integer upon which to operate. |
1
bits in n
, if N
is a fundamental integer type. Definition at line 260 of file util.hpp.
Referenced by BOOST_AUTO_TEST_CASE(), and count_leading_zeros_impl().
Guarantees portable results for division by 100; round towards 0.
Definition at line 52 of file util.hpp.
Referenced by utils::apply_modifier(), and campaign_controller::show_carryover_message().
bool in_ranges | ( | const Cmp | c, |
const std::vector< std::pair< Cmp, Cmp > > & | ranges | ||
) |
Definition at line 195 of file util.hpp.
Referenced by basic_unit_filter_impl::internal_matches_filter(), terrain_filter::match_internal(), and matches_simple_filter().
|
inline |
Definition at line 34 of file util.hpp.
Referenced by actions::shroud_clearer::clear_loc(), default_map_generator_job::default_generate_map(), joystick_manager::get_direction(), is_odd(), and is_vertically_higher_than().
|
inline |
Definition at line 37 of file util.hpp.
References is_even().
Referenced by actions::shroud_clearer::clear_loc(), display::draw_minimap_units(), default_map_generator::generate_map(), joystick_manager::get_direction(), display::get_location_y(), image::getMinimap(), is_vertically_higher_than(), rotate_180_surface(), editor::map_fragment::rotate_60_ccw(), and editor::map_fragment::rotate_60_cw().
To lexical_cast | ( | From | a | ) |
double lexical_cast< double, const char * > | ( | const char * | a | ) |
double lexical_cast< double, const std::string & > | ( | const std::string & | a | ) |
float lexical_cast< float, const char * > | ( | const char * | a | ) |
float lexical_cast< float, const std::string & > | ( | const std::string & | a | ) |
int lexical_cast< int, const char * > | ( | const char * | a | ) |
int lexical_cast< int, const std::string & > | ( | const std::string & | a | ) |
long lexical_cast< long, const char * > | ( | const char * | a | ) |
long lexical_cast< long, const std::string & > | ( | const std::string & | a | ) |
size_t lexical_cast< size_t, const char * > | ( | const char * | a | ) |
size_t lexical_cast< size_t, const std::string & > | ( | const std::string & | a | ) |
To lexical_cast_default | ( | From | a, |
To | def = To() |
||
) |
Definition at line 103 of file util.hpp.
Referenced by t_translation::string_to_builder_number_().
double lexical_cast_default< double, const char * > | ( | const char * | a, |
double | def | ||
) |
double lexical_cast_default< double, const std::string & > | ( | const std::string & | a, |
double | def | ||
) |
float lexical_cast_default< float, const char * > | ( | const char * | a, |
float | def | ||
) |
float lexical_cast_default< float, const std::string & > | ( | const std::string & | a, |
float | def | ||
) |
int lexical_cast_default< int, const char * > | ( | const char * | a, |
int | def | ||
) |
Definition at line 185 of file util.cpp.
Referenced by BOOST_AUTO_TEST_CASE().
int lexical_cast_default< int, const std::string & > | ( | const std::string & | a, |
int | def | ||
) |
long lexical_cast_default< long, const char * > | ( | const char * | a, |
long | def | ||
) |
long lexical_cast_default< long, const std::string & > | ( | const std::string & | a, |
long | def | ||
) |
size_t lexical_cast_default< size_t, const char * > | ( | const char * | a, |
size_t | def | ||
) |
size_t lexical_cast_default< size_t, const std::string & > | ( | const std::string & | a, |
size_t | def | ||
) |
To lexical_cast_in_range | ( | From | a, |
To | def, | ||
To | min, | ||
To | max | ||
) |
round (base_damage * bonus / divisor) to the closest integer, but up or down towards base_damage
Definition at line 60 of file util.hpp.
Referenced by attack_info(), and battle_context_unit_stats::battle_context_unit_stats().
|
inline |
Definition at line 67 of file util.hpp.
References floating_point_emulation::floor().
Referenced by statistics::attack_context::attack_expected_damage(), display::draw_minimap_units(), joystick_manager::get_mouse_axis_pair(), joystick_manager::get_scroll_axis_pair(), controller_base::handle_scroll(), display::scroll_to_tiles(), display::scroll_to_xy(), ai::default_recruitment::recruitment::update_average_lawful_bonus(), joystick_manager::update_highlighted_hex(), and ai::default_recruitment::recruitment::update_scouts_wanted().
|
inline |
Definition at line 76 of file util.hpp.
References floating_point_emulation::floor().
Referenced by variant::operator^().