OpenTTD Source: src/misc/history_func.hpp File Reference

OpenTTD Source 15.3

Functions
history_func.hpp File Reference

Functions for storing historical data. More...

`#include "../core/bitmath_func.hpp"`
`#include "../core/math_func.hpp"`
`#include "../timer/timer_game_economy.h"`
`#include "history_type.hpp"`

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.

## 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

  history | Historical data to fill from.
  valid_history | Mask of valid history records.
  hr | History range to fill with.
  fillers | Fillers to fill with history data.

Definition at line 119 of file history_func.hpp.

References GetHistory().

## ◆ 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

  history | Historical data to fill from, or nullptr if not present.
  valid_history | Mask of valid history records.
  hr | History range to fill with.
  fillers | Fillers to fill with history data.

Definition at line 139 of file history_func.hpp.

References 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

  total | Accrued total to average. Will be reset to zero.

Returns
  Average value for the month.

Definition at line 64 of file history_func.hpp.

References TimerGameEconomy::days_since_last_month.

## ◆ 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

  T | type of history data element.

Parameters

  | history | History data to extract from.
  | valid_history | Mask of valid history records.
  | hr | History range to get.
  | age | Age of data to get.
  | cur_month | Current economy month.
  [out] | result | Extracted historical data.

Returns
  True iff the data for this history range and age is valid.

Definition at line 83 of file history_func.hpp.

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

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_history | Mask of valid history records.
  hr | History range to test.
  age | Age 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

  T | type of history data element.

Parameters

  history | Historical data to rotate.
  valid_history | Mask of valid history records.
  hr | History range to rotate..
  cur_month | Current economy month.

Definition at line 41 of file history_func.hpp.

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

Referenced by RotateHistory(), and UpdateIndustryStatistics().

## ◆ 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

  T | type of history data element.

Parameters

  history | History elements to sum.

Returns
  Sum of history elements.

## ◆ 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_history | Valid history records.
  | hr | History range to update mask for.
  | cur_month | Current 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().