OpenTTD Source 20261129-master-g72a8bdc861
history_func.hpp File Reference

Functions for storing historical data. More...

Go to the source code of this file.

Functions

void UpdateValidHistory (ValidHistoryMask &valid_history, const HistoryRange &hr, uint cur_month)
 Update mask of valid records for a historical data.
bool IsValidHistory (ValidHistoryMask valid_history, const HistoryRange &hr, uint age)
 Test if history data is valid, without extracting data.
template<typename T>
T SumHistory (typename std::span< const T > history)
 Sum history data elements.
template<typename T>
void RotateHistory (HistoryData< T > &history, ValidHistoryMask valid_history, const HistoryRange &hr, uint cur_month)
 Rotate historical data.
template<typename T, typename Taccrued>
T GetAndResetAccumulatedAverage (Taccrued &total)
 Get an average value for the previous month, as reset for the next month.
template<typename T>
bool GetHistory (const HistoryData< T > &history, ValidHistoryMask valid_history, const HistoryRange &hr, uint age, T &result)
 Get historical data.
template<uint N, typename T, typename... Tfillers>
void FillFromHistory (const HistoryData< T > &history, ValidHistoryMask valid_history, const HistoryRange &hr, Tfillers &&... fillers)
 Fill some data with historical data.
template<uint N, typename T, typename... Tfillers>
void FillFromHistory (const HistoryData< T > *history, ValidHistoryMask valid_history, const HistoryRange &hr, Tfillers &&... fillers)
 Fill some data with optional historical data.
uint16_t RxCompress (uint32_t num)
 Compress unsigned integer into 16 bits, in a way that increases dynamic range at the expense of precision for large values.
uint32_t RxDecompress (uint16_t num)
 Decompress a range-compressed integer.

Variables

static constexpr uint RX_KNEE1 {0x100}
 First knee of range-compressed data.
static constexpr uint RX_KNEE2 {0x1000}
 Second knee of range-compressed data.
static constexpr uint RX_SHIFT1 {3}
 Shift to apply after first knee.
static constexpr uint RX_SHIFT2 {6}
 Shift to apply after second knee.
static constexpr uint RX_OFFSET1 {RX_KNEE1}
 Offset for uncompressed data after first knee.
static constexpr uint RX_OFFSET2 {((RX_KNEE2 - RX_KNEE1) << RX_SHIFT1) + RX_KNEE1}
 Offset for uncompressed data after second knee.

Detailed Description

Functions for storing historical data.

Definition in file history_func.hpp.

Function Documentation

◆ FillFromHistory() [1/2]

template<uint N, typename T, typename... Tfillers>
void FillFromHistory ( const HistoryData< T > & history,
ValidHistoryMask valid_history,
const HistoryRange & hr,
Tfillers &&... fillers )

Fill some data with historical data.

Parameters
historyHistorical data to fill from.
valid_historyMask of valid history records.
hrHistory range to fill with.
fillersFillers to fill with history data.

Definition at line 118 of file history_func.hpp.

References GetHistory(), and T.

Referenced by FillFromHistory(), IndustryProductionGraphWindow::UpdateStatistics(), StationGraphWindow::UpdateStatistics(), and TownCargoGraphWindow::UpdateStatistics().

◆ FillFromHistory() [2/2]

template<uint N, typename T, typename... Tfillers>
void FillFromHistory ( const HistoryData< T > * history,
ValidHistoryMask valid_history,
const HistoryRange & hr,
Tfillers &&... fillers )

Fill some data with optional historical data.

Parameters
historyHistorical data to fill from, or nullptr if not present.
valid_historyMask of valid history records.
hrHistory range to fill with.
fillersFillers to fill with history data.

Definition at line 138 of file history_func.hpp.

References FillFromHistory(), and IsValidHistory().

◆ GetAndResetAccumulatedAverage()

template<typename T, typename Taccrued>
T GetAndResetAccumulatedAverage ( Taccrued & total)

Get an average value for the previous month, as reset for the next month.

Parameters
totalAccrued total to average. Will be reset to zero.
Returns
Average value for the month.

Definition at line 64 of file history_func.hpp.

References ClampTo(), TimerGameEconomy::days_since_last_month, and T.

Referenced by UpdateIndustryStatistics().

◆ GetHistory()

template<typename T>
bool GetHistory ( const HistoryData< T > & history,
ValidHistoryMask valid_history,
const HistoryRange & hr,
uint age,
T & result )

Get historical data.

Template Parameters
Ttype of history data element.
Parameters
historyHistory data to extract from.
valid_historyMask of valid history records.
hrHistory range to get.
ageAge of data to get.
[out]resultExtracted historical data.
Returns
True iff the data for this history range and age is valid.

Definition at line 82 of file history_func.hpp.

References HistoryRange::division, HistoryRange::first, GetHistory(), HasBit(), TimerGameEconomy::month, HistoryRange::periods, SumHistory(), and T.

