Skip to content

Commit ef0c936

Browse files
authored
Merge pull request #445 from OpenVicProject/prefer/span
Move const reference vectors to span
2 parents 17c1312 + 3b75b5a commit ef0c936

File tree

10 files changed

+28
-19
lines changed

10 files changed

+28
-19
lines changed

src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "ResourceGatheringOperation.hpp"
22

3+
#include <span>
34
#include <vector>
45

56
#include "openvic-simulation/economy/production/Employee.hpp"
@@ -68,7 +69,7 @@ void ResourceGatheringOperation::initialise_rgo_size_multiplier() {
6869
ProvinceInstance& location = *location_ptr;
6970
ModifierEffectCache const& modifier_effect_cache = location.get_modifier_effect_cache();
7071
ProductionType const& production_type = *production_type_nullable;
71-
std::vector<Job> const& jobs = production_type.get_jobs();
72+
std::span<const Job> jobs = production_type.get_jobs();
7273
IndexedMap<PopType, pop_size_t> const& province_pop_type_distribution = location.get_pop_type_distribution();
7374

7475
pop_size_t total_worker_count_in_province = 0; //not counting equivalents
@@ -127,7 +128,7 @@ void ResourceGatheringOperation::rgo_tick(std::vector<fixed_point_t>& reusable_v
127128
}
128129

129130
ProductionType const& production_type = *production_type_nullable;
130-
std::vector<Job> const& jobs = production_type.get_jobs();
131+
std::span<const Job> jobs = production_type.get_jobs();
131132
IndexedMap<PopType, pop_size_t> const& province_pop_type_distribution = location.get_pop_type_distribution();
132133

133134
total_worker_count_in_province_cache = 0; //not counting equivalents
@@ -197,7 +198,7 @@ void ResourceGatheringOperation::hire() {
197198
proportion_to_hire = max_worker_count_real / available_worker_count_real;
198199
}
199200

200-
std::vector<Job> const& jobs = production_type.get_jobs();
201+
std::span<const Job> jobs = production_type.get_jobs();
201202
for (Pop& pop : location.get_mutable_pops()){
202203
PopType const& pop_type = *pop.get_type();
203204
for (Job const& job : jobs) {

src/openvic-simulation/economy/trading/GoodMarket.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ void GoodMarket::execute_buy_orders(
393393
const fixed_point_t new_price,
394394
IndexedMap<CountryInstance, fixed_point_t> const& actual_bought_per_country,
395395
IndexedMap<CountryInstance, fixed_point_t> const& supply_per_country,
396-
std::vector<fixed_point_t> const& quantity_bought_per_order
396+
std::span<const fixed_point_t> quantity_bought_per_order
397397
) {
398398
quantity_traded_yesterday = fixed_point_t::_0();
399399
for (size_t i = 0; i < buy_up_to_orders.size(); i++) {

src/openvic-simulation/economy/trading/GoodMarket.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace OpenVic {
3232
const fixed_point_t new_price,
3333
IndexedMap<CountryInstance, fixed_point_t> const& actual_bought_per_country,
3434
IndexedMap<CountryInstance, fixed_point_t> const& supply_per_country,
35-
std::vector<fixed_point_t> const& quantity_bought_per_order
35+
std::span<const fixed_point_t> quantity_bought_per_order
3636
);
3737

3838
protected:

src/openvic-simulation/history/ProvinceHistory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void ProvinceHistoryManager::reserve_more_province_histories(size_t size) {
172172
}
173173

174174
void ProvinceHistoryManager::lock_province_histories(MapDefinition const& map_definition, bool detailed_errors) {
175-
std::vector<ProvinceDefinition> const& provinces = map_definition.get_province_definitions();
175+
std::span<const ProvinceDefinition> provinces = map_definition.get_province_definitions();
176176

177177
std::vector<bool> province_checklist(provinces.size());
178178
for (auto [province, history_map] : mutable_iterator(province_histories)) {

src/openvic-simulation/map/MapDefinition.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cctype>
55
#include <charconv>
66
#include <cstdint>
7+
#include <span>
78
#include <system_error>
89
#include <vector>
910

@@ -497,7 +498,7 @@ static bool _parse_province_colour(colour_t& colour, std::array<std::string_view
497498
return ret;
498499
}
499500

500-
bool MapDefinition::load_province_definitions(std::vector<LineObject> const& lines) {
501+
bool MapDefinition::load_province_definitions(std::span<const LineObject> lines) {
501502
bool ret = true;
502503

503504
if (lines.empty()) {
@@ -568,7 +569,7 @@ bool MapDefinition::load_region_colours(ast::NodeCPtr root, std::vector<colour_t
568569
})(root);
569570
}
570571

571-
bool MapDefinition::load_region_file(ast::NodeCPtr root, std::vector<colour_t> const& colours) {
572+
bool MapDefinition::load_region_file(ast::NodeCPtr root, std::span<const colour_t> colours) {
572573
const bool ret = expect_dictionary_reserve_length(
573574
regions,
574575
[this, &colours](std::string_view region_identifier, ast::NodeCPtr region_node) -> bool {
@@ -992,7 +993,7 @@ bool MapDefinition::_generate_standard_province_adjacencies() {
992993
return changed;
993994
}
994995

995-
bool MapDefinition::generate_and_load_province_adjacencies(std::vector<LineObject> const& additional_adjacencies) {
996+
bool MapDefinition::generate_and_load_province_adjacencies(std::span<const LineObject> additional_adjacencies) {
996997
bool ret = _generate_standard_province_adjacencies();
997998
if (!ret) {
998999
Logger::error("Failed to generate standard province adjacencies!");

src/openvic-simulation/map/MapDefinition.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <cstdint>
44
#include <filesystem>
5+
#include <span>
56
#include <string_view>
67
#include <vector>
78

@@ -121,13 +122,13 @@ namespace OpenVic {
121122

122123
bool add_region(std::string_view identifier, std::vector<ProvinceDefinition const*>&& provinces, colour_t colour);
123124

124-
bool load_province_definitions(std::vector<ovdl::csv::LineObject> const& lines);
125+
bool load_province_definitions(std::span<const ovdl::csv::LineObject> lines);
125126
/* Must be loaded after adjacencies so we know what provinces are coastal, and so can have a port */
126127
bool load_province_positions(BuildingTypeManager const& building_type_manager, ast::NodeCPtr root);
127128
static bool load_region_colours(ast::NodeCPtr root, std::vector<colour_t>& colours);
128-
bool load_region_file(ast::NodeCPtr root, std::vector<colour_t> const& colours);
129+
bool load_region_file(ast::NodeCPtr root, std::span<const colour_t> colours);
129130
bool load_map_images(fs::path const& province_path, fs::path const& terrain_path, fs::path const& rivers_path, bool detailed_errors);
130-
bool generate_and_load_province_adjacencies(std::vector<ovdl::csv::LineObject> const& additional_adjacencies);
131+
bool generate_and_load_province_adjacencies(std::span<const ovdl::csv::LineObject> additional_adjacencies);
131132
bool load_climate_file(ModifierManager const& modifier_manager, ast::NodeCPtr root);
132133
bool load_continent_file(ModifierManager const& modifier_manager, ast::NodeCPtr root);
133134
};

src/openvic-simulation/map/ProvinceInstance.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "ProvinceInstance.hpp"
22

3+
#include <span>
4+
35
#include "openvic-simulation/country/CountryInstance.hpp"
46
#include "openvic-simulation/defines/Define.hpp"
57
#include "openvic-simulation/DefinitionManager.hpp"
@@ -219,7 +221,7 @@ bool ProvinceInstance::add_pop(Pop&& pop) {
219221
}
220222

221223
bool ProvinceInstance::add_pop_vec(
222-
std::vector<PopBase> const& pop_vec,
224+
std::span<const PopBase> pop_vec,
223225
MarketInstance& market_instance,
224226
ArtisanalProducerFactoryPattern& artisanal_producer_factory_pattern
225227
) {
@@ -410,7 +412,7 @@ fixed_point_t ProvinceInstance::get_modifier_effect_value(ModifierEffect const&
410412

411413
bool ProvinceInstance::convert_rgo_worker_pops_to_equivalent(ProductionType const& production_type) {
412414
bool is_valid_operation = true;
413-
std::vector<Job> const& jobs = production_type.get_jobs();
415+
std::span<const Job> jobs = production_type.get_jobs();
414416
for (Pop& pop : pops) {
415417
for (Job const& job : jobs) {
416418
PopType const* const job_pop_type = job.get_pop_type();

src/openvic-simulation/map/ProvinceInstance.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <span>
4+
35
#include <plf_colony.h>
46

57
#include "openvic-simulation/country/CountryInstance.hpp"
@@ -223,7 +225,7 @@ namespace OpenVic {
223225

224226
bool add_pop(Pop&& pop);
225227
bool add_pop_vec(
226-
std::vector<PopBase> const& pop_vec,
228+
std::span<const PopBase> pop_vec,
227229
MarketInstance& market_instance,
228230
ArtisanalProducerFactoryPattern& artisanal_producer_factory_pattern
229231
);

src/openvic-simulation/utility/BMP.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "BMP.hpp"
22

33
#include <cstring>
4+
#include <span>
45

56
#include "openvic-simulation/types/OrderedContainers.hpp"
67
#include "openvic-simulation/utility/Logger.hpp"
@@ -181,7 +182,7 @@ uint16_t BMP::get_bits_per_pixel() const {
181182
return header.bits_per_pixel;
182183
}
183184

184-
std::vector<BMP::palette_colour_t> const& BMP::get_palette() const {
185+
std::span<const BMP::palette_colour_t> BMP::get_palette() const {
185186
if (!palette_read) {
186187
Logger::warning("Trying to get BMP palette before loading");
187188
}
@@ -218,7 +219,7 @@ bool BMP::read_pixel_data() {
218219
return pixel_data_read;
219220
}
220221

221-
std::vector<uint8_t> const& BMP::get_pixel_data() const {
222+
std::span<const uint8_t> BMP::get_pixel_data() const {
222223
if (!pixel_data_read) {
223224
Logger::warning("Trying to get BMP pixel data before loading");
224225
}

src/openvic-simulation/utility/BMP.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <filesystem>
44
#include <fstream>
5+
#include <span>
56
#include <vector>
67

78
namespace OpenVic {
@@ -56,7 +57,7 @@ namespace OpenVic {
5657
int32_t get_width() const;
5758
int32_t get_height() const;
5859
uint16_t get_bits_per_pixel() const;
59-
std::vector<palette_colour_t> const& get_palette() const;
60-
std::vector<uint8_t> const& get_pixel_data() const;
60+
std::span<const palette_colour_t> get_palette() const;
61+
std::span<const uint8_t> get_pixel_data() const;
6162
};
6263
}

0 commit comments

Comments
 (0)