Referenced by FillFromHistory(), and GetHistory().

◆ IsValidHistory()

bool IsValidHistory ( ValidHistoryMask valid_history,
const HistoryRange & hr,
uint age )

Test if history data is valid, without extracting data.

Parameters
valid_historyMask of valid history records.
hrHistory range to test.
ageAge of data to test.
Returns
True iff the data for history range and age is valid.

Definition at line 47 of file history.cpp.

References HistoryRange::division, HistoryRange::first, HasBit(), IsValidHistory(), TimerGameEconomy::month, and HistoryRange::periods.

Referenced by FillFromHistory(), and IsValidHistory().

◆ RotateHistory()

template<typename T>
void RotateHistory ( HistoryData< T > & history,
ValidHistoryMask valid_history,
const HistoryRange & hr,
uint cur_month )

Rotate historical data.

Note
Call only for the largest history range sub-division.
Template Parameters
Ttype of history data element.
Parameters
historyHistorical data to rotate.
valid_historyMask of valid history records.
hrHistory range to rotate..
cur_monthCurrent economy month.

Definition at line 41 of file history_func.hpp.

References HistoryRange::division, HistoryRange::first, HasBit(), HistoryRange::last, RotateHistory(), SumHistory(), and HistoryRange::total_division.

Referenced by RotateHistory(), and UpdateIndustryStatistics().

◆ RxCompress()

uint16_t RxCompress ( uint32_t num)
inline

Compress unsigned integer into 16 bits, in a way that increases dynamic range at the expense of precision for large values.

Note
The largest compressable value is 3963072. Values beyond this will be clamped.
Parameters
numValue to compress.
Returns
Range compressed value.

Definition at line 174 of file history_func.hpp.

References ClampTo(), RX_KNEE1, RX_KNEE2, RX_OFFSET1, RX_OFFSET2, RX_SHIFT1, and RX_SHIFT2.

Referenced by SumHistory().

◆ RxDecompress()

uint32_t RxDecompress ( uint16_t num)
inline

Decompress a range-compressed integer.

Parameters
numRange compressed value.
Returns
Decompressed value.

Definition at line 186 of file history_func.hpp.

References RX_KNEE1, RX_KNEE2, RX_OFFSET1, RX_OFFSET2, RX_SHIFT1, and RX_SHIFT2.

Referenced by SumHistory(), and StationGraphWindow::UpdateStatistics().

◆ SumHistory()

template<typename T>
T SumHistory ( typename std::span< const T > history)

Sum history data elements.

Note
The summation should prevent overflowing, and perform transformations relevant to the type of data.
Template Parameters
Ttype of history data element.
Parameters
historyHistory elements to sum.
Returns
Sum of history elements.

References T.

Referenced by GetHistory(), and RotateHistory().

◆ UpdateValidHistory()

void UpdateValidHistory ( ValidHistoryMask & valid_history,
const HistoryRange & hr,
uint cur_month )

Update mask of valid records for a historical data.

Note
Call only for the largest history range sub-division.
Parameters
[in,out]valid_historyValid history records.
hrHistory range to update mask for.
cur_monthCurrent economy month.

Definition at line 25 of file history.cpp.

References HistoryRange::division, HistoryRange::first, GB(), HasBit(), HistoryRange::last, HistoryRange::records, SB(), HistoryRange::total_division, and UpdateValidHistory().

Referenced by DoCreateNewIndustry(), UpdateIndustryStatistics(), and UpdateValidHistory().

Variable Documentation

◆ RX_KNEE1

uint RX_KNEE1 {0x100}
staticconstexpr

First knee of range-compressed data.

Definition at line 156 of file history_func.hpp.

Referenced by RxCompress(), and RxDecompress().

◆ RX_KNEE2

uint RX_KNEE2 {0x1000}
staticconstexpr

Second knee of range-compressed data.

Definition at line 158 of file history_func.hpp.

Referenced by RxCompress(), and RxDecompress().

◆ RX_OFFSET1

uint RX_OFFSET1 {RX_KNEE1}
staticconstexpr

Offset for uncompressed data after first knee.

Definition at line 164 of file history_func.hpp.

Referenced by RxCompress(), and RxDecompress().

◆ RX_OFFSET2

uint RX_OFFSET2 {((RX_KNEE2 - RX_KNEE1) << RX_SHIFT1) + RX_KNEE1}
staticconstexpr

Offset for uncompressed data after second knee.

Definition at line 166 of file history_func.hpp.

Referenced by RxCompress(), and RxDecompress().

◆ RX_SHIFT1

uint RX_SHIFT1 {3}
staticconstexpr

Shift to apply after first knee.

Definition at line 160 of file history_func.hpp.

Referenced by RxCompress(), and RxDecompress().

◆ RX_SHIFT2

uint RX_SHIFT2 {6}
staticconstexpr

Shift to apply after second knee.

Definition at line 162 of file history_func.hpp.

Referenced by RxCompress(), and RxDecompress().