From b24ca2db4e9fbb089cb4a6adb78ac18364690412 Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Wed, 5 Nov 2025 12:09:11 -0500 Subject: [PATCH] Refactor types and utility directories to core Add more explicit header includes Format header includes Responsibility of classes and functions in headers moved to core have been reduced --- misc/scripts/exp_lut_generator.py | 2 +- misc/scripts/sin_lut_generator.py | 2 +- src/headless/main.cpp | 23 +- src/openvic-simulation/Alias.hpp | 13 + src/openvic-simulation/GameManager.cpp | 12 +- src/openvic-simulation/GameManager.hpp | 6 +- src/openvic-simulation/InstanceManager.cpp | 2 +- src/openvic-simulation/InstanceManager.hpp | 7 +- .../console/ConsoleInstance.cpp | 9 +- .../console/ConsoleInstance.hpp | 18 +- .../{utility => core}/Algorithm.hpp | 0 .../{utility => core}/Compare.hpp | 2 +- .../{utility => core}/FormatValidate.hpp | 0 .../{utility => core}/Hash.hpp | 0 .../{utility => core}/Logger.hpp | 3 +- .../{utility => core}/Math.hpp | 2 +- .../Getters.hpp => core/Property.hpp} | 79 +- .../{types => core}/Signal.hpp | 12 +- .../{utility => core}/Typedefs.hpp | 47 + .../{utility => core/container}/Allocator.hpp | 2 +- .../{types => core/container}/AnyRef.hpp | 0 .../container}/BasicIterator.hpp | 4 +- .../core/container/CaseContainer.hpp | 15 + .../container}/ClampedValue.hpp | 18 +- .../{types => core/container}/CowPtr.hpp | 4 +- .../{types => core/container}/CowVector.hpp | 16 +- .../core/container/FixedVector.hpp | 200 +++ .../{types => core/container}/FlagStrings.cpp | 17 +- .../{types => core/container}/FlagStrings.hpp | 11 +- .../{types => core/container}/FunctionRef.hpp | 2 +- .../core/container/HasColour.hpp | 28 + .../core/container/HasIdentifier.hpp | 56 + .../core/container/HasIdentifierAndColour.hpp | 26 + .../{types => core/container}/HasIndex.hpp | 8 +- .../container}/IdentifierRegistry.hpp | 249 ++-- .../core/container/IndexedFlatMap.hpp | 1159 ++++++++++++++++ .../container/MutableIterator.hpp} | 2 +- .../{types => core/container}/RingBuffer.hpp | 2 +- .../{types => core/container}/StackString.hpp | 2 +- .../container}/ValueHistory.hpp | 30 +- .../{utility => core/error}/Error.hpp | 0 .../{utility => core/error}/ErrorMacros.hpp | 4 +- .../{utility => core/io}/BMP.cpp | 52 +- .../{utility => core/io}/BMP.hpp | 4 +- .../core/memory/Allocator.hpp | 49 + src/openvic-simulation/core/memory/Colony.hpp | 13 + src/openvic-simulation/core/memory/CowPtr.hpp | 12 + .../core/memory/CowVector.hpp | 12 + src/openvic-simulation/core/memory/Deque.hpp | 23 + .../memory}/FixedPointMap.hpp | 13 +- .../core/memory/FixedVector.hpp | 11 + src/openvic-simulation/core/memory/Format.hpp | 27 + src/openvic-simulation/core/memory/New.hpp | 68 + .../core/memory/OrderedMap.hpp | 239 ++++ .../core/memory/OrderedSet.hpp | 57 + src/openvic-simulation/core/memory/Queue.hpp | 10 + .../core/memory/SharedPtr.hpp | 23 + src/openvic-simulation/core/memory/Stack.hpp | 11 + src/openvic-simulation/core/memory/String.hpp | 22 + .../core/memory/StringMap.hpp | 18 + .../core/memory/StringSet.hpp | 14 + .../core/memory/StringStream.hpp | 23 + .../memory/Tracker.hpp} | 0 .../core/memory/UniquePtr.hpp | 108 ++ src/openvic-simulation/core/memory/Vector.hpp | 11 + .../{types => core/object}/Colour.cpp | 0 .../{types => core/object}/Colour.hpp | 166 ++- .../{types => core/object}/Date.cpp | 6 +- .../{types => core/object}/Date.hpp | 56 +- .../object}/FixedPoint.cpp | 2 +- .../object}/FixedPoint.hpp | 71 +- .../object/FixedPoint}/Atomic.hpp | 5 +- .../object/FixedPoint/LUT_2_16_EXP_2001.hpp} | 0 .../object/FixedPoint/LUT_2_16_EXP_e.hpp} | 0 .../object/FixedPoint/LUT_sin.hpp} | 0 .../{types => core/object}/Vector.cpp | 0 .../{types => core/object}/Vector.hpp | 10 +- .../{types => core/object}/VectorN.inc | 0 .../{utility => core/portable}/Deque.hpp | 2 +- .../portable}/ForwardableSpan.hpp | 26 +- .../random}/RandomGenerator.hpp | 2 +- .../random}/WeightedSampling.hpp | 6 +- .../reactive/DependencyTracker.hpp | 17 +- .../reactive/DerivedState.hpp | 33 +- .../reactive/MutableState.hpp | 43 +- .../string/CharConv.hpp} | 167 +-- .../core/string/Utility.hpp | 201 +++ .../{utility => core/template}/Concepts.hpp | 7 +- .../core/template/ContainerHash.hpp | 91 ++ .../{types => core/template}/EnumBitfield.hpp | 0 .../{types => core/thread}/NullMutex.hpp | 2 +- .../{types => core/thread}/SpinMutex.hpp | 0 .../{utility => core/thread}/ThreadPool.cpp | 30 +- .../{utility => core/thread}/ThreadPool.hpp | 41 +- .../{types => core/ui}/TextFormat.hpp | 0 .../country/CountryDefinition.cpp | 14 +- .../country/CountryDefinition.hpp | 12 +- .../country/CountryInstance.cpp | 42 +- .../country/CountryInstance.hpp | 71 +- .../country/CountryInstanceDeps.hpp | 28 +- .../country/CountryParty.hpp | 8 +- .../country/SharedCountryValues.cpp | 2 +- .../country/SharedCountryValues.hpp | 6 +- .../dataloader/Dataloader.cpp | 34 +- .../dataloader/Dataloader.hpp | 8 +- .../dataloader/ModManager.cpp | 19 +- .../dataloader/ModManager.hpp | 9 +- .../dataloader/NodeTools.cpp | 33 +- .../dataloader/NodeTools.hpp | 41 +- .../dataloader/Vic2PathSearch.cpp | 23 +- .../dataloader/Vic2PathSearch_Windows.hpp | 5 +- src/openvic-simulation/defines/AIDefines.hpp | 6 +- .../defines/CountryDefines.hpp | 8 +- src/openvic-simulation/defines/Define.hpp | 4 +- .../defines/DiplomacyDefines.hpp | 6 +- .../defines/EconomyDefines.hpp | 6 +- .../defines/GraphicsDefines.hpp | 2 +- .../defines/MilitaryDefines.hpp | 8 +- .../defines/PopsDefines.hpp | 8 +- .../diplomacy/CountryRelation.hpp | 13 +- .../diplomacy/DiplomaticAction.cpp | 6 +- .../diplomacy/DiplomaticAction.hpp | 4 +- .../economy/BuildingInstance.cpp | 2 + .../economy/BuildingInstance.hpp | 9 +- .../economy/BuildingType.cpp | 12 +- .../economy/BuildingType.hpp | 19 +- .../economy/GoodDefinition.cpp | 4 +- .../economy/GoodDefinition.hpp | 9 +- .../economy/GoodInstance.hpp | 9 +- .../economy/production/ArtisanalProducer.cpp | 20 +- .../economy/production/ArtisanalProducer.hpp | 18 +- .../economy/production/Employee.cpp | 1 + .../economy/production/Employee.hpp | 4 +- .../economy/production/FactoryProducer.cpp | 11 +- .../economy/production/FactoryProducer.hpp | 16 +- .../economy/production/ProductionType.cpp | 35 +- .../economy/production/ProductionType.hpp | 23 +- .../production/ResourceGatheringOperation.cpp | 9 +- .../production/ResourceGatheringOperation.hpp | 12 +- .../ResourceGatheringOperationDeps.hpp | 4 +- .../economy/trading/BuyResult.hpp | 2 +- .../economy/trading/BuyUpToOrder.hpp | 2 +- .../economy/trading/GoodMarket.cpp | 14 +- .../economy/trading/GoodMarket.hpp | 11 +- .../economy/trading/MarketInstance.cpp | 8 +- .../economy/trading/MarketInstance.hpp | 4 +- .../economy/trading/MarketSellOrder.hpp | 4 +- .../economy/trading/SellResult.hpp | 2 +- src/openvic-simulation/history/Bookmark.cpp | 6 +- src/openvic-simulation/history/Bookmark.hpp | 6 +- .../history/CountryHistory.cpp | 14 +- .../history/CountryHistory.hpp | 31 +- .../history/DiplomaticHistory.cpp | 9 +- .../history/DiplomaticHistory.hpp | 8 +- src/openvic-simulation/history/HistoryMap.hpp | 10 +- src/openvic-simulation/history/Period.cpp | 2 +- src/openvic-simulation/history/Period.hpp | 2 +- .../history/ProvinceHistory.cpp | 20 +- .../history/ProvinceHistory.hpp | 25 +- .../interface/GFXObject.cpp | 2 +- .../interface/GFXObject.hpp | 5 +- .../interface/GFXSprite.hpp | 11 +- src/openvic-simulation/interface/GUI.cpp | 9 +- src/openvic-simulation/interface/GUI.hpp | 2 +- src/openvic-simulation/interface/LoadBase.hpp | 4 +- src/openvic-simulation/interface/UI.cpp | 4 +- src/openvic-simulation/map/Crime.cpp | 4 + src/openvic-simulation/map/Crime.hpp | 2 +- src/openvic-simulation/map/MapDefinition.cpp | 38 +- src/openvic-simulation/map/MapDefinition.hpp | 15 +- src/openvic-simulation/map/MapInstance.cpp | 4 +- src/openvic-simulation/map/MapInstance.hpp | 8 +- src/openvic-simulation/map/Mapmode.cpp | 17 +- src/openvic-simulation/map/Mapmode.hpp | 8 +- .../map/ProvinceDefinition.cpp | 7 +- .../map/ProvinceDefinition.hpp | 17 +- .../map/ProvinceInstance.cpp | 4 +- .../map/ProvinceInstance.hpp | 20 +- .../map/ProvinceInstanceDeps.hpp | 8 +- src/openvic-simulation/map/Region.cpp | 2 +- src/openvic-simulation/map/Region.hpp | 4 +- src/openvic-simulation/map/State.cpp | 35 +- src/openvic-simulation/map/State.hpp | 26 +- src/openvic-simulation/map/TerrainType.cpp | 14 +- src/openvic-simulation/map/TerrainType.hpp | 16 +- .../military/Deployment.cpp | 16 +- .../military/Deployment.hpp | 11 +- src/openvic-simulation/military/Leader.hpp | 8 +- .../military/LeaderTrait.hpp | 4 +- .../military/UnitInstance.hpp | 6 +- .../military/UnitInstanceGroup.cpp | 21 +- .../military/UnitInstanceGroup.hpp | 13 +- src/openvic-simulation/military/UnitType.cpp | 17 +- src/openvic-simulation/military/UnitType.hpp | 25 +- src/openvic-simulation/military/Wargoal.cpp | 9 +- src/openvic-simulation/military/Wargoal.hpp | 15 +- src/openvic-simulation/misc/Decision.hpp | 2 +- src/openvic-simulation/misc/Event.cpp | 13 +- src/openvic-simulation/misc/Event.hpp | 14 +- src/openvic-simulation/misc/GameAction.cpp | 16 +- src/openvic-simulation/misc/GameAction.hpp | 2 +- .../misc/GameRulesManager.hpp | 2 +- .../misc/SimulationClock.cpp | 1 + .../misc/SimulationClock.hpp | 4 +- src/openvic-simulation/misc/SongChance.hpp | 2 +- src/openvic-simulation/misc/SoundEffect.hpp | 6 +- src/openvic-simulation/modifier/Modifier.hpp | 4 +- .../modifier/ModifierEffect.cpp | 7 +- .../modifier/ModifierEffect.hpp | 4 +- .../modifier/ModifierEffectCache.hpp | 4 +- .../modifier/ModifierManager.cpp | 6 +- .../modifier/ModifierManager.hpp | 7 +- .../modifier/ModifierSum.cpp | 12 +- .../modifier/ModifierSum.hpp | 8 +- .../modifier/ModifierValue.cpp | 5 +- .../modifier/ModifierValue.hpp | 5 +- .../modifier/StaticModifierCache.hpp | 2 +- .../pathfinding/AStarPathing.cpp | 8 +- .../pathfinding/AStarPathing.hpp | 2 +- .../pathfinding/FringePathing.cpp | 5 +- .../pathfinding/FringePathing.hpp | 2 +- .../pathfinding/PathingBase.hpp | 8 +- .../pathfinding/PointMap.cpp | 16 +- .../pathfinding/PointMap.hpp | 14 +- .../player/PlayerManager.hpp | 4 +- src/openvic-simulation/politics/BaseIssue.hpp | 6 +- .../politics/Government.cpp | 11 +- .../politics/Government.hpp | 15 +- src/openvic-simulation/politics/Ideology.cpp | 11 +- src/openvic-simulation/politics/Ideology.hpp | 13 +- .../politics/IssueManager.cpp | 15 +- .../politics/IssueManager.hpp | 2 +- .../politics/NationalFocus.cpp | 14 +- .../politics/NationalFocus.hpp | 18 +- .../politics/NationalValue.hpp | 2 +- .../politics/PartyPolicy.hpp | 2 +- .../politics/PoliticsInstanceManager.hpp | 6 +- src/openvic-simulation/politics/Rebel.cpp | 16 +- src/openvic-simulation/politics/Rebel.hpp | 13 +- src/openvic-simulation/politics/Reform.hpp | 4 +- src/openvic-simulation/politics/Rule.cpp | 23 +- src/openvic-simulation/politics/Rule.hpp | 17 +- src/openvic-simulation/pop/Culture.cpp | 17 +- src/openvic-simulation/pop/Culture.hpp | 16 +- src/openvic-simulation/pop/Pop.cpp | 39 +- src/openvic-simulation/pop/Pop.hpp | 30 +- src/openvic-simulation/pop/PopManager.cpp | 32 +- src/openvic-simulation/pop/PopManager.hpp | 6 +- src/openvic-simulation/pop/PopType.cpp | 11 +- src/openvic-simulation/pop/PopType.hpp | 40 +- .../pop/PopValuesFromProvince.cpp | 2 +- .../pop/PopValuesFromProvince.hpp | 6 +- src/openvic-simulation/pop/PopsAggregate.cpp | 8 +- src/openvic-simulation/pop/PopsAggregate.hpp | 22 +- src/openvic-simulation/pop/Religion.cpp | 4 +- src/openvic-simulation/pop/Religion.hpp | 9 +- src/openvic-simulation/research/Invention.hpp | 15 +- .../research/Technology.hpp | 22 +- src/openvic-simulation/scripts/Condition.cpp | 17 +- src/openvic-simulation/scripts/Condition.hpp | 7 +- .../scripts/ConditionalWeight.hpp | 8 +- .../testing/Requirement.hpp | 2 +- src/openvic-simulation/testing/TestScript.hpp | 7 +- src/openvic-simulation/testing/Testing.cpp | 6 +- src/openvic-simulation/testing/Testing.hpp | 3 +- .../testing/test_scripts/A_001_file_tests.cpp | 4 +- .../types/BuildingLevel.hpp | 7 - src/openvic-simulation/types/FixedVector.hpp | 198 --- .../types/HasIdentifier.hpp | 112 -- .../types/IndexedFlatMap.hpp | 1173 ----------------- .../types/OrderedContainers.hpp | 206 --- .../types/OrderedContainersMath.hpp | 173 --- src/openvic-simulation/types/PopSize.hpp | 7 - src/openvic-simulation/types/PopSprite.hpp | 7 - .../types/ProvinceLifeRating.hpp | 7 - .../types/TechnologyUnlockLevel.hpp | 7 - src/openvic-simulation/types/UniqueId.hpp | 7 - src/openvic-simulation/types/UnitVariant.hpp | 7 - .../utility/CompilerFeatureTesting.hpp | 41 - .../utility/ConstexprIntToStr.hpp | 58 - src/openvic-simulation/utility/Containers.hpp | 292 ---- .../utility/DequeMemory.hpp | 16 - tests/src/{utility => core}/DualAdjacent.cpp | 3 +- tests/src/{types => core}/Signal.cpp | 4 +- .../{types => core/container}/FixedVector.cpp | 104 +- .../{types => core/container}/RingBuffer.cpp | 2 +- tests/src/{types => core/object}/Approx.hpp | 6 +- .../object}/AtomicFixedPoint.cpp | 4 +- tests/src/{types => core/object}/Colour.cpp | 4 +- tests/src/{types => core/object}/Colour.hpp | 5 +- tests/src/{types => core/object}/Date.cpp | 4 +- .../src/{types => core/object}/FixedPoint.cpp | 5 +- tests/src/{types => core/object}/Numeric.hpp | 3 +- tests/src/{types => core/object}/Timespan.cpp | 4 +- tests/src/{types => core/object}/Vector.hpp | 4 +- tests/src/{types => core/object}/Vector2.cpp | 5 +- tests/src/{types => core/object}/Vector3.cpp | 5 +- tests/src/{types => core/object}/Vector4.cpp | 5 +- .../{utility => core/random}/ExtendedMath.hpp | 0 .../random}/WeightedSampling.cpp | 39 +- tests/src/core/reactive/DerivedState.cpp | 80 ++ .../reactive/MutableState.cpp | 8 +- tests/src/dataloader/NodeTools.cpp | 29 +- tests/src/pathfinding/AStarPathing.cpp | 6 +- tests/src/pathfinding/FringePathing.cpp | 6 +- tests/src/pathfinding/Pathing.hpp | 7 +- tests/src/pathfinding/PointMap.cpp | 2 +- tests/src/utility/reactive/DerivedState.cpp | 79 -- 308 files changed, 4525 insertions(+), 4074 deletions(-) create mode 100644 src/openvic-simulation/Alias.hpp rename src/openvic-simulation/{utility => core}/Algorithm.hpp (100%) rename src/openvic-simulation/{utility => core}/Compare.hpp (92%) rename src/openvic-simulation/{utility => core}/FormatValidate.hpp (100%) rename src/openvic-simulation/{utility => core}/Hash.hpp (100%) rename src/openvic-simulation/{utility => core}/Logger.hpp (98%) rename src/openvic-simulation/{utility => core}/Math.hpp (97%) rename src/openvic-simulation/{utility/Getters.hpp => core/Property.hpp} (76%) rename src/openvic-simulation/{types => core}/Signal.hpp (98%) rename src/openvic-simulation/{utility => core}/Typedefs.hpp (52%) rename src/openvic-simulation/{utility => core/container}/Allocator.hpp (97%) rename src/openvic-simulation/{types => core/container}/AnyRef.hpp (100%) rename src/openvic-simulation/{types => core/container}/BasicIterator.hpp (98%) create mode 100644 src/openvic-simulation/core/container/CaseContainer.hpp rename src/openvic-simulation/{types => core/container}/ClampedValue.hpp (74%) rename src/openvic-simulation/{types => core/container}/CowPtr.hpp (98%) rename src/openvic-simulation/{types => core/container}/CowVector.hpp (98%) create mode 100644 src/openvic-simulation/core/container/FixedVector.hpp rename src/openvic-simulation/{types => core/container}/FlagStrings.cpp (69%) rename src/openvic-simulation/{types => core/container}/FlagStrings.hpp (60%) rename src/openvic-simulation/{types => core/container}/FunctionRef.hpp (98%) create mode 100644 src/openvic-simulation/core/container/HasColour.hpp create mode 100644 src/openvic-simulation/core/container/HasIdentifier.hpp create mode 100644 src/openvic-simulation/core/container/HasIdentifierAndColour.hpp rename src/openvic-simulation/{types => core/container}/HasIndex.hpp (80%) rename src/openvic-simulation/{types => core/container}/IdentifierRegistry.hpp (78%) create mode 100644 src/openvic-simulation/core/container/IndexedFlatMap.hpp rename src/openvic-simulation/{utility/TslHelper.hpp => core/container/MutableIterator.hpp} (98%) rename src/openvic-simulation/{types => core/container}/RingBuffer.hpp (99%) rename src/openvic-simulation/{types => core/container}/StackString.hpp (95%) rename src/openvic-simulation/{types => core/container}/ValueHistory.hpp (96%) rename src/openvic-simulation/{utility => core/error}/Error.hpp (100%) rename src/openvic-simulation/{utility => core/error}/ErrorMacros.hpp (97%) rename src/openvic-simulation/{utility => core/io}/BMP.cpp (82%) rename src/openvic-simulation/{utility => core/io}/BMP.hpp (95%) create mode 100644 src/openvic-simulation/core/memory/Allocator.hpp create mode 100644 src/openvic-simulation/core/memory/Colony.hpp create mode 100644 src/openvic-simulation/core/memory/CowPtr.hpp create mode 100644 src/openvic-simulation/core/memory/CowVector.hpp create mode 100644 src/openvic-simulation/core/memory/Deque.hpp rename src/openvic-simulation/{types/fixed_point => core/memory}/FixedPointMap.hpp (90%) create mode 100644 src/openvic-simulation/core/memory/FixedVector.hpp create mode 100644 src/openvic-simulation/core/memory/Format.hpp create mode 100644 src/openvic-simulation/core/memory/New.hpp create mode 100644 src/openvic-simulation/core/memory/OrderedMap.hpp create mode 100644 src/openvic-simulation/core/memory/OrderedSet.hpp create mode 100644 src/openvic-simulation/core/memory/Queue.hpp create mode 100644 src/openvic-simulation/core/memory/SharedPtr.hpp create mode 100644 src/openvic-simulation/core/memory/Stack.hpp create mode 100644 src/openvic-simulation/core/memory/String.hpp create mode 100644 src/openvic-simulation/core/memory/StringMap.hpp create mode 100644 src/openvic-simulation/core/memory/StringSet.hpp create mode 100644 src/openvic-simulation/core/memory/StringStream.hpp rename src/openvic-simulation/{utility/MemoryTracker.hpp => core/memory/Tracker.hpp} (100%) create mode 100644 src/openvic-simulation/core/memory/UniquePtr.hpp create mode 100644 src/openvic-simulation/core/memory/Vector.hpp rename src/openvic-simulation/{types => core/object}/Colour.cpp (100%) rename src/openvic-simulation/{types => core/object}/Colour.hpp (84%) rename src/openvic-simulation/{types => core/object}/Date.cpp (99%) rename src/openvic-simulation/{types => core/object}/Date.hpp (94%) rename src/openvic-simulation/{types/fixed_point => core/object}/FixedPoint.cpp (96%) rename src/openvic-simulation/{types/fixed_point => core/object}/FixedPoint.hpp (92%) rename src/openvic-simulation/{types/fixed_point => core/object/FixedPoint}/Atomic.hpp (98%) rename src/openvic-simulation/{types/fixed_point/FixedPointLUT_2_16_EXP_2001.hpp => core/object/FixedPoint/LUT_2_16_EXP_2001.hpp} (100%) rename src/openvic-simulation/{types/fixed_point/FixedPointLUT_2_16_EXP_e.hpp => core/object/FixedPoint/LUT_2_16_EXP_e.hpp} (100%) rename src/openvic-simulation/{types/fixed_point/FixedPointLUT_sin.hpp => core/object/FixedPoint/LUT_sin.hpp} (100%) rename src/openvic-simulation/{types => core/object}/Vector.cpp (100%) rename src/openvic-simulation/{types => core/object}/Vector.hpp (83%) rename src/openvic-simulation/{types => core/object}/VectorN.inc (100%) rename src/openvic-simulation/{utility => core/portable}/Deque.hpp (99%) rename src/openvic-simulation/{utility => core/portable}/ForwardableSpan.hpp (93%) rename src/openvic-simulation/{utility => core/random}/RandomGenerator.hpp (97%) rename src/openvic-simulation/{utility => core/random}/WeightedSampling.hpp (80%) rename src/openvic-simulation/{utility => core}/reactive/DependencyTracker.hpp (78%) rename src/openvic-simulation/{utility => core}/reactive/DerivedState.hpp (77%) rename src/openvic-simulation/{utility => core}/reactive/MutableState.hpp (83%) rename src/openvic-simulation/{utility/StringUtils.hpp => core/string/CharConv.hpp} (70%) create mode 100644 src/openvic-simulation/core/string/Utility.hpp rename src/openvic-simulation/{utility => core/template}/Concepts.hpp (97%) create mode 100644 src/openvic-simulation/core/template/ContainerHash.hpp rename src/openvic-simulation/{types => core/template}/EnumBitfield.hpp (100%) rename src/openvic-simulation/{types => core/thread}/NullMutex.hpp (91%) rename src/openvic-simulation/{types => core/thread}/SpinMutex.hpp (100%) rename src/openvic-simulation/{utility => core/thread}/ThreadPool.cpp (93%) rename src/openvic-simulation/{utility => core/thread}/ThreadPool.hpp (68%) rename src/openvic-simulation/{types => core/ui}/TextFormat.hpp (100%) delete mode 100644 src/openvic-simulation/types/BuildingLevel.hpp delete mode 100644 src/openvic-simulation/types/FixedVector.hpp delete mode 100644 src/openvic-simulation/types/HasIdentifier.hpp delete mode 100644 src/openvic-simulation/types/IndexedFlatMap.hpp delete mode 100644 src/openvic-simulation/types/OrderedContainers.hpp delete mode 100644 src/openvic-simulation/types/OrderedContainersMath.hpp delete mode 100644 src/openvic-simulation/types/PopSize.hpp delete mode 100644 src/openvic-simulation/types/PopSprite.hpp delete mode 100644 src/openvic-simulation/types/ProvinceLifeRating.hpp delete mode 100644 src/openvic-simulation/types/TechnologyUnlockLevel.hpp delete mode 100644 src/openvic-simulation/types/UniqueId.hpp delete mode 100644 src/openvic-simulation/types/UnitVariant.hpp delete mode 100644 src/openvic-simulation/utility/CompilerFeatureTesting.hpp delete mode 100644 src/openvic-simulation/utility/ConstexprIntToStr.hpp delete mode 100644 src/openvic-simulation/utility/Containers.hpp delete mode 100644 src/openvic-simulation/utility/DequeMemory.hpp rename tests/src/{utility => core}/DualAdjacent.cpp (99%) rename tests/src/{types => core}/Signal.cpp (99%) rename tests/src/{types => core/container}/FixedVector.cpp (64%) rename tests/src/{types => core/container}/RingBuffer.cpp (99%) rename tests/src/{types => core/object}/Approx.hpp (95%) rename tests/src/{types => core/object}/AtomicFixedPoint.cpp (92%) rename tests/src/{types => core/object}/Colour.cpp (99%) rename tests/src/{types => core/object}/Colour.hpp (87%) rename tests/src/{types => core/object}/Date.cpp (99%) rename tests/src/{types => core/object}/FixedPoint.cpp (99%) rename tests/src/{types => core/object}/Numeric.hpp (91%) rename tests/src/{types => core/object}/Timespan.cpp (96%) rename tests/src/{types => core/object}/Vector.hpp (96%) rename tests/src/{types => core/object}/Vector2.cpp (98%) rename tests/src/{types => core/object}/Vector3.cpp (98%) rename tests/src/{types => core/object}/Vector4.cpp (98%) rename tests/src/{utility => core/random}/ExtendedMath.hpp (100%) rename tests/src/{utility => core/random}/WeightedSampling.cpp (59%) create mode 100644 tests/src/core/reactive/DerivedState.cpp rename tests/src/{utility => core}/reactive/MutableState.cpp (83%) delete mode 100644 tests/src/utility/reactive/DerivedState.cpp diff --git a/misc/scripts/exp_lut_generator.py b/misc/scripts/exp_lut_generator.py index 05800fe9c..5c48ab746 100755 --- a/misc/scripts/exp_lut_generator.py +++ b/misc/scripts/exp_lut_generator.py @@ -77,7 +77,7 @@ def generate_exp_lut(divisor_base: int, divisor_power: int, exp_base: decimal.De source += "}\n// clang-format on\n" fixed_point_lut_path: str = os.path.join( - os.path.dirname(__file__), f"../../src/openvic-simulation/types/fixed_point/FixedPointLUT_{lut_identifier}.hpp" + os.path.dirname(__file__), f"../../src/openvic-simulation/core/object/FixedPoint/LUT_{lut_identifier}.hpp" ) with open(fixed_point_lut_path, "w", newline="\n") as file: file.write(source) diff --git a/misc/scripts/sin_lut_generator.py b/misc/scripts/sin_lut_generator.py index 35e65e45d..a6a0d6e47 100755 --- a/misc/scripts/sin_lut_generator.py +++ b/misc/scripts/sin_lut_generator.py @@ -119,7 +119,7 @@ def generate_sin_lut(precision: int, count_log2: int): source += "}\n// clang-format on\n" fixed_point_sin_path: str = os.path.join( - os.path.dirname(__file__), "../../src/openvic-simulation/types/fixed_point/FixedPointLUT_sin.hpp" + os.path.dirname(__file__), "../../src/openvic-simulation/core/object/FixedPoint/LUT_sin.hpp" ) with open(fixed_point_sin_path, "w", newline="\n") as file: file.write(source) diff --git a/src/headless/main.cpp b/src/headless/main.cpp index 4562290d3..2dcf2de59 100644 --- a/src/headless/main.cpp +++ b/src/headless/main.cpp @@ -1,7 +1,17 @@ +#include +#include #include +#include +#include #include +#include #include +#include #include +#include +#include +#include +#include #include #include @@ -13,6 +23,10 @@ #include #include +#include +#include +#include +#include #include #include #include @@ -20,9 +34,6 @@ #include #include #include -#include -#include -#include using namespace OpenVic; @@ -257,7 +268,7 @@ static bool run_headless(fs::path const& root, memory::vector& m SPDLOG_INFO("===== Ranking system test... ====="); if (game_manager.get_instance_manager()) { const auto print_ranking_list = [ // - ](std::string_view title, OpenVic::utility::forwardable_span countries) -> void { + ](std::string_view title, OpenVic::forwardable_span countries) -> void { memory::string countries_str; for (CountryInstance* country : countries) { countries_str += fmt::format( @@ -275,7 +286,7 @@ static bool run_headless(fs::path const& root, memory::vector& m CountryInstanceManager const& country_instance_manager = game_manager.get_instance_manager()->get_country_instance_manager(); - OpenVic::utility::forwardable_span great_powers = country_instance_manager.get_great_powers(); + OpenVic::forwardable_span great_powers = country_instance_manager.get_great_powers(); print_ranking_list("Great Powers", great_powers); print_ranking_list("Secondary Powers", country_instance_manager.get_secondary_powers()); print_ranking_list("All countries", country_instance_manager.get_total_ranking()); @@ -373,7 +384,7 @@ static bool run_headless(fs::path const& root, memory::vector& m */ int main(int argc, char const* argv[]) { - char const* program_name = StringUtils::get_filename(argc > 0 ? argv[0] : nullptr, ""); + char const* program_name = OpenVic::get_filename(argc > 0 ? argv[0] : nullptr, ""); fs::path root; memory::vector mods; mods.reserve(argc); diff --git a/src/openvic-simulation/Alias.hpp b/src/openvic-simulation/Alias.hpp new file mode 100644 index 000000000..5e2509e76 --- /dev/null +++ b/src/openvic-simulation/Alias.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include + +namespace OpenVic { + using building_level_t = std::int16_t; + using life_rating_t = std::int8_t; + using pop_size_t = std::int32_t; + using pop_sprite_t = std::uint8_t; + using technology_unlock_level_t = std::int8_t; + using unique_id_t = std::uint64_t; + using unit_variant_t = std::uint8_t; +} diff --git a/src/openvic-simulation/GameManager.cpp b/src/openvic-simulation/GameManager.cpp index a29e23550..665271c2e 100644 --- a/src/openvic-simulation/GameManager.cpp +++ b/src/openvic-simulation/GameManager.cpp @@ -1,7 +1,6 @@ #include "GameManager.hpp" #include -#include #include #include @@ -9,9 +8,10 @@ #include +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/memory/OrderedSet.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" #include "openvic-simulation/dataloader/Dataloader.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/utility/Logger.hpp" using namespace OpenVic; @@ -77,7 +77,7 @@ bool GameManager::load_mods(memory::vector const& mods_to_find) bool ret = true; - vector_ordered_set load_list; + memory::vector_ordered_set load_list; /* Check loaded mod descriptors for requested mods, using either full name or user directory name * (Historical Project Mod 0.4.6 or HPM both valid, for example), and load them plus their dependencies. @@ -96,7 +96,7 @@ bool GameManager::load_mods(memory::vector const& mods_to_find) } Mod const* mod_ptr = &*it; - vector_ordered_set dependencies = mod_ptr->generate_dependency_list(&ret); + memory::vector_ordered_set dependencies = mod_ptr->generate_dependency_list(&ret); if(!ret) { continue; } @@ -120,7 +120,7 @@ bool GameManager::load_mods(memory::vector const& mods_to_find) Dataloader::path_vector_t roots = dataloader.get_roots(); roots.reserve(load_list.size() + roots.size()); - vector_ordered_set replace_paths; + memory::vector_ordered_set replace_paths; /* Actually registers all roots and replace paths to be loaded by the game. */ for (Mod const* mod : load_list) { diff --git a/src/openvic-simulation/GameManager.hpp b/src/openvic-simulation/GameManager.hpp index 9ae92f103..57dc6df71 100644 --- a/src/openvic-simulation/GameManager.hpp +++ b/src/openvic-simulation/GameManager.hpp @@ -1,15 +1,15 @@ #pragma once +#include #include #include #include "openvic-simulation/DefinitionManager.hpp" #include "openvic-simulation/InstanceManager.hpp" -#include "openvic-simulation/dataloader/ModManager.hpp" #include "openvic-simulation/dataloader/Dataloader.hpp" -#include "openvic-simulation/misc/GameRulesManager.hpp" +#include "openvic-simulation/dataloader/ModManager.hpp" #include "openvic-simulation/gen/commit_info.gen.hpp" -#include "openvic-simulation/utility/Containers.hpp" +#include "openvic-simulation/misc/GameRulesManager.hpp" #include diff --git a/src/openvic-simulation/InstanceManager.cpp b/src/openvic-simulation/InstanceManager.cpp index 0b94ab463..088a59a68 100644 --- a/src/openvic-simulation/InstanceManager.cpp +++ b/src/openvic-simulation/InstanceManager.cpp @@ -2,7 +2,7 @@ #include "openvic-simulation/DefinitionManager.hpp" #include "openvic-simulation/console/ConsoleInstance.hpp" -#include "openvic-simulation/utility/Logger.hpp" +#include "openvic-simulation/core/Logger.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/InstanceManager.hpp b/src/openvic-simulation/InstanceManager.hpp index eddff8dbe..d5a732077 100644 --- a/src/openvic-simulation/InstanceManager.hpp +++ b/src/openvic-simulation/InstanceManager.hpp @@ -1,6 +1,9 @@ #pragma once #include "openvic-simulation/console/ConsoleInstance.hpp" +#include "openvic-simulation/core/container/FlagStrings.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/thread/ThreadPool.hpp" #include "openvic-simulation/country/CountryInstance.hpp" #include "openvic-simulation/country/CountryInstanceDeps.hpp" #include "openvic-simulation/diplomacy/CountryRelation.hpp" @@ -16,10 +19,6 @@ #include "openvic-simulation/misc/SimulationClock.hpp" #include "openvic-simulation/politics/PoliticsInstanceManager.hpp" #include "openvic-simulation/pop/PopDeps.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/FlagStrings.hpp" -#include "openvic-simulation/utility/ThreadPool.hpp" -#include "openvic-simulation/utility/Containers.hpp" #include diff --git a/src/openvic-simulation/console/ConsoleInstance.cpp b/src/openvic-simulation/console/ConsoleInstance.cpp index 7a6112a61..c464b9481 100644 --- a/src/openvic-simulation/console/ConsoleInstance.cpp +++ b/src/openvic-simulation/console/ConsoleInstance.cpp @@ -6,11 +6,16 @@ #include #include #include +#include #include #include "openvic-simulation/DefinitionManager.hpp" #include "openvic-simulation/InstanceManager.hpp" +#include "openvic-simulation/core/error/ErrorMacros.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/object/Date.hpp" #include "openvic-simulation/country/CountryInstance.hpp" #include "openvic-simulation/map/ProvinceInstance.hpp" #include "openvic-simulation/military/UnitType.hpp" @@ -18,10 +23,6 @@ #include "openvic-simulation/misc/Event.hpp" #include "openvic-simulation/research/Invention.hpp" #include "openvic-simulation/research/Technology.hpp" -#include "openvic-simulation/types/Colour.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/ErrorMacros.hpp" using namespace OpenVic; using namespace std::string_view_literals; diff --git a/src/openvic-simulation/console/ConsoleInstance.hpp b/src/openvic-simulation/console/ConsoleInstance.hpp index 0d008299e..60bdad0ce 100644 --- a/src/openvic-simulation/console/ConsoleInstance.hpp +++ b/src/openvic-simulation/console/ConsoleInstance.hpp @@ -1,18 +1,20 @@ #pragma once +#include #include #include #include +#include +#include #include -#include -#include "openvic-simulation/types/Colour.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/FunctionRef.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/container/FunctionRef.hpp" +#include "openvic-simulation/core/memory/Format.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/object/Date.hpp" #include @@ -99,7 +101,7 @@ namespace OpenVic { Technology const* validate_tech_name(std::string_view value_string); private: - string_map_t commands; + memory::string_map_t commands; write_func_t write_func; diff --git a/src/openvic-simulation/utility/Algorithm.hpp b/src/openvic-simulation/core/Algorithm.hpp similarity index 100% rename from src/openvic-simulation/utility/Algorithm.hpp rename to src/openvic-simulation/core/Algorithm.hpp diff --git a/src/openvic-simulation/utility/Compare.hpp b/src/openvic-simulation/core/Compare.hpp similarity index 92% rename from src/openvic-simulation/utility/Compare.hpp rename to src/openvic-simulation/core/Compare.hpp index 57da83fe9..ffb3835e0 100644 --- a/src/openvic-simulation/utility/Compare.hpp +++ b/src/openvic-simulation/core/Compare.hpp @@ -3,7 +3,7 @@ #include #include -#include "openvic-simulation/utility/Concepts.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" namespace OpenVic { [[nodiscard]] inline constexpr auto three_way_compare(auto&& left, auto&& right) diff --git a/src/openvic-simulation/utility/FormatValidate.hpp b/src/openvic-simulation/core/FormatValidate.hpp similarity index 100% rename from src/openvic-simulation/utility/FormatValidate.hpp rename to src/openvic-simulation/core/FormatValidate.hpp diff --git a/src/openvic-simulation/utility/Hash.hpp b/src/openvic-simulation/core/Hash.hpp similarity index 100% rename from src/openvic-simulation/utility/Hash.hpp rename to src/openvic-simulation/core/Hash.hpp diff --git a/src/openvic-simulation/utility/Logger.hpp b/src/openvic-simulation/core/Logger.hpp similarity index 98% rename from src/openvic-simulation/utility/Logger.hpp rename to src/openvic-simulation/core/Logger.hpp index 11bac05cb..d54d11fec 100644 --- a/src/openvic-simulation/utility/Logger.hpp +++ b/src/openvic-simulation/core/Logger.hpp @@ -9,7 +9,8 @@ #include #include -#include "openvic-simulation/utility/Containers.hpp" +#include "openvic-simulation/core/memory/Stack.hpp" +#include "openvic-simulation/core/memory/String.hpp" namespace spdlog { template diff --git a/src/openvic-simulation/utility/Math.hpp b/src/openvic-simulation/core/Math.hpp similarity index 97% rename from src/openvic-simulation/utility/Math.hpp rename to src/openvic-simulation/core/Math.hpp index af3193e60..8050714c0 100644 --- a/src/openvic-simulation/utility/Math.hpp +++ b/src/openvic-simulation/core/Math.hpp @@ -5,7 +5,7 @@ #include #include -#include "openvic-simulation/utility/Typedefs.hpp" +#include "openvic-simulation/core/Typedefs.hpp" namespace OpenVic { template diff --git a/src/openvic-simulation/utility/Getters.hpp b/src/openvic-simulation/core/Property.hpp similarity index 76% rename from src/openvic-simulation/utility/Getters.hpp rename to src/openvic-simulation/core/Property.hpp index 8851a7de8..5ad5fc740 100644 --- a/src/openvic-simulation/utility/Getters.hpp +++ b/src/openvic-simulation/core/Property.hpp @@ -1,59 +1,14 @@ #pragma once -#include #include -#include #include #include #include -#include -#include "openvic-simulation/utility/Containers.hpp" - -namespace OpenVic::utility { - template - constexpr auto substring_as_array(std::string_view str, std::index_sequence) { - return std::array { str[Idxs]... }; - } - - template - constexpr auto type_name_array() { -#if defined(__clang__) - constexpr auto prefix = std::string_view { "[T = " }; - constexpr auto suffix = std::string_view { "]" }; - constexpr auto function = std::string_view { __PRETTY_FUNCTION__ }; -#elif defined(__GNUC__) - constexpr auto prefix = std::string_view { "with T = " }; - constexpr auto suffix = std::string_view { "]" }; - constexpr auto function = std::string_view { __PRETTY_FUNCTION__ }; -#elif defined(_MSC_VER) - constexpr auto prefix = std::string_view { "type_name_array<" }; - constexpr auto suffix = std::string_view { ">(void)" }; - constexpr auto function = std::string_view { __FUNCSIG__ }; -#else -#error Unsupported compiler -#endif - - constexpr auto start = function.find(prefix) + prefix.size(); - constexpr auto end = function.rfind(suffix); - - static_assert(start < end); - - constexpr auto name = function.substr(start, (end - start)); - return substring_as_array(name, std::make_index_sequence {}); - } - - template - struct type_name_holder { - static inline constexpr auto value = type_name_array(); - }; - - template - constexpr auto type_name() -> std::string_view { - constexpr auto& value = type_name_holder::value; - return std::string_view { value.data(), value.size() }; - } +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/portable/ForwardableSpan.hpp" // IWYU pragma: keep +namespace OpenVic { #if !defined(_MSC_VER) #pragma GCC diagnostic push #pragma clang diagnostic ignored "-Wunknown-warning-option" @@ -82,22 +37,22 @@ namespace OpenVic::utility { #define OV_DETAIL_GET_TYPE_BASE_CLASS(CLASS) \ static constexpr std::string_view get_type_static() { \ - return ::OpenVic::utility::type_name(); \ + return ::OpenVic::type_name(); \ } \ constexpr virtual std::string_view get_type() const = 0; \ static constexpr std::string_view get_base_type_static() { \ - return ::OpenVic::utility::type_name(); \ + return ::OpenVic::type_name(); \ } \ constexpr virtual std::string_view get_base_type() const { \ return get_base_type_static(); \ } \ template \ constexpr bool is_type() const { \ - return get_type().compare(::OpenVic::utility::type_name()) == 0; \ + return get_type().compare(::OpenVic::type_name()) == 0; \ } \ template \ constexpr bool is_derived_from() const { \ - return is_type() || get_base_type().compare(::OpenVic::utility::type_name()) == 0; \ + return is_type() || get_base_type().compare(::OpenVic::type_name()) == 0; \ } \ template \ constexpr T* cast_to() { \ @@ -116,21 +71,21 @@ namespace OpenVic::utility { #define OV_DETAIL_GET_TYPE \ struct _self_type_tag {}; \ - constexpr auto _self_type_helper() -> decltype(::OpenVic::utility::Writer<_self_type_tag, decltype(this)> {}); \ - using type = ::OpenVic::utility::Read<_self_type_tag>; \ + constexpr auto _self_type_helper() -> decltype(::OpenVic::Writer<_self_type_tag, decltype(this)> {}); \ + using type = ::OpenVic::Read<_self_type_tag>; \ static constexpr std::string_view get_type_static() { \ - return ::OpenVic::utility::type_name(); \ + return ::OpenVic::type_name(); \ } \ constexpr std::string_view get_type() const override { \ - return ::OpenVic::utility::type_name>(); \ + return ::OpenVic::type_name>(); \ } #define OV_DETAIL_GET_BASE_TYPE(CLASS) \ static constexpr std::string_view get_base_type_static() { \ - return ::OpenVic::utility::type_name(); \ + return ::OpenVic::type_name(); \ } \ constexpr std::string_view get_base_type() const override { \ - return ::OpenVic::utility::type_name>(); \ + return ::OpenVic::type_name>(); \ } /* Create const and non-const reference getters for a variable, applied to its name in its declaration, e @@ -229,7 +184,8 @@ public: \ // TODO: Special logic to decide argument type and control assignment. #define PROPERTY_RW(NAME, ...) PROPERTY_RW_ACCESS(NAME, private, __VA_ARGS__) -#define PROPERTY_RW_CUSTOM_NAME(NAME, GETTER_NAME, SETTER_NAME, ...) PROPERTY_RW_FULL(NAME, GETTER_NAME, SETTER_NAME, private, __VA_ARGS__) +#define PROPERTY_RW_CUSTOM_NAME(NAME, GETTER_NAME, SETTER_NAME, ...) \ + PROPERTY_RW_FULL(NAME, GETTER_NAME, SETTER_NAME, private, __VA_ARGS__) #define PROPERTY_RW_ACCESS(NAME, ACCESS, ...) PROPERTY_RW_FULL(NAME, get_##NAME, set_##NAME, ACCESS, __VA_ARGS__) #define PROPERTY_RW_FULL(NAME, GETTER_NAME, SETTER_NAME, ACCESS, ...) \ PROPERTY_FULL(NAME, GETTER_NAME, ACCESS, __VA_ARGS__) \ @@ -247,6 +203,7 @@ public: \ #define PROPERTY_PTR_ACCESS(NAME, ACCESS, ...) \ NAME __VA_OPT__(=) __VA_ARGS__; \ static_assert(std::is_pointer_v && !std::is_const_v>); \ +\ public: \ [[nodiscard]] constexpr decltype(NAME) get_##NAME() { \ return NAME; \ @@ -254,14 +211,14 @@ public: \ [[nodiscard]] constexpr std::add_pointer_t>> get_##NAME() const { \ return NAME; \ } \ -ACCESS: + ACCESS: #define SPAN_PROPERTY(NAME) SPAN_PROPERTY_ACCESS(NAME, private) #define SPAN_PROPERTY_ACCESS(NAME, ACCESS) \ NAME; \ \ public: \ - [[nodiscard]] constexpr auto get_##NAME() const -> OpenVic::utility::forwardable_span { \ + [[nodiscard]] constexpr auto get_##NAME() const->OpenVic::forwardable_span { \ return NAME; \ } \ ACCESS: diff --git a/src/openvic-simulation/types/Signal.hpp b/src/openvic-simulation/core/Signal.hpp similarity index 98% rename from src/openvic-simulation/types/Signal.hpp rename to src/openvic-simulation/core/Signal.hpp index a9c439898..9bbb43f80 100644 --- a/src/openvic-simulation/types/Signal.hpp +++ b/src/openvic-simulation/core/Signal.hpp @@ -12,11 +12,13 @@ #include #include -#include "openvic-simulation/types/CowPtr.hpp" -#include "openvic-simulation/types/CowVector.hpp" -#include "openvic-simulation/types/NullMutex.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/container/CowPtr.hpp" +#include "openvic-simulation/core/container/CowVector.hpp" +#include "openvic-simulation/core/memory/New.hpp" +#include "openvic-simulation/core/memory/SharedPtr.hpp" // IWYU pragma: keep +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/thread/NullMutex.hpp" // Based heavily on https://github.com/palacaze/sigslot and https://github.com/mousebyte/sigslot20 diff --git a/src/openvic-simulation/utility/Typedefs.hpp b/src/openvic-simulation/core/Typedefs.hpp similarity index 52% rename from src/openvic-simulation/utility/Typedefs.hpp rename to src/openvic-simulation/core/Typedefs.hpp index 0ed2bf07d..903586025 100644 --- a/src/openvic-simulation/utility/Typedefs.hpp +++ b/src/openvic-simulation/core/Typedefs.hpp @@ -1,5 +1,9 @@ #pragma once +#include +#include +#include + #if defined(__GNUC__) #define OV_likely(x) __builtin_expect(!!(x), 1) #define OV_unlikely(x) __builtin_expect(!!(x), 0) @@ -44,6 +48,49 @@ #endif // OV_NO_UNIQUE_ADDRESS namespace OpenVic { + template + constexpr auto substring_as_array(std::string_view str, std::index_sequence) { + return std::array { str[Idxs]... }; + } + + template + constexpr auto type_name_array() { +#if defined(__clang__) + constexpr auto prefix = std::string_view { "[T = " }; + constexpr auto suffix = std::string_view { "]" }; + constexpr auto function = std::string_view { __PRETTY_FUNCTION__ }; +#elif defined(__GNUC__) + constexpr auto prefix = std::string_view { "with T = " }; + constexpr auto suffix = std::string_view { "]" }; + constexpr auto function = std::string_view { __PRETTY_FUNCTION__ }; +#elif defined(_MSC_VER) + constexpr auto prefix = std::string_view { "type_name_array<" }; + constexpr auto suffix = std::string_view { ">(void)" }; + constexpr auto function = std::string_view { __FUNCSIG__ }; +#else +#error Unsupported compiler +#endif + + constexpr auto start = function.find(prefix) + prefix.size(); + constexpr auto end = function.rfind(suffix); + + static_assert(start < end); + + constexpr auto name = function.substr(start, (end - start)); + return substring_as_array(name, std::make_index_sequence {}); + } + + template + struct type_name_holder { + static inline constexpr auto value = type_name_array(); + }; + + template + constexpr auto type_name() -> std::string_view { + constexpr auto& value = type_name_holder::value; + return std::string_view { value.data(), value.size() }; + } + [[noreturn]] inline void unreachable() { // Uses compiler specific extensions if possible. // Even if no extension is used, undefined behavior is still raised by diff --git a/src/openvic-simulation/utility/Allocator.hpp b/src/openvic-simulation/core/container/Allocator.hpp similarity index 97% rename from src/openvic-simulation/utility/Allocator.hpp rename to src/openvic-simulation/core/container/Allocator.hpp index 5408ca02c..474743536 100644 --- a/src/openvic-simulation/utility/Allocator.hpp +++ b/src/openvic-simulation/core/container/Allocator.hpp @@ -2,7 +2,7 @@ #include -#include "openvic-simulation/utility/Concepts.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" namespace OpenVic { template diff --git a/src/openvic-simulation/types/AnyRef.hpp b/src/openvic-simulation/core/container/AnyRef.hpp similarity index 100% rename from src/openvic-simulation/types/AnyRef.hpp rename to src/openvic-simulation/core/container/AnyRef.hpp diff --git a/src/openvic-simulation/types/BasicIterator.hpp b/src/openvic-simulation/core/container/BasicIterator.hpp similarity index 98% rename from src/openvic-simulation/types/BasicIterator.hpp rename to src/openvic-simulation/core/container/BasicIterator.hpp index 126542092..f1155a4c2 100644 --- a/src/openvic-simulation/types/BasicIterator.hpp +++ b/src/openvic-simulation/core/container/BasicIterator.hpp @@ -3,8 +3,8 @@ #include #include -#include "openvic-simulation/utility/Typedefs.hpp" -#include "openvic-simulation/utility/Compare.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/Compare.hpp" namespace OpenVic { template diff --git a/src/openvic-simulation/core/container/CaseContainer.hpp b/src/openvic-simulation/core/container/CaseContainer.hpp new file mode 100644 index 000000000..52114fbdd --- /dev/null +++ b/src/openvic-simulation/core/container/CaseContainer.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "openvic-simulation/core/template/Concepts.hpp" + +namespace OpenVic { + /* Intermediate struct that "remembers" Case, instead of just decomposing it into its hash and equal components, + * needed so that templates can deduce the Case with which a type was defined. */ + template typename Container, string_map_case Case, typename... Args> + struct template_case_container_t : Container { + using container_t = Container; + using container_t::container_t; + + using case_t = Case; + }; +} diff --git a/src/openvic-simulation/types/ClampedValue.hpp b/src/openvic-simulation/core/container/ClampedValue.hpp similarity index 74% rename from src/openvic-simulation/types/ClampedValue.hpp rename to src/openvic-simulation/core/container/ClampedValue.hpp index 5aea6bf64..15aa06c56 100644 --- a/src/openvic-simulation/types/ClampedValue.hpp +++ b/src/openvic-simulation/core/container/ClampedValue.hpp @@ -2,9 +2,9 @@ #include -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Getters.hpp" -#include "openvic-simulation/utility/reactive/MutableState.hpp" +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/reactive/MutableState.hpp" namespace OpenVic { struct ReadOnlyClampedValue { @@ -13,20 +13,14 @@ namespace OpenVic { fixed_point_t PROPERTY_ACCESS(max, protected); OV_STATE_PROPERTY_ACCESS(fixed_point_t, value, protected); constexpr ReadOnlyClampedValue() {}; - ReadOnlyClampedValue( - const fixed_point_t new_min, - const fixed_point_t new_max - ) : min { new_min }, max { new_max } {}; + ReadOnlyClampedValue(const fixed_point_t new_min, const fixed_point_t new_max) : min { new_min }, max { new_max } {}; }; struct ClampedValue : public ReadOnlyClampedValue { public: constexpr ClampedValue() {}; - ClampedValue( - const fixed_point_t new_min, - const fixed_point_t new_max, - const fixed_point_t new_value - ) : ReadOnlyClampedValue(new_min, new_max) { + ClampedValue(const fixed_point_t new_min, const fixed_point_t new_max, const fixed_point_t new_value) + : ReadOnlyClampedValue(new_min, new_max) { set_value(new_value); }; diff --git a/src/openvic-simulation/types/CowPtr.hpp b/src/openvic-simulation/core/container/CowPtr.hpp similarity index 98% rename from src/openvic-simulation/types/CowPtr.hpp rename to src/openvic-simulation/core/container/CowPtr.hpp index 77bd2f820..e10b32f81 100644 --- a/src/openvic-simulation/types/CowPtr.hpp +++ b/src/openvic-simulation/core/container/CowPtr.hpp @@ -7,8 +7,8 @@ #include #include -#include "openvic-simulation/utility/Concepts.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" namespace OpenVic { /** diff --git a/src/openvic-simulation/types/CowVector.hpp b/src/openvic-simulation/core/container/CowVector.hpp similarity index 98% rename from src/openvic-simulation/types/CowVector.hpp rename to src/openvic-simulation/core/container/CowVector.hpp index edd7fd6f8..e6bc748e6 100644 --- a/src/openvic-simulation/types/CowVector.hpp +++ b/src/openvic-simulation/core/container/CowVector.hpp @@ -11,11 +11,11 @@ #include #include -#include "openvic-simulation/types/BasicIterator.hpp" -#include "openvic-simulation/utility/Allocator.hpp" -#include "openvic-simulation/utility/Compare.hpp" -#include "openvic-simulation/utility/Concepts.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" +#include "openvic-simulation/core/Compare.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/container/Allocator.hpp" +#include "openvic-simulation/core/container/BasicIterator.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" namespace OpenVic { /** @@ -93,8 +93,7 @@ namespace OpenVic { swap(other, *this); } else if (!other.empty()) { _data = _allocate_payload(other.size()); - _data->array_end = - uninitialized_move(other._data->array, other._data->array_end, _data->array, alloc); + _data->array_end = uninitialized_move(other._data->array, other._data->array_end, _data->array, alloc); destroy(_data->array, _data->array_end, alloc); _data->array_end = _data->array; } @@ -436,8 +435,7 @@ namespace OpenVic { if constexpr (move_insertable_allocator) { _relocate(_data->array, _data->array_end, new_data->array, alloc); } else { - new_data->array_end = - uninitialized_move(_data->array, _data->array_end, new_data->array, alloc); + new_data->array_end = uninitialized_move(_data->array, _data->array_end, new_data->array, alloc); destroy(_data->array, _data->array_end, alloc); } _deallocate_payload(_data); diff --git a/src/openvic-simulation/core/container/FixedVector.hpp b/src/openvic-simulation/core/container/FixedVector.hpp new file mode 100644 index 000000000..82da89d97 --- /dev/null +++ b/src/openvic-simulation/core/container/FixedVector.hpp @@ -0,0 +1,200 @@ +#pragma once + +#include +#include +#include +#include + +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" + +namespace OpenVic { + // fixed capacity + not movable + not copyable + template> + class FixedVector { + private: + using allocator_traits = std::allocator_traits; + const size_t _max_size; + size_t _size; + OV_NO_UNIQUE_ADDRESS Allocator _allocator; + T* const _data_start_ptr; + + public: + using const_reference = T const&; + using difference_type = ptrdiff_t; + using reference = T&; + using size_type = size_t; + using value_type = T; + + constexpr size_t size() const { + return _size; + } + constexpr size_t capacity() const { + return _max_size; + } + constexpr size_t max_size() const { + return _max_size; + } + constexpr T* data() { + return _data_start_ptr; + } + constexpr T const* data() const { + return _data_start_ptr; + } + constexpr bool empty() const { + return _size == 0; + } + + explicit FixedVector(const size_t capacity) + : _max_size(capacity), _size(0), _allocator(), _data_start_ptr(allocator_traits::allocate(_allocator, capacity)) {} + + // Generator (size_t i) -> U (where T is constructable from U) + template + // The generator must NOT return a tuple + requires(!specialization_of>, std::tuple>) + // The type must be constructible from the generator's single return value + && std::constructible_from()(std::declval()))> + FixedVector(size_t capacity, GeneratorTemplateType&& generator) + : _max_size(capacity), _size(capacity), _allocator(), + _data_start_ptr(allocator_traits::allocate(_allocator, capacity)) { + for (size_t i = 0; i < capacity; ++i) { + allocator_traits::construct(_allocator, begin() + i, generator(i)); + } + } + + // Generator (size_t i) -> std::tuple (where T is constructable from Args) + template + // The generator must return a tuple + requires specialization_of>, std::tuple> + // The tuple must be constructible into a T + && requires(GeneratorTemplateType&& generator) { + { + std::apply( + [](auto&&... args) { + T obj { std::forward(args)... }; + }, + generator(std::declval()) + ) + }; + } + FixedVector(size_t max_size, GeneratorTemplateType&& generator) + : _max_size(max_size), _size(max_size), _allocator(), + _data_start_ptr(allocator_traits::allocate(_allocator, max_size)) { + for (size_t i = 0; i < max_size; ++i) { + std::apply( + [this, i](auto&&... args) { + allocator_traits::construct(_allocator, begin() + i, std::forward(args)...); + }, + generator(i) + ); + } + } + + FixedVector(const FixedVector&) = delete; + FixedVector& operator=(const FixedVector&) = delete; + FixedVector(FixedVector&&) = delete; + FixedVector& operator=(FixedVector&&) = delete; + + ~FixedVector() { + clear(); + allocator_traits::deallocate(_allocator, _data_start_ptr, _max_size); + } + + using iterator = T*; + using const_iterator = const T*; + + iterator begin() { + return _data_start_ptr; + } + const_iterator begin() const { + return _data_start_ptr; + } + const_iterator cbegin() const { + return _data_start_ptr; + } + + iterator end() { + return begin() + _size; + } + const_iterator end() const { + return begin() + _size; + } + const_iterator cend() const { + return cbegin() + _size; + } + + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + + reverse_iterator rbegin() { + return reverse_iterator(end()); + } + const_reverse_iterator rbegin() const { + return const_reverse_iterator(end()); + } + const_reverse_iterator crbegin() const { + return const_reverse_iterator(end()); + } + + reverse_iterator rend() { + return reverse_iterator(begin()); + } + const_reverse_iterator rend() const { + return const_reverse_iterator(begin()); + } + const_reverse_iterator crend() const { + return const_reverse_iterator(begin()); + } + + T& operator[](size_t index) { + assert(index < _size && "Index out of bounds."); + return _data_start_ptr[index]; + } + const T& operator[](size_t index) const { + assert(index < _size && "Index out of bounds."); + return _data_start_ptr[index]; + } + + T& front() { + assert(!empty()); + return *begin(); + } + const T& front() const { + assert(!empty()); + return *cbegin(); + } + T& back() { + assert(!empty()); + return *(end() - 1); + } + const T& back() const { + assert(!empty()); + return *(cend() - 1); + } + + template + iterator emplace_back(Args&&... args) { + if (_size >= _max_size) { + return end(); + } + allocator_traits::construct(_allocator, begin() + _size, std::forward(args)...); + ++_size; + return end() - 1; + } + + void pop_back() { + if (_size > 0) { + allocator_traits::destroy(_allocator, end() - 1); + --_size; + } + } + + void clear() { + for (iterator it = end(); it != begin();) { + --it; + allocator_traits::destroy(_allocator, it); + } + _size = 0; + } + }; +} diff --git a/src/openvic-simulation/types/FlagStrings.cpp b/src/openvic-simulation/core/container/FlagStrings.cpp similarity index 69% rename from src/openvic-simulation/types/FlagStrings.cpp rename to src/openvic-simulation/core/container/FlagStrings.cpp index 72d3b7b0a..936ddad94 100644 --- a/src/openvic-simulation/types/FlagStrings.cpp +++ b/src/openvic-simulation/core/container/FlagStrings.cpp @@ -1,6 +1,9 @@ #include "FlagStrings.hpp" -#include "openvic-simulation/utility/Logger.hpp" +#include + +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" using namespace OpenVic; @@ -13,10 +16,7 @@ bool FlagStrings::set_flag(std::string_view flag, bool warn) { } if (!flags.emplace(flag).second && warn) { - spdlog::warn_s( - "Attempted to set {} flag \"{}\": already set!", - name, flag - ); + spdlog::warn_s("Attempted to set {} flag \"{}\": already set!", name, flag); } return true; @@ -29,10 +29,7 @@ bool FlagStrings::clear_flag(std::string_view flag, bool warn) { } if (flags.erase(flag) == 0 && warn) { - spdlog::warn_s( - "Attempted to clear {} flag \"{}\": not set!", - name, flag - ); + spdlog::warn_s("Attempted to clear {} flag \"{}\": not set!", name, flag); } return true; @@ -42,7 +39,7 @@ bool FlagStrings::has_flag(std::string_view flag) const { return flags.contains(flag); } -bool FlagStrings::apply_flag_map(string_map_t const& flag_map, bool warn) { +bool FlagStrings::apply_flag_map(memory::string_map_t const& flag_map, bool warn) { bool ret = true; for (auto const& [flag, set] : flag_map) { diff --git a/src/openvic-simulation/types/FlagStrings.hpp b/src/openvic-simulation/core/container/FlagStrings.hpp similarity index 60% rename from src/openvic-simulation/types/FlagStrings.hpp rename to src/openvic-simulation/core/container/FlagStrings.hpp index da9f256f1..bba46066a 100644 --- a/src/openvic-simulation/types/FlagStrings.hpp +++ b/src/openvic-simulation/core/container/FlagStrings.hpp @@ -2,14 +2,15 @@ #include -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" +#include "openvic-simulation/core/memory/StringSet.hpp" namespace OpenVic { - struct FlagStrings { private: - string_set_t PROPERTY(flags); + memory::string_set_t PROPERTY(flags); memory::string name; public: @@ -22,6 +23,6 @@ namespace OpenVic { // Go through the map of flags setting or clearing each based on whether // its value is true or false (used for applying history entries). - bool apply_flag_map(string_map_t const& flag_map, bool warn); + bool apply_flag_map(memory::string_map_t const& flag_map, bool warn); }; } diff --git a/src/openvic-simulation/types/FunctionRef.hpp b/src/openvic-simulation/core/container/FunctionRef.hpp similarity index 98% rename from src/openvic-simulation/types/FunctionRef.hpp rename to src/openvic-simulation/core/container/FunctionRef.hpp index 88f6284bb..432201df7 100644 --- a/src/openvic-simulation/types/FunctionRef.hpp +++ b/src/openvic-simulation/core/container/FunctionRef.hpp @@ -4,7 +4,7 @@ #include #include -#include "openvic-simulation/types/AnyRef.hpp" +#include "openvic-simulation/core/container/AnyRef.hpp" namespace OpenVic { // Based on https://github.com/think-cell/think-cell-library/blob/b9c84dd7fc926fad80829ed49705fa51afe36e87/tc/base/ref.h diff --git a/src/openvic-simulation/core/container/HasColour.hpp b/src/openvic-simulation/core/container/HasColour.hpp new file mode 100644 index 000000000..090437c7d --- /dev/null +++ b/src/openvic-simulation/core/container/HasColour.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/object/Colour.hpp" + +namespace OpenVic { + /* + * Base class for objects with associated colour information. + */ + template + class _HasColour { + const ColourT PROPERTY(colour); + + protected: + _HasColour(ColourT new_colour, bool cannot_be_null) : colour { new_colour } { + assert(!cannot_be_null || !colour.is_null()); + } + _HasColour(_HasColour const&) = default; + + public: + _HasColour(_HasColour&&) = default; + _HasColour& operator=(_HasColour const&) = delete; + _HasColour& operator=(_HasColour&&) = delete; + }; + + using HasColour = _HasColour; + using HasAlphaColour = _HasColour; +} diff --git a/src/openvic-simulation/core/container/HasIdentifier.hpp b/src/openvic-simulation/core/container/HasIdentifier.hpp new file mode 100644 index 000000000..29e2d2362 --- /dev/null +++ b/src/openvic-simulation/core/container/HasIdentifier.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include +#include +#include + +#include + +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" + +namespace OpenVic { + + /* + * Base class for objects with a non-empty string identifier. Uniquely named instances of a type derived from this class + * can be entered into an IdentifierRegistry instance. + */ + class HasIdentifier { + /* Not const so it can be moved rather than needing to be copied. */ + memory::string PROPERTY(identifier); + + protected: + HasIdentifier(std::string_view new_identifier) : identifier { new_identifier } { + assert(!identifier.empty()); + } + HasIdentifier(HasIdentifier const&) = default; + + public: + HasIdentifier(HasIdentifier&&) = default; + HasIdentifier& operator=(HasIdentifier const&) = delete; + HasIdentifier& operator=(HasIdentifier&&) = delete; + }; + + inline std::ostream& operator<<(std::ostream& stream, HasIdentifier const& obj) { + return stream << obj.get_identifier(); + } + inline std::ostream& operator<<(std::ostream& stream, HasIdentifier const* obj) { + return obj != nullptr ? stream << *obj : stream << ""; + } +} + +template +requires(!OpenVic::has_get_name) +struct fmt::formatter : fmt::formatter { + fmt::format_context::iterator format(T const& has_id, fmt::format_context& ctx) const { + return fmt::formatter::format(has_id.get_identifier(), ctx); + } +}; + +template +requires(!OpenVic::has_get_identifier) +struct fmt::formatter : fmt::formatter { + fmt::format_context::iterator format(T const& has_id, fmt::format_context& ctx) const { + return fmt::formatter::format(has_id.get_name(), ctx); + } +}; diff --git a/src/openvic-simulation/core/container/HasIdentifierAndColour.hpp b/src/openvic-simulation/core/container/HasIdentifierAndColour.hpp new file mode 100644 index 000000000..36fd2e921 --- /dev/null +++ b/src/openvic-simulation/core/container/HasIdentifierAndColour.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/HasColour.hpp" + +namespace OpenVic { + /* + * Base class for objects with a unique string identifier and associated colour information. + */ + template + class _HasIdentifierAndColour : public HasIdentifier, public _HasColour { + protected: + _HasIdentifierAndColour(std::string_view new_identifier, ColourT new_colour, bool cannot_be_null) + : HasIdentifier { new_identifier }, _HasColour { new_colour, cannot_be_null } {} + _HasIdentifierAndColour(_HasIdentifierAndColour const&) = default; + + public: + _HasIdentifierAndColour(_HasIdentifierAndColour&&) = default; + _HasIdentifierAndColour& operator=(_HasIdentifierAndColour const&) = delete; + _HasIdentifierAndColour& operator=(_HasIdentifierAndColour&&) = delete; + }; + + using HasIdentifierAndColour = _HasIdentifierAndColour; + using HasIdentifierAndAlphaColour = _HasIdentifierAndColour; +} \ No newline at end of file diff --git a/src/openvic-simulation/types/HasIndex.hpp b/src/openvic-simulation/core/container/HasIndex.hpp similarity index 80% rename from src/openvic-simulation/types/HasIndex.hpp rename to src/openvic-simulation/core/container/HasIndex.hpp index d85b0be17..896a61ea9 100644 --- a/src/openvic-simulation/types/HasIndex.hpp +++ b/src/openvic-simulation/core/container/HasIndex.hpp @@ -1,14 +1,16 @@ #pragma once #include +#include -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/Property.hpp" namespace OpenVic { - template + template class HasIndex { public: using index_t = IndexT; + private: // always 0-based, this may be used for indexing arrays const index_t PROPERTY(index); @@ -26,4 +28,4 @@ namespace OpenVic { return index == rhs.index; } }; -} \ No newline at end of file +} diff --git a/src/openvic-simulation/types/IdentifierRegistry.hpp b/src/openvic-simulation/core/container/IdentifierRegistry.hpp similarity index 78% rename from src/openvic-simulation/types/IdentifierRegistry.hpp rename to src/openvic-simulation/core/container/IdentifierRegistry.hpp index 9bb7b4bc2..51bf09208 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.hpp +++ b/src/openvic-simulation/core/container/IdentifierRegistry.hpp @@ -1,27 +1,35 @@ #pragma once +#include +#include +#include +#include #include +#include + +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" +#include "openvic-simulation/core/memory/UniquePtr.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" -#include "openvic-simulation/utility/Concepts.hpp" -#include "openvic-simulation/utility/Getters.hpp" -#include "openvic-simulation/utility/Logger.hpp" -#include "openvic-simulation/utility/Concepts.hpp" namespace OpenVic { /* Callbacks for trying to add duplicate keys via UniqueKeyRegistry::add_item */ static bool duplicate_fail_callback(std::string_view registry_name, std::string_view duplicate_identifier) { spdlog::error_s( - "Failure adding item to the {} registry - an item with the identifier \"{}\" already exists!", - registry_name, duplicate_identifier + "Failure adding item to the {} registry - an item with the identifier \"{}\" already exists!", registry_name, + duplicate_identifier ); return false; } static bool duplicate_warning_callback(std::string_view registry_name, std::string_view duplicate_identifier) { spdlog::warn_s( - "Warning adding item to the {} registry - an item with the identifier \"{}\" already exists!", - registry_name, duplicate_identifier + "Warning adding item to the {} registry - an item with the identifier \"{}\" already exists!", registry_name, + duplicate_identifier ); return true; } @@ -31,13 +39,12 @@ namespace OpenVic { /* Registry Value Info - the type that is being registered, and a unique identifier string getter. */ template - concept RegistryValueInfo = requires( - typename ValueInfo::internal_value_type& item, typename ValueInfo::internal_value_type const& const_item - ) { - { ValueInfo::get_identifier(item) } -> std::same_as; - { ValueInfo::get_external_value(item) } -> std::same_as; - { ValueInfo::get_external_value(const_item) } -> std::same_as; - }; + concept RegistryValueInfo = + requires(typename ValueInfo::internal_value_type& item, typename ValueInfo::internal_value_type const& const_item) { + { ValueInfo::get_identifier(item) } -> std::same_as; + { ValueInfo::get_external_value(item) } -> std::same_as; + { ValueInfo::get_external_value(const_item) } -> std::same_as; + }; template struct RegistryValueInfoHasGetIdentifier { using internal_value_type = Value; @@ -71,12 +78,11 @@ namespace OpenVic { /* Registry Item Info - how individual elements of the registered type are stored, and type from item getters. */ template typename ItemInfo, typename Value> - concept RegistryItemInfo = requires( - typename ItemInfo::item_type& item, typename ItemInfo::item_type const& const_item - ) { - { ItemInfo::get_value(item) } -> std::same_as; - { ItemInfo::get_value(const_item) } -> std::same_as; - }; + concept RegistryItemInfo = + requires(typename ItemInfo::item_type& item, typename ItemInfo::item_type const& const_item) { + { ItemInfo::get_value(item) } -> std::same_as; + { ItemInfo::get_value(const_item) } -> std::same_as; + }; template struct RegistryItemInfoValue { using item_type = Value; @@ -113,12 +119,9 @@ namespace OpenVic { /* Registry Storage Info - how items are stored and indexed, and item-index conversion functions. */ template typename StorageInfo, typename Item> - concept RegistryStorageInfo = - std::same_as::storage_type::value_type, Item> && - requires( - typename StorageInfo::storage_type& items, typename StorageInfo::storage_type const& const_items, - typename StorageInfo::index_type index - ) { + concept RegistryStorageInfo = std::same_as::storage_type::value_type, Item> && + requires(typename StorageInfo::storage_type& items, typename StorageInfo::storage_type const& const_items, + typename StorageInfo::index_type index) { { StorageInfo::get_back_index(items) } -> std::same_as::index_type>; { StorageInfo::get_item_from_index(items, index) } -> std::same_as; { StorageInfo::get_item_from_index(const_items, index) } -> std::same_as; @@ -157,13 +160,11 @@ namespace OpenVic { template< RegistryValueInfo ValueInfo, /* The type that is being registered and that has unique string identifiers */ template typename _ItemInfo, /* How the type is being stored, usually either by value or std::unique_ptr */ - template typename _StorageInfo = RegistryStorageInfoVector, /* How items are stored, including indexing type */ + template typename _StorageInfo = + RegistryStorageInfoVector, /* How items are stored, including indexing type */ string_map_case Case = StringMapCaseSensitive /* Identifier map parameters */ - > - requires( - RegistryItemInfo<_ItemInfo, typename ValueInfo::internal_value_type> && - RegistryStorageInfo<_StorageInfo, typename _ItemInfo::item_type> - ) + > + requires(RegistryItemInfo<_ItemInfo, typename ValueInfo::internal_value_type> && RegistryStorageInfo<_StorageInfo, typename _ItemInfo::item_type>) class UniqueKeyRegistry { public: using internal_value_type = typename ValueInfo::internal_value_type; @@ -174,7 +175,7 @@ namespace OpenVic { private: using StorageInfo = _StorageInfo; using index_type = typename StorageInfo::index_type; - using identifier_index_map_t = template_string_map_t; + using identifier_index_map_t = memory::template_string_map_t; public: using storage_type = typename StorageInfo::storage_type; @@ -195,10 +196,8 @@ namespace OpenVic { return emplace_via_move(std::move(item), duplicate_fail_callback); } - constexpr bool emplace_via_move( - item_type&& item, - NodeTools::Callback auto duplicate_callback - ) { + constexpr bool + emplace_via_move(item_type&& item, NodeTools::Callback auto duplicate_callback) { if (locked) { spdlog::error_s("Cannot add item to the {} registry - locked!", name); return false; @@ -225,8 +224,7 @@ namespace OpenVic { template constexpr bool emplace_item( const std::string_view new_identifier, - NodeTools::Callback auto duplicate_callback, - Args&&... args + NodeTools::Callback auto duplicate_callback, Args&&... args ) { if (locked) { spdlog::error_s("Cannot add item to the {} registry - locked!", name); @@ -249,19 +247,13 @@ namespace OpenVic { return true; } - + constexpr bool emplace_via_copy(item_type const& item_to_copy) { - return emplace_item( - ValueInfo::get_identifier(ItemInfo::get_value(item_to_copy)), - item_to_copy - ); + return emplace_item(ValueInfo::get_identifier(ItemInfo::get_value(item_to_copy)), item_to_copy); } template - constexpr bool emplace_item( - const std::string_view new_identifier, - Args&&... args - ) { + constexpr bool emplace_item(const std::string_view new_identifier, Args&&... args) { return emplace_item(new_identifier, duplicate_fail_callback, std::forward(args)...); } @@ -302,19 +294,13 @@ namespace OpenVic { constexpr void reserve(std::size_t size) { if constexpr (storage_type_reservable) { if (locked) { - spdlog::error_s( - "Failed to reserve space for {} items in {} registry - already locked!", - size, name - ); + spdlog::error_s("Failed to reserve space for {} items in {} registry - already locked!", size, name); } else { items.reserve(size); identifier_index_map.reserve(size); } } else { - spdlog::error_s( - "Cannot reserve space for {} {} - storage_type not reservable!", - size, name - ); + spdlog::error_s("Cannot reserve space for {} {} - storage_type not reservable!", size, name); } } @@ -357,8 +343,7 @@ namespace OpenVic { return reinterpret_cast(item); \ } \ spdlog::error_s( \ - "Invalid type for item \"{}\": {} (expected {})", \ - identifier, item->get_type(), T::get_type_static() \ + "Invalid type for item \"{}\": {} (expected {})", identifier, item->get_type(), T::get_type_static() \ ); \ } \ return nullptr; \ @@ -378,10 +363,7 @@ namespace OpenVic { if (allow_empty) { \ return true; \ } else { \ - spdlog::log_s( \ - warn ? spdlog::level::warn : spdlog::level::err, \ - "Invalid {} identifier: empty!", name \ - ); \ + spdlog::log_s(warn ? spdlog::level::warn : spdlog::level::err, "Invalid {} identifier: empty!", name); \ return warn; \ } \ } \ @@ -389,10 +371,7 @@ namespace OpenVic { if (item != nullptr) { \ return callback(*item); \ } \ - spdlog::log_s( \ - warn ? spdlog::level::warn : spdlog::level::err, \ - "Invalid {} identifier: {}", name, identifier \ - ); \ + spdlog::log_s(warn ? spdlog::level::warn : spdlog::level::err, "Invalid {} identifier: {}", name, identifier); \ return warn; \ }; \ } \ @@ -432,62 +411,43 @@ namespace OpenVic { return expect_item_assign_and_default(key_value_invalid_callback(name), callback); \ } \ constexpr NodeTools::NodeCallback auto expect_item_dictionary_and_length_and_default( \ - NodeTools::LengthCallback auto length_callback, \ - NodeTools::KeyValueCallback auto default_callback, \ + NodeTools::LengthCallback auto length_callback, NodeTools::KeyValueCallback auto default_callback, \ NodeTools::Callback auto callback \ ) CONST { \ - return NodeTools::expect_list_and_length( \ - length_callback, expect_item_assign_and_default(default_callback, callback) \ - ); \ + return NodeTools::expect_list_and_length(length_callback, expect_item_assign_and_default(default_callback, callback)); \ } \ constexpr NodeTools::NodeCallback auto expect_item_dictionary_and_length( \ NodeTools::LengthCallback auto length_callback, \ NodeTools::Callback auto callback \ ) CONST { \ - return expect_item_dictionary_and_length_and_default( \ - length_callback, \ - key_value_invalid_callback(name), \ - callback \ - ); \ + return expect_item_dictionary_and_length_and_default(length_callback, key_value_invalid_callback(name), callback); \ } \ constexpr NodeTools::NodeCallback auto expect_item_dictionary_and_default( \ NodeTools::KeyValueCallback auto default_callback, \ NodeTools::Callback auto callback \ ) CONST { \ - return expect_item_dictionary_and_length_and_default( \ - NodeTools::default_length_callback, \ - default_callback, \ - callback \ - ); \ + return expect_item_dictionary_and_length_and_default(NodeTools::default_length_callback, default_callback, callback); \ } \ constexpr NodeTools::NodeCallback auto expect_item_dictionary( \ NodeTools::Callback auto callback \ ) CONST { \ return expect_item_dictionary_and_length_and_default( \ - NodeTools::default_length_callback, \ - key_value_invalid_callback(name), \ - callback \ + NodeTools::default_length_callback, key_value_invalid_callback(name), callback \ ); \ } \ constexpr NodeTools::NodeCallback auto expect_item_dictionary_reserve_length_and_default( \ - reservable auto& reservable, \ - NodeTools::KeyValueCallback auto default_callback, \ + reservable auto& reservable, NodeTools::KeyValueCallback auto default_callback, \ NodeTools::Callback auto callback \ ) CONST { \ return expect_item_dictionary_and_length_and_default( \ - NodeTools::reserve_length_callback(reservable), \ - default_callback, \ - callback \ + NodeTools::reserve_length_callback(reservable), default_callback, callback \ ); \ } \ constexpr NodeTools::NodeCallback auto expect_item_dictionary_reserve_length( \ - reservable auto& reservable, \ - NodeTools::Callback auto callback \ + reservable auto& reservable, NodeTools::Callback auto callback \ ) CONST { \ return expect_item_dictionary_and_length_and_default( \ - NodeTools::reserve_length_callback(reservable), \ - key_value_invalid_callback(name), \ - callback \ + NodeTools::reserve_length_callback(reservable), key_value_invalid_callback(name), callback \ ); \ } @@ -526,22 +486,19 @@ namespace OpenVic { * a fixed point to fixed point function fixed_point_functor, which will be applied to ever parsed value. */ template FixedPointFunctor = std::identity> constexpr NodeTools::NodeCallback auto expect_item_decimal_map( - NodeTools::Callback&&> auto callback, + NodeTools::Callback&&> auto callback, FixedPointFunctor fixed_point_functor = {} ) const { return [this, callback, fixed_point_functor](ast::NodeCPtr node) -> bool { - fixed_point_map_t map; - - bool ret = expect_item_dictionary( - [&map, fixed_point_functor](external_value_type const& key, ast::NodeCPtr value) -> bool { - return NodeTools::expect_fixed_point( - [&map, fixed_point_functor, &key](fixed_point_t val) -> bool { - map.emplace(&key, fixed_point_functor(val)); - return true; - } - )(value); - } - )(node); + memory::fixed_point_map_t map; + + bool ret = + expect_item_dictionary([&map, fixed_point_functor](external_value_type const& key, ast::NodeCPtr value) -> bool { + return NodeTools::expect_fixed_point([&map, fixed_point_functor, &key](fixed_point_t val) -> bool { + map.emplace(&key, fixed_point_functor(val)); + return true; + })(value); + })(node); ret &= callback(std::move(map)); @@ -553,52 +510,45 @@ namespace OpenVic { /* Item Specialisations */ template< RegistryValueInfo ValueInfo, template typename StorageInfo = RegistryStorageInfoVector, - string_map_case Case = StringMapCaseSensitive - > - requires - RegistryStorageInfo::item_type> + string_map_case Case = StringMapCaseSensitive> + requires RegistryStorageInfo< + StorageInfo, typename RegistryItemInfoValue::item_type> using ValueRegistry = UniqueKeyRegistry; template< RegistryValueInfo ValueInfo, template typename StorageInfo = RegistryStorageInfoVector, - string_map_case Case = StringMapCaseSensitive - > - requires - RegistryStorageInfo::item_type> + string_map_case Case = StringMapCaseSensitive> + requires RegistryStorageInfo< + StorageInfo, typename RegistryItemInfoInstance::item_type> using InstanceRegistry = UniqueKeyRegistry; template< RegistryValueInfo ValueInfo, template typename StorageInfo = RegistryStorageInfoVector, - string_map_case Case = StringMapCaseSensitive - > - requires - RegistryStorageInfo::item_type> + string_map_case Case = StringMapCaseSensitive> + requires RegistryStorageInfo< + StorageInfo, typename RegistryItemInfoBaseInstance::item_type> using BaseInstanceRegistry = UniqueKeyRegistry; /* has_get_identifier Specialisations */ template< has_get_identifier Value, template typename StorageInfo = RegistryStorageInfoVector, - string_map_case Case = StringMapCaseSensitive - > + string_map_case Case = StringMapCaseSensitive> using IdentifierRegistry = ValueRegistry, StorageInfo, Case>; template< has_get_identifier Value, template typename StorageInfo = RegistryStorageInfoVector, - string_map_case Case = StringMapCaseSensitive - > + string_map_case Case = StringMapCaseSensitive> using IdentifierPointerRegistry = ValueRegistry>, StorageInfo, Case>; template< has_get_identifier Value, template typename StorageInfo = RegistryStorageInfoVector, - string_map_case Case = StringMapCaseSensitive - > + string_map_case Case = StringMapCaseSensitive> using IdentifierInstanceRegistry = InstanceRegistry, StorageInfo, Case>; template< has_get_identifier Value, template typename StorageInfo = RegistryStorageInfoVector, - string_map_case Case = StringMapCaseSensitive - > + string_map_case Case = StringMapCaseSensitive> using IdentifierBaseInstanceRegistry = BaseInstanceRegistry, StorageInfo, Case>; /* Case-Insensitive has_get_identifier Specialisations */ @@ -612,18 +562,19 @@ namespace OpenVic { using CaseInsensitiveIdentifierInstanceRegistry = IdentifierInstanceRegistry; template typename StorageInfo = RegistryStorageInfoVector> - using CaseInsensitiveIdentifierBaseInstanceRegistry = IdentifierBaseInstanceRegistry; + using CaseInsensitiveIdentifierBaseInstanceRegistry = + IdentifierBaseInstanceRegistry; -/* Macros to generate declaration and constant accessor methods for a UniqueKeyRegistry member variable. */ + /* Macros to generate declaration and constant accessor methods for a UniqueKeyRegistry member variable. */ -#define IDENTIFIER_REGISTRY(name, ...) \ - IDENTIFIER_REGISTRY_CUSTOM_PLURAL(name, name##s, __VA_ARGS__) +#define IDENTIFIER_REGISTRY(name, ...) IDENTIFIER_REGISTRY_CUSTOM_PLURAL(name, name##s, __VA_ARGS__) #define IDENTIFIER_REGISTRY_CUSTOM_PLURAL(singular, plural, ...) \ IDENTIFIER_REGISTRY_FULL_CUSTOM(singular, plural, plural, plural, __VA_ARGS__) #define IDENTIFIER_REGISTRY_FULL_CUSTOM(singular, plural, registry, debug_name, ...) \ - registry { #debug_name __VA_OPT__(,) __VA_ARGS__ }; \ + registry { #debug_name __VA_OPT__(, ) __VA_ARGS__ }; \ +\ public: \ constexpr void lock_##plural() { \ registry.lock(); \ @@ -632,11 +583,15 @@ public: \ return registry.is_locked(); \ } \ template \ - constexpr void reserve_##plural(size_t size) requires(decltype(registry)::storage_type_reservable) { \ + constexpr void reserve_##plural(size_t size) \ + requires(decltype(registry)::storage_type_reservable) \ + { \ registry.reserve(size); \ } \ template \ - constexpr void reserve_more_##plural(size_t size) requires(decltype(registry)::storage_type_reservable) { \ + constexpr void reserve_more_##plural(size_t size) \ + requires(decltype(registry)::storage_type_reservable) \ + { \ registry.reserve_more(size); \ } \ constexpr bool has_##singular##_identifier(std::string_view identifier) const { \ @@ -646,7 +601,9 @@ public: \ return registry.size(); \ } \ template \ - constexpr std::size_t get_##plural##_capacity() const requires(decltype(registry)::storage_type_reservable) { \ + constexpr std::size_t get_##plural##_capacity() const \ + requires(decltype(registry)::storage_type_reservable) \ + { \ return registry.capacity(); \ } \ constexpr bool plural##_empty() const { \ @@ -657,7 +614,7 @@ public: \ } \ template FixedPointFunctor = std::identity> \ constexpr NodeTools::NodeCallback auto expect_##singular##_decimal_map( \ - NodeTools::Callback&&> auto callback, \ + NodeTools::Callback&&> auto callback, \ FixedPointFunctor fixed_point_functor = {} \ ) const { \ return registry.expect_item_decimal_map(callback, fixed_point_functor); \ @@ -665,16 +622,15 @@ public: \ IDENTIFIER_REGISTRY_INTERNAL_SHARED(singular, plural, registry, const) \ private: -/* Macros to generate non-constant accessor methods for a UniqueKeyRegistry member variable. */ + /* Macros to generate non-constant accessor methods for a UniqueKeyRegistry member variable. */ -#define IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS(name) \ - IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_CUSTOM_PLURAL(name, name##s) +#define IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS(name) IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_CUSTOM_PLURAL(name, name##s) #define IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_CUSTOM_PLURAL(singular, plural) \ IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_FULL_CUSTOM(singular, plural, plural, plural) #define IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_FULL_CUSTOM(singular, plural, registry, debug_name) \ - IDENTIFIER_REGISTRY_INTERNAL_SHARED(singular, plural, registry,) + IDENTIFIER_REGISTRY_INTERNAL_SHARED(singular, plural, registry, ) #define IDENTIFIER_REGISTRY_INTERNAL_SHARED(singular, plural, registry, const_kw) \ constexpr decltype(registry)::external_value_type const_kw& get_front_##singular() const_kw { \ @@ -683,7 +639,8 @@ public: \ constexpr decltype(registry)::external_value_type const_kw& get_back_##singular() const_kw { \ return registry.back(); \ } \ - constexpr decltype(registry)::external_value_type const_kw* get_##singular##_by_identifier(std::string_view identifier) const_kw { \ + constexpr decltype(registry)::external_value_type const_kw* get_##singular##_by_identifier(std::string_view identifier) \ + const_kw { \ return registry.get_item_by_identifier(identifier); \ } \ template T> \ @@ -731,8 +688,7 @@ public: \ return registry.expect_item_assign(callback); \ } \ constexpr NodeTools::NodeCallback auto expect_##singular##_dictionary_and_length_and_default( \ - NodeTools::LengthCallback auto length_callback, \ - NodeTools::KeyValueCallback auto default_callback, \ + NodeTools::LengthCallback auto length_callback, NodeTools::KeyValueCallback auto default_callback, \ NodeTools::Callback auto callback \ ) const_kw { \ return registry.expect_item_dictionary_and_length_and_default(length_callback, default_callback, callback); \ @@ -749,8 +705,7 @@ public: \ return registry.expect_item_dictionary(callback); \ } \ constexpr NodeTools::NodeCallback auto expect_##singular##_dictionary_reserve_length_and_default( \ - reservable auto& reservable, \ - NodeTools::KeyValueCallback auto default_callback, \ + reservable auto& reservable, NodeTools::KeyValueCallback auto default_callback, \ NodeTools::Callback auto callback \ ) const_kw { \ return registry.expect_item_dictionary_reserve_length_and_default(reservable, default_callback, callback); \ diff --git a/src/openvic-simulation/core/container/IndexedFlatMap.hpp b/src/openvic-simulation/core/container/IndexedFlatMap.hpp new file mode 100644 index 000000000..57b522ebe --- /dev/null +++ b/src/openvic-simulation/core/container/IndexedFlatMap.hpp @@ -0,0 +1,1159 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/memory/FixedVector.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/portable/ForwardableSpan.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" + +#include + +#define OV_IFLATMAP_PROPERTY(KEYTYPE, VALUETYPE, NAME) OV_IFLATMAP_PROPERTY_ACCESS(KEYTYPE, VALUETYPE, NAME, private) +#define OV_IFLATMAP_PROPERTY_ACCESS(KEYTYPE, VALUETYPE, NAME, ACCESS) \ + IndexedFlatMap NAME; \ +\ +public: \ + [[nodiscard]] constexpr IndexedFlatMap const& get_##NAME() const { \ + return NAME; \ + } \ + [[nodiscard]] auto get_##NAME(KEYTYPE const& key) const->decltype(OpenVic::_get_property(NAME.at_index(0))); \ + ACCESS: + +namespace OpenVic { + /** + * @brief A dictionary-like type that uses std::vector for contiguous storage, + * providing O(1) access time by directly using an integer index obtained from the KeyType. + * It strictly assumes that keys provided at construction are ordered and continuous + * in their index values. + * @tparam KeyType The type of keys used to access values. This type must provide a + * `std::size_t getIndex() const` method. + * @tparam ValueType The type of values to be stored in the map. + * + * This class assumes that an integer index can be obtained from the key type + * via its `getIndex()` method. The indices used with this map must be within + * the specified range [min_index, max_index]. + * + * @warning This class stores a `std::span` of the provided keys. This means the + * `IndexedFlatMap` does NOT own the lifetime of the keys. The caller is + * responsible for ensuring that the underlying data (the `std::vector` or array) + * that the `std::span` refers to remains valid and outlives the `IndexedFlatMap` instance. + */ + template + struct IndexedFlatMap { + using keys_span_type = OpenVic::forwardable_span; + using values_vector_type = std::conditional_t< + std::is_move_constructible_v || std::is_copy_constructible_v, memory::vector, + memory::FixedVector>; + + private: + values_vector_type values; + keys_span_type keys; // non-owning! + std::size_t min_index; + std::size_t max_index; + + /** + * @brief Converts an external key's index to an internal vector index. + * Logs a fatal error if the key's index is out of bounds. + * @param key The key whose index is to be converted. + * @return The internal index, or 0 if out of bounds (after logging error). + */ + constexpr std::size_t get_internal_index_from_key(KeyType const& key) const { + static_assert(has_get_index); + const std::size_t index = key.get_index(); + if (index < min_index || index > max_index) { + spdlog::error_s( + "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> attempted to access key with index {} which is outside the " + "map's defined range [{}, {}].", + type_name(), type_name(), index, min_index, max_index + ); + assert(index >= min_index && index <= max_index); + return 0; + } + return index - min_index; + } + + /** + * @brief Validates that the provided span of keys is ordered and continuous. + * Logs errors if validation fails. + * @param new_keys The span of keys to validate. + * @return True if keys are valid, false otherwise. + */ + static bool validate_new_keys(keys_span_type new_keys) { + static_assert(has_get_index); + if (new_keys.empty()) { + spdlog::warn_s( + "DEVELOPER: OpenVic::IndexedFlatMap<{}, {}> should not be constructed with empty key span.", + type_name(), type_name() + ); + return false; + } + + const std::size_t min_index = new_keys.front().get_index(); + const std::size_t max_index = new_keys.back().get_index(); + const std::size_t expected_capacity = max_index - min_index + 1; + + if (new_keys.size() != expected_capacity) { + spdlog::error_s( + "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> must be constructed with a continuous span of keys with " + "incremental indices. Expected capacity {} but got {} keys.", + type_name(), type_name(), expected_capacity, new_keys.size() + ); + assert(new_keys.size() == expected_capacity); + return false; + } + + for (std::size_t i = 0; i < new_keys.size(); ++i) { + if (new_keys[i].get_index() != min_index + i) { + spdlog::error_s( + "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> must be constructed with a continuous span of keys with " + "incremental indices. " + "Expected index {} but got {} at position {}.", + type_name(), type_name(), min_index + i, new_keys[i].get_index(), i + ); + assert(new_keys[i].get_index() == min_index + i); + return false; + } + } + + return true; + } + + // could be rewritten to return iterators for both this and other. + // that would overcomplicate it with const & non-const + template + keys_span_type get_shared_keys( // + IndexedFlatMap const& other + ) const { + if (other.get_min_index() >= min_index && other.get_max_index() <= max_index) { + return other.get_keys(); + } + + const std::size_t min_shared_index = std::max(other.get_min_index(), min_index); + const std::size_t max_shared_index = std::min(other.get_max_index(), max_index); + + if (min_shared_index > max_shared_index) { + return {}; + } + + const std::size_t shared_count = 1 + max_shared_index - min_shared_index; + const std::size_t other_keys_offset = min_shared_index - other.get_min_index(); + return { other.get_keys().data() + other_keys_offset, shared_count }; + } + + /** + * @brief Checks if the 'other' IndexedFlatMap's key span is a subset of 'this' map's key span, + * based purely on index ranges. + * Logs a fatal error if not compatible. + * @param other The other IndexedFlatMap to compare with. + * @return True if compatible as a subset, false otherwise. + */ + template + bool check_subset_span_match(IndexedFlatMap const& other) const { + // Check if 'other's index range is contained within 'this's index range + if (!(other.get_min_index() >= min_index && other.get_max_index() <= max_index)) { + spdlog::error_s( + "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> subset operation requires the right-hand map's index range " + "({}-{}) to be a subset of the left-hand map's index range ({}-{}).", + type_name(), type_name(), other.get_min_index(), other.get_max_index(), min_index, + max_index + ); + assert(other.get_min_index() >= min_index && other.get_max_index() <= max_index); + return false; + } + + // There is no check for keys.data() being identical as KeyType objects are considered functionally equivalent if + // their get_index() values match. + return true; + } + + // vector + constexpr IndexedFlatMap() + requires std::is_move_constructible_v || std::is_copy_constructible_v + : values(), keys(), min_index(0), max_index(0) {} + + // FixedVector + constexpr IndexedFlatMap() + requires(!std::is_move_constructible_v) && (!std::is_copy_constructible_v) + : values(0), keys(), min_index(0), max_index(0) {} + + public: + static constexpr IndexedFlatMap create_empty() { + return {}; + } + + /** vector + * @brief Constructs an IndexedFlatMap based on a provided span of ordered and continuous keys + * and a key-based generator. + * The map's range [min_idx, max_idx] is determined by the first and last key in the span. + * All elements are initialized using the `value_generator`. + * @param new_keys A `std::span` of KeyType objects. These keys MUST be + * ordered by their index and continuous (i.e., `new_keys[i+1].getIndex() == new_keys[i].getIndex() + 1`). + * The `IndexedFlatMap` stores a reference to this span; the caller is responsible + * for ensuring the underlying data outlives this map instance. + * @param value_generator A callable that takes a `KeyType const&` and returns a single argument to construct + * `ValueType`. This is used to generate values for each key in the provided span. + */ + template + requires(std::is_move_constructible_v || std::is_copy_constructible_v) + // value_generator(key) doesn't return std:tuple<...> + && (!specialization_of< + std::remove_cvref_t>, std::tuple>) + // ValueType(value_generator(key)) is valid constructor call + && std::constructible_from< + ValueType, decltype(std::declval()(std::declval()))> + IndexedFlatMap(keys_span_type new_keys, GeneratorTemplateType value_generator) + : keys(new_keys), min_index { new_keys.front().get_index() }, max_index { new_keys.back().get_index() }, values() { + static_assert(has_get_index); + if (!validate_new_keys(new_keys)) { + return; + } + + values.reserve(new_keys.size()); + for (KeyType const& key : keys) { + values.emplace_back( + std::forward()(std::declval()))>( + value_generator(key) + ) + ); + } + } + + /** FixedVector + * @brief Constructs an IndexedFlatMap based on a provided span of ordered and continuous keys + * and a key-based generator. + * The map's range [min_idx, max_idx] is determined by the first and last key in the span. + * All elements are initialized using the `value_generator`. + * @param new_keys A `std::span` of KeyType objects. These keys MUST be + * ordered by their index and continuous (i.e., `new_keys[i+1].getIndex() == new_keys[i].getIndex() + 1`). + * The `IndexedFlatMap` stores a reference to this span; the caller is responsible + * for ensuring the underlying data outlives this map instance. + * @param value_generator A callable that takes a `KeyType const&` and returns a single argument to construct + * `ValueType`. This is used to generate values for each key in the provided span. + */ + template + requires(!std::is_move_constructible_v) && + (!std::is_copy_constructible_v) + // value_generator(key) doesn't return std:tuple<...> + && (!specialization_of< + std::remove_cvref_t>, std::tuple>) + // ValueType(value_generator(key)) is valid constructor call + && std::constructible_from< + ValueType, decltype(std::declval()(std::declval()))> + IndexedFlatMap(keys_span_type new_keys, GeneratorTemplateType value_generator) + : keys(new_keys), min_index { new_keys.front().get_index() }, max_index { new_keys.back().get_index() }, + values(new_keys.size()) { + static_assert(has_get_index); + if (!validate_new_keys(new_keys)) { + return; + } + + for (KeyType const& key : keys) { + values.emplace_back( + std::forward()(std::declval()))>( + value_generator(key) + ) + ); + } + } + + /** vector + * @brief Constructs an IndexedFlatMap based on a provided span of ordered and continuous keys + * and a key-based generator. + * The map's range [min_idx, max_idx] is determined by the first and last key in the span. + * All elements are initialized using the `value_generator`. + * @param new_keys A `std::span` of KeyType objects. These keys MUST be + * ordered by their index and continuous (i.e., `new_keys[i+1].getIndex() == new_keys[i].getIndex() + 1`). + * The `IndexedFlatMap` stores a reference to this span; the caller is responsible + * for ensuring the underlying data outlives this map instance. + * @param value_generator A callable that takes a `KeyType const&` and returns arguments to construct `ValueType`. + * This is used to generate values for each key in the provided span. + */ + template + requires(std::is_move_constructible_v || std::is_copy_constructible_v) + // value_generator(key) returns tuple + && specialization_of< + std::remove_cvref_t>, std::tuple> + // ValueType(...value_generator(key)) is valid constructor call + && requires(GeneratorTemplateType&& value_generator) { + { + std::apply( + [](auto&&... args) { + ValueType obj { std::forward(args)... }; + }, + value_generator(std::declval()) + ) + }; + } + IndexedFlatMap(keys_span_type new_keys, GeneratorTemplateType&& value_generator) + : keys(new_keys), min_index { new_keys.front().get_index() }, max_index { new_keys.back().get_index() }, values() { + static_assert(has_get_index); + if (!validate_new_keys(new_keys)) { + return; + } + + values.reserve(new_keys.size()); + for (KeyType const& key : keys) { + std::apply( + [this](auto&&... args) { + values.emplace_back(std::forward(args)...); + }, + value_generator(key) + ); + } + } + + /** FixedVector + * @brief Constructs an IndexedFlatMap based on a provided span of ordered and continuous keys + * and a key-based generator. + * The map's range [min_idx, max_idx] is determined by the first and last key in the span. + * All elements are initialized using the `value_generator`. + * @param new_keys A `std::span` of KeyType objects. These keys MUST be + * ordered by their index and continuous (i.e., `new_keys[i+1].getIndex() == new_keys[i].getIndex() + 1`). + * The `IndexedFlatMap` stores a reference to this span; the caller is responsible + * for ensuring the underlying data outlives this map instance. + * @param value_generator A callable that takes a `KeyType const&` and returns arguments to construct `ValueType`. + * This is used to generate values for each key in the provided span. + */ + template + requires(!std::is_move_constructible_v) && + (!std::is_copy_constructible_v) + // value_generator(key) returns tuple + && specialization_of< + std::remove_cvref_t>, std::tuple> + // ValueType(...value_generator(key)) is valid constructor call + && requires(GeneratorTemplateType value_generator) { + { + std::apply( + [](auto&&... args) { + ValueType obj { std::forward(args)... }; + }, + value_generator(std::declval()) + ) + }; + } + IndexedFlatMap(keys_span_type new_keys, GeneratorTemplateType value_generator) + : keys(new_keys), min_index { new_keys.front().get_index() }, max_index { new_keys.back().get_index() }, + values(new_keys.size()) { + static_assert(has_get_index); + if (!validate_new_keys(new_keys)) { + return; + } + + for (KeyType const& key : keys) { + std::apply( + [this](auto&&... args) { + values.emplace_back(std::forward(args)...); + }, + value_generator(key) + ); + } + } + + /** vector + * @brief Constructs an IndexedFlatMap based on a provided span of ordered and continuous keys. + * All elements are constructed by passing `KeyType const&` or default-constructed if there is no constructor taking + * `KeyType const&`. + * @param new_keys A `std::span` of KeyType objects. These keys MUST be + * ordered by their index and continuous (i.e., `new_keys[i+1].getIndex() == new_keys[i].getIndex() + 1`). + * The `IndexedFlatMap` stores a reference to this span; the caller is responsible + * for ensuring the underlying data outlives this map instance. + * + * @note This constructor requires `ValueType` to be `std::default_initializable || std::constructible_from`. + */ + IndexedFlatMap(keys_span_type new_keys) + requires(std::is_move_constructible_v || std::is_copy_constructible_v) && + (std::default_initializable || std::constructible_from) + : keys(new_keys), min_index { new_keys.front().get_index() }, max_index { new_keys.back().get_index() }, values() { + static_assert(has_get_index); + if (!validate_new_keys(new_keys)) { + return; + } + + if constexpr (std::constructible_from) { + values.reserve(new_keys.size()); + for (KeyType const& key : keys) { + values.emplace_back(key); + } + } else { + // Resize and default-construct elements + values.resize(new_keys.size()); + } + } + + /** FixedVector + * @brief Constructs an IndexedFlatMap based on a provided span of ordered and continuous keys. + * All elements are constructed by passing `KeyType const&` or default-constructed if there is no constructor taking + * `KeyType const&`. + * @param new_keys A `std::span` of KeyType objects. These keys MUST be + * ordered by their index and continuous (i.e., `new_keys[i+1].getIndex() == new_keys[i].getIndex() + 1`). + * The `IndexedFlatMap` stores a reference to this span; the caller is responsible + * for ensuring the underlying data outlives this map instance. + * + * @note This constructor requires `ValueType` to be `std::default_initializable || std::constructible_from`. + */ + IndexedFlatMap(keys_span_type new_keys) + requires(!std::is_move_constructible_v) && (!std::is_copy_constructible_v) && + (std::default_initializable || std::constructible_from) + : keys(new_keys), min_index { new_keys.front().get_index() }, max_index { new_keys.back().get_index() }, + values(new_keys.size()) { + static_assert(has_get_index); + if (!validate_new_keys(new_keys)) { + return; + } + + if constexpr (std::constructible_from) { + for (KeyType const& key : keys) { + values.emplace_back(key); + } + } else { + // Resize and default-construct elements + for (KeyType const& key : keys) { + values.emplace_back(); + } + } + } + + /** + * @brief Sets the value associated with a key using copy assignment. + */ + void set(KeyType const& key, ValueType const& value) + requires std::assignable_from + { + static_assert(has_get_index); + values[get_internal_index_from_key(key)] = value; + } + + /** + * @brief Sets the value associated with a key using move assignment (via std::swap). + */ + void set(KeyType const& key, ValueType&& value) + requires std::movable + { + static_assert(has_get_index); + std::swap(values[get_internal_index_from_key(key)], value); + } + + constexpr ValueType& at(KeyType const& key) { + static_assert(has_get_index); + return values[get_internal_index_from_key(key)]; + } + + constexpr ValueType const& at(KeyType const& key) const { + static_assert(has_get_index); + return values[get_internal_index_from_key(key)]; + } + + constexpr ValueType& at_index(const std::size_t index) { + if (index < min_index || index > max_index) { + spdlog::error_s( + "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> attempted to access index {} which is outside the map's defined " + "range [{}, {}].", + type_name(), type_name(), index, min_index, max_index + ); + assert(index >= min_index && index <= max_index); + } + return values[index - min_index]; + } + + constexpr ValueType const& at_index(const std::size_t index) const { + if (index < min_index || index > max_index) { + spdlog::error_s( + "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> attempted to access index {} which is outside the map's defined " + "range [{}, {}].", + type_name(), type_name(), index, min_index, max_index + ); + assert(index >= min_index && index <= max_index); + } + return values[index - min_index]; + } + + constexpr bool contains(KeyType const& key) const { + static_assert(has_get_index); + return contains_index(key.get_index()); + } + + constexpr bool contains_index(const std::size_t external_index) const { + return external_index >= min_index && external_index <= max_index; + } + + constexpr keys_span_type const& get_keys() const { + return keys; + } + + constexpr OpenVic::forwardable_span get_values() { + return values; + } + + constexpr OpenVic::forwardable_span get_values() const { + return values; + } + + constexpr std::size_t get_count() const { + return keys.size(); + } + + constexpr bool empty() const { + return keys.empty(); + } + + constexpr std::size_t get_min_index() const { + return min_index; + } + + constexpr std::size_t get_max_index() const { + return max_index; + } + + /** + * @brief Fills all elements in the map with a specified value. + * The capacity and index range remain unchanged. + * @param value The value to fill all elements with. + * + * @note This method requires `ValueType` to be copy-constructible and copy-assignable. + */ + void fill(ValueType const& value) + requires std::copy_constructible && std::assignable_from + { + values.assign(values.size(), value); + } + + constexpr void copy_values_from(IndexedFlatMap const& other) + requires std::assignable_from + { + static_assert(has_get_index); + for (KeyType const& key : get_shared_keys(other)) { + set(key, other.at(key)); + } + } + + /** + * @brief Reinitializes all elements in the map using a provided value generator function. + * This overload is chosen for types that are copyable (have a copy constructor and copy assignment). + * The capacity and index range remain unchanged. + * @param value_generator A callable that takes a `KeyType const&` and + * returns a `ValueType`. This is used to generate a new value for each slot. + * + * @note This method requires `ValueType` to be copy-assignable. + */ + void reinitialize_with_generator(fu2::function value_generator) + requires std::copyable + { + for (iterator it = begin(); it < end(); it++) { + auto& [key, value] = *it; + value = value_generator(key); // Copy/Move assignment + } + } + + /** + * @brief Reinitializes all elements in the map using a provided value generator function. + * This overload is chosen for types that are movable but NOT copyable. + * The capacity and index range remain unchanged. + * @param value_generator A callable that takes a `KeyType const&` and + * returns a `ValueType`. This is used to generate a new value for each slot. + * + * @note This method destroys existing elements and constructs new ones in place, + * which is often more efficient for move-only types. + */ + void reinitialize_with_generator(fu2::function value_generator) + requires std::movable && (!std::copyable) + { + values.clear(); + for (KeyType const& key : keys) { + // Emplace directly, using move construction if generator returns rvalue + values.emplace_back(value_generator(key)); + } + } + + // --- Mathematical Operators (Valarray-like functionality) --- + + // Unary plus operator + IndexedFlatMap operator+() const { + // Unary plus typically returns a copy of itself + return *this; + } + + // Unary minus operator + IndexedFlatMap operator-() const + requires unary_negatable + { + static_assert(has_get_index); + return IndexedFlatMap(keys, [&](KeyType const& key) { + return -this->at(key); + }); + } + + template + IndexedFlatMap operator+(IndexedFlatMap const& other) const + requires addable + { + static_assert(has_get_index); + if (!check_subset_span_match(other)) { + return IndexedFlatMap(); + } + + // Create a new map with the same keys as 'this' + return IndexedFlatMap(keys, [&](KeyType const& key) { + if (other.contains(key)) { + // If the key exists in 'other' (i.e., within its min_index/max_index), + // perform the operation. + return this->at(key) + other.at(key); + } else { + // If the key does not exist in 'other' (i.e., outside its min_index/max_index), + // retain the value from 'this'. + return this->at(key); + } + }); + } + + template + IndexedFlatMap operator-(IndexedFlatMap const& other) const + requires subtractable + { + static_assert(has_get_index); + if (!check_subset_span_match(other)) { + return IndexedFlatMap(); + } + + return IndexedFlatMap(keys, [&](KeyType const& key) { + if (other.contains(key)) { + return this->at(key) - other.at(key); + } else { + return this->at(key); + } + }); + } + + template + IndexedFlatMap operator*(IndexedFlatMap const& other) const + requires multipliable + { + static_assert(has_get_index); + if (!check_subset_span_match(other)) { + return IndexedFlatMap(); + } + + return IndexedFlatMap(keys, [&](KeyType const& key) { + if (other.contains(key)) { + return this->at(key) * other.at(key); + } else { + return this->at(key); + } + }); + } + + template + IndexedFlatMap operator/(IndexedFlatMap const& other) const + requires divisible + { + static_assert(has_get_index); + if (!check_subset_span_match(other)) { + return IndexedFlatMap(); + } + + return IndexedFlatMap(keys, [&](KeyType const& key) { + // Add a basic division by zero check for each element + if (other.contains(key)) { + if (other.at(key) == static_cast(0)) { + spdlog::error_s( + "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> division by zero detected at key index {}.", + type_name(), type_name(), key.get_index() + ); + assert(other.at(key) != static_cast(0)); + // continue and let it throw + } + return this->at(key) / other.at(key); + } else { + return this->at(key); // Retain original value if key not in 'other' + } + }); + } + + template + IndexedFlatMap divide_handle_zero( + IndexedFlatMap const& other, + fu2::function handle_div_by_zero + ) const + requires divisible + { + static_assert(has_get_index); + if (!check_subset_span_match(other)) { + return IndexedFlatMap(); + } + + return IndexedFlatMap(keys, [&](KeyType const& key) { + if (other.contains(key)) { + if (other.at(key) == static_cast(0)) { + return handle_div_by_zero(this->at(key), other.at(key)); + } + return this->at(key) / other.at(key); + } else { + return this->at(key); // Retain original value if key not in 'other' + } + }); + } + + // Binary addition operator (Map + Scalar) + template + IndexedFlatMap operator+(ScalarType const& scalar) const + requires addable + { + static_assert(has_get_index); + return IndexedFlatMap(keys, [&](KeyType const& key) { + return this->at(key) + scalar; + }); + } + + // Binary subtraction operator (Map - Scalar) + template + IndexedFlatMap operator-(ScalarType const& scalar) const + requires subtractable + { + static_assert(has_get_index); + return IndexedFlatMap(keys, [&](KeyType const& key) { + return this->at(key) - scalar; + }); + } + + // Binary multiplication operator (Map * Scalar) + template + IndexedFlatMap operator*(ScalarType const& scalar) const + requires multipliable + { + static_assert(has_get_index); + return IndexedFlatMap(keys, [&](KeyType const& key) { + return this->at(key) * scalar; + }); + } + + // Binary division operator (Map / Scalar) + template + IndexedFlatMap operator/(ScalarType const& scalar) const + requires divisible + { + static_assert(has_get_index); + if (scalar == static_cast(0)) { + spdlog::error_s( + "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> division by zero for scalar operation.", type_name(), + type_name() + ); + assert(scalar != static_cast(0)); + // continue and let it throw + } + return IndexedFlatMap(keys, [&](KeyType const& key) { + return this->at(key) / scalar; + }); + } + + template + IndexedFlatMap& operator+=(IndexedFlatMap const& other) + requires add_assignable + { + static_assert(has_get_index); + if (!check_subset_span_match(other)) { + return *this; // Return current state on error + } + + // Iterate only over the keys that are present in 'other' + for (KeyType const& key : other.get_keys()) { + this->at(key) += other.at(key); + } + return *this; + } + + template + IndexedFlatMap& operator-=(IndexedFlatMap const& other) + requires subtract_assignable + { + static_assert(has_get_index); + if (!check_subset_span_match(other)) { + return *this; + } + + for (KeyType const& key : other.get_keys()) { + this->at(key) -= other.at(key); + } + return *this; + } + + template + IndexedFlatMap& operator*=(IndexedFlatMap const& other) + requires multiply_assignable + { + static_assert(has_get_index); + if (!check_subset_span_match(other)) { + return *this; + } + + for (KeyType const& key : other.get_keys()) { + this->at(key) *= other.at(key); + } + return *this; + } + + template + IndexedFlatMap& operator/=(IndexedFlatMap const& other) + requires divide_assignable + { + static_assert(has_get_index); + if (!check_subset_span_match(other)) { + return *this; + } + + for (KeyType const& key : other.get_keys()) { + if (other.at(key) == static_cast(0)) { + spdlog::error_s( + "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> compound division by zero detected at key index {}.", + type_name(), type_name(), key.get_index() + ); + assert(other.at(key) != static_cast(0)); + // continue and let it throw + } + this->at(key) /= other.at(key); + } + return *this; + } + + template + IndexedFlatMap& divide_assign_handle_zero( + IndexedFlatMap const& other, + fu2::function handle_div_by_zero + ) + requires divide_assignable + { + static_assert(has_get_index); + if (!check_subset_span_match(other)) { + return *this; + } + + for (KeyType const& key : other.get_keys()) { + if (other.at(key) == static_cast(0)) { + handle_div_by_zero(this->at(key), other.at(key)); + } else { + this->at(key) /= other.at(key); + } + } + return *this; + } + + // Compound assignment addition operator (Map += Scalar) + template + IndexedFlatMap& operator+=(ScalarType const& scalar) + requires add_assignable + { + for (ValueType& val : values) { + val += scalar; + } + return *this; + } + + // Compound assignment subtraction operator (Map -= Scalar) + template + IndexedFlatMap& operator-=(ScalarType const& scalar) + requires subtract_assignable + { + for (ValueType& val : values) { + val -= scalar; + } + return *this; + } + + // Compound assignment multiplication operator (Map *= Scalar) + template + IndexedFlatMap& operator*=(ScalarType const& scalar) + requires multiply_assignable + { + for (ValueType& val : values) { + val *= scalar; + } + return *this; + } + + // Compound assignment division operator (Map /= Scalar) + template + IndexedFlatMap& operator/=(ScalarType const& scalar) + requires divide_assignable + { + if (scalar == static_cast(0)) { + spdlog::error_s( + "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> compound division by zero for scalar operation.", + type_name(), type_name() + ); + assert(scalar != static_cast(0)); + // continue and let it throw + } + for (ValueType& val : values) { + val /= scalar; + } + return *this; + } + + template + constexpr IndexedFlatMap& mul_add(IndexedFlatMap const& other, ScalarType const& factor) + requires mul_add_assignable + { + static_assert(has_get_index); + for (KeyType const& key : get_shared_keys(other)) { + at(key) += other.at(key) * factor; + } + + return *this; + } + + template + constexpr IndexedFlatMap& + mul_add(IndexedFlatMap const& a, IndexedFlatMap const& b) + requires mul_add_assignable + { + static_assert(has_get_index); + if (a.get_min_index() != b.get_min_index() || a.get_max_index() != b.get_max_index()) { + spdlog::error_s( + "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> attempted mul_add where a and b don't have the same keys. This " + "is not implemented.", + type_name(), type_name() + ); + assert(a.get_min_index() == b.get_min_index() && a.get_max_index() == b.get_max_index()); + } + + for (KeyType const& key : get_shared_keys(a)) { + at(key) += a.at(key) * b.at(key); + } + + return *this; + } + + template + constexpr IndexedFlatMap& rescale(ScalarType const& new_total) + requires std::default_initializable && add_assignable && + multiply_assignable && divide_assignable + { + static_assert(has_get_index); + bool has_any_non_zero_value = false; + const ValueType zero = static_cast(0); + ValueType old_total {}; + for (ValueType const& value : values) { + old_total += value; + has_any_non_zero_value |= value != zero; + } + IndexedFlatMap& this_ref = *this; + if (has_any_non_zero_value) { + this_ref *= new_total; + this_ref /= old_total; + } + return this_ref; + } + + // --- Iterators for Key-Value Pairs --- + template + class BasicIterator { + public: + using value_type = std::pair>; + using difference_type = std::ptrdiff_t; + using reference = value_type; + using iterator_category = std::random_access_iterator_tag; + + private: + std::conditional_t + value_it; + typename keys_span_type::iterator key_it; + + public: + // Constructor + BasicIterator( + std::conditional_t + val_it, + typename keys_span_type::iterator k_it + ) + : value_it(val_it), key_it(k_it) {} + + // Dereference operator + reference operator*() const { + return { *key_it, *value_it }; + } + + // Arrow operator (for member access) + // Note: This returns a proxy object, as std::pair is not a class type. + // For direct member access, it's often better to dereference and use dot operator: (*it).first + // However, for compatibility with some algorithms, a proxy is provided. + struct Proxy { + KeyType const& first; + std::conditional_t second; + // Add operator-> for nested access if needed, e.g., Proxy->first.some_member() + // For now, direct access to first/second is assumed. + }; + Proxy operator->() const { + return { *key_it, *value_it }; + } + + // Pre-increment + BasicIterator& operator++() { + ++value_it; + ++key_it; + return *this; + } + + // Post-increment + BasicIterator operator++(int) { + BasicIterator temp = *this; + ++(*this); + return temp; + } + + // Pre-decrement + BasicIterator& operator--() { + --value_it; + --key_it; + return *this; + } + + // Post-decrement + BasicIterator operator--(int) { + BasicIterator temp = *this; + --(*this); + return temp; + } + + // Random access operators + BasicIterator& operator+=(difference_type n) { + value_it += n; + key_it += n; + return *this; + } + + BasicIterator operator+(difference_type n) const { + BasicIterator temp = *this; + temp += n; + return temp; + } + + BasicIterator& operator-=(difference_type n) { + value_it -= n; + key_it -= n; + return *this; + } + + BasicIterator operator-(difference_type n) const { + BasicIterator temp = *this; + temp -= n; + return temp; + } + + difference_type operator-(BasicIterator const& other) const { + return value_it - other.value_it; + } + + // Comparison operators + bool operator==(BasicIterator const& other) const { + return value_it == other.value_it; + } + + bool operator!=(BasicIterator const& other) const { + return !(*this == other); + } + + bool operator<(BasicIterator const& other) const { + return value_it < other.value_it; + } + + bool operator>(BasicIterator const& other) const { + return value_it > other.value_it; + } + + bool operator<=(BasicIterator const& other) const { + return value_it <= other.value_it; + } + + bool operator>=(BasicIterator const& other) const { + return value_it >= other.value_it; + } + + // Conversion to const_iterator + operator BasicIterator() const { + return BasicIterator(value_it, key_it); + } + }; + + using iterator = BasicIterator; + using const_iterator = BasicIterator; + + // Begin and End methods + // note cbegin & cend for std::span requires c++23 so begin & end are used instead + iterator begin() { + return iterator(values.begin(), keys.begin()); + } + + const_iterator begin() const { + return const_iterator(values.cbegin(), keys.begin()); + } + + const_iterator cbegin() const { + return const_iterator(values.cbegin(), keys.begin()); + } + + iterator end() { + return iterator(values.end(), keys.end()); + } + + const_iterator end() const { + return const_iterator(values.cend(), keys.end()); + } + + const_iterator cend() const { + return const_iterator(values.cend(), keys.end()); + } + }; + + // Non-member binary operators for (Scalar op Map) + template + IndexedFlatMap operator+(ScalarType const& scalar, IndexedFlatMap const& map) + requires addable + { + return map + scalar; // Delegate to the member operator + } + + template + IndexedFlatMap operator-(ValueType const& scalar, IndexedFlatMap const& map) + requires subtractable + { + static_assert(has_get_index); + // Scalar - Map is not simply map - scalar, so we implement it directly + return IndexedFlatMap(map.get_keys(), [&](KeyType const& key) { + return scalar - map.at(key); + }); + } + + template + IndexedFlatMap operator*(ScalarType const& scalar, IndexedFlatMap const& map) + requires multipliable + { + return map * scalar; // Delegate to the member operator + } + + template + IndexedFlatMap operator/(ScalarType const& scalar, IndexedFlatMap const& map) + requires divisible + { + static_assert(has_get_index); + return IndexedFlatMap(map.get_keys(), [&](KeyType const& key) { + if (map.at(key) == static_cast(0)) { + spdlog::error_s( + "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> scalar division by zero detected at key index {}.", + type_name(), type_name(), key.get_index() + ); + assert(map.at(key) != static_cast(0)); + // continue and let it throw + } + return scalar / map.at(key); + }); + } +} diff --git a/src/openvic-simulation/utility/TslHelper.hpp b/src/openvic-simulation/core/container/MutableIterator.hpp similarity index 98% rename from src/openvic-simulation/utility/TslHelper.hpp rename to src/openvic-simulation/core/container/MutableIterator.hpp index 93dd18c84..64a9ffdf9 100644 --- a/src/openvic-simulation/utility/TslHelper.hpp +++ b/src/openvic-simulation/core/container/MutableIterator.hpp @@ -3,7 +3,7 @@ #include #include -#include "openvic-simulation/types/OrderedContainers.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" namespace OpenVic { template diff --git a/src/openvic-simulation/types/RingBuffer.hpp b/src/openvic-simulation/core/container/RingBuffer.hpp similarity index 99% rename from src/openvic-simulation/types/RingBuffer.hpp rename to src/openvic-simulation/core/container/RingBuffer.hpp index 28fc75dd7..94b43bf17 100644 --- a/src/openvic-simulation/types/RingBuffer.hpp +++ b/src/openvic-simulation/core/container/RingBuffer.hpp @@ -18,7 +18,7 @@ #include #include -#include "openvic-simulation/utility/Typedefs.hpp" +#include "openvic-simulation/core/Typedefs.hpp" namespace OpenVic { diff --git a/src/openvic-simulation/types/StackString.hpp b/src/openvic-simulation/core/container/StackString.hpp similarity index 95% rename from src/openvic-simulation/types/StackString.hpp rename to src/openvic-simulation/core/container/StackString.hpp index 3cfa4a36a..21a89ae77 100644 --- a/src/openvic-simulation/types/StackString.hpp +++ b/src/openvic-simulation/core/container/StackString.hpp @@ -5,7 +5,7 @@ #include #include -#include "openvic-simulation/utility/Containers.hpp" +#include "openvic-simulation/core/memory/String.hpp" namespace OpenVic { template diff --git a/src/openvic-simulation/types/ValueHistory.hpp b/src/openvic-simulation/core/container/ValueHistory.hpp similarity index 96% rename from src/openvic-simulation/types/ValueHistory.hpp rename to src/openvic-simulation/core/container/ValueHistory.hpp index b931d4e94..0e7f571df 100644 --- a/src/openvic-simulation/types/ValueHistory.hpp +++ b/src/openvic-simulation/core/container/ValueHistory.hpp @@ -2,7 +2,7 @@ #include -#include "openvic-simulation/types/RingBuffer.hpp" +#include "openvic-simulation/core/container/RingBuffer.hpp" namespace OpenVic { @@ -11,31 +11,31 @@ namespace OpenVic { using base_type = RingBuffer; using typename base_type::allocator_type; - using typename base_type::size_type; - using typename base_type::iterator; using typename base_type::const_iterator; - using typename base_type::reverse_iterator; - using typename base_type::const_reverse_iterator; - using typename base_type::reference; - using typename base_type::const_reference; - using typename base_type::pointer; using typename base_type::const_pointer; + using typename base_type::const_reference; + using typename base_type::const_reverse_iterator; using typename base_type::difference_type; + using typename base_type::iterator; + using typename base_type::pointer; + using typename base_type::reference; + using typename base_type::reverse_iterator; + using typename base_type::size_type; using base_type::operator[]; - using base_type::size; - using base_type::capacity; - using base_type::empty; + using base_type::back; using base_type::begin; - using base_type::end; + using base_type::capacity; using base_type::cbegin; using base_type::cend; - using base_type::rbegin; - using base_type::rend; using base_type::crbegin; using base_type::crend; + using base_type::empty; + using base_type::end; using base_type::front; - using base_type::back; using base_type::push_back; + using base_type::rbegin; + using base_type::rend; + using base_type::size; constexpr ValueHistory() {}; explicit ValueHistory(size_type capacity) : base_type(capacity) {} diff --git a/src/openvic-simulation/utility/Error.hpp b/src/openvic-simulation/core/error/Error.hpp similarity index 100% rename from src/openvic-simulation/utility/Error.hpp rename to src/openvic-simulation/core/error/Error.hpp diff --git a/src/openvic-simulation/utility/ErrorMacros.hpp b/src/openvic-simulation/core/error/ErrorMacros.hpp similarity index 97% rename from src/openvic-simulation/utility/ErrorMacros.hpp rename to src/openvic-simulation/core/error/ErrorMacros.hpp index 8eae7d456..d9fcfe28b 100644 --- a/src/openvic-simulation/utility/ErrorMacros.hpp +++ b/src/openvic-simulation/core/error/ErrorMacros.hpp @@ -1,7 +1,7 @@ #pragma once -#include "openvic-simulation/utility/Logger.hpp" // IWYU pragma: keep for loggers -#include "openvic-simulation/utility/Typedefs.hpp" // IWYU pragma: keep for macros +#include "openvic-simulation/core/Logger.hpp" // IWYU pragma: keep for loggers +#include "openvic-simulation/core/Typedefs.hpp" // IWYU pragma: keep for macros /** * Try using `OV_ERR_FAIL_COND_MSG`. diff --git a/src/openvic-simulation/utility/BMP.cpp b/src/openvic-simulation/core/io/BMP.cpp similarity index 82% rename from src/openvic-simulation/utility/BMP.cpp rename to src/openvic-simulation/core/io/BMP.cpp index 2bb39cd17..92b4ddd0c 100644 --- a/src/openvic-simulation/utility/BMP.cpp +++ b/src/openvic-simulation/core/io/BMP.cpp @@ -5,9 +5,8 @@ #include -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/utility/Logger.hpp" -#include "openvic-simulation/utility/Containers.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/memory/OrderedSet.hpp" using namespace OpenVic; @@ -51,42 +50,30 @@ bool BMP::read_header() { // Validate constants static constexpr uint16_t BMP_SIGNATURE = 0x4d42; if (header.signature != BMP_SIGNATURE) { - spdlog::error_s( - "Invalid BMP signature: {} (must be {})", - header.signature, BMP_SIGNATURE - ); + spdlog::error_s("Invalid BMP signature: {} (must be {})", header.signature, BMP_SIGNATURE); header_validated = false; } static constexpr uint32_t DIB_HEADER_SIZE = 40; if (header.dib_header_size != DIB_HEADER_SIZE) { - spdlog::error_s( - "Invalid BMP DIB header size: {} (must be {})", - header.dib_header_size, DIB_HEADER_SIZE - ); + spdlog::error_s("Invalid BMP DIB header size: {} (must be {})", header.dib_header_size, DIB_HEADER_SIZE); header_validated = false; } static constexpr uint16_t NUM_PLANES = 1; if (header.num_planes != NUM_PLANES) { - spdlog::error_s( - "Invalid BMP plane count: {} (must be {})", - header.num_planes, NUM_PLANES - ); + spdlog::error_s("Invalid BMP plane count: {} (must be {})", header.num_planes, NUM_PLANES); header_validated = false; } static constexpr uint16_t COMPRESSION = 0; // Only support uncompressed BMPs if (header.compression != COMPRESSION) { - spdlog::error_s( - "Invalid BMP compression method: {} (must be {})", - header.compression, COMPRESSION - ); + spdlog::error_s("Invalid BMP compression method: {} (must be {})", header.compression, COMPRESSION); header_validated = false; } // Validate sizes and dimensions if (header.image_size_bytes > 0 && header.file_size != header.offset + header.image_size_bytes) { spdlog::error_s( - "Invalid BMP memory sizes: file size = {} != {} = {} + {} = image data offset + image data size", - header.file_size, header.offset + header.image_size_bytes, header.offset, header.image_size_bytes + "Invalid BMP memory sizes: file size = {} != {} = {} + {} = image data offset + image data size", header.file_size, + header.offset + header.image_size_bytes, header.offset, header.image_size_bytes ); header_validated = false; } @@ -106,12 +93,9 @@ bool BMP::read_header() { // Validate colours #define VALID_BITS_PER_PIXEL 1, 2, 4, 8, 16, 24, 32 #define STR(x) #x - static const ordered_set BITS_PER_PIXEL { VALID_BITS_PER_PIXEL }; + static const memory::ordered_set BITS_PER_PIXEL { VALID_BITS_PER_PIXEL }; if (!BITS_PER_PIXEL.contains(header.bits_per_pixel)) { - spdlog::error_s( - "Invalid BMP bits per pixel: {} (must be one of " STR(VALID_BITS_PER_PIXEL) ")", - header.bits_per_pixel - ); + spdlog::error_s("Invalid BMP bits per pixel: {} (must be one of " STR(VALID_BITS_PER_PIXEL) ")", header.bits_per_pixel); header_validated = false; } #undef VALID_BITS_PER_PIXEL @@ -119,24 +103,22 @@ bool BMP::read_header() { static constexpr uint16_t PALETTE_BITS_PER_PIXEL_LIMIT = 8; if (header.num_colours != 0 && header.bits_per_pixel > PALETTE_BITS_PER_PIXEL_LIMIT) { spdlog::error_s( - "Invalid BMP palette size: {} (should be 0 as bits per pixel is {} > {})", - header.num_colours, header.bits_per_pixel, PALETTE_BITS_PER_PIXEL_LIMIT + "Invalid BMP palette size: {} (should be 0 as bits per pixel is {} > {})", header.num_colours, + header.bits_per_pixel, PALETTE_BITS_PER_PIXEL_LIMIT ); header_validated = false; } // TODO - validate important_colours - palette_size = header.bits_per_pixel > PALETTE_BITS_PER_PIXEL_LIMIT ? 0 + palette_size = header.bits_per_pixel > PALETTE_BITS_PER_PIXEL_LIMIT + ? 0 // Use header.num_colours if it's greater than 0 and at most 1 << header.bits_per_pixel - : (0 < header.num_colours && (header.num_colours - 1) >> header.bits_per_pixel == 0 - ? header.num_colours : 1 << header.bits_per_pixel); + : (0 < header.num_colours && (header.num_colours - 1) >> header.bits_per_pixel == 0 ? header.num_colours + : 1 << header.bits_per_pixel); const uint32_t expected_offset = palette_size * PALETTE_COLOUR_SIZE + sizeof(header); if (header.offset != expected_offset) { - spdlog::error_s( - "Invalid BMP image data offset: {} (should be {})", - header.offset, expected_offset - ); + spdlog::error_s("Invalid BMP image data offset: {} (should be {})", header.offset, expected_offset); header_validated = false; } diff --git a/src/openvic-simulation/utility/BMP.hpp b/src/openvic-simulation/core/io/BMP.hpp similarity index 95% rename from src/openvic-simulation/utility/BMP.hpp rename to src/openvic-simulation/core/io/BMP.hpp index 7319d4063..c1ac71c4e 100644 --- a/src/openvic-simulation/utility/BMP.hpp +++ b/src/openvic-simulation/core/io/BMP.hpp @@ -4,7 +4,7 @@ #include #include -#include "openvic-simulation/utility/Containers.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" namespace OpenVic { namespace fs = std::filesystem; @@ -12,6 +12,7 @@ namespace OpenVic { class BMP { #pragma pack(push) #pragma pack(1) + // clang-format off struct header_t { uint16_t signature; // Signature: 0x4d42 (or 'B' 'M') uint32_t file_size; // File size in bytes @@ -30,6 +31,7 @@ namespace OpenVic { uint32_t num_colours; // Number of colours uint32_t important_colours; // Important colours } header {}; + //clang-format on #pragma pack(pop) public: diff --git a/src/openvic-simulation/core/memory/Allocator.hpp b/src/openvic-simulation/core/memory/Allocator.hpp new file mode 100644 index 000000000..27fa4cd41 --- /dev/null +++ b/src/openvic-simulation/core/memory/Allocator.hpp @@ -0,0 +1,49 @@ +#pragma once + +#include +#include + +#include "openvic-simulation/core/memory/Tracker.hpp" + +#include +#include +#include +#include + +namespace OpenVic::memory { + template + tracker make_tracked_allocator( // + RawAllocator&& alloc + ) { +#ifdef DEBUG_ENABLED + return foonathan::memory::make_tracked_allocator( + typename tracker::tracker {}, std::forward(alloc) + ); +#else + return std::forward(alloc); +#endif + } + + template + using allocator_reference = foonathan::memory::allocator_reference; + + template + allocator_reference make_allocator_reference(RawAllocator&& alloc) { + return foonathan::memory::make_allocator_reference(alloc); + } + + template + using allocator_deleter = foonathan::memory::allocator_deleter; + + template + struct will_optimize_deleter_for : std::false_type {}; + + template<> + struct will_optimize_deleter_for : std::true_type {}; + + template<> + struct will_optimize_deleter_for : std::true_type {}; + + template<> + struct will_optimize_deleter_for : std::true_type {}; +} diff --git a/src/openvic-simulation/core/memory/Colony.hpp b/src/openvic-simulation/core/memory/Colony.hpp new file mode 100644 index 000000000..ccc792577 --- /dev/null +++ b/src/openvic-simulation/core/memory/Colony.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include + +#include "openvic-simulation/core/memory/Tracker.hpp" + +#include +#include + +namespace OpenVic::memory { + template + using colony = plf::colony>>; +} diff --git a/src/openvic-simulation/core/memory/CowPtr.hpp b/src/openvic-simulation/core/memory/CowPtr.hpp new file mode 100644 index 000000000..a6bbe4cf6 --- /dev/null +++ b/src/openvic-simulation/core/memory/CowPtr.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include "openvic-simulation/core/container/CowPtr.hpp" +#include "openvic-simulation/core/memory/Tracker.hpp" + +#include +#include + +namespace OpenVic::memory { + template + using cow_ptr = cow_ptr>>; +} diff --git a/src/openvic-simulation/core/memory/CowVector.hpp b/src/openvic-simulation/core/memory/CowVector.hpp new file mode 100644 index 000000000..aacdb2b1d --- /dev/null +++ b/src/openvic-simulation/core/memory/CowVector.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include "openvic-simulation/core/container/CowVector.hpp" +#include "openvic-simulation/core/memory/Tracker.hpp" + +#include +#include + +namespace OpenVic::memory { + template + using cow_vector = cow_vector>>; +} diff --git a/src/openvic-simulation/core/memory/Deque.hpp b/src/openvic-simulation/core/memory/Deque.hpp new file mode 100644 index 000000000..54cd94596 --- /dev/null +++ b/src/openvic-simulation/core/memory/Deque.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include + +#include "openvic-simulation/core/memory/Tracker.hpp" +#include "openvic-simulation/core/portable/Deque.hpp" + +#include +#include +#include + +namespace OpenVic::memory::utility { + template + FOONATHAN_ALIAS_TEMPLATE(deque, OpenVic::utility::deque>); + + template + FOONATHAN_ALIAS_TEMPLATE(deque_scoped_alloc, OpenVic::utility::deque>>); +} + +namespace OpenVic::memory { + template + using deque = OpenVic::memory::utility::deque>; +} diff --git a/src/openvic-simulation/types/fixed_point/FixedPointMap.hpp b/src/openvic-simulation/core/memory/FixedPointMap.hpp similarity index 90% rename from src/openvic-simulation/types/fixed_point/FixedPointMap.hpp rename to src/openvic-simulation/core/memory/FixedPointMap.hpp index 82762167f..ef64b5054 100644 --- a/src/openvic-simulation/types/fixed_point/FixedPointMap.hpp +++ b/src/openvic-simulation/core/memory/FixedPointMap.hpp @@ -1,13 +1,13 @@ #pragma once -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/types/OrderedContainersMath.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include -namespace OpenVic { +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +namespace OpenVic::memory { template - using fixed_point_map_t = ordered_map; + using fixed_point_map_t = memory::ordered_map; template using fixed_point_map_value_t = typename fixed_point_map_t::value_type; @@ -26,7 +26,7 @@ namespace OpenVic { } template - constexpr fixed_point_map_t& fixed_point_map_mul_add( + constexpr fixed_point_map_t& fixed_point_map_mul_add( // fixed_point_map_t& lhs, fixed_point_map_t const& rhs, fixed_point_t factor ) { if (factor != 0) { @@ -90,7 +90,6 @@ namespace OpenVic { if (value_cmp != std::strong_ordering::equal) { return value_cmp == std::strong_ordering::less; } - } return rit != rhs.rcend(); diff --git a/src/openvic-simulation/core/memory/FixedVector.hpp b/src/openvic-simulation/core/memory/FixedVector.hpp new file mode 100644 index 000000000..a2817bb3f --- /dev/null +++ b/src/openvic-simulation/core/memory/FixedVector.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include "openvic-simulation/core/container/FixedVector.hpp" +#include "openvic-simulation/core/memory/Tracker.hpp" + +#include + +namespace OpenVic::memory { + template + using FixedVector = FixedVector>>; +} diff --git a/src/openvic-simulation/core/memory/Format.hpp b/src/openvic-simulation/core/memory/Format.hpp new file mode 100644 index 000000000..cf7b67ccc --- /dev/null +++ b/src/openvic-simulation/core/memory/Format.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include + +#include +#include + +#include "openvic-simulation/core/memory/String.hpp" + +#include + +namespace OpenVic::memory::fmt { + template + using basic_memory_buffer = + ::fmt::basic_memory_buffer>>; + + inline static memory::string vformat(::fmt::string_view fmt, ::fmt::format_args args) { + memory::fmt::basic_memory_buffer buf {}; + ::fmt::vformat_to(std::back_inserter(buf), fmt, args); + return memory::string(buf.data(), buf.size()); + } + + template + memory::string format(::fmt::string_view fmt, const Args&... args) { + return memory::fmt::vformat(fmt, ::fmt::make_format_args(args...)); + } +} diff --git a/src/openvic-simulation/core/memory/New.hpp b/src/openvic-simulation/core/memory/New.hpp new file mode 100644 index 000000000..177a207c8 --- /dev/null +++ b/src/openvic-simulation/core/memory/New.hpp @@ -0,0 +1,68 @@ +#pragma once + +#include +#include +#include +#include + +#include "openvic-simulation/core/memory/Allocator.hpp" + +#include + +namespace OpenVic::memory { + template + requires(!std::is_array_v) + static inline T* allocate_new(RawAllocator&& alloc, Args&&... args) { + allocator_reference alloc_ref = ::OpenVic::memory::make_allocator_reference(alloc); + T* ptr = static_cast(alloc_ref.allocate_node(sizeof(T), alignof(T))); + ::new (ptr) T(std::forward(args)...); + return ptr; + } + + template + requires(std::is_unbounded_array_v) + static inline T* allocate_new(RawAllocator&& alloc, std::size_t n) { + allocator_reference alloc_ref = ::OpenVic::memory::make_allocator_reference(alloc); + return static_cast(alloc_ref.allocate_node(sizeof(T) * n, alignof(T))); + } + + template + requires(!std::is_array_v) + static inline T* make_new(Args&&... args) { + return allocate_new(make_tracked_allocator(RawAllocator {}), std::forward(args)...); + } + + template + requires(std::is_unbounded_array_v) + static inline T* make_new(std::size_t n) { + return allocate_new(make_tracked_allocator(RawAllocator {}), n); + } + + template + requires(!std::is_array_v) + static inline void dealloc_delete(RawAllocator&& alloc, T* ptr) { + ptr->~T(); + allocator_reference alloc_ref = ::OpenVic::memory::make_allocator_reference(alloc); + alloc_ref.deallocate(ptr, sizeof(T), alignof(T)); + } + + template + requires(std::is_unbounded_array_v) + static inline void dealloc_delete(RawAllocator&& alloc, T* ptr, std::size_t n) { + std::destroy_n(ptr, n); + allocator_reference alloc_ref = ::OpenVic::memory::make_allocator_reference(alloc); + alloc_ref.deallocate(ptr, sizeof(T) * n, alignof(T)); + } + + template + requires(!std::is_array_v) + static inline void delete_at(T* ptr) { + dealloc_delete(make_tracked_allocator(RawAllocator {}), ptr); + } + + template + requires(std::is_unbounded_array_v) + static inline void delete_at(T* ptr, std::size_t n) { + dealloc_delete(make_tracked_allocator(RawAllocator {}), ptr, n); + } +} diff --git a/src/openvic-simulation/core/memory/OrderedMap.hpp b/src/openvic-simulation/core/memory/OrderedMap.hpp new file mode 100644 index 000000000..cad28f1a9 --- /dev/null +++ b/src/openvic-simulation/core/memory/OrderedMap.hpp @@ -0,0 +1,239 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include + +#include "openvic-simulation/core/memory/Tracker.hpp" +#include "openvic-simulation/core/portable/Deque.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" +#include "openvic-simulation/core/template/ContainerHash.hpp" + +#include +#include + +namespace OpenVic::memory { + // Useful for contiguous memory + template< + class Key, class T, class Hash = container_hash, class KeyEqual = std::equal_to<>, + class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, + class Allocator = foonathan::memory::std_allocator, tracker>> + using vector_ordered_map = + tsl::ordered_map, Allocator>, IndexType>; + + // Useful for stable memory addresses (so long as you don't remove or insert values) + template< + class Key, class T, class Hash = container_hash, class KeyEqual = std::equal_to<>, + class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, + class Allocator = foonathan::memory::std_allocator, tracker>> + using deque_ordered_map = + tsl::ordered_map, Allocator>, IndexType>; + + template< + class Key, class T, class Hash = container_hash, class KeyEqual = std::equal_to<>, + class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, + class Allocator = foonathan::memory::std_allocator, tracker>> + using ordered_map = vector_ordered_map; + + // Useful for contiguous memory + template< + class Key, class T, class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, + class Allocator = foonathan::memory::std_allocator, memory::tracker>> + using case_insensitive_vector_ordered_map = vector_ordered_map< + Key, T, case_insensitive_string_hash, case_insensitive_string_equal, RawAllocator, IndexType, Allocator>; + + // Useful for stable memory addresses (so long as you don't remove or insert values) + template< + class Key, class T, class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, + class Allocator = foonathan::memory::std_allocator, memory::tracker>> + using case_insensitive_deque_ordered_map = deque_ordered_map< + Key, T, case_insensitive_string_hash, case_insensitive_string_equal, RawAllocator, IndexType, Allocator>; + + template< + class Key, class T, class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, + class Allocator = foonathan::memory::std_allocator, memory::tracker>> + using case_insensitive_ordered_map = case_insensitive_vector_ordered_map; + + + template + constexpr static ValueType get_total(memory::ordered_map const& map) + requires std::is_default_constructible_v + { + ValueType running_total {}; + for (auto const& [key, value] : map) { + running_total += value; + } + return running_total; + } + + template + constexpr static typename memory::ordered_map::const_iterator get_largest_item( // + memory::ordered_map const& map + ) + requires std::three_way_comparable_with + { + constexpr auto pred = [](typename memory::ordered_map::value_type const& lhs, + typename memory::ordered_map::value_type const& rhs) -> bool { + return lhs.second < rhs.second; + }; + + return std::max_element(map.begin(), map.end(), pred); + } + + /* This function includes a key comparator to choose between entries with equal values. */ + template + constexpr static typename memory::ordered_map::const_iterator get_largest_item_tie_break( // + memory::ordered_map const& map, const auto key_pred + ) + requires std::three_way_comparable_with + { + constexpr auto pred = [key_pred]( + typename memory::ordered_map::value_type const& lhs, + typename memory::ordered_map::value_type const& rhs + ) -> bool { + return lhs.second < rhs.second || (lhs.second == rhs.second && key_pred(lhs.first, rhs.first)); + }; + + return std::max_element(map.begin(), map.end(), pred); + } + + template + constexpr static std::pair< + typename memory::ordered_map::const_iterator, + typename memory::ordered_map::const_iterator> + get_largest_two_items( // + memory::ordered_map const& map + ) + requires std::three_way_comparable_with + { + typename memory::ordered_map::const_iterator largest = map.end(), second_largest = map.end(); + + for (typename memory::ordered_map::const_iterator it = map.begin(); it != map.end(); ++it) { + if (largest == map.end() || it->second > largest->second) { + second_largest = largest; + largest = it; + } else if (second_largest == map.end() || it->second > second_largest->second) { + second_largest = it; + } + } + + return std::make_pair(std::move(largest), std::move(second_largest)); + } +} + +namespace OpenVic { + template + concept derived_ordered_map = derived_from_specialization_of; + + template + constexpr memory::ordered_map operator-(memory::ordered_map const& rhs) { + memory::ordered_map result {}; + auto view = // + rhs | ranges::views::transform([](KeyType const& key, ValueType const& value) { + return std::make_pair(key, -value); + }); + result.insert(view.begin(), view.end()); + return result; + } + + template + constexpr memory::ordered_map& operator+=( // + memory::ordered_map& lhs, memory::ordered_map const& rhs + ) + requires add_assignable + { + for (auto const& [key, rhs_value] : rhs) { + lhs[key] += rhs_value; + } + return lhs; + } + + template + constexpr memory::ordered_map& operator-=( // + memory::ordered_map& lhs, memory::ordered_map const& rhs + ) + requires subtract_assignable + { + for (auto const& [key, rhs_value] : rhs) { + lhs[key] -= rhs_value; + } + return lhs; + } + + template + constexpr memory::ordered_map& operator*=( // + memory::ordered_map& lhs, memory::ordered_map const& rhs + ) + requires multiply_assignable + { + for (auto const& [key, rhs_value] : rhs) { + lhs[key] *= rhs_value; + } + return lhs; + } + + template + constexpr memory::ordered_map& operator/=( // + memory::ordered_map& lhs, memory::ordered_map const& rhs + ) + requires divide_assignable + { + for (auto const& [key, rhs_value] : rhs) { + lhs[key] = rhs_value; + } + return lhs; + } + + template + constexpr memory::ordered_map& operator+=( // + memory::ordered_map& lhs, RhsValueType const& rhs + ) + requires add_assignable + { + for (typename decltype(lhs)::iterator it = lhs.begin(); it != lhs.end(); ++it) { + it.value() += rhs; + } + return lhs; + } + + template + constexpr memory::ordered_map& operator-=( // + memory::ordered_map& lhs, RhsValueType const& rhs + ) + requires subtract_assignable + { + for (typename decltype(lhs)::iterator it = lhs.begin(); it != lhs.end(); ++it) { + it.value() -= rhs; + } + return lhs; + } + + template + constexpr memory::ordered_map& operator*=( // + memory::ordered_map& lhs, RhsValueType const& rhs + ) + requires multiply_assignable + { + for (typename decltype(lhs)::iterator it = lhs.begin(); it != lhs.end(); ++it) { + it.value() *= rhs; + } + return lhs; + } + + template + constexpr memory::ordered_map& operator/=( // + memory::ordered_map& lhs, RhsValueType const& rhs + ) + requires divide_assignable + { + for (auto it = lhs.begin(); it != lhs.end(); ++it) { + it.value() /= rhs; + } + return lhs; + } +} diff --git a/src/openvic-simulation/core/memory/OrderedSet.hpp b/src/openvic-simulation/core/memory/OrderedSet.hpp new file mode 100644 index 000000000..c93cd12e9 --- /dev/null +++ b/src/openvic-simulation/core/memory/OrderedSet.hpp @@ -0,0 +1,57 @@ +#pragma once + +#include +#include +#include + +#include + +#include "openvic-simulation/core/Hash.hpp" +#include "openvic-simulation/core/memory/Tracker.hpp" +#include "openvic-simulation/core/portable/Deque.hpp" +#include "openvic-simulation/core/template/ContainerHash.hpp" + +#include +#include + +namespace OpenVic::memory { + // Useful for contiguous memory + template< + class Key, class Hash = container_hash, class KeyEqual = std::equal_to<>, + class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, + class Allocator = foonathan::memory::std_allocator>> + using vector_ordered_set = tsl::ordered_set, IndexType>; + + // Useful for stable memory addresses (so long as you don't remove or insert values) + template< + class Key, class Hash = container_hash, class KeyEqual = std::equal_to<>, + class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, + class Allocator = foonathan::memory::std_allocator>> + using deque_ordered_set = + tsl::ordered_set, IndexType>; + + template< + class Key, class Hash = container_hash, class KeyEqual = std::equal_to<>, + class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, + class Allocator = foonathan::memory::std_allocator>> + using ordered_set = vector_ordered_set; + + // Useful for contiguous memory + template< + class Key, class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, + class Allocator = foonathan::memory::std_allocator>> + using case_insensitive_vector_ordered_set = vector_ordered_set< + Key, case_insensitive_string_hash, case_insensitive_string_equal, RawAllocator, IndexType, Allocator>; + + // Useful for stable memory addresses (so long as you don't remove or insert values) + template< + class Key, class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, + class Allocator = foonathan::memory::std_allocator>> + using case_insensitive_deque_ordered_set = + deque_ordered_set; + + template< + class Key, class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, + class Allocator = foonathan::memory::std_allocator>> + using case_insensitive_ordered_set = case_insensitive_vector_ordered_set; +} diff --git a/src/openvic-simulation/core/memory/Queue.hpp b/src/openvic-simulation/core/memory/Queue.hpp new file mode 100644 index 000000000..bd88965a9 --- /dev/null +++ b/src/openvic-simulation/core/memory/Queue.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include + +#include "openvic-simulation/core/memory/Deque.hpp" + +namespace OpenVic::memory { + template> + using queue = std::queue; +} diff --git a/src/openvic-simulation/core/memory/SharedPtr.hpp b/src/openvic-simulation/core/memory/SharedPtr.hpp new file mode 100644 index 000000000..65aaf2b7d --- /dev/null +++ b/src/openvic-simulation/core/memory/SharedPtr.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include +#include + +#include "openvic-simulation/core/memory/Allocator.hpp" + +#include +#include + +namespace OpenVic::memory { + template + std::shared_ptr allocate_shared(RawAllocator&& alloc, Args&&... args) { + return foonathan::memory::allocate_shared( + make_tracked_allocator(std::forward(alloc)), std::forward(args)... + ); + } + + template + static inline std::shared_ptr make_shared(Args&&... args) { + return allocate_shared(RawAllocator {}, std::forward(args)...); + } +} diff --git a/src/openvic-simulation/core/memory/Stack.hpp b/src/openvic-simulation/core/memory/Stack.hpp new file mode 100644 index 000000000..4bd0a429b --- /dev/null +++ b/src/openvic-simulation/core/memory/Stack.hpp @@ -0,0 +1,11 @@ +#pragma once + + +#include + +#include "openvic-simulation/core/memory/Deque.hpp" + +namespace OpenVic::memory { + template> + using stack = std::stack; +} diff --git a/src/openvic-simulation/core/memory/String.hpp b/src/openvic-simulation/core/memory/String.hpp new file mode 100644 index 000000000..33752fc98 --- /dev/null +++ b/src/openvic-simulation/core/memory/String.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "openvic-simulation/core/memory/Tracker.hpp" + +#include +#include + +namespace OpenVic::memory { + template + using basic_string = + std::basic_string, foonathan::memory::std_allocator>>; + + template + using string_alloc = basic_string; + + using string = string_alloc<>; + + template + using wstring_alloc = basic_string; + + using wstring = wstring_alloc<>; +} diff --git a/src/openvic-simulation/core/memory/StringMap.hpp b/src/openvic-simulation/core/memory/StringMap.hpp new file mode 100644 index 000000000..dcd9a22c8 --- /dev/null +++ b/src/openvic-simulation/core/memory/StringMap.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "openvic-simulation/core/container/CaseContainer.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" +#include "openvic-simulation/core/template/ContainerHash.hpp" + +namespace OpenVic::memory { + /* Template for map with string keys, supporting search by string_view without creating an intermediate string. */ + template + using template_string_map_t = template_case_container_t; + + template + using string_map_t = template_string_map_t; + template + using case_insensitive_string_map_t = template_string_map_t; +} diff --git a/src/openvic-simulation/core/memory/StringSet.hpp b/src/openvic-simulation/core/memory/StringSet.hpp new file mode 100644 index 000000000..5bab3de48 --- /dev/null +++ b/src/openvic-simulation/core/memory/StringSet.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include "openvic-simulation/core/container/CaseContainer.hpp" +#include "openvic-simulation/core/memory/OrderedSet.hpp" +#include "openvic-simulation/core/memory/String.hpp" + +namespace OpenVic::memory { + /* Template for set with string elements, supporting search by string_view without creating an intermediate string. */ + template + using template_string_set_t = template_case_container_t; + + using string_set_t = template_string_set_t; + using case_insensitive_string_set_t = template_string_set_t; +} diff --git a/src/openvic-simulation/core/memory/StringStream.hpp b/src/openvic-simulation/core/memory/StringStream.hpp new file mode 100644 index 000000000..79a3ad873 --- /dev/null +++ b/src/openvic-simulation/core/memory/StringStream.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include +#include + +#include "openvic-simulation/core/memory/Tracker.hpp" + +#include +#include + +namespace OpenVic::memory { + template, class RawAllocator = foonathan::memory::default_allocator> + using basic_stringstream = + std::basic_stringstream>>; + + template + using stringstream_alloc = basic_stringstream, RawAllocator>; + template + using wstringstream_alloc = basic_stringstream, RawAllocator>; + + using stringstream = stringstream_alloc<>; + using wstringstream = wstringstream_alloc<>; +} diff --git a/src/openvic-simulation/utility/MemoryTracker.hpp b/src/openvic-simulation/core/memory/Tracker.hpp similarity index 100% rename from src/openvic-simulation/utility/MemoryTracker.hpp rename to src/openvic-simulation/core/memory/Tracker.hpp diff --git a/src/openvic-simulation/core/memory/UniquePtr.hpp b/src/openvic-simulation/core/memory/UniquePtr.hpp new file mode 100644 index 000000000..61c306fbe --- /dev/null +++ b/src/openvic-simulation/core/memory/UniquePtr.hpp @@ -0,0 +1,108 @@ +#pragma once + +#include +#include + +#include "openvic-simulation/core/memory/Allocator.hpp" +#include "openvic-simulation/core/memory/Tracker.hpp" + +#include +#include + +namespace OpenVic::memory { +#ifdef DEBUG_ENABLED + template + using unique_ptr = foonathan::memory::unique_ptr>; + + template + using unique_base_ptr = foonathan::memory::unique_base_ptr>; + + template + requires(!std::is_array_v) + unique_ptr> allocate_unique(RawAllocator&& alloc, Args&&... args) { + return foonathan::memory::allocate_unique( + make_tracked_allocator(std::forward(alloc)), std::forward(args)... + ); + } + + template + requires(std::is_array_v) + unique_ptr> allocate_unique(RawAllocator&& alloc, std::size_t size) { + return foonathan::memory::allocate_unique>( + make_tracked_allocator(std::forward(alloc)), size + ); + } +#else + template + using unique_ptr = std::conditional_t< + will_optimize_deleter_for::value, std::unique_ptr, + foonathan::memory::unique_ptr>>; + + template + using unique_base_ptr = std::conditional_t< + will_optimize_deleter_for::value, std::unique_ptr, + foonathan::memory::unique_base_ptr>>; + + template + requires(!std::is_array_v) + unique_ptr> allocate_unique(RawAllocator&& alloc, Args&&... args) { + if constexpr (will_optimize_deleter_for::value) { + return std::make_unique(std::forward(args)...); + } else { + return foonathan::memory::allocate_unique( + make_tracked_allocator(std::forward(alloc)), std::forward(args)... + ); + } + } + + template + requires(std::is_array_v) + unique_ptr> allocate_unique(RawAllocator&& alloc, std::size_t size) { + if constexpr (will_optimize_deleter_for::value) { + return std::make_unique(size); + } else { + return foonathan::memory::allocate_unique>( + make_tracked_allocator(std::forward(alloc)), size + ); + } + } +#endif + + template + requires(!std::is_array_v) + static inline unique_ptr make_unique(Args&&... args) { + return allocate_unique(RawAllocator {}, std::forward(args)...); + } + + template + requires(std::is_unbounded_array_v) + static inline unique_ptr make_unique(size_t n) { + return allocate_unique(make_tracked_allocator(RawAllocator {}), n); + } + + template + requires(!std::is_array_v) + static inline unique_ptr make_unique_for_overwrite() { + using raw_ptr = std::unique_ptr>>; + + tracker alloc = make_tracked_allocator(RawAllocator {}); + auto memory = alloc.allocate_node(sizeof(T), alignof(T)); + // raw_ptr deallocates memory in case of constructor exception + raw_ptr result(static_cast(memory), { alloc }); + // pass ownership to return value using a deleter that calls destructor + return { result.release(), { alloc } }; + } + + template + requires(std::is_unbounded_array_v) + static inline unique_ptr make_unique_for_overwrite(size_t size) { + using raw_ptr = std::unique_ptr>>; + + tracker alloc = make_tracked_allocator(RawAllocator {}); + auto memory = alloc.allocate_array(size, sizeof(T), alignof(T)); + // raw_ptr deallocates memory in case of constructor exception + raw_ptr result(static_cast(memory), { alloc, size }); + // pass ownership to return value using a deleter that calls destructor + return { result.release(), { alloc, size } }; + } +} diff --git a/src/openvic-simulation/core/memory/Vector.hpp b/src/openvic-simulation/core/memory/Vector.hpp new file mode 100644 index 000000000..b756c733c --- /dev/null +++ b/src/openvic-simulation/core/memory/Vector.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include "openvic-simulation/core/memory/Tracker.hpp" + +#include +#include + +namespace OpenVic::memory { + template + using vector = foonathan::memory::vector>; +} diff --git a/src/openvic-simulation/types/Colour.cpp b/src/openvic-simulation/core/object/Colour.cpp similarity index 100% rename from src/openvic-simulation/types/Colour.cpp rename to src/openvic-simulation/core/object/Colour.cpp diff --git a/src/openvic-simulation/types/Colour.hpp b/src/openvic-simulation/core/object/Colour.hpp similarity index 84% rename from src/openvic-simulation/types/Colour.hpp rename to src/openvic-simulation/core/object/Colour.hpp index 87f1c2c8f..0d59b13c7 100644 --- a/src/openvic-simulation/types/Colour.hpp +++ b/src/openvic-simulation/core/object/Colour.hpp @@ -12,7 +12,6 @@ #include #include #include -#include #include #include #include @@ -25,10 +24,10 @@ #include -#include "openvic-simulation/types/StackString.hpp" -#include "openvic-simulation/utility/Concepts.hpp" -#include "openvic-simulation/utility/StringUtils.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/container/StackString.hpp" +#include "openvic-simulation/core/string/CharConv.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" namespace OpenVic { template @@ -57,10 +56,12 @@ namespace OpenVic { OV_SPEED_INLINE static constexpr integer_type make_rgb_integer(value_type red, value_type green, value_type blue) { return (red << red_shift) | (green << green_shift) | (blue << blue_shift); } - OV_SPEED_INLINE static constexpr integer_type make_rgba_integer(value_type red, value_type green, value_type blue, value_type alpha) { + OV_SPEED_INLINE static constexpr integer_type + make_rgba_integer(value_type red, value_type green, value_type blue, value_type alpha) { return (make_rgb_integer(red, green, blue) << component_bit_size) | (alpha << rgba_alpha_shift); } - OV_SPEED_INLINE static constexpr integer_type make_argb_integer(value_type red, value_type green, value_type blue, value_type alpha) { + OV_SPEED_INLINE static constexpr integer_type + make_argb_integer(value_type red, value_type green, value_type blue, value_type alpha) { return make_rgb_integer(red, green, blue) | static_cast(alpha << argb_alpha_shift); } @@ -192,39 +193,28 @@ namespace OpenVic { } } - OV_SPEED_INLINE static constexpr basic_colour_t from_argb(integer_type integer) - { + OV_SPEED_INLINE static constexpr basic_colour_t from_argb(integer_type integer) { if constexpr (colour_traits::has_alpha) { - return { - colour_traits::red_from_argb(integer), colour_traits::green_from_argb(integer), - colour_traits::blue_from_argb(integer), colour_traits::alpha_from_argb(integer) - }; + return { colour_traits::red_from_argb(integer), colour_traits::green_from_argb(integer), + colour_traits::blue_from_argb(integer), colour_traits::alpha_from_argb(integer) }; } else { return from_rgb(integer); } } - OV_SPEED_INLINE static constexpr basic_colour_t from_rgba(integer_type integer) - { + OV_SPEED_INLINE static constexpr basic_colour_t from_rgba(integer_type integer) { if constexpr (colour_traits::has_alpha) { - return { - colour_traits::red_from_rgb(integer), colour_traits::green_from_rgb(integer), - colour_traits::blue_from_rgb(integer), colour_traits::alpha_from_rgba(integer) - }; + return { colour_traits::red_from_rgb(integer), colour_traits::green_from_rgb(integer), + colour_traits::blue_from_rgb(integer), colour_traits::alpha_from_rgba(integer) }; } else { - return { - colour_traits::red_from_rgb(integer), colour_traits::green_from_rgb(integer), - colour_traits::blue_from_rgb(integer) - }; + return { colour_traits::red_from_rgb(integer), colour_traits::green_from_rgb(integer), + colour_traits::blue_from_rgb(integer) }; } } - OV_SPEED_INLINE static constexpr basic_colour_t from_rgb(integer_type integer) - { - return { - colour_traits::red_from_argb(integer), colour_traits::green_from_argb(integer), - colour_traits::blue_from_argb(integer) - }; + OV_SPEED_INLINE static constexpr basic_colour_t from_rgb(integer_type integer) { + return { colour_traits::red_from_argb(integer), colour_traits::green_from_argb(integer), + colour_traits::blue_from_argb(integer) }; } OV_SPEED_INLINE static constexpr basic_colour_t from_floats( // @@ -232,10 +222,8 @@ namespace OpenVic { ) requires(colour_traits::has_alpha) { - return { - colour_traits::red_from_float(r), colour_traits::green_from_float(g), colour_traits::blue_from_float(b), - colour_traits::alpha_from_float(a) - }; + return { colour_traits::red_from_float(r), colour_traits::green_from_float(g), colour_traits::blue_from_float(b), + colour_traits::alpha_from_float(a) }; } OV_SPEED_INLINE static constexpr basic_colour_t from_floats(float r, float g, float b) @@ -252,7 +240,7 @@ namespace OpenVic { first += 2; } - std::from_chars_result result = StringUtils::from_chars(first, last, value, 16); + std::from_chars_result result = OpenVic::from_chars(first, last, value, 16); return result; } @@ -267,7 +255,8 @@ namespace OpenVic { return result; } - OV_SPEED_INLINE static constexpr basic_colour_t from_string(std::string_view str, std::from_chars_result* from_chars = nullptr) { + OV_SPEED_INLINE static constexpr basic_colour_t + from_string(std::string_view str, std::from_chars_result* from_chars = nullptr) { basic_colour_t result {}; if (from_chars == nullptr) { result.from_chars(str.data(), str.data() + str.size()); @@ -287,7 +276,8 @@ namespace OpenVic { return result; } - OV_SPEED_INLINE static constexpr basic_colour_t from_rgba_string(std::string_view str, std::from_chars_result* from_chars = nullptr) { + OV_SPEED_INLINE static constexpr basic_colour_t + from_rgba_string(std::string_view str, std::from_chars_result* from_chars = nullptr) { basic_colour_t result {}; if (from_chars == nullptr) { result.from_chars_rgba(str.data(), str.data() + str.size()); @@ -307,7 +297,8 @@ namespace OpenVic { return result; } - OV_SPEED_INLINE static constexpr basic_colour_t from_argb_string(std::string_view str, std::from_chars_result* from_chars = nullptr) { + OV_SPEED_INLINE static constexpr basic_colour_t + from_argb_string(std::string_view str, std::from_chars_result* from_chars = nullptr) { basic_colour_t result {}; if (from_chars == nullptr) { result.from_chars_argb(str.data(), str.data() + str.size()); @@ -327,7 +318,8 @@ namespace OpenVic { return result; } - OV_SPEED_INLINE static constexpr basic_colour_t from_rgb_string(std::string_view str, std::from_chars_result* from_chars = nullptr) { + OV_SPEED_INLINE static constexpr basic_colour_t + from_rgb_string(std::string_view str, std::from_chars_result* from_chars = nullptr) { basic_colour_t result {}; if (from_chars == nullptr) { result.from_chars_rgb(str.data(), str.data() + str.size()); @@ -361,19 +353,13 @@ namespace OpenVic { : red(r), green(g), blue(b) {} template - requires( - _ColourTraits::has_alpha && std::same_as && - std::same_as - ) + requires(_ColourTraits::has_alpha && std::same_as && std::same_as) OV_SPEED_INLINE explicit constexpr basic_colour_t(basic_colour_t const& colour) requires(colour_traits::has_alpha) : basic_colour_t { colour.red, colour.green, colour.blue, colour.alpha } {} template - requires( - !_ColourTraits::has_alpha && std::same_as && - std::same_as - ) + requires(!_ColourTraits::has_alpha && std::same_as && std::same_as) OV_SPEED_INLINE explicit constexpr basic_colour_t( basic_colour_t const& colour, value_type a = max_value ) @@ -381,10 +367,7 @@ namespace OpenVic { : basic_colour_t { colour.red, colour.green, colour.blue, a } {} template - requires( - std::same_as && - std::same_as - ) + requires(std::same_as && std::same_as) OV_SPEED_INLINE explicit constexpr basic_colour_t(basic_colour_t const& colour) requires(!colour_traits::has_alpha) : basic_colour_t { colour.red, colour.green, colour.blue } {} @@ -443,10 +426,11 @@ namespace OpenVic { return colour_traits::alpha_to_float(alpha); } - OV_SPEED_INLINE constexpr std::to_chars_result to_hex_chars(char* first, char* last, bool alpha = colour_traits::has_alpha) const { + OV_SPEED_INLINE constexpr std::to_chars_result + to_hex_chars(char* first, char* last, bool alpha = colour_traits::has_alpha) const { constexpr size_t component_str_width = (std::bit_width(max_value) + 3) / 4; - std::to_chars_result result = StringUtils::to_chars(first, last, alpha ? as_rgba() : as_rgb(), 16); + std::to_chars_result result = OpenVic::to_chars(first, last, alpha ? as_rgba() : as_rgb(), 16); if (OV_unlikely(result.ec != std::errc {})) { return result; } @@ -566,7 +550,9 @@ namespace OpenVic { return new_colour; } - OV_SPEED_INLINE constexpr basic_colour_t full_invert() const requires(colour_traits::has_alpha) { + OV_SPEED_INLINE constexpr basic_colour_t full_invert() const + requires(colour_traits::has_alpha) + { basic_colour_t new_colour = *this; new_colour.red = max_value - new_colour.red; new_colour.green = max_value - new_colour.green; @@ -698,22 +684,22 @@ class fmt::detail::is_tuple_like_ { template T> struct fmt::formatter { - constexpr void set_brackets(basic_string_view open, basic_string_view close) { + constexpr void set_brackets(fmt::basic_string_view open, fmt::basic_string_view close) { _opening_bracket = open; _closing_bracket = close; } - constexpr void set_separator(basic_string_view sep) { + constexpr void set_separator(fmt::basic_string_view sep) { _separator = sep; } - constexpr format_parse_context::iterator parse(format_parse_context& ctx) { - format_parse_context::iterator it = ctx.begin(), end = ctx.end(); + constexpr fmt::format_parse_context::iterator parse(fmt::format_parse_context& ctx) { + fmt::format_parse_context::iterator it = ctx.begin(), end = ctx.end(); if (it == end || *it == '}') { return it; } - it = detail::parse_align(it, end, _specs); + it = fmt::detail::parse_align(it, end, _specs); if (it == end) { return it; } @@ -728,7 +714,7 @@ struct fmt::formatter { char c = *it; if ((c >= '0' && c <= '9') || c == '{') { - it = detail::parse_width(it, end, _specs, _specs.width_ref, ctx); + it = fmt::detail::parse_width(it, end, _specs, _specs.width_ref, ctx); if (it == end) { return it; } @@ -737,7 +723,7 @@ struct fmt::formatter { switch (*it) { case 'X': _specs.set_upper(); [[fallthrough]]; case 'x': - _specs.set_type(presentation_type::hex); + _specs.set_type(fmt::presentation_type::hex); ++it; break; } @@ -793,24 +779,24 @@ struct fmt::formatter { } if (it != end && (*it == ':' || _value_format_type != value_format_type::none)) { - if (_specs.align() != align::none) { - report_error("invalid format specifier"); + if (_specs.align() != fmt::align::none) { + fmt::report_error("invalid format specifier"); } - + if (_specs.alt()) { - report_error("invalid format specifier"); + fmt::report_error("invalid format specifier"); } - if (_specs.width != 0 || _specs.dynamic_width() != arg_id_kind::none) { - report_error("invalid format specifier"); + if (_specs.width != 0 || _specs.dynamic_width() != fmt::arg_id_kind::none) { + fmt::report_error("invalid format specifier"); } if (_specs.upper()) { - report_error("invalid format specifier"); + fmt::report_error("invalid format specifier"); } - if (_specs.type() == presentation_type::hex) { - report_error("invalid format specifier"); + if (_specs.type() == fmt::presentation_type::hex) { + fmt::report_error("invalid format specifier"); } if (*it == ':') { @@ -827,19 +813,19 @@ struct fmt::formatter { } if (_value_format_type != value_format_type::none) { - report_error("invalid format specifier"); + fmt::report_error("invalid format specifier"); } return it; } - format_context::iterator format(T const& colour, format_context& ctx) const { - format_specs specs { _specs }; + fmt::format_context::iterator format(T const& colour, fmt::format_context& ctx) const { + fmt::format_specs specs { _specs }; if (_specs.dynamic()) { - detail::handle_dynamic_spec(_specs.dynamic_width(), specs.width, _specs.width_ref, ctx); + fmt::detail::handle_dynamic_spec(_specs.dynamic_width(), specs.width, _specs.width_ref, ctx); } - format_context::iterator out = ctx.out(); + fmt::format_context::iterator out = ctx.out(); if (_value_format_type == value_format_type::none) { typename T::stack_string result = [&]() -> typename T::stack_string { @@ -856,16 +842,16 @@ struct fmt::formatter { return out; } - if (_specs.type() == presentation_type::hex) { + if (_specs.type() == fmt::presentation_type::hex) { bool upper = _specs.upper(); if (_specs.alt()) { if (upper) { - out = detail::write(out, "0X"); + out = fmt::detail::write(out, "0X"); } else { - out = detail::write(out, "0x"); + out = fmt::detail::write(out, "0x"); } } - + if (!upper) { std::array lower; size_t i = 0; @@ -876,13 +862,13 @@ struct fmt::formatter { } ++i; } - return detail::write(out, string_view { lower.data(), i }, specs, ctx.locale()); + return fmt::detail::write(out, fmt::string_view { lower.data(), i }, specs, ctx.locale()); } } else if (_specs.alt()) { - *out++ = '#'; + *out++ = '#'; } - return detail::write(out, string_view { result.data(), result.size() }, specs, ctx.locale()); + return fmt::detail::write(out, fmt::string_view { result.data(), result.size() }, specs, ctx.locale()); } auto write_value = [&](T::value_type const& comp) { @@ -894,36 +880,36 @@ struct fmt::formatter { } }; - out = detail::copy(_opening_bracket, out); + out = fmt::detail::copy(_opening_bracket, out); size_t size = colour.size(); if (size == 4) { switch (_alpha_handle_type) { case alpha_handle_type::argb: write_value(colour[--size]); - out = detail::copy(_separator, out); + out = fmt::detail::copy(_separator, out); break; case alpha_handle_type::no_alpha: --size; break; default: break; } } else if (_alpha_handle_type == alpha_handle_type::argb) { write_value(T::max_value); - out = detail::copy(_separator, out); + out = fmt::detail::copy(_separator, out); } size_t i = 0; for (; i < size; ++i) { if (i > 0) { - out = detail::copy(_separator, out); + out = fmt::detail::copy(_separator, out); } write_value(colour[i]); } if (i == 3 && _alpha_handle_type == alpha_handle_type::rgba) { - out = detail::copy(_separator, out); + out = fmt::detail::copy(_separator, out); write_value(T::max_value); } - return detail::copy(_closing_bracket, out); + return fmt::detail::copy(_closing_bracket, out); } private: @@ -935,9 +921,9 @@ struct fmt::formatter { enum value_format_type { none, value, integer, floating } _value_format_type = value_format_type::none; enum alpha_handle_type { follow_type, no_alpha, argb, rgba } _alpha_handle_type = alpha_handle_type::follow_type; - basic_string_view _separator = detail::string_literal {}; - basic_string_view _opening_bracket = detail::string_literal {}; - basic_string_view _closing_bracket = detail::string_literal {}; + fmt::basic_string_view _separator = fmt::detail::string_literal {}; + fmt::basic_string_view _opening_bracket = fmt::detail::string_literal {}; + fmt::basic_string_view _closing_bracket = fmt::detail::string_literal {}; }; namespace std { diff --git a/src/openvic-simulation/types/Date.cpp b/src/openvic-simulation/core/object/Date.cpp similarity index 99% rename from src/openvic-simulation/types/Date.cpp rename to src/openvic-simulation/core/object/Date.cpp index 9596ee993..1d58382d9 100644 --- a/src/openvic-simulation/types/Date.cpp +++ b/src/openvic-simulation/core/object/Date.cpp @@ -1,8 +1,8 @@ #include "Date.hpp" -#include +#include #include -#include +#include #include #include #include @@ -11,7 +11,7 @@ #include -#include "openvic-simulation/utility/Typedefs.hpp" +#include "openvic-simulation/core/Typedefs.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/types/Date.hpp b/src/openvic-simulation/core/object/Date.hpp similarity index 94% rename from src/openvic-simulation/types/Date.hpp rename to src/openvic-simulation/core/object/Date.hpp index a6a5e62b4..fafe2a495 100644 --- a/src/openvic-simulation/types/Date.hpp +++ b/src/openvic-simulation/core/object/Date.hpp @@ -9,19 +9,19 @@ #include #include #include -#include #include #include #include -#include "openvic-simulation/types/StackString.hpp" -#include "openvic-simulation/utility/ErrorMacros.hpp" -#include "openvic-simulation/utility/Getters.hpp" -#include "openvic-simulation/utility/StringUtils.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" -#include "openvic-simulation/utility/Hash.hpp" +#include "openvic-simulation/core/Hash.hpp" +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/container/StackString.hpp" +#include "openvic-simulation/core/error/ErrorMacros.hpp" +#include "openvic-simulation/core/memory/Format.hpp" +#include "openvic-simulation/core/string/CharConv.hpp" namespace OpenVic { // A relative period between points in time, measured in days @@ -119,9 +119,8 @@ namespace OpenVic { static constexpr Timespan::day_t MONTHS_IN_YEAR = DAYS_IN_MONTH.size(); static constexpr Timespan::day_t MAX_DAYS_IN_MONTH = *ranges::max_element(DAYS_IN_MONTH); - static constexpr Timespan::day_t DAYS_IN_YEAR = std::accumulate( - DAYS_IN_MONTH.begin(), DAYS_IN_MONTH.end(), Timespan::day_t { 0 } - ); + static constexpr Timespan::day_t DAYS_IN_YEAR = + std::accumulate(DAYS_IN_MONTH.begin(), DAYS_IN_MONTH.end(), Timespan::day_t { 0 }); static_assert(DAYS_IN_YEAR == 365); static constexpr std::array DAYS_UP_TO_MONTH = [] { @@ -148,8 +147,8 @@ namespace OpenVic { static constexpr char SEPARATOR_CHARACTER = '.'; static constexpr std::array MONTH_NAMES { - "January", "February", "March", "April", "May", "June", // - "July", "August", "September", "October", "November", "December" // + "January", "February", "March", "April", "May", "June", // + "July", "August", "September", "October", "November", "December" // }; static constexpr std::string_view INVALID_MONTH_NAME = "Invalid Month"; @@ -177,7 +176,8 @@ namespace OpenVic { // Negative Timespans indicate dates before Jan 1st, Year 0. OV_ALWAYS_INLINE constexpr Date(Timespan new_timespan) : timespan { new_timespan } {} // Year month day specification - OV_ALWAYS_INLINE constexpr Date(year_t year = 0, month_t month = 1, day_t day = 1) : timespan { _date_to_timespan(year, month, day) } {} + OV_ALWAYS_INLINE constexpr Date(year_t year = 0, month_t month = 1, day_t day = 1) + : timespan { _date_to_timespan(year, month, day) } {} OV_SPEED_INLINE constexpr Timespan::day_t get_day_of_year() const { Timespan::day_t day_in_year = static_cast(timespan) % DAYS_IN_YEAR; @@ -188,10 +188,9 @@ namespace OpenVic { } OV_SPEED_INLINE constexpr year_t get_year() const { - return (timespan >= 0 - ? static_cast(timespan) - : static_cast(timespan) - DAYS_IN_YEAR + 1 - ) / DAYS_IN_YEAR; + return (timespan >= 0 ? static_cast(timespan) + : static_cast(timespan) - DAYS_IN_YEAR + 1) / + DAYS_IN_YEAR; } OV_SPEED_INLINE constexpr month_t get_month() const { return MONTH_FROM_DAY_IN_YEAR[get_day_of_year()]; @@ -301,7 +300,7 @@ namespace OpenVic { } } - std::to_chars_result result = StringUtils::to_chars(first, last, year); + std::to_chars_result result = OpenVic::to_chars(first, last, year); if (OV_unlikely(result.ec != std::errc {})) { return result; } @@ -320,7 +319,7 @@ namespace OpenVic { *result.ptr = '0'; result.ptr++; } - result = StringUtils::to_chars(result.ptr, last, get_month()); + result = OpenVic::to_chars(result.ptr, last, get_month()); if (OV_unlikely(result.ec != std::errc {})) { return result; } @@ -339,12 +338,13 @@ namespace OpenVic { *result.ptr = '0'; result.ptr++; } - result = StringUtils::to_chars(result.ptr, last, get_day()); + result = OpenVic::to_chars(result.ptr, last, get_day()); return result; } struct stack_string; - OV_SPEED_INLINE constexpr stack_string to_array(bool pad_year = false, bool pad_month = true, bool pad_day = true) const; + OV_SPEED_INLINE constexpr stack_string + to_array(bool pad_year = false, bool pad_month = true, bool pad_day = true) const; struct stack_string final : StackString< fmt::detail::count_digits(uint64_t(std::numeric_limits::max())) + @@ -396,7 +396,7 @@ namespace OpenVic { const char* first, const char* last, year_t& year, month_t& month, day_t& day ) { int32_t year_check = year; - from_chars_result result = { StringUtils::from_chars(first, last, year_check) }; + from_chars_result result = { OpenVic::from_chars(first, last, year_check) }; result.type_first = first; result.type = errc_type::year; if (OV_unlikely(result.ec != std::errc {})) { @@ -404,8 +404,8 @@ namespace OpenVic { } if (OV_unlikely( - year_check > std::numeric_limits::max() || year_check < std::numeric_limits::min() - )) { + year_check > std::numeric_limits::max() || year_check < std::numeric_limits::min() + )) { result.ec = std::errc::value_too_large; result.ptr = first; return result; @@ -426,7 +426,7 @@ namespace OpenVic { result.ptr++; first = result.ptr; month_t month_check = month; - result = { StringUtils::from_chars(first, last, month_check) }; + result = { OpenVic::from_chars(first, last, month_check) }; result.type_first = first; result.type = errc_type::month; if (OV_unlikely(result.ec != std::errc {})) { @@ -460,7 +460,7 @@ namespace OpenVic { result.ptr++; first = result.ptr; day_t day_check = day; - result = { StringUtils::from_chars(first, last, day_check) }; + result = { OpenVic::from_chars(first, last, day_check) }; result.type_first = first; result.type = errc_type::day; if (OV_unlikely(result.ec != std::errc {})) { @@ -563,7 +563,9 @@ namespace OpenVic { from_chars->ec == std::errc::value_too_large && from_chars->type == errc_type::day && from_chars->ptr == from_chars->type_first, date, - memory::fmt::format("Day value cannot be larger than {} for {}.", DAYS_IN_MONTH[date.get_month() - 1], date.get_month()) + memory::fmt::format( + "Day value cannot be larger than {} for {}.", DAYS_IN_MONTH[date.get_month() - 1], date.get_month() + ) ); return date; diff --git a/src/openvic-simulation/types/fixed_point/FixedPoint.cpp b/src/openvic-simulation/core/object/FixedPoint.cpp similarity index 96% rename from src/openvic-simulation/types/fixed_point/FixedPoint.cpp rename to src/openvic-simulation/core/object/FixedPoint.cpp index 3fce1193b..80cf69b85 100644 --- a/src/openvic-simulation/types/fixed_point/FixedPoint.cpp +++ b/src/openvic-simulation/core/object/FixedPoint.cpp @@ -3,7 +3,7 @@ #include #include -#include "openvic-simulation/utility/Typedefs.hpp" +#include "openvic-simulation/core/Typedefs.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/types/fixed_point/FixedPoint.hpp b/src/openvic-simulation/core/object/FixedPoint.hpp similarity index 92% rename from src/openvic-simulation/types/fixed_point/FixedPoint.hpp rename to src/openvic-simulation/core/object/FixedPoint.hpp index cf215b8c8..9dded72a0 100644 --- a/src/openvic-simulation/types/fixed_point/FixedPoint.hpp +++ b/src/openvic-simulation/core/object/FixedPoint.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include #include @@ -10,27 +9,26 @@ #include #include #include -#include #include #include -#include "openvic-simulation/types/StackString.hpp" -#include "openvic-simulation/utility/Getters.hpp" -#include "openvic-simulation/utility/Logger.hpp" -#include "openvic-simulation/utility/StringUtils.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" -#include "openvic-simulation/utility/Math.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/Math.hpp" +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/container/StackString.hpp" +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/string/CharConv.hpp" /* Sin lookup table */ -#include "openvic-simulation/types/fixed_point/FixedPointLUT_sin.hpp" +#include "openvic-simulation/core/object/FixedPoint/LUT_sin.hpp" /* Base e exponential lookup table */ -#include "openvic-simulation/types/fixed_point/FixedPointLUT_2_16_EXP_e.hpp" +#include "openvic-simulation/core/object/FixedPoint/LUT_2_16_EXP_e.hpp" /* Base 2001 exponential lookup table */ -#include "openvic-simulation/types/fixed_point/FixedPointLUT_2_16_EXP_2001.hpp" +#include "openvic-simulation/core/object/FixedPoint/LUT_2_16_EXP_2001.hpp" namespace OpenVic { enum class midpoint_rounding { AWAY_ZERO, TO_ZERO }; @@ -77,7 +75,8 @@ namespace OpenVic { public: OV_ALWAYS_INLINE constexpr fixed_point_t() : value { 0 } {} - OV_ALWAYS_INLINE constexpr fixed_point_t(int32_t new_value) : value { static_cast(new_value) << PRECISION } {} + OV_ALWAYS_INLINE constexpr fixed_point_t(int32_t new_value) + : value { static_cast(new_value) << PRECISION } {} static const fixed_point_t max; static const fixed_point_t min; @@ -176,9 +175,7 @@ namespace OpenVic { } OV_SPEED_INLINE constexpr fixed_point_t sqrt() const { - return !is_negative() - ? parse_raw(OpenVic::sqrt(static_cast(value) << PRECISION)) - : _0; + return !is_negative() ? parse_raw(OpenVic::sqrt(static_cast(value) << PRECISION)) : _0; } OV_SPEED_INLINE constexpr bool is_integer() const { @@ -187,7 +184,7 @@ namespace OpenVic { template OV_SPEED_INLINE constexpr T unsafe_truncate() const { - if constexpr(std::unsigned_integral) { + if constexpr (std::unsigned_integral) { assert(OV_likely(!is_negative())); } return value >> PRECISION; @@ -199,9 +196,9 @@ namespace OpenVic { if (!std::is_constant_evaluated()) { if (OV_unlikely(value != 0 && abs() < ONE)) { spdlog::warn_s( - "0 < abs(Fixed point) < 1, truncation will result in zero, this may be a bug. raw_value: {} as float: {}", - get_raw_value(), - static_cast(*this) + "0 < abs(Fixed point) < 1, truncation will result in zero, this may be a bug. raw_value: {} as float: " + "{}", + get_raw_value(), static_cast(*this) ); } } @@ -320,7 +317,7 @@ namespace OpenVic { std::to_chars_result result {}; if (decimal_places == static_cast(-1)) { - result = StringUtils::to_chars(first, last, abs().unsafe_truncate()); + result = OpenVic::to_chars(first, last, abs().unsafe_truncate()); if (OV_unlikely(result.ec != std::errc {})) { return result; } @@ -353,7 +350,7 @@ namespace OpenVic { fixed_point_t val = this->abs() + err; - result = StringUtils::to_chars(first, last, val.unsafe_truncate()); + result = OpenVic::to_chars(first, last, val.unsafe_truncate()); if (OV_unlikely(result.ec != std::errc {})) { return result; } @@ -379,7 +376,9 @@ namespace OpenVic { } struct stack_string; - OV_SPEED_INLINE constexpr stack_string to_array(size_t decimal_places = -1) const; + OV_SPEED_INLINE constexpr stack_string to_array( // + size_t decimal_places = -1 + ) const; struct stack_string final : StackString<25> { protected: @@ -433,7 +432,7 @@ namespace OpenVic { from_chars = from_chars_integer(begin, dot_pointer, result); } - if (from_chars.ec != std::errc{}) { + if (from_chars.ec != std::errc {}) { return from_chars; } @@ -444,7 +443,7 @@ namespace OpenVic { result += result.is_negative() || (*begin == '-' && result == _0) ? -adder : adder; } - if (from_chars.ec != std::errc{}) { + if (from_chars.ec != std::errc {}) { return { begin, from_chars.ec }; } @@ -622,8 +621,10 @@ namespace OpenVic { return *this; } - //Preserves accuracy. Performing a normal multiplication of small values results in 0. - OV_SPEED_INLINE constexpr static fixed_point_t mul_div(fixed_point_t const& a, fixed_point_t const& b, fixed_point_t const& denominator) { + // Preserves accuracy. Performing a normal multiplication of small values results in 0. + OV_SPEED_INLINE constexpr static fixed_point_t mul_div( // + fixed_point_t const& a, fixed_point_t const& b, fixed_point_t const& denominator + ) { return parse_raw(a.value * b.value / denominator.value); } @@ -678,10 +679,12 @@ namespace OpenVic { private: // Deterministic // Can produce negative values - OV_SPEED_INLINE static constexpr std::from_chars_result from_chars_integer(char const* str, char const* const end, fixed_point_t& value) { + OV_SPEED_INLINE static constexpr std::from_chars_result from_chars_integer( // + char const* str, char const* const end, fixed_point_t& value + ) { int64_t parsed_value = 0; - std::from_chars_result result = StringUtils::string_to_int64(str, end, parsed_value); - if (result.ec == std::errc{}) { + std::from_chars_result result = string_to_int64(str, end, parsed_value); + if (result.ec == std::errc {}) { value = parse(parsed_value); } return result; @@ -689,15 +692,17 @@ namespace OpenVic { // Deterministic // Cannot produce negative values - OV_SPEED_INLINE static constexpr std::from_chars_result from_chars_fraction(char const* begin, char const* end, fixed_point_t& value) { + OV_SPEED_INLINE static constexpr std::from_chars_result from_chars_fraction( // + char const* begin, char const* end, fixed_point_t& value + ) { if (begin && *begin == '-') { return { begin, std::errc::invalid_argument }; } end = end - begin > PRECISION ? begin + PRECISION : end; uint64_t parsed_value; - std::from_chars_result result = StringUtils::string_to_uint64(begin, end, parsed_value); - if (result.ec != std::errc{}) { + std::from_chars_result result = string_to_uint64(begin, end, parsed_value); + if (result.ec != std::errc {}) { return result; } diff --git a/src/openvic-simulation/types/fixed_point/Atomic.hpp b/src/openvic-simulation/core/object/FixedPoint/Atomic.hpp similarity index 98% rename from src/openvic-simulation/types/fixed_point/Atomic.hpp rename to src/openvic-simulation/core/object/FixedPoint/Atomic.hpp index ed75b08d5..399bfc30a 100644 --- a/src/openvic-simulation/types/fixed_point/Atomic.hpp +++ b/src/openvic-simulation/core/object/FixedPoint/Atomic.hpp @@ -1,10 +1,9 @@ #pragma once #include -#include -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" namespace std { template<> diff --git a/src/openvic-simulation/types/fixed_point/FixedPointLUT_2_16_EXP_2001.hpp b/src/openvic-simulation/core/object/FixedPoint/LUT_2_16_EXP_2001.hpp similarity index 100% rename from src/openvic-simulation/types/fixed_point/FixedPointLUT_2_16_EXP_2001.hpp rename to src/openvic-simulation/core/object/FixedPoint/LUT_2_16_EXP_2001.hpp diff --git a/src/openvic-simulation/types/fixed_point/FixedPointLUT_2_16_EXP_e.hpp b/src/openvic-simulation/core/object/FixedPoint/LUT_2_16_EXP_e.hpp similarity index 100% rename from src/openvic-simulation/types/fixed_point/FixedPointLUT_2_16_EXP_e.hpp rename to src/openvic-simulation/core/object/FixedPoint/LUT_2_16_EXP_e.hpp diff --git a/src/openvic-simulation/types/fixed_point/FixedPointLUT_sin.hpp b/src/openvic-simulation/core/object/FixedPoint/LUT_sin.hpp similarity index 100% rename from src/openvic-simulation/types/fixed_point/FixedPointLUT_sin.hpp rename to src/openvic-simulation/core/object/FixedPoint/LUT_sin.hpp diff --git a/src/openvic-simulation/types/Vector.cpp b/src/openvic-simulation/core/object/Vector.cpp similarity index 100% rename from src/openvic-simulation/types/Vector.cpp rename to src/openvic-simulation/core/object/Vector.cpp diff --git a/src/openvic-simulation/types/Vector.hpp b/src/openvic-simulation/core/object/Vector.hpp similarity index 83% rename from src/openvic-simulation/types/Vector.hpp rename to src/openvic-simulation/core/object/Vector.hpp index b60adc060..89d75540f 100644 --- a/src/openvic-simulation/types/Vector.hpp +++ b/src/openvic-simulation/core/object/Vector.hpp @@ -5,11 +5,11 @@ #include -#include "openvic-simulation/types/BasicIterator.hpp" // IWYU pragma: keep -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Concepts.hpp" // IWYU pragma: keep -#include "openvic-simulation/utility/Math.hpp" // IWYU pragma: keep -#include "openvic-simulation/utility/Typedefs.hpp" // IWYU pragma: keep +#include "openvic-simulation/core/Math.hpp" // IWYU pragma: keep +#include "openvic-simulation/core/Typedefs.hpp" // IWYU pragma: keep +#include "openvic-simulation/core/container/BasicIterator.hpp" // IWYU pragma: keep +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" // IWYU pragma: keep #define VEC_TYPE vec2_t #define FOR_VEC_COMPONENTS(F, SEP) F(x) SEP F(y) diff --git a/src/openvic-simulation/types/VectorN.inc b/src/openvic-simulation/core/object/VectorN.inc similarity index 100% rename from src/openvic-simulation/types/VectorN.inc rename to src/openvic-simulation/core/object/VectorN.inc diff --git a/src/openvic-simulation/utility/Deque.hpp b/src/openvic-simulation/core/portable/Deque.hpp similarity index 99% rename from src/openvic-simulation/utility/Deque.hpp rename to src/openvic-simulation/core/portable/Deque.hpp index 92c499c08..95f80dcd1 100644 --- a/src/openvic-simulation/utility/Deque.hpp +++ b/src/openvic-simulation/core/portable/Deque.hpp @@ -1909,7 +1909,7 @@ namespace OpenVic::utility { } #endif -#include "openvic-simulation/utility/Concepts.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" namespace OpenVic { template diff --git a/src/openvic-simulation/utility/ForwardableSpan.hpp b/src/openvic-simulation/core/portable/ForwardableSpan.hpp similarity index 93% rename from src/openvic-simulation/utility/ForwardableSpan.hpp rename to src/openvic-simulation/core/portable/ForwardableSpan.hpp index 712c85dc1..d326bd9c4 100644 --- a/src/openvic-simulation/utility/ForwardableSpan.hpp +++ b/src/openvic-simulation/core/portable/ForwardableSpan.hpp @@ -9,10 +9,10 @@ #include #include -#include "openvic-simulation/types/BasicIterator.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/container/BasicIterator.hpp" -namespace OpenVic::utility::_detail::forwardable_span { +namespace OpenVic::_detail::forwardable_span { static constexpr std::size_t dynamic_extent = std::numeric_limits::max(); template @@ -347,7 +347,7 @@ namespace OpenVic::utility::_detail::forwardable_span { span(Range&&) -> span>>; } -namespace OpenVic::utility { +namespace OpenVic { static constexpr std::size_t dynamic_extent = _detail::forwardable_span::dynamic_extent; template @@ -357,37 +357,37 @@ namespace OpenVic::utility { namespace std { namespace ranges { template - inline constexpr bool enable_borrowed_range> = true; + inline constexpr bool enable_borrowed_range> = true; template - inline constexpr bool enable_view> = true; + inline constexpr bool enable_view> = true; } template - [[nodiscard]] inline OpenVic::utility::forwardable_span< + [[nodiscard]] inline OpenVic::forwardable_span< const std::byte, Extent == dynamic_extent ? dynamic_extent : Extent * sizeof(T)> - as_bytes(OpenVic::utility::forwardable_span sp) { + as_bytes(OpenVic::forwardable_span sp) { auto data = reinterpret_cast(sp.data()); auto size = sp.size_bytes(); constexpr std::size_t extent = Extent == dynamic_extent ? dynamic_extent : Extent * sizeof(T); - return OpenVic::utility::forwardable_span { data, size }; + return OpenVic::forwardable_span { data, size }; } template requires(!std::is_const_v) - inline OpenVic::utility::forwardable_span - as_writable_bytes [[nodiscard]] (OpenVic::utility::forwardable_span sp) { + inline OpenVic::forwardable_span + as_writable_bytes [[nodiscard]] (OpenVic::forwardable_span sp) { auto data = reinterpret_cast(sp.data()); auto size = sp.size_bytes(); constexpr std::size_t extent = Extent == dynamic_extent ? dynamic_extent : Extent * sizeof(T); - return OpenVic::utility::forwardable_span { data, size }; + return OpenVic::forwardable_span { data, size }; } } #else #include -namespace OpenVic::utility { +namespace OpenVic { template using forwardable_span = std::span; } diff --git a/src/openvic-simulation/utility/RandomGenerator.hpp b/src/openvic-simulation/core/random/RandomGenerator.hpp similarity index 97% rename from src/openvic-simulation/utility/RandomGenerator.hpp rename to src/openvic-simulation/core/random/RandomGenerator.hpp index c884af36e..f569f5001 100644 --- a/src/openvic-simulation/utility/RandomGenerator.hpp +++ b/src/openvic-simulation/core/random/RandomGenerator.hpp @@ -6,7 +6,7 @@ #include #include -#include "openvic-simulation/utility/Typedefs.hpp" +#include "openvic-simulation/core/Typedefs.hpp" #include diff --git a/src/openvic-simulation/utility/WeightedSampling.hpp b/src/openvic-simulation/core/random/WeightedSampling.hpp similarity index 80% rename from src/openvic-simulation/utility/WeightedSampling.hpp rename to src/openvic-simulation/core/random/WeightedSampling.hpp index e200a88bd..3e30a22dd 100644 --- a/src/openvic-simulation/utility/WeightedSampling.hpp +++ b/src/openvic-simulation/core/random/WeightedSampling.hpp @@ -3,11 +3,11 @@ #include #include -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" namespace OpenVic { - OV_ALWAYS_INLINE static size_t sample_weighted_index( + OV_SPEED_INLINE static size_t sample_weighted_index( const uint32_t random_value, const std::span positive_weights, const fixed_point_t weights_sum diff --git a/src/openvic-simulation/utility/reactive/DependencyTracker.hpp b/src/openvic-simulation/core/reactive/DependencyTracker.hpp similarity index 78% rename from src/openvic-simulation/utility/reactive/DependencyTracker.hpp rename to src/openvic-simulation/core/reactive/DependencyTracker.hpp index 1ea60b491..ad22f86fa 100644 --- a/src/openvic-simulation/utility/reactive/DependencyTracker.hpp +++ b/src/openvic-simulation/core/reactive/DependencyTracker.hpp @@ -2,7 +2,8 @@ #include -#include "openvic-simulation/types/Signal.hpp" +#include "openvic-simulation/core/Signal.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" namespace OpenVic { struct DependencyTracker { @@ -27,11 +28,12 @@ namespace OpenVic { changed(); } + protected: signal<> changed; bool is_dirty = true; std::mutex is_dirty_lock; - + virtual ~DependencyTracker() { disconnect_all(); } @@ -44,20 +46,19 @@ namespace OpenVic { const std::lock_guard lock_guard { connections_lock }; connections.clear(); } + public: void track(signal<>& dependency_changed) { const std::lock_guard lock_guard { connections_lock }; - connections.emplace_back( - dependency_changed.connect(&DependencyTracker::mark_dirty, this) - ); + connections.emplace_back(dependency_changed.connect(&DependencyTracker::mark_dirty, this)); } template void track(signal& dependency_changed) { const std::lock_guard lock_guard { connections_lock }; - connections.emplace_back( - dependency_changed.connect([this](Args...) { mark_dirty(); }) - ); + connections.emplace_back(dependency_changed.connect([this](Args...) { + mark_dirty(); + })); } }; } diff --git a/src/openvic-simulation/utility/reactive/DerivedState.hpp b/src/openvic-simulation/core/reactive/DerivedState.hpp similarity index 77% rename from src/openvic-simulation/utility/reactive/DerivedState.hpp rename to src/openvic-simulation/core/reactive/DerivedState.hpp index eaae07b6f..7d5ddce5d 100644 --- a/src/openvic-simulation/utility/reactive/DerivedState.hpp +++ b/src/openvic-simulation/core/reactive/DerivedState.hpp @@ -1,16 +1,17 @@ #pragma once -#include "DependencyTracker.hpp" +#include +#include +#include -#include +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/Signal.hpp" +#include "openvic-simulation/core/reactive/DependencyTracker.hpp" -#include "openvic-simulation/types/Signal.hpp" -#include "openvic-simulation/utility/Getters.hpp" -#include "openvic-simulation/utility/Logger.hpp" -#include "openvic-simulation/utility/reactive/DependencyTracker.hpp" +#include namespace OpenVic { - template + template struct DerivedState : public DependencyTracker { private: T cached_value; @@ -33,7 +34,7 @@ namespace OpenVic { "Its value will never change again. " "If it should be reactive, ensure 'calculate' accesses its dependencies. " "Alternatively use a plain variable or MutableState.", - OpenVic::utility::type_name() + OpenVic::type_name() ); } @@ -44,17 +45,15 @@ namespace OpenVic { public: template explicit DerivedState(Func&& new_calculate) - requires std::is_default_constructible_v - && std::is_convertible_v> - : calculate { std::forward(new_calculate) }, - cached_value() {} + requires std::is_default_constructible_v && std::is_convertible_v> + : calculate { std::forward(new_calculate) }, cached_value() {} template - requires std::is_convertible_v - && std::is_convertible_v> + requires std::is_convertible_v && + std::is_convertible_v> explicit DerivedState(Func&& new_calculate, InitialValue&& empty_initial_value) : calculate { std::forward(new_calculate) }, - cached_value { std::forward(empty_initial_value) } {} + cached_value { std::forward(empty_initial_value) } {} DerivedState(DerivedState&&) = delete; DerivedState(DerivedState const&) = delete; @@ -77,8 +76,8 @@ namespace OpenVic { recalculate_if_dirty(); return cached_value; } - - //special case where connection may be discarded as the observer handles it + + // special case where connection may be discarded as the observer handles it template requires OpenVic::_detail::signal::Callable connection connect_using_observer(Pmf&& pmf, Ptr&& ptr, OpenVic::_detail::signal::group_id gid = 0) { diff --git a/src/openvic-simulation/utility/reactive/MutableState.hpp b/src/openvic-simulation/core/reactive/MutableState.hpp similarity index 83% rename from src/openvic-simulation/utility/reactive/MutableState.hpp rename to src/openvic-simulation/core/reactive/MutableState.hpp index b1548e710..175fac33e 100644 --- a/src/openvic-simulation/utility/reactive/MutableState.hpp +++ b/src/openvic-simulation/core/reactive/MutableState.hpp @@ -1,18 +1,24 @@ #pragma once -#include "openvic-simulation/types/Signal.hpp" -#include "openvic-simulation/utility/Concepts.hpp" -#include "openvic-simulation/utility/reactive/DependencyTracker.hpp" +#include +#include + +#include "openvic-simulation/core/Signal.hpp" +#include "openvic-simulation/core/reactive/DependencyTracker.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" namespace OpenVic { - template + template struct ReadOnlyMutableState { private: signal changed; + protected: T value; - constexpr ReadOnlyMutableState() requires std::is_default_constructible_v : value() {} + constexpr ReadOnlyMutableState() + requires std::is_default_constructible_v + : value() {} constexpr explicit ReadOnlyMutableState(T const& new_value) : value { new_value } {} constexpr explicit ReadOnlyMutableState(T&& new_value) : value { std::move(new_value) } {} ReadOnlyMutableState(ReadOnlyMutableState&&) = delete; @@ -29,6 +35,7 @@ namespace OpenVic { value = new_value; changed(value); } + public: template requires std::invocable&> @@ -43,8 +50,8 @@ namespace OpenVic { [[nodiscard]] constexpr T const& get_untracked() const { return value; } - - //special case where connection may be discarded as the observer handles it + + // special case where connection may be discarded as the observer handles it template requires OpenVic::_detail::signal::Callable connection connect_using_observer(Pmf&& pmf, Ptr&& ptr, OpenVic::_detail::signal::group_id gid = 0) { @@ -71,9 +78,11 @@ namespace OpenVic { return changed.disconnect(std::forward(args)...); } }; - template + template struct MutableState : public ReadOnlyMutableState { - constexpr MutableState() requires std::is_default_constructible_v : ReadOnlyMutableState() {} + constexpr MutableState() + requires std::is_default_constructible_v + : ReadOnlyMutableState() {} constexpr explicit MutableState(T const& new_value) : ReadOnlyMutableState(new_value) {} constexpr explicit MutableState(T&& new_value) : ReadOnlyMutableState(std::move(new_value)) {} MutableState(MutableState&&) = delete; @@ -91,21 +100,29 @@ namespace OpenVic { } using ReadOnlyMutableState::value; - MutableState& operator++() requires pre_incrementable { + MutableState& operator++() + requires pre_incrementable + { set(++value); return *this; } - void operator++(int) requires post_incrementable { + void operator++(int) + requires post_incrementable + { set(value++); } - MutableState& operator--() requires pre_decrementable { + MutableState& operator--() + requires pre_decrementable + { set(--value); return *this; } - void operator--(int) requires post_decrementable { + void operator--(int) + requires post_decrementable + { set(value--); } diff --git a/src/openvic-simulation/utility/StringUtils.hpp b/src/openvic-simulation/core/string/CharConv.hpp similarity index 70% rename from src/openvic-simulation/utility/StringUtils.hpp rename to src/openvic-simulation/core/string/CharConv.hpp index b10c06cdc..bf3d357d9 100644 --- a/src/openvic-simulation/utility/StringUtils.hpp +++ b/src/openvic-simulation/core/string/CharConv.hpp @@ -1,26 +1,12 @@ #pragma once -#include -#include #include -#include +#include #include -#include #include -#include #include -#include -#include -#include - -#include -#include -#include - -#include "openvic-simulation/utility/Containers.hpp" - -namespace OpenVic::StringUtils { +namespace OpenVic { template [[nodiscard]] inline constexpr std::from_chars_result from_chars( // char const* const first, char const* const last, T& raw_value, const int base = 10 @@ -163,7 +149,7 @@ namespace OpenVic::StringUtils { constexpr bool use_chunks = sizeof(_unsigned) > sizeof(size_t); if constexpr (use_chunks) { // For 64-bit numbers on 32-bit platforms, work in chunks to avoid 64-bit - // divisions. + // divisions. while (value > 0xFFFF'FFFFU) { // Performance note: Ryu's division workaround would be faster here. unsigned long chunk = static_cast(value % 1'000'000'000); @@ -250,11 +236,12 @@ namespace OpenVic::StringUtils { }; //[neargye] constexpr copy chars. P1944 fix this? trivial_copy(first, r_next, static_cast(digits_written)); - + return { first + digits_written, std::errc {} }; } + /* The constexpr function 'string_to_uint64' will convert a string into a uint64_t integer value. * The function takes four parameters: the input string (as a pair of pointers marking the start and * end of the string), the value reference to assign, and the base for numerical conversion. @@ -263,7 +250,8 @@ namespace OpenVic::StringUtils { * still starts with "0", otherwise 10. The std::from_chars_result return value is used to report whether * or not conversion was successful. */ - [[nodiscard]] inline constexpr std::from_chars_result string_to_uint64(char const* str, char const* const end, uint64_t& value, int base = 10) { + [[nodiscard]] inline constexpr std::from_chars_result + string_to_uint64(char const* str, char const* const end, uint64_t& value, int base = 10) { // If base is zero, base is determined by the string prefix. if (base == 0) { if (str && *str == '0') { @@ -286,11 +274,15 @@ namespace OpenVic::StringUtils { return from_chars(str, end, value, base); } - [[nodiscard]] inline constexpr std::from_chars_result string_to_uint64(char const* const str, size_t length, uint64_t& value, int base = 10) { + [[nodiscard]] inline constexpr std::from_chars_result string_to_uint64( // + char const* const str, size_t length, std::uint64_t& value, int base = 10 + ) { return string_to_uint64(str, str + length, value, base); } - [[nodiscard]] inline constexpr std::from_chars_result string_to_uint64(std::string_view str, uint64_t& value, int base = 10) { + [[nodiscard]] inline constexpr std::from_chars_result string_to_uint64( // + std::string_view str, std::uint64_t& value, int base = 10 + ) { return string_to_uint64(str.data(), str.length(), value, base); } @@ -302,7 +294,9 @@ namespace OpenVic::StringUtils { * still starts with "0", otherwise 10. The std::from_chars_result return value is used to report whether * or not conversion was successful. */ - [[nodiscard]] inline constexpr std::from_chars_result string_to_int64(char const* str, char const* const end, int64_t& value, int base = 10) { + [[nodiscard]] inline constexpr std::from_chars_result string_to_int64( // + char const* str, char const* const end, std::int64_t& value, int base = 10 + ) { // If base is zero, base is determined by the string prefix. if (base == 0) { if (str && *str == '0') { @@ -325,132 +319,15 @@ namespace OpenVic::StringUtils { return from_chars(str, end, value, base); } - [[nodiscard]] inline constexpr std::from_chars_result string_to_int64(char const* str, size_t length, int64_t& value, int base = 10) { + [[nodiscard]] inline constexpr std::from_chars_result string_to_int64( // + char const* str, size_t length, std::int64_t& value, int base = 10 + ) { return string_to_int64(str, str + length, value, base); } - [[nodiscard]] inline constexpr std::from_chars_result string_to_int64(std::string_view str, int64_t& value, int base = 10) { + [[nodiscard]] inline constexpr std::from_chars_result string_to_int64( // + std::string_view str, std::int64_t& value, int base = 10 + ) { return string_to_int64(str.data(), str.length(), value, base); } - - inline constexpr bool strings_equal_case_insensitive(std::string_view const& lhs, std::string_view const& rhs) { - if (lhs.size() != rhs.size()) { - return false; - } - constexpr auto ichar_equals = [](unsigned char l, unsigned char r) { - return std::tolower(l) == std::tolower(r); - }; - return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), ichar_equals); - } - - inline memory::string string_tolower(std::string_view str) { - memory::string result { str }; - std::transform(result.begin(), result.end(), result.begin(), - [](unsigned char c) -> unsigned char { return std::tolower(c); } - ); - return result; - } - - inline memory::string string_toupper(std::string_view str) { - memory::string result { str }; - std::transform(result.begin(), result.end(), result.begin(), - [](unsigned char c) -> unsigned char { return std::toupper(c); } - ); - return result; - } - - inline constexpr std::string_view bool_to_yes_no(bool b) { - return b ? "yes" : "no"; - } - - inline constexpr std::string_view get_filename(std::string_view path) { - size_t pos = path.size(); - while (pos > 0 && path[pos - 1] != '/' && path[pos - 1] != '\\') { - --pos; - } - path.remove_prefix(pos); - return path; - } - - inline constexpr char const* get_filename(char const* filepath, char const* default_path = nullptr) { - const std::string_view filename { get_filename(std::string_view { filepath }) }; - if (!filename.empty()) { - return filename.data(); - } - return default_path; - } - - inline memory::string make_forward_slash_path(std::string_view path) { - memory::string ret { path }; - std::replace(ret.begin(), ret.end(), '\\', '/'); - for (char& c : ret) { - if (c == '\\') { - c = '/'; - } - } - return ret; - } - - inline constexpr std::string_view remove_leading_slashes(std::string_view path) { - size_t count = 0; - while (count < path.size() && (path[count] == '/' || path[count] == '\\')) { - ++count; - } - path.remove_prefix(count); - return path; - } - - template - requires(std::is_same_v && ...) - inline memory::string _append_string_views(Args... args) { - memory::string ret; - ret.reserve((args.size() + ...)); - (ret.append(args), ...); - return ret; - } - - template - requires(std::is_convertible_v && ...) - inline memory::string append_string_views(Args... args) { - return _append_string_views(std::string_view { args }...); - } - - template - static memory::string string_join(tsl::ordered_map const& map, std::string_view delimiter = ", ") { - if (map.empty()) { - return ""; - } - - static auto transformer = [](std::pair const& pair) -> std::string_view { - return pair.first; - }; - return map | ranges::views::transform(transformer) | ranges::views::join(delimiter) | ranges::to(); - } - - inline constexpr size_t get_extension_pos(std::string_view const& path) { - size_t pos = path.size(); - while (pos > 0 && path[--pos] != '.') {} - return pos; - } - - inline constexpr std::string_view get_extension(std::string_view path) { - if (!path.empty()) { - const size_t pos = get_extension_pos(path); - if (path[pos] == '.') { - path.remove_prefix(pos); - return path; - } - } - return {}; - } - - inline constexpr std::string_view remove_extension(std::string_view path) { - if (!path.empty()) { - const size_t pos = get_extension_pos(path); - if (path[pos] == '.') { - path.remove_suffix(path.size() - pos); - } - } - return path; - } } diff --git a/src/openvic-simulation/core/string/Utility.hpp b/src/openvic-simulation/core/string/Utility.hpp new file mode 100644 index 000000000..3c24cad20 --- /dev/null +++ b/src/openvic-simulation/core/string/Utility.hpp @@ -0,0 +1,201 @@ +#pragma once + +#include +#include +#include + +#include + +#include +#include +#include + +#include "openvic-simulation/core/memory/String.hpp" + +namespace OpenVic { + inline constexpr bool strings_equal_case_insensitive(std::string_view const& lhs, std::string_view const& rhs) { + if (lhs.size() != rhs.size()) { + return false; + } + constexpr auto ichar_equals = [](unsigned char l, unsigned char r) { + return std::tolower(l) == std::tolower(r); + }; + return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), ichar_equals); + } + + inline memory::string string_tolower(std::string_view str) { + memory::string result { str }; + std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) -> unsigned char { + return std::tolower(c); + }); + return result; + } + + inline memory::string string_toupper(std::string_view str) { + memory::string result { str }; + std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) -> unsigned char { + return std::toupper(c); + }); + return result; + } + + inline constexpr std::string_view bool_to_yes_no(bool b) { + return b ? "yes" : "no"; + } + + inline constexpr std::string_view get_filename(std::string_view path) { + size_t pos = path.size(); + while (pos > 0 && path[pos - 1] != '/' && path[pos - 1] != '\\') { + --pos; + } + path.remove_prefix(pos); + return path; + } + + inline constexpr char const* get_filename(char const* filepath, char const* default_path = nullptr) { + const std::string_view filename { get_filename(std::string_view { filepath }) }; + if (!filename.empty()) { + return filename.data(); + } + return default_path; + } + + inline memory::string make_forward_slash_path(std::string_view path) { + memory::string ret { path }; + std::replace(ret.begin(), ret.end(), '\\', '/'); + for (char& c : ret) { + if (c == '\\') { + c = '/'; + } + } + return ret; + } + + inline constexpr std::string_view remove_leading_slashes(std::string_view path) { + size_t count = 0; + while (count < path.size() && (path[count] == '/' || path[count] == '\\')) { + ++count; + } + path.remove_prefix(count); + return path; + } + + template + requires(std::is_same_v && ...) + inline memory::string _append_string_views(Args... args) { + memory::string ret; + ret.reserve((args.size() + ...)); + (ret.append(args), ...); + return ret; + } + + template + requires(std::is_convertible_v && ...) + inline memory::string append_string_views(Args... args) { + return _append_string_views(std::string_view { args }...); + } + + template + static memory::string + string_join(tsl::ordered_map const& map, std::string_view delimiter = ", ") { + if (map.empty()) { + return ""; + } + + static auto transformer = [](std::pair const& pair) -> std::string_view { + return pair.first; + }; + return map | ranges::views::transform(transformer) | ranges::views::join(delimiter) | ranges::to(); + } + + inline constexpr size_t get_extension_pos(std::string_view const& path) { + size_t pos = path.size(); + while (pos > 0 && path[--pos] != '.') {} + return pos; + } + + inline constexpr std::string_view get_extension(std::string_view path) { + if (!path.empty()) { + const size_t pos = get_extension_pos(path); + if (path[pos] == '.') { + path.remove_prefix(pos); + return path; + } + } + return {}; + } + + inline constexpr std::string_view remove_extension(std::string_view path) { + if (!path.empty()) { + const size_t pos = get_extension_pos(path); + if (path[pos] == '.') { + path.remove_suffix(path.size() - pos); + } + } + return path; + } + + constexpr bool valid_basic_identifier_char(char c) { + return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9') || c == '_'; + } + constexpr bool valid_basic_identifier(std::string_view identifier) { + return std::all_of(identifier.begin(), identifier.end(), valid_basic_identifier_char); + } + constexpr std::string_view extract_basic_identifier_prefix(std::string_view identifier) { + size_t len = 0; + while (len < identifier.size() && valid_basic_identifier_char(identifier[len])) { + ++len; + } + return { identifier.data(), len }; + } + + template + constexpr decltype(auto) append_sequence(std::integer_sequence, std::integer_sequence) { + return std::integer_sequence {}; + } + + template + struct string_sequence_to_view; + + template + struct string_sequence_to_view> { + static constexpr decltype(auto) get() { + return std::string_view { c_str }; + } + + static constexpr const char c_str[] { chars..., char {} }; + }; + + template + constexpr decltype(auto) integer_to_string_sequence() { + constexpr auto digits = []() { + return "0123456789abcdefghijklmnopqrstuvwxyz"; + }; + + constexpr std::size_t remainder = value % 10; + constexpr std::size_t next_value = value / 10; + + if constexpr (next_value != 0) { + return append_sequence( + integer_to_string_sequence(), std::integer_sequence {} + ); + } else { + return std::integer_sequence {}; + } + } + template + constexpr std::string_view make_string() { + return string_sequence_to_view())> {}.c_str; + } + + template + constexpr auto generate_itosv_array(std::integer_sequence) { + return std::array { make_string()... }; + } + + // Make array of N string views, countings up from 0 to N - 1 + template + constexpr auto make_itosv_array() { + return generate_itosv_array(std::make_integer_sequence()); + } +} diff --git a/src/openvic-simulation/utility/Concepts.hpp b/src/openvic-simulation/core/template/Concepts.hpp similarity index 97% rename from src/openvic-simulation/utility/Concepts.hpp rename to src/openvic-simulation/core/template/Concepts.hpp index bab360795..1a71bc6ea 100644 --- a/src/openvic-simulation/utility/Concepts.hpp +++ b/src/openvic-simulation/core/template/Concepts.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -79,8 +80,8 @@ namespace OpenVic { // This adds to capacity rather than size so that it can be used multiple times in a row. // If it added to size, it would only reserve enough for max(arguments...) template - concept reservable = requires(T& t, size_t size) { - { t.capacity() } -> std::same_as; + concept reservable = requires(T& t, std::size_t size) { + { t.capacity() } -> std::same_as; t.reserve(size); }; @@ -107,7 +108,7 @@ namespace OpenVic { template concept has_get_index = requires(T const& key) { - { key.get_index() } -> std::convertible_to; + { key.get_index() } -> std::convertible_to; }; template diff --git a/src/openvic-simulation/core/template/ContainerHash.hpp b/src/openvic-simulation/core/template/ContainerHash.hpp new file mode 100644 index 000000000..b26432412 --- /dev/null +++ b/src/openvic-simulation/core/template/ContainerHash.hpp @@ -0,0 +1,91 @@ +#pragma once + +#include +#include +#include +#include + +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/string/Utility.hpp" + +namespace OpenVic { + struct ordered_container_string_hash { + using is_transparent = void; + [[nodiscard]] std::size_t operator()(char const* txt) const { + return std::hash {}(txt); + } + [[nodiscard]] std::size_t operator()(std::string_view txt) const { + return std::hash {}(txt); + } + [[nodiscard]] std::size_t operator()(std::string const& txt) const { + return std::hash {}(txt); + } + [[nodiscard]] std::size_t operator()(memory::string const& txt) const { + return std::hash {}(txt); + } + }; + + template + struct container_hash : std::hash {}; + + template<> + struct container_hash : ordered_container_string_hash {}; + template<> + struct container_hash : ordered_container_string_hash {}; + template<> + struct container_hash : ordered_container_string_hash {}; + template<> + struct container_hash : ordered_container_string_hash {}; + template + struct container_hash : std::hash {}; + + + /* Case-Insensitive Containers */ + struct case_insensitive_string_hash { + using is_transparent = void; + + private: + /* Based on the byte array hashing functions in MSVC's . */ + [[nodiscard]] static constexpr size_t _hash_bytes_case_insensitive(char const* first, size_t count) { + constexpr size_t _offset_basis = 14695981039346656037ULL; + constexpr size_t _prime = 1099511628211ULL; + size_t hash = _offset_basis; + for (size_t i = 0; i < count; ++i) { + hash ^= static_cast(std::tolower(static_cast(first[i]))); + hash *= _prime; + } + return hash; + } + + public: + [[nodiscard]] constexpr size_t operator()(char const* txt) const { + return operator()(std::string_view { txt }); + } + [[nodiscard]] constexpr size_t operator()(std::string_view txt) const { + return _hash_bytes_case_insensitive(txt.data(), txt.length()); + } + [[nodiscard]] constexpr size_t operator()(std::string const& txt) const { + return _hash_bytes_case_insensitive(txt.data(), txt.length()); + } + [[nodiscard]] constexpr size_t operator()(memory::string const& txt) const { + return _hash_bytes_case_insensitive(txt.data(), txt.length()); + } + }; + + struct case_insensitive_string_equal { + using is_transparent = void; + + [[nodiscard]] constexpr bool operator()(std::string_view const& lhs, std::string_view const& rhs) const { + return strings_equal_case_insensitive(lhs, rhs); + } + }; + + struct StringMapCaseSensitive { + using hash = container_hash; + using equal = std::equal_to<>; + }; + struct StringMapCaseInsensitive { + using hash = case_insensitive_string_hash; + using equal = case_insensitive_string_equal; + }; +} diff --git a/src/openvic-simulation/types/EnumBitfield.hpp b/src/openvic-simulation/core/template/EnumBitfield.hpp similarity index 100% rename from src/openvic-simulation/types/EnumBitfield.hpp rename to src/openvic-simulation/core/template/EnumBitfield.hpp diff --git a/src/openvic-simulation/types/NullMutex.hpp b/src/openvic-simulation/core/thread/NullMutex.hpp similarity index 91% rename from src/openvic-simulation/types/NullMutex.hpp rename to src/openvic-simulation/core/thread/NullMutex.hpp index 09750b98d..deb3ea849 100644 --- a/src/openvic-simulation/types/NullMutex.hpp +++ b/src/openvic-simulation/core/thread/NullMutex.hpp @@ -1,6 +1,6 @@ #pragma once -#include "openvic-simulation/utility/Typedefs.hpp" +#include "openvic-simulation/core/Typedefs.hpp" namespace OpenVic { struct null_mutex { diff --git a/src/openvic-simulation/types/SpinMutex.hpp b/src/openvic-simulation/core/thread/SpinMutex.hpp similarity index 100% rename from src/openvic-simulation/types/SpinMutex.hpp rename to src/openvic-simulation/core/thread/SpinMutex.hpp diff --git a/src/openvic-simulation/utility/ThreadPool.cpp b/src/openvic-simulation/core/thread/ThreadPool.cpp similarity index 93% rename from src/openvic-simulation/utility/ThreadPool.cpp rename to src/openvic-simulation/core/thread/ThreadPool.cpp index f204d1806..2d9c4c1cb 100644 --- a/src/openvic-simulation/utility/ThreadPool.cpp +++ b/src/openvic-simulation/core/thread/ThreadPool.cpp @@ -1,11 +1,19 @@ #include "ThreadPool.hpp" +#include +#include +#include +#include +#include +#include +#include + +#include "openvic-simulation/core/memory/Vector.hpp" #include "openvic-simulation/country/CountryInstance.hpp" #include "openvic-simulation/economy/GoodDefinition.hpp" // IWYU pragma: keep for constructor requirement #include "openvic-simulation/economy/GoodInstance.hpp" #include "openvic-simulation/economy/trading/GoodMarket.hpp" #include "openvic-simulation/map/ProvinceInstance.hpp" -#include "openvic-simulation/utility/Containers.hpp" using namespace OpenVic; @@ -16,10 +24,10 @@ void ThreadPool::loop_until_cancelled( ModifierEffectCache const& modifier_effect_cache, PopsDefines const& pop_defines, ProductionTypeManager const& production_type_manager, - utility::forwardable_span country_keys, - utility::forwardable_span good_keys, - utility::forwardable_span strata_keys, - utility::forwardable_span work_bundles + forwardable_span country_keys, + forwardable_span good_keys, + forwardable_span strata_keys, + forwardable_span work_bundles ) { IndexedFlatMap reusable_goods_mask { good_keys }; IndexedFlatMap reusable_country_map_0 { country_keys }, @@ -180,11 +188,11 @@ void ThreadPool::initialise_threadpool( ModifierEffectCache const& modifier_effect_cache, PopsDefines const& pop_defines, ProductionTypeManager const& production_type_manager, - utility::forwardable_span good_keys, - utility::forwardable_span strata_keys, - utility::forwardable_span goods, - utility::forwardable_span countries, - utility::forwardable_span provinces + forwardable_span good_keys, + forwardable_span strata_keys, + forwardable_span goods, + forwardable_span countries, + forwardable_span provinces ) { if (threads.size() > 0) { spdlog::error_s("Attempted to initialise ThreadPool again."); @@ -298,4 +306,4 @@ void ThreadPool::process_country_ticks_before_map() { void ThreadPool::process_country_ticks_after_map(){ process_work(work_t::COUNTRY_TICK_AFTER_MAP); -} \ No newline at end of file +} diff --git a/src/openvic-simulation/utility/ThreadPool.hpp b/src/openvic-simulation/core/thread/ThreadPool.hpp similarity index 68% rename from src/openvic-simulation/utility/ThreadPool.hpp rename to src/openvic-simulation/core/thread/ThreadPool.hpp index da6e79bc6..b72d0f37d 100644 --- a/src/openvic-simulation/utility/ThreadPool.hpp +++ b/src/openvic-simulation/core/thread/ThreadPool.hpp @@ -1,15 +1,16 @@ #pragma once +#include #include +#include #include #include -#include +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/portable/ForwardableSpan.hpp" +#include "openvic-simulation/core/random/RandomGenerator.hpp" #include "openvic-simulation/pop/PopValuesFromProvince.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/ForwardableSpan.hpp" -#include "openvic-simulation/utility/RandomGenerator.hpp" namespace OpenVic { struct GameRulesManager; @@ -26,17 +27,17 @@ namespace OpenVic { struct WorkBundle { public: RandomU32 random_number_generator; - utility::forwardable_span countries_chunk; - utility::forwardable_span goods_chunk; - utility::forwardable_span provinces_chunk; + forwardable_span countries_chunk; + forwardable_span goods_chunk; + forwardable_span provinces_chunk; constexpr WorkBundle() {} WorkBundle( RandomU32::state_type rng_state, - utility::forwardable_span new_countries_chunk, - utility::forwardable_span new_goods_chunk, - utility::forwardable_span new_provinces_chunk + forwardable_span new_countries_chunk, + forwardable_span new_goods_chunk, + forwardable_span new_provinces_chunk ) : random_number_generator { rng_state }, countries_chunk { new_countries_chunk }, goods_chunk { new_goods_chunk }, @@ -74,10 +75,10 @@ namespace OpenVic { ModifierEffectCache const& modifier_effect_cache, PopsDefines const& pop_defines, ProductionTypeManager const& production_type_manager, - utility::forwardable_span country_keys, - utility::forwardable_span good_keys, - utility::forwardable_span strata_keys, - utility::forwardable_span work_bundles + forwardable_span country_keys, + forwardable_span good_keys, + forwardable_span strata_keys, + forwardable_span work_bundles ); void await_completion(); void process_work(const work_t work_type); @@ -92,11 +93,11 @@ namespace OpenVic { ModifierEffectCache const& modifier_effect_cache, PopsDefines const& pop_defines, ProductionTypeManager const& production_type_manager, - utility::forwardable_span good_keys, - utility::forwardable_span strata_keys, - utility::forwardable_span goods, - utility::forwardable_span countries, - utility::forwardable_span provinces + forwardable_span good_keys, + forwardable_span strata_keys, + forwardable_span goods, + forwardable_span countries, + forwardable_span provinces ); void process_good_execute_orders(); diff --git a/src/openvic-simulation/types/TextFormat.hpp b/src/openvic-simulation/core/ui/TextFormat.hpp similarity index 100% rename from src/openvic-simulation/types/TextFormat.hpp rename to src/openvic-simulation/core/ui/TextFormat.hpp diff --git a/src/openvic-simulation/country/CountryDefinition.cpp b/src/openvic-simulation/country/CountryDefinition.cpp index 4018b6bea..069cc10e4 100644 --- a/src/openvic-simulation/country/CountryDefinition.cpp +++ b/src/openvic-simulation/country/CountryDefinition.cpp @@ -1,19 +1,23 @@ #include "CountryDefinition.hpp" #include +#include #include +#include "openvic-simulation/DefinitionManager.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/string/Utility.hpp" #include "openvic-simulation/dataloader/Dataloader.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/DefinitionManager.hpp" #include "openvic-simulation/politics/Government.hpp" #include "openvic-simulation/politics/Ideology.hpp" #include "openvic-simulation/politics/PartyPolicy.hpp" #include "openvic-simulation/pop/Culture.hpp" -#include "openvic-simulation/types/Colour.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/utility/Logger.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -99,7 +103,7 @@ bool CountryDefinitionManager::load_countries( if (load_country_data_file( definition_manager, key, is_dynamic, Dataloader::parse_defines( - dataloader.lookup_file(StringUtils::append_string_views(common_dir, filepath)) + dataloader.lookup_file(OpenVic::append_string_views(common_dir, filepath)) ).get_file_node() )) { return true; diff --git a/src/openvic-simulation/country/CountryDefinition.hpp b/src/openvic-simulation/country/CountryDefinition.hpp index 213624025..594eee3ee 100644 --- a/src/openvic-simulation/country/CountryDefinition.hpp +++ b/src/openvic-simulation/country/CountryDefinition.hpp @@ -4,11 +4,11 @@ #include +#include "openvic-simulation/core/container/HasIdentifierAndColour.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" #include "openvic-simulation/country/CountryParty.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/HasIndex.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" namespace OpenVic { struct CountryDefinitionManager; @@ -23,8 +23,8 @@ namespace OpenVic { struct CountryDefinition : HasIdentifierAndColour, HasIndex { friend struct CountryDefinitionManager; - using unit_names_map_t = ordered_map; - using government_colour_map_t = ordered_map; + using unit_names_map_t = memory::ordered_map; + using government_colour_map_t = memory::ordered_map; private: GraphicalCultureType const& PROPERTY(graphical_culture); diff --git a/src/openvic-simulation/country/CountryInstance.cpp b/src/openvic-simulation/country/CountryInstance.cpp index fdc94c664..3b2f47e7a 100644 --- a/src/openvic-simulation/country/CountryInstance.cpp +++ b/src/openvic-simulation/country/CountryInstance.cpp @@ -1,39 +1,45 @@ #include "CountryInstance.hpp" #include "CountryInstanceDeps.hpp" +#include +#include #include #include +#include #include -#include "openvic-simulation/country/SharedCountryValues.hpp" -#include "openvic-simulation/country/CountryDefinition.hpp" +#include "openvic-simulation/Alias.hpp" #include "openvic-simulation/DefinitionManager.hpp" +#include "openvic-simulation/InstanceManager.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/container/ClampedValue.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/error/ErrorMacros.hpp" +#include "openvic-simulation/core/memory/Format.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/portable/ForwardableSpan.hpp" +#include "openvic-simulation/country/CountryDefinition.hpp" +#include "openvic-simulation/country/SharedCountryValues.hpp" #include "openvic-simulation/diplomacy/CountryRelation.hpp" #include "openvic-simulation/economy/BuildingType.hpp" #include "openvic-simulation/economy/production/ProductionType.hpp" #include "openvic-simulation/economy/trading/MarketInstance.hpp" #include "openvic-simulation/history/CountryHistory.hpp" -#include "openvic-simulation/InstanceManager.hpp" #include "openvic-simulation/map/Crime.hpp" #include "openvic-simulation/map/MapInstance.hpp" -#include "openvic-simulation/misc/GameRulesManager.hpp" #include "openvic-simulation/military/UnitType.hpp" +#include "openvic-simulation/misc/GameRulesManager.hpp" #include "openvic-simulation/modifier/ModifierEffectCache.hpp" #include "openvic-simulation/modifier/StaticModifierCache.hpp" #include "openvic-simulation/pop/Pop.hpp" #include "openvic-simulation/pop/PopType.hpp" #include "openvic-simulation/research/Invention.hpp" #include "openvic-simulation/research/Technology.hpp" -#include "openvic-simulation/types/ClampedValue.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/IndexedFlatMap.hpp" -#include "openvic-simulation/types/PopSize.hpp" #include "openvic-simulation/types/UnitBranchType.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/ErrorMacros.hpp" -#include -#include "openvic-simulation/utility/Typedefs.hpp" using namespace OpenVic; @@ -1159,7 +1165,7 @@ void CountryInstance::start_research(Technology const& technology, const Date to } void CountryInstance::apply_foreign_investments( - fixed_point_map_t const& investments, CountryInstanceManager const& country_instance_manager + memory::fixed_point_map_t const& investments, CountryInstanceManager const& country_instance_manager ) { for (auto const& [country, money_invested] : investments) { foreign_investments[&country_instance_manager.get_country_instance_by_definition(*country)] = money_invested; @@ -1209,7 +1215,7 @@ bool CountryInstance::apply_history_to_country(CountryHistoryEntry const& entry, } set_optional_state(tech_school, entry.get_tech_school()); constexpr auto set_bool_map_to_indexed_map = - [](IndexedFlatMap& target, ordered_map source) { + [](IndexedFlatMap& target, memory::ordered_map source) { for (auto const& [key, value] : source) { target[*key] = value; } @@ -1906,7 +1912,7 @@ void CountryInstance::after_sell(void* actor, SellResult const& sell_result, mem void CountryInstance::country_tick_before_map( IndexedFlatMap& reusable_goods_mask, - utility::forwardable_span< + forwardable_span< memory::vector, VECTORS_FOR_COUNTRY_TICK > reusable_vectors, @@ -2006,7 +2012,7 @@ void CountryInstance::country_tick_before_map( void CountryInstance::manage_national_stockpile( IndexedFlatMap& reusable_goods_mask, - utility::forwardable_span< + forwardable_span< memory::vector, VECTORS_FOR_COUNTRY_TICK > reusable_vectors, @@ -2555,7 +2561,7 @@ CountryInstanceManager::CountryInstanceManager( CountryInstanceDeps const& country_instance_deps, GoodInstanceManager const& new_good_instance_manager, PopsDefines const& new_pop_defines, - utility::forwardable_span pop_type_keys, + forwardable_span pop_type_keys, ThreadPool& new_thread_pool ) : thread_pool { new_thread_pool }, diff --git a/src/openvic-simulation/country/CountryInstance.hpp b/src/openvic-simulation/country/CountryInstance.hpp index 108b75aec..ea25a4746 100644 --- a/src/openvic-simulation/country/CountryInstance.hpp +++ b/src/openvic-simulation/country/CountryInstance.hpp @@ -4,28 +4,29 @@ #include +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/container/ClampedValue.hpp" +#include "openvic-simulation/core/container/FlagStrings.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/container/ValueHistory.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/memory/OrderedSet.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/object/FixedPoint/Atomic.hpp" +#include "openvic-simulation/core/portable/ForwardableSpan.hpp" +#include "openvic-simulation/core/reactive/DerivedState.hpp" +#include "openvic-simulation/core/reactive/MutableState.hpp" #include "openvic-simulation/country/SharedCountryValues.hpp" #include "openvic-simulation/diplomacy/CountryRelation.hpp" #include "openvic-simulation/military/UnitBranchedGetterMacro.hpp" #include "openvic-simulation/modifier/ModifierSum.hpp" #include "openvic-simulation/politics/Rule.hpp" #include "openvic-simulation/pop/PopsAggregate.hpp" -#include "openvic-simulation/types/ClampedValue.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/fixed_point/Atomic.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/FlagStrings.hpp" -#include "openvic-simulation/types/HasIndex.hpp" -#include "openvic-simulation/types/IndexedFlatMap.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/types/TechnologyUnlockLevel.hpp" #include "openvic-simulation/types/UnitBranchType.hpp" -#include "openvic-simulation/types/UnitVariant.hpp" -#include "openvic-simulation/types/ValueHistory.hpp" -#include "openvic-simulation/utility/Getters.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/reactive/DerivedState.hpp" -#include "openvic-simulation/utility/reactive/MutableState.hpp" namespace OpenVic { struct BaseIssue; @@ -139,14 +140,14 @@ namespace OpenVic { Date PROPERTY(lose_great_power_date); size_t PROPERTY(total_rank, 0); - ordered_set PROPERTY(owned_provinces); - ordered_set PROPERTY(controlled_provinces); - ordered_set PROPERTY(core_provinces); - ordered_set PROPERTY(states); + memory::ordered_set PROPERTY(owned_provinces); + memory::ordered_set PROPERTY(controlled_provinces); + memory::ordered_set PROPERTY(core_provinces); + memory::ordered_set PROPERTY(states); - ordered_set PROPERTY(neighbouring_countries); + memory::ordered_set PROPERTY(neighbouring_countries); - string_map_t PROPERTY(script_variables); + memory::string_map_t PROPERTY(script_variables); // The total/resultant modifier affecting this country, including owned province contributions. ModifierSum PROPERTY(modifier_sum); @@ -157,7 +158,7 @@ namespace OpenVic { memory::vector> PROPERTY(industrial_power_from_states); memory::vector> PROPERTY(industrial_power_from_investments); size_t PROPERTY(industrial_rank, 0); - fixed_point_map_t PROPERTY(foreign_investments); + memory::fixed_point_map_t PROPERTY(foreign_investments); OV_IFLATMAP_PROPERTY(BuildingType, technology_unlock_level_t, building_type_unlock_levels); // TODO - total amount of each good produced @@ -247,7 +248,7 @@ namespace OpenVic { OV_STATE_PROPERTY(Date, expected_research_completion_date); OV_STATE_PROPERTY(fixed_point_t, research_point_stockpile); OV_STATE_PROPERTY(fixed_point_t, daily_research_points); - fixed_point_map_t PROPERTY(research_points_from_pop_types); + memory::fixed_point_map_t PROPERTY(research_points_from_pop_types); OV_STATE_PROPERTY(TechnologySchool const*, tech_school, nullptr); // TODO - cached possible inventions with %age chance @@ -272,7 +273,7 @@ namespace OpenVic { /* Population */ size_t PROPERTY(national_focus_capacity, 0); Culture const* PROPERTY(primary_culture, nullptr); - ordered_set PROPERTY(accepted_cultures); + memory::ordered_set PROPERTY(accepted_cultures); Religion const* PROPERTY(religion, nullptr); // TODO - population change over last 30 days @@ -321,9 +322,9 @@ namespace OpenVic { fixed_point_t pop_demand; fixed_point_t available_amount; - ordered_map need_consumption_per_pop_type; - ordered_map input_consumption_per_production_type; - ordered_map production_per_production_type; + memory::ordered_map need_consumption_per_pop_type; + memory::ordered_map input_consumption_per_production_type; + memory::ordered_map production_per_production_type; good_data_t(); good_data_t(good_data_t&&) = default; @@ -343,7 +344,7 @@ namespace OpenVic { // The last time this country lost a war, i.e. accepted a peace offer sent from their offer tab or the enemy's demand // tab, even white peace. Used for the "has_recently_lost_war" condition (true if the date is less than 5 years ago). Date PROPERTY(last_war_loss_date); - vector_ordered_set PROPERTY(war_enemies); + memory::vector_ordered_set PROPERTY(war_enemies); OV_STATE_PROPERTY(CountryInstance const*, sphere_owner, nullptr); // TODO - colonial power, current wars @@ -367,7 +368,7 @@ namespace OpenVic { fixed_point_t PROPERTY(max_ship_supply); fixed_point_t PROPERTY_RW(leadership_point_stockpile); fixed_point_t PROPERTY(monthly_leadership_points); - fixed_point_map_t PROPERTY(leadership_points_from_pop_types); + memory::fixed_point_map_t PROPERTY(leadership_points_from_pop_types); int32_t PROPERTY(create_leader_count, 0); // War exhaustion is stored as a raw decimal rather than a proportion, it is usually in the range 0-100. // The current war exhaustion value should only ever be modified via change_war_exhaustion(delta) which also @@ -605,7 +606,7 @@ namespace OpenVic { // Sets the investment of each country in the map (rather than adding to them), leaving the rest unchanged. void apply_foreign_investments( - fixed_point_map_t const& investments, + memory::fixed_point_map_t const& investments, CountryInstanceManager const& country_instance_manager ); @@ -618,7 +619,7 @@ namespace OpenVic { void manage_national_stockpile( IndexedFlatMap& reusable_goods_mask, - utility::forwardable_span< + forwardable_span< memory::vector, VECTORS_FOR_COUNTRY_TICK > reusable_vectors, @@ -656,7 +657,7 @@ namespace OpenVic { void update_gamestate(const Date today, MapInstance& map_instance); void country_tick_before_map( IndexedFlatMap& reusable_goods_mask, - utility::forwardable_span< + forwardable_span< memory::vector, VECTORS_FOR_COUNTRY_TICK > reusable_vectors, @@ -710,15 +711,15 @@ namespace OpenVic { CountryInstanceDeps const& country_instance_deps, GoodInstanceManager const& new_good_instance_manager, PopsDefines const& new_pop_defines, - utility::forwardable_span pop_type_keys, + forwardable_span pop_type_keys, ThreadPool& new_thread_pool ); - constexpr OpenVic::utility::forwardable_span get_country_instances() { + constexpr forwardable_span get_country_instances() { return country_instance_by_definition.get_values(); } - constexpr OpenVic::utility::forwardable_span get_country_instances() const { + constexpr forwardable_span get_country_instances() const { return country_instance_by_definition.get_values(); } CountryInstance* get_country_instance_by_identifier(std::string_view identifier); diff --git a/src/openvic-simulation/country/CountryInstanceDeps.hpp b/src/openvic-simulation/country/CountryInstanceDeps.hpp index 1166873fd..630ef07dc 100644 --- a/src/openvic-simulation/country/CountryInstanceDeps.hpp +++ b/src/openvic-simulation/country/CountryInstanceDeps.hpp @@ -1,8 +1,8 @@ #pragma once -#include "openvic-simulation/types/Date.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/portable/ForwardableSpan.hpp" #include "openvic-simulation/types/UnitBranchType.hpp" -#include "openvic-simulation/utility/ForwardableSpan.hpp" namespace OpenVic { struct BuildingType; @@ -27,28 +27,28 @@ namespace OpenVic { struct UnitTypeManager; struct CountryInstanceDeps { - utility::forwardable_span building_types; + forwardable_span building_types; CountryDefines const& country_defines; CountryRelationManager& country_relations_manager; - utility::forwardable_span crimes; + forwardable_span crimes; Date fallback_date_for_never_completing_research; DiplomacyDefines const& diplomacy_defines; EconomyDefines const& economy_defines; - utility::forwardable_span inventions; - utility::forwardable_span ideologies; + forwardable_span inventions; + forwardable_span ideologies; GameRulesManager const& game_rules_manager; - utility::forwardable_span good_instances; + forwardable_span good_instances; GoodInstanceManager& good_instance_manager; - utility::forwardable_span government_types; + forwardable_span government_types; MarketInstance& market_instance; MilitaryDefines const& military_defines; ModifierEffectCache const& modifier_effect_cache; - utility::forwardable_span pop_types; - utility::forwardable_span reforms; - utility::forwardable_span regiment_types; - utility::forwardable_span ship_types; - utility::forwardable_span stratas; - utility::forwardable_span technologies; + forwardable_span pop_types; + forwardable_span reforms; + forwardable_span regiment_types; + forwardable_span ship_types; + forwardable_span stratas; + forwardable_span technologies; UnitTypeManager const& unit_type_manager; }; } \ No newline at end of file diff --git a/src/openvic-simulation/country/CountryParty.hpp b/src/openvic-simulation/country/CountryParty.hpp index 731451ed3..f782e3b62 100644 --- a/src/openvic-simulation/country/CountryParty.hpp +++ b/src/openvic-simulation/country/CountryParty.hpp @@ -1,9 +1,9 @@ #pragma once -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/IndexedFlatMap.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/container/HasIdentifierAndColour.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/object/Date.hpp" namespace OpenVic { struct Ideology; diff --git a/src/openvic-simulation/country/SharedCountryValues.cpp b/src/openvic-simulation/country/SharedCountryValues.cpp index 13469a5a9..6817f5ddc 100644 --- a/src/openvic-simulation/country/SharedCountryValues.cpp +++ b/src/openvic-simulation/country/SharedCountryValues.cpp @@ -4,7 +4,7 @@ #include "openvic-simulation/economy/GoodInstance.hpp" #include "openvic-simulation/pop/PopNeedsMacro.hpp" #include "openvic-simulation/pop/PopType.hpp" -#include "openvic-simulation/types/IndexedFlatMap.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/country/SharedCountryValues.hpp b/src/openvic-simulation/country/SharedCountryValues.hpp index 1b0af5a4f..afb5145b1 100644 --- a/src/openvic-simulation/country/SharedCountryValues.hpp +++ b/src/openvic-simulation/country/SharedCountryValues.hpp @@ -1,9 +1,9 @@ #pragma once +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/reactive/MutableState.hpp" #include "openvic-simulation/pop/PopNeedsMacro.hpp" -#include "openvic-simulation/types/IndexedFlatMap.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/reactive/MutableState.hpp" namespace OpenVic { struct CountryInstanceManager; diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index 9b5557c7f..9387363e5 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -13,20 +13,20 @@ #include #include "openvic-simulation/DefinitionManager.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/memory/StringSet.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/string/Utility.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" #include "openvic-simulation/interface/UI.hpp" #include "openvic-simulation/misc/GameRulesManager.hpp" #include "openvic-simulation/misc/SoundEffect.hpp" -#include "openvic-simulation/utility/Logger.hpp" -#include "openvic-simulation/utility/StringUtils.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/Concepts.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; using namespace ovdl; -using StringUtils::append_string_views; - #if !defined(_WIN32) #define FILESYSTEM_NEEDS_FORWARD_SLASHES #endif @@ -34,7 +34,7 @@ using StringUtils::append_string_views; static fs::path ensure_forward_slash_path(std::string_view path) { #if defined(FILESYSTEM_NEEDS_FORWARD_SLASHES) /* Back-slashes need to be converted into forward-slashes */ - return StringUtils::make_forward_slash_path(StringUtils::remove_leading_slashes(path)); + return OpenVic::make_forward_slash_path(OpenVic::remove_leading_slashes(path)); #else return path; #endif @@ -77,7 +77,7 @@ bool Dataloader::set_roots(path_span_t new_roots, path_span_t new_replace_paths, fs::path Dataloader::lookup_file(std::string_view path, bool print_error) const { const fs::path filepath { ensure_forward_slash_path(path) }; - const std::string_view filename = StringUtils::get_filename(path); + const std::string_view filename = OpenVic::get_filename(path); for (fs::path const& root : roots) { const fs::path composed = root / filepath; if (fs::is_regular_file(composed)) { @@ -100,7 +100,7 @@ fs::path Dataloader::lookup_file(std::string_view path, bool print_error) const for (fs::directory_entry const& entry : fs::directory_iterator { composed.parent_path(), ec }) { if (entry.is_regular_file()) { const fs::path file = entry; - if (StringUtils::strings_equal_case_insensitive(file.filename().string(), filename)) { + if (OpenVic::strings_equal_case_insensitive(file.filename().string(), filename)) { if (root == roots.back()) { bool ignore = false; for (fs::path const& replace_path : replace_paths) { @@ -127,7 +127,7 @@ fs::path Dataloader::lookup_file(std::string_view path, bool print_error) const } fs::path Dataloader::lookup_image_file(std::string_view path) const { - const std::string_view path_without_extension = StringUtils::remove_extension(path); + const std::string_view path_without_extension = OpenVic::remove_extension(path); if (path.substr(path_without_extension.size()) == ".tga") { const fs::path ret = lookup_file(memory::fmt::format("{}.dds", path_without_extension), false); if (!ret.empty()) { @@ -158,7 +158,7 @@ Dataloader::path_vector_t Dataloader::_lookup_files_in_dir( fs::path file; fs::path const* root = nullptr; }; - string_map_t found_files; + memory::string_map_t found_files; for (fs::path const& root : roots) { const size_t root_len = root.string().size(); std::error_code ec; @@ -172,7 +172,7 @@ Dataloader::path_vector_t Dataloader::_lookup_files_in_dir( const memory::string full_path = file.string(memory::string::allocator_type{}); std::string_view relative_path = full_path; relative_path.remove_prefix(root_len); - relative_path = StringUtils::remove_leading_slashes(relative_path); + relative_path = OpenVic::remove_leading_slashes(relative_path); const std::string_view key = unique_key(relative_path); if (!key.empty()) { const typename decltype(found_files)::const_iterator it = found_files.find(key); @@ -202,7 +202,7 @@ Dataloader::path_vector_t Dataloader::lookup_files_in_dir_recursive(std::string_ } static std::string_view _extract_basic_identifier_prefix_from_path(std::string_view path) { - return extract_basic_identifier_prefix(StringUtils::get_filename(path)); + return extract_basic_identifier_prefix(OpenVic::get_filename(path)); }; Dataloader::path_vector_t Dataloader::lookup_basic_identifier_prefixed_files_in_dir( @@ -228,9 +228,9 @@ bool Dataloader::apply_to_files(path_span_t files, apply_files_callback_t callba return ret; } -string_set_t Dataloader::lookup_dirs_in_dir(std::string_view path) const { +memory::string_set_t Dataloader::lookup_dirs_in_dir(std::string_view path) const { const fs::path dirpath { ensure_forward_slash_path(path) }; - string_set_t ret; + memory::string_set_t ret; for (fs::path const& root : roots) { std::error_code ec; for (fs::directory_entry const& entry : fs::directory_iterator { root / dirpath, ec }) { @@ -654,7 +654,7 @@ bool Dataloader::_load_history(DefinitionManager& definition_manager, bool unuse /* Pop History */ static constexpr std::string_view pop_history_directory = "history/pops/"; - const string_set_t pop_history_dirs = lookup_dirs_in_dir(pop_history_directory); + const memory::string_set_t pop_history_dirs = lookup_dirs_in_dir(pop_history_directory); const Date last_bookmark_date = definition_manager.get_history_manager().get_bookmark_manager().get_last_bookmark_date(); @@ -666,7 +666,7 @@ bool Dataloader::_load_history(DefinitionManager& definition_manager, bool unuse bool non_integer_size = false; ret &= apply_to_files( - lookup_files_in_dir(StringUtils::append_string_views(pop_history_directory, dir), ".txt"), + lookup_files_in_dir(OpenVic::append_string_views(pop_history_directory, dir), ".txt"), [this, &definition_manager, &province_history_manager, date, &non_integer_size]( fs::path const& file ) -> bool { diff --git a/src/openvic-simulation/dataloader/Dataloader.hpp b/src/openvic-simulation/dataloader/Dataloader.hpp index 54662d23d..6d09e7d59 100644 --- a/src/openvic-simulation/dataloader/Dataloader.hpp +++ b/src/openvic-simulation/dataloader/Dataloader.hpp @@ -6,10 +6,10 @@ #include #include -#include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/core/memory/StringSet.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" #include "openvic-simulation/dataloader/ModManager.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/Concepts.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" #include @@ -121,7 +121,7 @@ namespace OpenVic { using apply_files_callback_t = fu2::function_base, false, false, bool(fs::path const&)>; bool apply_to_files(path_span_t files, apply_files_callback_t callback) const; - string_set_t lookup_dirs_in_dir(std::string_view path) const; + memory::string_set_t lookup_dirs_in_dir(std::string_view path) const; /* Load all mod descriptors present in the mod/ directory. Importantly, loads dependencies and replace_paths for us to check. */ bool load_mod_descriptors(ModManager& mod_manager) const; diff --git a/src/openvic-simulation/dataloader/ModManager.cpp b/src/openvic-simulation/dataloader/ModManager.cpp index 33ef4ad49..43308a88e 100644 --- a/src/openvic-simulation/dataloader/ModManager.cpp +++ b/src/openvic-simulation/dataloader/ModManager.cpp @@ -1,13 +1,16 @@ #include "ModManager.hpp" +#include #include +#include "openvic-simulation/core/FormatValidate.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/error/ErrorMacros.hpp" +#include "openvic-simulation/core/memory/OrderedSet.hpp" +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/utility/ErrorMacros.hpp" -#include "openvic-simulation/utility/FormatValidate.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -27,13 +30,13 @@ Mod::Mod( replace_paths { new_replace_paths }, dependencies { new_dependencies } {} -vector_ordered_set Mod::generate_dependency_list(bool* success) const { +memory::vector_ordered_set Mod::generate_dependency_list(bool* success) const { static constexpr size_t MAX_RECURSE = 16; size_t current_recurse = 0; - vector_ordered_set result; + memory::vector_ordered_set result; - auto dep_cycle = [this, ¤t_recurse](auto self, Mod const* mod, vector_ordered_set& dep_list) -> bool { + auto dep_cycle = [this, ¤t_recurse](auto self, Mod const* mod, memory::vector_ordered_set& dep_list) -> bool { bool ret = true; for (std::string_view dep_identifier : mod->get_dependencies()) { if (!mod_manager.has_mod_identifier(dep_identifier)) { diff --git a/src/openvic-simulation/dataloader/ModManager.hpp b/src/openvic-simulation/dataloader/ModManager.hpp index 341fea58b..7b7931b64 100644 --- a/src/openvic-simulation/dataloader/ModManager.hpp +++ b/src/openvic-simulation/dataloader/ModManager.hpp @@ -1,10 +1,13 @@ #pragma once #include +#include #include -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/memory/OrderedSet.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" namespace OpenVic { @@ -29,7 +32,7 @@ namespace OpenVic { ); Mod(Mod&&) = default; - vector_ordered_set generate_dependency_list(bool* success = nullptr) const; + memory::vector_ordered_set generate_dependency_list(bool* success = nullptr) const; }; struct ModManager { diff --git a/src/openvic-simulation/dataloader/NodeTools.cpp b/src/openvic-simulation/dataloader/NodeTools.cpp index d453b6b1e..13673561f 100644 --- a/src/openvic-simulation/dataloader/NodeTools.cpp +++ b/src/openvic-simulation/dataloader/NodeTools.cpp @@ -2,9 +2,10 @@ #include #include -#include +#include #include #include +#include #include #include @@ -15,10 +16,12 @@ #include #include -#include "openvic-simulation/types/Colour.hpp" -#include "openvic-simulation/types/TextFormat.hpp" -#include "openvic-simulation/types/Vector.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/object/Vector.hpp" +#include "openvic-simulation/core/ui/TextFormat.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -36,10 +39,10 @@ static NodeCallback auto _expect_type(Callback auto&& callback) { } spdlog::error_s( "Invalid node type {} when expecting {}", - ast::get_type_name(node->kind()), utility::type_name() + ast::get_type_name(node->kind()), type_name() ); } else { - spdlog::error_s("Null node when expecting {}", utility::type_name()); + spdlog::error_s("Null node when expecting {}", type_name()); } return false; }; @@ -59,12 +62,12 @@ static NodeCallback auto _abstract_statement_node_callback(Callback<_NodeStateme } spdlog::error_s( "Invalid node type {} when expecting {} or {}", - ast::get_type_name(node->kind()), utility::type_name(), utility::type_name() + ast::get_type_name(node->kind()), type_name(), type_name() ); } else { spdlog::error_s( "Null node when expecting {} or {}", - utility::type_name(), utility::type_name() + type_name(), type_name() ); } return false; @@ -122,12 +125,12 @@ node_callback_t NodeTools::expect_identifier_or_string(callback_tkind()), utility::type_name(), utility::type_name() + ast::get_type_name(node->kind()), type_name(), type_name() ); } else { spdlog::error_s( "Null node when expecting {} or {}", - utility::type_name(), utility::type_name() + type_name(), type_name() ); } return false; @@ -135,7 +138,7 @@ node_callback_t NodeTools::expect_identifier_or_string(callback_t callback) { - static const case_insensitive_string_map_t bool_map { { "yes", true }, { "no", false } }; + static const memory::case_insensitive_string_map_t bool_map { { "yes", true }, { "no", false } }; return expect_identifier(expect_mapped_string(bool_map, callback)); } @@ -151,7 +154,7 @@ node_callback_t NodeTools::expect_int_bool(callback_t callback) { node_callback_t NodeTools::expect_int64(callback_t callback, int base) { return expect_identifier([callback, base](std::string_view identifier) mutable -> bool { int64_t val; - std::from_chars_result result = StringUtils::string_to_int64(identifier, val, base); + std::from_chars_result result = OpenVic::string_to_int64(identifier, val, base); if (result.ec == std::errc{}) { return callback(val); } @@ -163,7 +166,7 @@ node_callback_t NodeTools::expect_int64(callback_t callback, int base) node_callback_t NodeTools::expect_uint64(callback_t callback, int base) { return expect_identifier([callback, base](std::string_view identifier) mutable -> bool { uint64_t val; - std::from_chars_result result = StringUtils::string_to_uint64(identifier, val, base); + std::from_chars_result result = OpenVic::string_to_uint64(identifier, val, base); if (result.ec == std::errc{}) { return callback(val); } @@ -284,7 +287,7 @@ NodeCallback auto _expect_vec2(Callback> auto&& callback) { node_callback_t NodeTools::expect_text_format(callback_t callback) { using enum text_format_t; - static const string_map_t format_map = { + static const memory::string_map_t format_map = { { "left", left }, { "right", right }, { "centre", centre }, diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp index 0eb6a4865..0f67f200d 100644 --- a/src/openvic-simulation/dataloader/NodeTools.hpp +++ b/src/openvic-simulation/dataloader/NodeTools.hpp @@ -13,17 +13,20 @@ #include -#include "openvic-simulation/types/Colour.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/IndexedFlatMap.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/types/TextFormat.hpp" -#include "openvic-simulation/types/Vector.hpp" -#include "openvic-simulation/utility/FormatValidate.hpp" -#include "openvic-simulation/utility/Getters.hpp" -#include "openvic-simulation/utility/TslHelper.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/Concepts.hpp" +#include "openvic-simulation/core/FormatValidate.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/container/MutableIterator.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/object/Vector.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" +#include "openvic-simulation/core/ui/TextFormat.hpp" #include @@ -39,10 +42,10 @@ namespace OpenVic { #ifdef _MSC_VER // type_name starts with "struct " using namespace std::string_view_literals; #define NODE_CASE(Node) \ - case Node: return OpenVic::utility::type_name().substr("struct "sv.size()); + case Node: return OpenVic::type_name().substr("struct "sv.size()); #else #define NODE_CASE(Node) \ - case Node: return OpenVic::utility::type_name(); + case Node: return OpenVic::type_name(); #endif switch (kind) { using enum NodeKind; @@ -132,7 +135,7 @@ using namespace std::string_view_literals; inline bool map_key_value_invalid_callback(Map const& key_map, std::string_view key, ast::NodeCPtr) { spdlog::error_s( "Invalid dictionary key \"{}\". Valid values are [{}]", - key, StringUtils::string_join(key_map) + key, string_join(key_map) ); return false; } @@ -141,7 +144,7 @@ using namespace std::string_view_literals; inline bool map_key_value_ignore_invalid_callback(Map const& key_map, std::string_view key, ast::NodeCPtr) { spdlog::warn_s( "Invalid dictionary key \"{}\" is ignored. Valid values are [{}]", - key, StringUtils::string_join(key_map) + key, OpenVic::string_join(key_map) ); return true; } @@ -265,7 +268,7 @@ using namespace std::string_view_literals; using enum dictionary_entry_t::expected_count_t; template - using template_key_map_t = template_string_map_t; + using template_key_map_t = memory::template_string_map_t; using key_map_t = template_key_map_t; using case_insensitive_key_map_t = template_key_map_t; @@ -519,17 +522,17 @@ using namespace std::string_view_literals; template Callback auto expect_mapped_string( - template_string_map_t const& map, Callback auto&& callback, bool warn = false + memory::template_string_map_t const& map, Callback auto&& callback, bool warn = false ) { return [&map, callback = FWD(callback), warn](std::string_view string) mutable -> bool { - const typename template_string_map_t::const_iterator it = map.find(string); + const typename memory::template_string_map_t::const_iterator it = map.find(string); if (it != map.end()) { return callback(it->second); } spdlog::log_s( warn ? spdlog::level::warn : spdlog::level::err, "\"{}\" is not a valid key. Valid keys: [{}]", - string, StringUtils::string_join(map) + string, OpenVic::string_join(map) ); return warn; }; diff --git a/src/openvic-simulation/dataloader/Vic2PathSearch.cpp b/src/openvic-simulation/dataloader/Vic2PathSearch.cpp index ba1df150d..2e3ee4119 100644 --- a/src/openvic-simulation/dataloader/Vic2PathSearch.cpp +++ b/src/openvic-simulation/dataloader/Vic2PathSearch.cpp @@ -1,18 +1,27 @@ +#include +#include +#include #include +#include +#include +#include +#include +#include +#include #include #include #include +#include #include #include -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/utility/ConstexprIntToStr.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/Logger.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/string/Utility.hpp" #include "Dataloader.hpp" @@ -40,7 +49,7 @@ using namespace ovdl; // Windows and Mac by default act like case insensitive filesystems static constexpr bool path_equals(std::string_view lhs, std::string_view rhs) { #if defined(FILESYSTEM_CASE_INSENSITIVE) - return StringUtils::strings_equal_case_insensitive(lhs, rhs); + return OpenVic::strings_equal_case_insensitive(lhs, rhs); #else return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); #endif @@ -266,7 +275,7 @@ static fs::path _search_for_game_path(fs::path hint_path) { } // Array of strings contain "0" to std::to_string(max_amount_of_steam_libraries - 1) - static constexpr auto library_indexes = OpenVic::ConstexprIntToStr::make_itosv_array(); + static constexpr auto library_indexes = OpenVic::make_itosv_array(); for (auto const& index : library_indexes) { decltype(current_node) node = std::nullopt; @@ -377,7 +386,7 @@ fs::path Dataloader::search_for_game_path(fs::path hint_path) { }; using hint_path_t = fs::path; using game_path_t = fs::path; - static ordered_map _cached_paths; + static memory::ordered_map _cached_paths; auto it = _cached_paths.find(hint_path); if (it != _cached_paths.end()) { diff --git a/src/openvic-simulation/dataloader/Vic2PathSearch_Windows.hpp b/src/openvic-simulation/dataloader/Vic2PathSearch_Windows.hpp index 59516fc06..c5f72948d 100644 --- a/src/openvic-simulation/dataloader/Vic2PathSearch_Windows.hpp +++ b/src/openvic-simulation/dataloader/Vic2PathSearch_Windows.hpp @@ -1,6 +1,5 @@ #pragma once -#include #pragma comment(lib, "advapi32.lib") #include @@ -8,8 +7,8 @@ #include -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/Concepts.hpp" +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" namespace OpenVic::Windows { inline memory::wstring convert(std::string_view as) { diff --git a/src/openvic-simulation/defines/AIDefines.hpp b/src/openvic-simulation/defines/AIDefines.hpp index 5cbc41f53..8b4dfb5b9 100644 --- a/src/openvic-simulation/defines/AIDefines.hpp +++ b/src/openvic-simulation/defines/AIDefines.hpp @@ -1,9 +1,9 @@ #pragma once #include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/Property.hpp" namespace OpenVic { struct DefineManager; diff --git a/src/openvic-simulation/defines/CountryDefines.hpp b/src/openvic-simulation/defines/CountryDefines.hpp index 5bbedd408..682c1a215 100644 --- a/src/openvic-simulation/defines/CountryDefines.hpp +++ b/src/openvic-simulation/defines/CountryDefines.hpp @@ -1,10 +1,10 @@ #pragma once #include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/ProvinceLifeRating.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/Property.hpp" namespace OpenVic { struct DefineManager; diff --git a/src/openvic-simulation/defines/Define.hpp b/src/openvic-simulation/defines/Define.hpp index e1b2bc2f2..bf8940148 100644 --- a/src/openvic-simulation/defines/Define.hpp +++ b/src/openvic-simulation/defines/Define.hpp @@ -7,8 +7,8 @@ #include "openvic-simulation/defines/GraphicsDefines.hpp" #include "openvic-simulation/defines/MilitaryDefines.hpp" #include "openvic-simulation/defines/PopsDefines.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/Property.hpp" namespace OpenVic { struct DefineManager { diff --git a/src/openvic-simulation/defines/DiplomacyDefines.hpp b/src/openvic-simulation/defines/DiplomacyDefines.hpp index 07587a29b..2a8c62c9b 100644 --- a/src/openvic-simulation/defines/DiplomacyDefines.hpp +++ b/src/openvic-simulation/defines/DiplomacyDefines.hpp @@ -1,9 +1,9 @@ #pragma once #include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/Property.hpp" namespace OpenVic { struct DefineManager; diff --git a/src/openvic-simulation/defines/EconomyDefines.hpp b/src/openvic-simulation/defines/EconomyDefines.hpp index 1da082fed..2e767b43e 100644 --- a/src/openvic-simulation/defines/EconomyDefines.hpp +++ b/src/openvic-simulation/defines/EconomyDefines.hpp @@ -1,9 +1,9 @@ #pragma once #include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/Property.hpp" namespace OpenVic { struct DefineManager; diff --git a/src/openvic-simulation/defines/GraphicsDefines.hpp b/src/openvic-simulation/defines/GraphicsDefines.hpp index 6f8218771..0fd563aaf 100644 --- a/src/openvic-simulation/defines/GraphicsDefines.hpp +++ b/src/openvic-simulation/defines/GraphicsDefines.hpp @@ -1,7 +1,7 @@ #pragma once #include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/Property.hpp" namespace OpenVic { struct DefineManager; diff --git a/src/openvic-simulation/defines/MilitaryDefines.hpp b/src/openvic-simulation/defines/MilitaryDefines.hpp index a44ae68c4..382682b2f 100644 --- a/src/openvic-simulation/defines/MilitaryDefines.hpp +++ b/src/openvic-simulation/defines/MilitaryDefines.hpp @@ -1,10 +1,10 @@ #pragma once #include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/PopSize.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/Property.hpp" namespace OpenVic { struct DefineManager; diff --git a/src/openvic-simulation/defines/PopsDefines.hpp b/src/openvic-simulation/defines/PopsDefines.hpp index 0ed9cfbac..44eb6f2b0 100644 --- a/src/openvic-simulation/defines/PopsDefines.hpp +++ b/src/openvic-simulation/defines/PopsDefines.hpp @@ -1,10 +1,10 @@ #pragma once #include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/PopSize.hpp" -#include "openvic-simulation/types/ProvinceLifeRating.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/Property.hpp" namespace OpenVic { struct DefineManager; diff --git a/src/openvic-simulation/diplomacy/CountryRelation.hpp b/src/openvic-simulation/diplomacy/CountryRelation.hpp index 75ee2fffd..877579a4f 100644 --- a/src/openvic-simulation/diplomacy/CountryRelation.hpp +++ b/src/openvic-simulation/diplomacy/CountryRelation.hpp @@ -9,9 +9,10 @@ #include #include -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/utility/Hash.hpp" +#include "openvic-simulation/core/Compare.hpp" +#include "openvic-simulation/core/Hash.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/object/Date.hpp" namespace OpenVic { struct CountryInstance; @@ -244,7 +245,7 @@ namespace OpenVic { private: #define RELATION_PAIR_MAP(PAIR_TYPE, VALUE_TYPE, NAME, FUNC_NAME, RECIPIENT_CONST) \ - vector_ordered_map NAME; \ + memory::vector_ordered_map NAME; \ \ public: \ VALUE_TYPE get_##FUNC_NAME(CountryInstance const* country, CountryInstance const* recipient) const; \ @@ -268,8 +269,8 @@ public: \ RELATION_PAIR_MAP(CountryInstancePair, influence_value_type, influence, influence_with, const); RELATION_PAIR_MAP(CountryInstancePair, influence_priority_value_type, influence_priority, influence_priority_with, const); - vector_ordered_map discredits; - vector_ordered_map embassy_bans; + memory::vector_ordered_map discredits; + memory::vector_ordered_map embassy_bans; public: std::optional get_discredited_date(CountryInstance const* country, CountryInstance const* recipient) const; diff --git a/src/openvic-simulation/diplomacy/DiplomaticAction.cpp b/src/openvic-simulation/diplomacy/DiplomaticAction.cpp index 92f7ea98c..c79b58e49 100644 --- a/src/openvic-simulation/diplomacy/DiplomaticAction.cpp +++ b/src/openvic-simulation/diplomacy/DiplomaticAction.cpp @@ -4,11 +4,11 @@ #include #include "openvic-simulation/InstanceManager.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/object/Date.hpp" #include "openvic-simulation/country/CountryInstance.hpp" #include "openvic-simulation/diplomacy/CountryRelation.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/utility/Logger.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/diplomacy/DiplomaticAction.hpp b/src/openvic-simulation/diplomacy/DiplomaticAction.hpp index 3e499de2e..9adbf06ea 100644 --- a/src/openvic-simulation/diplomacy/DiplomaticAction.hpp +++ b/src/openvic-simulation/diplomacy/DiplomaticAction.hpp @@ -7,10 +7,10 @@ #include #include +#include "openvic-simulation/core/container/FunctionRef.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" #include "openvic-simulation/country/CountryInstance.hpp" #include "openvic-simulation/diplomacy/CountryRelation.hpp" -#include "openvic-simulation/types/FunctionRef.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" namespace OpenVic { struct InstanceManager; diff --git a/src/openvic-simulation/economy/BuildingInstance.cpp b/src/openvic-simulation/economy/BuildingInstance.cpp index 88df57794..6b042f9a8 100644 --- a/src/openvic-simulation/economy/BuildingInstance.cpp +++ b/src/openvic-simulation/economy/BuildingInstance.cpp @@ -1,5 +1,7 @@ #include "BuildingInstance.hpp" +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/economy/BuildingType.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/economy/BuildingInstance.hpp b/src/openvic-simulation/economy/BuildingInstance.hpp index e2590c536..ee7ee9049 100644 --- a/src/openvic-simulation/economy/BuildingInstance.hpp +++ b/src/openvic-simulation/economy/BuildingInstance.hpp @@ -1,9 +1,10 @@ #pragma once -#include "openvic-simulation/types/BuildingLevel.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/economy/BuildingType.hpp" namespace OpenVic { struct BuildingType; diff --git a/src/openvic-simulation/economy/BuildingType.cpp b/src/openvic-simulation/economy/BuildingType.cpp index 08caa684a..a0d25ff09 100644 --- a/src/openvic-simulation/economy/BuildingType.cpp +++ b/src/openvic-simulation/economy/BuildingType.cpp @@ -1,5 +1,13 @@ #include "BuildingType.hpp" +#include +#include + +#include + +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/memory/Format.hpp" +#include "openvic-simulation/core/string/Utility.hpp" #include "openvic-simulation/economy/GoodDefinition.hpp" #include "openvic-simulation/economy/production/ProductionType.hpp" #include "openvic-simulation/modifier/ModifierEffectCache.hpp" @@ -138,12 +146,12 @@ bool BuildingTypeManager::load_buildings_file( static constexpr std::string_view max_prefix = "max_"; static constexpr std::string_view min_prefix = "min_build_"; ret &= modifier_manager.register_technology_modifier_effect( - this_building_type_effects.max_level, StringUtils::append_string_views(max_prefix, building_type.get_identifier()), + this_building_type_effects.max_level, append_string_views(max_prefix, building_type.get_identifier()), FORMAT_x1_0DP_POS, memory::fmt::format("${}$ $TECH_MAX_LEVEL$", building_type) ); // TODO - add custom localisation for "min_build_$building_type$" modifiers ret &= modifier_manager.register_terrain_modifier_effect( - this_building_type_effects.min_level, StringUtils::append_string_views(min_prefix, building_type.get_identifier()), + this_building_type_effects.min_level, append_string_views(min_prefix, building_type.get_identifier()), FORMAT_x1_0DP_NEG ); diff --git a/src/openvic-simulation/economy/BuildingType.hpp b/src/openvic-simulation/economy/BuildingType.hpp index f8b0d205f..d69513fe7 100644 --- a/src/openvic-simulation/economy/BuildingType.hpp +++ b/src/openvic-simulation/economy/BuildingType.hpp @@ -1,12 +1,13 @@ #pragma once +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/memory/StringSet.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/modifier/Modifier.hpp" -#include "openvic-simulation/types/BuildingLevel.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/HasIndex.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { struct GoodDefinition; @@ -27,7 +28,7 @@ namespace OpenVic { ModifierValue modifier; fixed_point_t completion_size = 0, cost = 0, colonial_range = 0, infrastructure = 0; building_level_t max_level = 0, fort_level = 0; - fixed_point_map_t goods_cost; + memory::fixed_point_map_t goods_cost; Timespan build_time; bool on_map = false, default_enabled = false, pop_build_factory = false, strategic_factory = false, advanced_factory = false, in_province = false, one_per_state = false, spawn_railway_track = false, @@ -45,7 +46,7 @@ namespace OpenVic { memory::string PROPERTY(on_completion); // probably sound played on completion fixed_point_t PROPERTY(completion_size); building_level_t PROPERTY(max_level); - fixed_point_map_t PROPERTY(goods_cost); + memory::fixed_point_map_t PROPERTY(goods_cost); fixed_point_t PROPERTY(cost); Timespan PROPERTY(build_time); // time bool PROPERTY(on_map); // onmap @@ -80,7 +81,7 @@ namespace OpenVic { struct BuildingTypeManager { private: IdentifierRegistry IDENTIFIER_REGISTRY(building_type); - string_set_t PROPERTY(building_type_types); + memory::string_set_t PROPERTY(building_type_types); memory::vector PROPERTY(province_building_types); BuildingType const* PROPERTY(port_building_type); diff --git a/src/openvic-simulation/economy/GoodDefinition.cpp b/src/openvic-simulation/economy/GoodDefinition.cpp index 2c84d529e..3dc15d3a4 100644 --- a/src/openvic-simulation/economy/GoodDefinition.cpp +++ b/src/openvic-simulation/economy/GoodDefinition.cpp @@ -1,7 +1,7 @@ #include "GoodDefinition.hpp" +#include "openvic-simulation/core/string/Utility.hpp" #include "openvic-simulation/modifier/ModifierManager.hpp" -#include "openvic-simulation/utility/StringUtils.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -57,7 +57,7 @@ bool GoodDefinitionManager::add_good_definition( if (is_tradeable == is_money) { spdlog::warn_s( "Good {} has tradeable: {} and money: {}. Money goods are never tradeable. All other goods are tradeable. Setting tradeable has no effect.", - identifier, StringUtils::bool_to_yes_no(is_tradeable), StringUtils::bool_to_yes_no(is_money) + identifier, bool_to_yes_no(is_tradeable), bool_to_yes_no(is_money) ); } diff --git a/src/openvic-simulation/economy/GoodDefinition.hpp b/src/openvic-simulation/economy/GoodDefinition.hpp index 67b186350..84d76a6e0 100644 --- a/src/openvic-simulation/economy/GoodDefinition.hpp +++ b/src/openvic-simulation/economy/GoodDefinition.hpp @@ -1,9 +1,10 @@ #pragma once -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/HasIndex.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/utility/Containers.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/HasIdentifierAndColour.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" namespace OpenVic { struct GoodDefinitionManager; diff --git a/src/openvic-simulation/economy/GoodInstance.hpp b/src/openvic-simulation/economy/GoodInstance.hpp index e071b5db8..ef50fb6e8 100644 --- a/src/openvic-simulation/economy/GoodInstance.hpp +++ b/src/openvic-simulation/economy/GoodInstance.hpp @@ -1,8 +1,9 @@ #pragma once +#include "openvic-simulation/core/container/HasIdentifierAndColour.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" +#include "openvic-simulation/core/portable/ForwardableSpan.hpp" #include "openvic-simulation/economy/trading/GoodMarket.hpp" -#include "openvic-simulation/types/HasIndex.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" namespace OpenVic { struct GoodDefinition; @@ -39,11 +40,11 @@ namespace OpenVic { GameRulesManager const& game_rules_manager ); - constexpr OpenVic::utility::forwardable_span get_good_instances() { + constexpr forwardable_span get_good_instances() { return good_instance_by_definition.get_values(); } - constexpr OpenVic::utility::forwardable_span get_good_instances() const { + constexpr forwardable_span get_good_instances() const { return good_instance_by_definition.get_values(); } GoodInstance* get_good_instance_by_identifier(std::string_view identifier); diff --git a/src/openvic-simulation/economy/production/ArtisanalProducer.cpp b/src/openvic-simulation/economy/production/ArtisanalProducer.cpp index 534767dd5..0b292b050 100644 --- a/src/openvic-simulation/economy/production/ArtisanalProducer.cpp +++ b/src/openvic-simulation/economy/production/ArtisanalProducer.cpp @@ -2,7 +2,17 @@ #include "ArtisanalProducerDeps.hpp" #include - +#include +#include + +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/random/RandomGenerator.hpp" +#include "openvic-simulation/core/random/WeightedSampling.hpp" #include "openvic-simulation/country/CountryInstance.hpp" #include "openvic-simulation/economy/GoodDefinition.hpp" #include "openvic-simulation/economy/GoodInstance.hpp" @@ -12,16 +22,12 @@ #include "openvic-simulation/modifier/ModifierEffectCache.hpp" #include "openvic-simulation/pop/Pop.hpp" #include "openvic-simulation/pop/PopValuesFromProvince.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/RandomGenerator.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" -#include "openvic-simulation/utility/WeightedSampling.hpp" using namespace OpenVic; ArtisanalProducer::ArtisanalProducer( ArtisanalProducerDeps const& artisanal_producer_deps, - fixed_point_map_t&& new_stockpile, + memory::fixed_point_map_t&& new_stockpile, ProductionType const* const new_production_type, fixed_point_t new_current_production ) : modifier_effect_cache { artisanal_producer_deps.modifier_effect_cache }, @@ -81,7 +87,7 @@ void ArtisanalProducer::artisan_tick( CountryInstance* country_to_report_economy_nullable = pop.get_location()->get_country_to_report_economy(); max_quantity_to_buy_per_good.clear(); IndexedFlatMap& wants_more_mask = reusable_goods_mask; - fixed_point_map_t const& input_goods = production_type.get_input_goods(); + memory::fixed_point_map_t const& input_goods = production_type.get_input_goods(); memory::vector& max_price_per_input = reusable_map_0; max_price_per_input.resize(input_goods.size(), 0); memory::vector& demand_per_input = reusable_map_1; diff --git a/src/openvic-simulation/economy/production/ArtisanalProducer.hpp b/src/openvic-simulation/economy/production/ArtisanalProducer.hpp index e0941111a..65c645b55 100644 --- a/src/openvic-simulation/economy/production/ArtisanalProducer.hpp +++ b/src/openvic-simulation/economy/production/ArtisanalProducer.hpp @@ -2,11 +2,11 @@ #include -#include "openvic-simulation/types/IndexedFlatMap.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" -#include "openvic-simulation/types/PopSize.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" namespace OpenVic { struct ArtisanalProducerDeps; @@ -22,10 +22,10 @@ namespace OpenVic { struct ArtisanalProducer { private: ModifierEffectCache const& modifier_effect_cache; - fixed_point_map_t stockpile; + memory::fixed_point_map_t stockpile; //only used during day tick (from artisan_tick() until MarketInstance.execute_orders()) - fixed_point_map_t max_quantity_to_buy_per_good; + memory::fixed_point_map_t max_quantity_to_buy_per_good; ProductionType const* PROPERTY(production_type_nullable); fixed_point_t PROPERTY(current_production); @@ -47,13 +47,13 @@ namespace OpenVic { public: ArtisanalProducer( ArtisanalProducerDeps const& artisanal_producer_deps, - fixed_point_map_t&& new_stockpile, + memory::fixed_point_map_t&& new_stockpile, ProductionType const* const new_production_type, const fixed_point_t new_current_production ); ArtisanalProducer(ArtisanalProducerDeps const& artisanal_producer_deps) : ArtisanalProducer( artisanal_producer_deps, - fixed_point_map_t {}, + memory::fixed_point_map_t {}, nullptr, fixed_point_t::_0 ) { }; diff --git a/src/openvic-simulation/economy/production/Employee.cpp b/src/openvic-simulation/economy/production/Employee.cpp index 6f4c17bd9..253f575f9 100644 --- a/src/openvic-simulation/economy/production/Employee.cpp +++ b/src/openvic-simulation/economy/production/Employee.cpp @@ -1,5 +1,6 @@ #include "Employee.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/country/CountryInstance.hpp" #include "openvic-simulation/pop/Pop.hpp" diff --git a/src/openvic-simulation/economy/production/Employee.hpp b/src/openvic-simulation/economy/production/Employee.hpp index a8d5b73c5..81b01a53e 100644 --- a/src/openvic-simulation/economy/production/Employee.hpp +++ b/src/openvic-simulation/economy/production/Employee.hpp @@ -1,7 +1,7 @@ #pragma once -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/PopSize.hpp" +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" namespace OpenVic { struct CountryInstance; diff --git a/src/openvic-simulation/economy/production/FactoryProducer.cpp b/src/openvic-simulation/economy/production/FactoryProducer.cpp index 333f67a9e..2f2cf3fee 100644 --- a/src/openvic-simulation/economy/production/FactoryProducer.cpp +++ b/src/openvic-simulation/economy/production/FactoryProducer.cpp @@ -1,5 +1,12 @@ #include "FactoryProducer.hpp" +#include +#include + +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" + using namespace OpenVic; FactoryProducer::FactoryProducer( @@ -8,8 +15,8 @@ FactoryProducer::FactoryProducer( fixed_point_t new_revenue_yesterday, fixed_point_t new_output_quantity_yesterday, fixed_point_t new_unsold_quantity_yesterday, - ordered_map&& new_employees, - fixed_point_map_t&& new_stockpile, + memory::ordered_map&& new_employees, + memory::fixed_point_map_t&& new_stockpile, fixed_point_t new_budget, fixed_point_t new_balance_yesterday, fixed_point_t new_received_investments_yesterday, diff --git a/src/openvic-simulation/economy/production/FactoryProducer.hpp b/src/openvic-simulation/economy/production/FactoryProducer.hpp index 208f6d3dc..5178425f4 100644 --- a/src/openvic-simulation/economy/production/FactoryProducer.hpp +++ b/src/openvic-simulation/economy/production/FactoryProducer.hpp @@ -1,12 +1,12 @@ #pragma once +#include #include -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/types/PopSize.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" namespace OpenVic { struct GoodDefinition; @@ -25,8 +25,8 @@ namespace OpenVic { fixed_point_t PROPERTY(output_quantity_yesterday); fixed_point_t PROPERTY(unsold_quantity_yesterday); fixed_point_t PROPERTY(size_multiplier); - ordered_map PROPERTY(employees); - fixed_point_map_t PROPERTY(stockpile); + memory::ordered_map PROPERTY(employees); + memory::fixed_point_map_t PROPERTY(stockpile); fixed_point_t PROPERTY(budget); fixed_point_t PROPERTY(balance_yesterday); fixed_point_t PROPERTY(received_investments_yesterday); @@ -41,7 +41,7 @@ namespace OpenVic { FactoryProducer( ProductionType const& new_production_type, fixed_point_t new_size_multiplier, fixed_point_t new_revenue_yesterday, fixed_point_t new_output_quantity_yesterday, fixed_point_t new_unsold_quantity_yesterday, - ordered_map&& new_employees, fixed_point_map_t&& new_stockpile, + memory::ordered_map&& new_employees, memory::fixed_point_map_t&& new_stockpile, fixed_point_t new_budget, fixed_point_t new_balance_yesterday, fixed_point_t new_received_investments_yesterday, fixed_point_t new_market_spendings_yesterday, fixed_point_t new_paychecks_yesterday, uint32_t new_unprofitable_days, uint32_t new_subsidised_days, uint32_t new_days_without_input, uint8_t new_hiring_priority, diff --git a/src/openvic-simulation/economy/production/ProductionType.cpp b/src/openvic-simulation/economy/production/ProductionType.cpp index fe204c464..cda9730c7 100644 --- a/src/openvic-simulation/economy/production/ProductionType.cpp +++ b/src/openvic-simulation/economy/production/ProductionType.cpp @@ -1,13 +1,26 @@ #include "ProductionType.hpp" +#include +#include +#include + #include +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/error/ErrorMacros.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/memory/Format.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/memory/OrderedSet.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/economy/GoodDefinition.hpp" #include "openvic-simulation/misc/GameRulesManager.hpp" #include "openvic-simulation/pop/PopManager.hpp" #include "openvic-simulation/pop/PopType.hpp" -#include "openvic-simulation/utility/ErrorMacros.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -29,11 +42,11 @@ ProductionType::ProductionType( memory::vector&& new_jobs, const template_type_t new_template_type, const pop_size_t new_base_workforce_size, - fixed_point_map_t&& new_input_goods, + memory::fixed_point_map_t&& new_input_goods, GoodDefinition const& new_output_good, const fixed_point_t new_base_output_quantity, memory::vector&& new_bonuses, - fixed_point_map_t&& new_maintenance_requirements, + memory::fixed_point_map_t&& new_maintenance_requirements, const bool new_is_coastal, const bool new_is_farm, const bool new_is_mine @@ -105,7 +118,7 @@ node_callback_t ProductionTypeManager::_expect_job( Job::effect_t effect_type { THROUGHPUT }; fixed_point_t effect_multiplier = 1, desired_workforce_share = 1; - static const string_map_t effect_map = { + static const memory::string_map_t effect_map = { { "input", INPUT }, { "output", OUTPUT }, { "throughput", THROUGHPUT } }; @@ -140,11 +153,11 @@ bool ProductionTypeManager::add_production_type( memory::vector&& jobs, const ProductionType::template_type_t template_type, const pop_size_t base_workforce_size, - fixed_point_map_t&& input_goods, + memory::fixed_point_map_t&& input_goods, GoodDefinition const* const output_good, const fixed_point_t base_output_quantity, memory::vector&& bonuses, - fixed_point_map_t&& maintenance_requirements, + memory::fixed_point_map_t&& maintenance_requirements, const bool is_coastal, const bool is_farm, const bool is_mine @@ -258,8 +271,8 @@ bool ProductionTypeManager::load_production_types_file( size_t expected_types = 0; /* Pass #1: find and store template identifiers */ - ordered_set templates; - ordered_map template_target_map; + memory::ordered_set templates; + memory::ordered_map template_target_map; bool ret = expect_dictionary( [this, &expected_types, &templates, &template_target_map, &template_symbol, &output_goods_symbol] (std::string_view key, ast::NodeCPtr value) -> bool { @@ -299,7 +312,7 @@ bool ProductionTypeManager::load_production_types_file( )(parser.get_file_node()); /* Pass #2: create and populate the template map */ - ordered_map template_node_map; + memory::ordered_map template_node_map; ret &= expect_dictionary( [this, &expected_types, &templates, &template_node_map](std::string_view key, ast::NodeCPtr value) -> bool { if (templates.contains(key)) { @@ -328,14 +341,14 @@ bool ProductionTypeManager::load_production_types_file( ProductionType::template_type_t template_type { FACTORY }; GoodDefinition const* output_good = nullptr; pop_size_t base_workforce_size = 0; - fixed_point_map_t input_goods, maintenance_requirements; + memory::fixed_point_map_t input_goods, maintenance_requirements; fixed_point_t base_output_quantity = 0; memory::vector bonuses; bool is_coastal = false, is_farm = false, is_mine = false; bool ret = true; - static const string_map_t template_type_map = { + static const memory::string_map_t template_type_map = { { "factory", FACTORY }, { "rgo", RGO }, { "artisan", ARTISAN } }; diff --git a/src/openvic-simulation/economy/production/ProductionType.hpp b/src/openvic-simulation/economy/production/ProductionType.hpp index 52432403c..08a3df8bb 100644 --- a/src/openvic-simulation/economy/production/ProductionType.hpp +++ b/src/openvic-simulation/economy/production/ProductionType.hpp @@ -2,13 +2,12 @@ #include +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/scripts/ConditionScript.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/types/IndexedFlatMap.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/PopSize.hpp" -#include "openvic-simulation/types/PopSprite.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { struct GameRulesManager; @@ -56,12 +55,12 @@ namespace OpenVic { const template_type_t PROPERTY(template_type); const pop_size_t PROPERTY(base_workforce_size); - fixed_point_map_t PROPERTY(input_goods); + memory::fixed_point_map_t PROPERTY(input_goods); GoodDefinition const& PROPERTY(output_good); const fixed_point_t PROPERTY(base_output_quantity); memory::vector PROPERTY(bonuses); - fixed_point_map_t PROPERTY(maintenance_requirements); + memory::fixed_point_map_t PROPERTY(maintenance_requirements); const bool PROPERTY_CUSTOM_PREFIX(coastal, is); const bool _is_farm, _is_mine; bool parse_scripts(DefinitionManager const& definition_manager); @@ -74,11 +73,11 @@ namespace OpenVic { memory::vector&& new_jobs, const template_type_t new_template_type, const pop_size_t new_base_workforce_size, - fixed_point_map_t&& new_input_goods, + memory::fixed_point_map_t&& new_input_goods, GoodDefinition const& new_output_good, const fixed_point_t new_base_output_quantity, memory::vector&& new_bonuses, - fixed_point_map_t&& new_maintenance_requirements, + memory::fixed_point_map_t&& new_maintenance_requirements, const bool new_is_coastal, const bool new_is_farm, const bool new_is_mine @@ -124,11 +123,11 @@ namespace OpenVic { memory::vector&& jobs, const ProductionType::template_type_t template_type, const pop_size_t base_workforce_size, - fixed_point_map_t&& input_goods, + memory::fixed_point_map_t&& input_goods, GoodDefinition const* const output_good, const fixed_point_t base_output_quantity, memory::vector&& bonuses, - fixed_point_map_t&& maintenance_requirements, + memory::fixed_point_map_t&& maintenance_requirements, const bool is_coastal, const bool is_farm, const bool is_mine diff --git a/src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp b/src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp index 92a257f4e..f8fec47de 100644 --- a/src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp +++ b/src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp @@ -1,6 +1,11 @@ #include "ResourceGatheringOperation.hpp" #include "ResourceGatheringOperationDeps.hpp" +#include + +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/country/CountryInstance.hpp" #include "openvic-simulation/economy/production/ProductionType.hpp" #include "openvic-simulation/economy/trading/MarketInstance.hpp" @@ -11,10 +16,6 @@ #include "openvic-simulation/modifier/ModifierEffectCache.hpp" #include "openvic-simulation/pop/Pop.hpp" #include "openvic-simulation/pop/PopType.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/PopSize.hpp" -#include "openvic-simulation/utility/Logger.hpp" -#include "openvic-simulation/utility/Containers.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/economy/production/ResourceGatheringOperation.hpp b/src/openvic-simulation/economy/production/ResourceGatheringOperation.hpp index d8928627c..bac752186 100644 --- a/src/openvic-simulation/economy/production/ResourceGatheringOperation.hpp +++ b/src/openvic-simulation/economy/production/ResourceGatheringOperation.hpp @@ -1,11 +1,13 @@ #pragma once +#include + +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/economy/production/Employee.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/IndexedFlatMap.hpp" -#include "openvic-simulation/types/PopSize.hpp" -#include "openvic-simulation/utility/Getters.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { struct MarketInstance; diff --git a/src/openvic-simulation/economy/production/ResourceGatheringOperationDeps.hpp b/src/openvic-simulation/economy/production/ResourceGatheringOperationDeps.hpp index 9a422e742..eb03bb457 100644 --- a/src/openvic-simulation/economy/production/ResourceGatheringOperationDeps.hpp +++ b/src/openvic-simulation/economy/production/ResourceGatheringOperationDeps.hpp @@ -1,6 +1,6 @@ #pragma once -#include "openvic-simulation/utility/ForwardableSpan.hpp" +#include "openvic-simulation/core/portable/ForwardableSpan.hpp" namespace OpenVic { struct MarketInstance; @@ -10,6 +10,6 @@ namespace OpenVic { struct ResourceGatheringOperationDeps { MarketInstance& market_instance; ModifierEffectCache const& modifier_effect_cache; - utility::forwardable_span pop_type_keys; + forwardable_span pop_type_keys; }; } \ No newline at end of file diff --git a/src/openvic-simulation/economy/trading/BuyResult.hpp b/src/openvic-simulation/economy/trading/BuyResult.hpp index 293f95b3d..d24fb05be 100644 --- a/src/openvic-simulation/economy/trading/BuyResult.hpp +++ b/src/openvic-simulation/economy/trading/BuyResult.hpp @@ -1,6 +1,6 @@ #pragma once -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" namespace OpenVic { struct GoodDefinition; diff --git a/src/openvic-simulation/economy/trading/BuyUpToOrder.hpp b/src/openvic-simulation/economy/trading/BuyUpToOrder.hpp index e8bd08805..546384586 100644 --- a/src/openvic-simulation/economy/trading/BuyUpToOrder.hpp +++ b/src/openvic-simulation/economy/trading/BuyUpToOrder.hpp @@ -1,7 +1,7 @@ #pragma once #include "openvic-simulation/economy/trading/BuyResult.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/Property.hpp" namespace OpenVic { struct CountryInstance; diff --git a/src/openvic-simulation/economy/trading/GoodMarket.cpp b/src/openvic-simulation/economy/trading/GoodMarket.cpp index 8b10f8ab4..02f3255e0 100644 --- a/src/openvic-simulation/economy/trading/GoodMarket.cpp +++ b/src/openvic-simulation/economy/trading/GoodMarket.cpp @@ -1,12 +1,20 @@ #include "GoodMarket.hpp" +#include +#include +#include +#include +#include + +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/portable/ForwardableSpan.hpp" #include "openvic-simulation/country/CountryInstance.hpp" #include "openvic-simulation/economy/GoodDefinition.hpp" #include "openvic-simulation/economy/trading/BuyUpToOrder.hpp" #include "openvic-simulation/economy/trading/MarketSellOrder.hpp" #include "openvic-simulation/misc/GameRulesManager.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Containers.hpp" using namespace OpenVic; static constexpr size_t MONTHS_OF_PRICE_HISTORY = 36; @@ -68,7 +76,7 @@ void GoodMarket::add_market_sell_order(GoodMarketSellOrder&& market_sell_order) void GoodMarket::execute_orders( IndexedFlatMap& reusable_country_map_0, IndexedFlatMap& reusable_country_map_1, - utility::forwardable_span< + forwardable_span< memory::vector, VECTORS_FOR_EXECUTE_ORDERS > reusable_vectors diff --git a/src/openvic-simulation/economy/trading/GoodMarket.hpp b/src/openvic-simulation/economy/trading/GoodMarket.hpp index 64383a4a8..168513f3f 100644 --- a/src/openvic-simulation/economy/trading/GoodMarket.hpp +++ b/src/openvic-simulation/economy/trading/GoodMarket.hpp @@ -2,13 +2,12 @@ #include +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/container/ValueHistory.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/portable/ForwardableSpan.hpp" #include "openvic-simulation/economy/trading/BuyUpToOrder.hpp" #include "openvic-simulation/economy/trading/MarketSellOrder.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/IndexedFlatMap.hpp" -#include "openvic-simulation/types/ValueHistory.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/ForwardableSpan.hpp" namespace OpenVic { struct CountryInstance; @@ -68,7 +67,7 @@ namespace OpenVic { void execute_orders( IndexedFlatMap& reusable_country_map_0, IndexedFlatMap& reusable_country_map_1, - utility::forwardable_span< + forwardable_span< memory::vector, VECTORS_FOR_EXECUTE_ORDERS > reusable_vectors diff --git a/src/openvic-simulation/economy/trading/MarketInstance.cpp b/src/openvic-simulation/economy/trading/MarketInstance.cpp index b436845df..b1d47c8bd 100644 --- a/src/openvic-simulation/economy/trading/MarketInstance.cpp +++ b/src/openvic-simulation/economy/trading/MarketInstance.cpp @@ -1,13 +1,15 @@ #include "MarketInstance.hpp" +#include + +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/thread/ThreadPool.hpp" #include "openvic-simulation/defines/CountryDefines.hpp" #include "openvic-simulation/economy/GoodDefinition.hpp" #include "openvic-simulation/economy/GoodInstance.hpp" #include "openvic-simulation/economy/trading/BuyUpToOrder.hpp" #include "openvic-simulation/economy/trading/MarketSellOrder.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/ThreadPool.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/economy/trading/MarketInstance.hpp b/src/openvic-simulation/economy/trading/MarketInstance.hpp index 57d4c527a..ba0e51080 100644 --- a/src/openvic-simulation/economy/trading/MarketInstance.hpp +++ b/src/openvic-simulation/economy/trading/MarketInstance.hpp @@ -1,7 +1,7 @@ #pragma once -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Containers.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" namespace OpenVic { struct BuyUpToOrder; diff --git a/src/openvic-simulation/economy/trading/MarketSellOrder.hpp b/src/openvic-simulation/economy/trading/MarketSellOrder.hpp index 21faabfcb..a78d10e10 100644 --- a/src/openvic-simulation/economy/trading/MarketSellOrder.hpp +++ b/src/openvic-simulation/economy/trading/MarketSellOrder.hpp @@ -1,8 +1,8 @@ #pragma once +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" #include "openvic-simulation/economy/trading/SellResult.hpp" -#include "openvic-simulation/utility/Getters.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { struct CountryInstance; diff --git a/src/openvic-simulation/economy/trading/SellResult.hpp b/src/openvic-simulation/economy/trading/SellResult.hpp index 9b830d65c..def94364c 100644 --- a/src/openvic-simulation/economy/trading/SellResult.hpp +++ b/src/openvic-simulation/economy/trading/SellResult.hpp @@ -1,6 +1,6 @@ #pragma once -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" namespace OpenVic { struct GoodDefinition; diff --git a/src/openvic-simulation/history/Bookmark.cpp b/src/openvic-simulation/history/Bookmark.cpp index eb44b3918..51b95e3e3 100644 --- a/src/openvic-simulation/history/Bookmark.cpp +++ b/src/openvic-simulation/history/Bookmark.cpp @@ -3,9 +3,9 @@ #include #include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/utility/Logger.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/Logger.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; diff --git a/src/openvic-simulation/history/Bookmark.hpp b/src/openvic-simulation/history/Bookmark.hpp index 50c80bd31..48451ef28 100644 --- a/src/openvic-simulation/history/Bookmark.hpp +++ b/src/openvic-simulation/history/Bookmark.hpp @@ -4,9 +4,9 @@ #include -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/HasIndex.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" namespace OpenVic { struct Bookmark : HasIdentifier, HasIndex { diff --git a/src/openvic-simulation/history/CountryHistory.cpp b/src/openvic-simulation/history/CountryHistory.cpp index d106358b4..2c5d3d679 100644 --- a/src/openvic-simulation/history/CountryHistory.cpp +++ b/src/openvic-simulation/history/CountryHistory.cpp @@ -1,11 +1,17 @@ #include "CountryHistory.hpp" -#include "openvic-simulation/country/CountryDefinition.hpp" +#include +#include + #include "openvic-simulation/DefinitionManager.hpp" +#include "openvic-simulation/core/FormatValidate.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" +#include "openvic-simulation/core/memory/UniquePtr.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/country/CountryDefinition.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/politics/Government.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/FormatValidate.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -33,7 +39,7 @@ memory::unique_ptr CountryHistoryMap::_make_entry(Date date return memory::make_unique(country, date, ideology_keys, government_type_keys); } -static constexpr auto _flag_callback(string_map_t& flags, bool value) { +static constexpr auto _flag_callback(memory::string_map_t& flags, bool value) { return [&flags, value](std::string_view flag) -> bool { auto [it, successful] = flags.emplace(flag, value); if (!successful) { diff --git a/src/openvic-simulation/history/CountryHistory.hpp b/src/openvic-simulation/history/CountryHistory.hpp index eeebf507c..ce2fd6d7e 100644 --- a/src/openvic-simulation/history/CountryHistory.hpp +++ b/src/openvic-simulation/history/CountryHistory.hpp @@ -2,13 +2,14 @@ #include +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/memory/OrderedSet.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" +#include "openvic-simulation/core/object/Date.hpp" #include "openvic-simulation/history/HistoryMap.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/IndexedFlatMap.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" -#include "openvic-simulation/types/TechnologyUnlockLevel.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { struct CountryHistoryMap; @@ -33,7 +34,7 @@ namespace OpenVic { CountryDefinition const& PROPERTY(country); std::optional PROPERTY(primary_culture); - ordered_map PROPERTY(accepted_cultures); + memory::ordered_map PROPERTY(accepted_cultures); std::optional PROPERTY(religion); std::optional PROPERTY(ruling_party); std::optional PROPERTY(last_election); @@ -44,12 +45,12 @@ namespace OpenVic { std::optional PROPERTY(national_value); std::optional PROPERTY_CUSTOM_PREFIX(civilised, is); std::optional PROPERTY(prestige); - ordered_set PROPERTY(reforms); + memory::ordered_set PROPERTY(reforms); std::optional PROPERTY(initial_oob); std::optional PROPERTY(tech_school); - ordered_map PROPERTY(technologies); - ordered_map PROPERTY(inventions); - fixed_point_map_t PROPERTY(foreign_investment); + memory::ordered_map PROPERTY(technologies); + memory::ordered_map PROPERTY(inventions); + memory::fixed_point_map_t PROPERTY(foreign_investment); std::optional PROPERTY(consciousness); std::optional PROPERTY(nonstate_consciousness); std::optional PROPERTY(literacy); @@ -57,10 +58,10 @@ namespace OpenVic { std::optional PROPERTY_CUSTOM_PREFIX(releasable_vassal, is); std::optional PROPERTY(colonial_points); // True for set, false for clear - string_map_t PROPERTY(country_flags); - string_map_t PROPERTY(global_flags); + memory::string_map_t PROPERTY(country_flags); + memory::string_map_t PROPERTY(global_flags); OV_IFLATMAP_PROPERTY(GovernmentType, GovernmentType const*, flag_overrides_by_government_type); - ordered_set PROPERTY(decisions); + memory::ordered_set PROPERTY(decisions); public: CountryHistoryEntry( @@ -96,7 +97,7 @@ namespace OpenVic { struct CountryHistoryManager { private: - ordered_map country_histories; + memory::ordered_map country_histories; bool locked = false; public: diff --git a/src/openvic-simulation/history/DiplomaticHistory.cpp b/src/openvic-simulation/history/DiplomaticHistory.cpp index 32f84f05f..2074a2d23 100644 --- a/src/openvic-simulation/history/DiplomaticHistory.cpp +++ b/src/openvic-simulation/history/DiplomaticHistory.cpp @@ -1,7 +1,14 @@ #include "DiplomaticHistory.hpp" +#include +#include +#include +#include + #include "openvic-simulation/DefinitionManager.hpp" -#include "openvic-simulation/utility/Containers.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/Date.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; diff --git a/src/openvic-simulation/history/DiplomaticHistory.hpp b/src/openvic-simulation/history/DiplomaticHistory.hpp index 890d7b725..a9e0a0f0b 100644 --- a/src/openvic-simulation/history/DiplomaticHistory.hpp +++ b/src/openvic-simulation/history/DiplomaticHistory.hpp @@ -1,12 +1,14 @@ #pragma once +#include #include +#include +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/Date.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/history/Period.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/utility/Getters.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { struct DiplomaticHistoryManager; diff --git a/src/openvic-simulation/history/HistoryMap.hpp b/src/openvic-simulation/history/HistoryMap.hpp index 2facca95d..be83c1297 100644 --- a/src/openvic-simulation/history/HistoryMap.hpp +++ b/src/openvic-simulation/history/HistoryMap.hpp @@ -2,10 +2,10 @@ #include +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/memory/UniquePtr.hpp" +#include "openvic-simulation/core/object/Date.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { @@ -30,7 +30,7 @@ namespace OpenVic { using entry_type = _Entry; private: - ordered_map> PROPERTY(entries); + memory::ordered_map> PROPERTY(entries); bool _try_load_history_entry( DefinitionManager const& definition_manager, Args... args, Date date, ast::NodeCPtr root @@ -116,7 +116,7 @@ namespace OpenVic { keys.push_back(entry.first); } std::sort(keys.begin(), keys.end()); - ordered_map> new_entries; + memory::ordered_map> new_entries; for (Date const& key : keys) { new_entries.emplace(key, std::move(entries[key])); } diff --git a/src/openvic-simulation/history/Period.cpp b/src/openvic-simulation/history/Period.cpp index 2c9518ba6..c3307ea3b 100644 --- a/src/openvic-simulation/history/Period.cpp +++ b/src/openvic-simulation/history/Period.cpp @@ -1,6 +1,6 @@ #include "Period.hpp" -#include "openvic-simulation/utility/Logger.hpp" +#include "openvic-simulation/core/Logger.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/history/Period.hpp b/src/openvic-simulation/history/Period.hpp index f59b16da7..84753a5ea 100644 --- a/src/openvic-simulation/history/Period.hpp +++ b/src/openvic-simulation/history/Period.hpp @@ -2,7 +2,7 @@ #include -#include "openvic-simulation/types/Date.hpp" +#include "openvic-simulation/core/object/Date.hpp" namespace OpenVic { struct Period { diff --git a/src/openvic-simulation/history/ProvinceHistory.cpp b/src/openvic-simulation/history/ProvinceHistory.cpp index 3d3ef7905..243a40848 100644 --- a/src/openvic-simulation/history/ProvinceHistory.cpp +++ b/src/openvic-simulation/history/ProvinceHistory.cpp @@ -1,11 +1,23 @@ #include "ProvinceHistory.hpp" -#include "openvic-simulation/dataloader/NodeTools.hpp" +#include +#include +#include +#include + +#include + +#include "openvic-simulation/Alias.hpp" #include "openvic-simulation/DefinitionManager.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" +#include "openvic-simulation/core/memory/UniquePtr.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/template/ContainerHash.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/economy/GoodDefinition.hpp" #include "openvic-simulation/map/ProvinceDefinition.hpp" -#include "openvic-simulation/utility/Logger.hpp" -#include "openvic-simulation/utility/Containers.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -30,7 +42,7 @@ bool ProvinceHistoryMap::_load_history_entry( TerrainTypeManager const& terrain_type_manager = definition_manager.get_map_definition().get_terrain_type_manager(); using enum colony_status_t; - static const string_map_t colony_status_map { + static const memory::string_map_t colony_status_map { { "0", STATE }, { "1", PROTECTORATE }, { "2", COLONY } }; diff --git a/src/openvic-simulation/history/ProvinceHistory.hpp b/src/openvic-simulation/history/ProvinceHistory.hpp index 9ee1635cb..acdbc17cd 100644 --- a/src/openvic-simulation/history/ProvinceHistory.hpp +++ b/src/openvic-simulation/history/ProvinceHistory.hpp @@ -1,18 +1,19 @@ #pragma once +#include #include +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/memory/UniquePtr.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/Date.hpp" #include "openvic-simulation/economy/BuildingType.hpp" #include "openvic-simulation/history/HistoryMap.hpp" #include "openvic-simulation/pop/Pop.hpp" -#include "openvic-simulation/types/BuildingLevel.hpp" #include "openvic-simulation/types/ColonyStatus.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" -#include "openvic-simulation/types/ProvinceLifeRating.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/Getters.hpp" namespace OpenVic { struct CountryDefinition; @@ -34,13 +35,13 @@ namespace OpenVic { std::optional PROPERTY(controller); std::optional PROPERTY(colonial); std::optional PROPERTY(slave); - ordered_map PROPERTY(cores); + memory::ordered_map PROPERTY(cores); std::optional PROPERTY(rgo_production_type_nullable); std::optional PROPERTY(life_rating); std::optional PROPERTY(terrain_type); - ordered_map PROPERTY(province_buildings); - ordered_map PROPERTY(state_buildings); - fixed_point_map_t PROPERTY(party_loyalties); + memory::ordered_map PROPERTY(province_buildings); + memory::ordered_map PROPERTY(state_buildings); + memory::fixed_point_map_t PROPERTY(party_loyalties); memory::vector PROPERTY(pops); bool _load_province_pop_history( @@ -73,7 +74,7 @@ namespace OpenVic { struct ProvinceHistoryManager { private: - ordered_map province_histories; + memory::ordered_map province_histories; bool locked = false; ProvinceHistoryMap* _get_or_make_province_history(ProvinceDefinition const& province); diff --git a/src/openvic-simulation/interface/GFXObject.cpp b/src/openvic-simulation/interface/GFXObject.cpp index c889bfbd3..36a13523c 100644 --- a/src/openvic-simulation/interface/GFXObject.cpp +++ b/src/openvic-simulation/interface/GFXObject.cpp @@ -1,7 +1,7 @@ #include "GFXObject.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/utility/Logger.hpp" +#include "openvic-simulation/core/Logger.hpp" using namespace OpenVic; using namespace OpenVic::GFX; diff --git a/src/openvic-simulation/interface/GFXObject.hpp b/src/openvic-simulation/interface/GFXObject.hpp index 49ad61c4e..eae595ce2 100644 --- a/src/openvic-simulation/interface/GFXObject.hpp +++ b/src/openvic-simulation/interface/GFXObject.hpp @@ -2,10 +2,9 @@ #include +#include "openvic-simulation/core/object/Vector.hpp" +#include "openvic-simulation/core/ui/TextFormat.hpp" #include "openvic-simulation/interface/LoadBase.hpp" -#include "openvic-simulation/types/TextFormat.hpp" -#include "openvic-simulation/types/Vector.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic::GFX { diff --git a/src/openvic-simulation/interface/GFXSprite.hpp b/src/openvic-simulation/interface/GFXSprite.hpp index 58e0c5726..fb0247b34 100644 --- a/src/openvic-simulation/interface/GFXSprite.hpp +++ b/src/openvic-simulation/interface/GFXSprite.hpp @@ -1,12 +1,19 @@ #pragma once +#include +#include + +#include "openvic-simulation/core/container/HasIdentifierAndColour.hpp" +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/object/Vector.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/interface/LoadBase.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" namespace OpenVic::GFX { struct Font : HasIdentifierAndAlphaColour { - using colour_codes_t = ordered_map; + using colour_codes_t = memory::ordered_map; private: memory::string PROPERTY(fontname); diff --git a/src/openvic-simulation/interface/GUI.cpp b/src/openvic-simulation/interface/GUI.cpp index 34105f13e..0fabb9002 100644 --- a/src/openvic-simulation/interface/GUI.cpp +++ b/src/openvic-simulation/interface/GUI.cpp @@ -1,10 +1,15 @@ #include "GUI.hpp" +#include + #include "openvic-simulation/DefinitionManager.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" +#include "openvic-simulation/core/memory/UniquePtr.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/interface/UI.hpp" #include "openvic-simulation/misc/SoundEffect.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" using namespace OpenVic; using namespace OpenVic::GUI; @@ -25,7 +30,7 @@ Element::Element() {} bool Element::_fill_key_map(NodeTools::case_insensitive_key_map_t& key_map, UIManager const& ui_manager) { bool ret = Named::_fill_key_map(key_map, ui_manager); using enum orientation_t; - static const string_map_t orientation_map = { + static const memory::string_map_t orientation_map = { { "UPPER_LEFT", UPPER_LEFT }, { "LOWER_LEFT", LOWER_LEFT }, { "LOWER_RIGHT", LOWER_RIGHT }, { "UPPER_RIGHT", UPPER_RIGHT }, { "CENTER", CENTER }, { "CENTER_UP", CENTER_UP }, { "CENTER_DOWN", CENTER_DOWN } diff --git a/src/openvic-simulation/interface/GUI.hpp b/src/openvic-simulation/interface/GUI.hpp index 505d96755..200912b0b 100644 --- a/src/openvic-simulation/interface/GUI.hpp +++ b/src/openvic-simulation/interface/GUI.hpp @@ -1,7 +1,7 @@ #pragma once #include "openvic-simulation/interface/GFXSprite.hpp" -#include "openvic-simulation/types/TextFormat.hpp" +#include "openvic-simulation/core/ui/TextFormat.hpp" #include "openvic-simulation/misc/SoundEffect.hpp" namespace OpenVic { diff --git a/src/openvic-simulation/interface/LoadBase.hpp b/src/openvic-simulation/interface/LoadBase.hpp index e787ef501..ca5d50274 100644 --- a/src/openvic-simulation/interface/LoadBase.hpp +++ b/src/openvic-simulation/interface/LoadBase.hpp @@ -1,7 +1,7 @@ #pragma once -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/Property.hpp" namespace OpenVic { diff --git a/src/openvic-simulation/interface/UI.cpp b/src/openvic-simulation/interface/UI.cpp index de6a29427..01a93f42c 100644 --- a/src/openvic-simulation/interface/UI.cpp +++ b/src/openvic-simulation/interface/UI.cpp @@ -2,8 +2,8 @@ #include "openvic-simulation/DefinitionManager.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/types/Colour.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; diff --git a/src/openvic-simulation/map/Crime.cpp b/src/openvic-simulation/map/Crime.cpp index bdfa180b0..77b43f1bb 100644 --- a/src/openvic-simulation/map/Crime.cpp +++ b/src/openvic-simulation/map/Crime.cpp @@ -1,5 +1,9 @@ #include "Crime.hpp" +#include +#include + +#include "openvic-simulation/core/Logger.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/modifier/ModifierManager.hpp" diff --git a/src/openvic-simulation/map/Crime.hpp b/src/openvic-simulation/map/Crime.hpp index 0404e672f..1e58bb1cb 100644 --- a/src/openvic-simulation/map/Crime.hpp +++ b/src/openvic-simulation/map/Crime.hpp @@ -1,7 +1,7 @@ #pragma once #include "openvic-simulation/modifier/Modifier.hpp" -#include "openvic-simulation/types/HasIndex.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" namespace OpenVic { struct CrimeManager; diff --git a/src/openvic-simulation/map/MapDefinition.cpp b/src/openvic-simulation/map/MapDefinition.cpp index 41696b009..d66cd1905 100644 --- a/src/openvic-simulation/map/MapDefinition.cpp +++ b/src/openvic-simulation/map/MapDefinition.cpp @@ -1,9 +1,9 @@ #include "MapDefinition.hpp" +#include #include #include #include -#include #include #include #include @@ -14,18 +14,20 @@ #include #include +#include "openvic-simulation/core/FormatValidate.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/io/BMP.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/memory/OrderedSet.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/object/Vector.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/map/ProvinceDefinition.hpp" #include "openvic-simulation/modifier/ModifierManager.hpp" -#include "openvic-simulation/types/Colour.hpp" -#include "openvic-simulation/types/OrderedContainersMath.hpp" -#include "openvic-simulation/types/Vector.hpp" -#include "openvic-simulation/utility/BMP.hpp" -#include "openvic-simulation/utility/FormatValidate.hpp" -#include "openvic-simulation/utility/Logger.hpp" -#include "openvic-simulation/utility/StringUtils.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" -#include "openvic-simulation/utility/Containers.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -534,7 +536,7 @@ static bool _parse_province_colour(colour_t& colour, std::array> terrain_type_pixels_list(province_definitions.size()); + memory::vector> terrain_type_pixels_list(province_definitions.size()); bool ret = true; - ordered_set unrecognised_province_colours; + memory::ordered_set unrecognised_province_colours; memory::vector pixels_per_province(province_definitions.size()); memory::vector pixel_position_sum_per_province(province_definitions.size()); @@ -921,8 +923,8 @@ bool MapDefinition::load_map_images(fs::path const& province_path, fs::path cons for (size_t array_index = 0; array_index < province_definitions.size(); ++array_index) { ProvinceDefinition* province = province_definitions.get_item_by_index(array_index); - fixed_point_map_t const& terrain_type_pixels = terrain_type_pixels_list[array_index]; - const fixed_point_map_const_iterator_t largest = get_largest_item(terrain_type_pixels); + memory::fixed_point_map_t const& terrain_type_pixels = terrain_type_pixels_list[array_index]; + const memory::fixed_point_map_const_iterator_t largest = memory::get_largest_item(terrain_type_pixels); province->default_terrain_type = largest != terrain_type_pixels.end() ? largest->first : nullptr; const fixed_point_t pixel_count = pixels_per_province[array_index]; @@ -1035,11 +1037,11 @@ bool MapDefinition::generate_and_load_province_adjacencies(std::span type_map { + static const memory::string_map_t type_map { { "land", LAND }, { "sea", STRAIT }, { "impassable", IMPASSABLE }, { "canal", CANAL } }; const std::string_view type_str = adjacency.get_value_for(2); - const string_map_t::const_iterator it = type_map.find(type_str); + const memory::string_map_t::const_iterator it = type_map.find(type_str); if (it == type_map.end()) { spdlog::error_s("Invalid adjacency type: \"{}\"", type_str); ret = false; @@ -1052,7 +1054,7 @@ bool MapDefinition::generate_and_load_province_adjacencies(std::span std::numeric_limits::max()) { spdlog::error_s("Invalid adjacency data: \"{}\"", data_str); diff --git a/src/openvic-simulation/map/MapDefinition.hpp b/src/openvic-simulation/map/MapDefinition.hpp index fed87cf59..7f347aec7 100644 --- a/src/openvic-simulation/map/MapDefinition.hpp +++ b/src/openvic-simulation/map/MapDefinition.hpp @@ -2,20 +2,21 @@ #include #include +#include #include #include +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/io/BMP.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/object/Vector.hpp" #include "openvic-simulation/map/ProvinceDefinition.hpp" #include "openvic-simulation/map/Region.hpp" #include "openvic-simulation/map/TerrainType.hpp" #include "openvic-simulation/pathfinding/PointMap.hpp" -#include "openvic-simulation/types/Colour.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/types/Vector.hpp" -#include "openvic-simulation/utility/BMP.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { namespace fs = std::filesystem; @@ -49,7 +50,7 @@ namespace OpenVic { #pragma pack(pop) private: - using colour_index_map_t = ordered_map; + using colour_index_map_t = memory::ordered_map; using river_t = memory::vector; IdentifierRegistry IDENTIFIER_REGISTRY(province_definition); diff --git a/src/openvic-simulation/map/MapInstance.cpp b/src/openvic-simulation/map/MapInstance.cpp index 8c3973fa3..b3aaf0bd9 100644 --- a/src/openvic-simulation/map/MapInstance.cpp +++ b/src/openvic-simulation/map/MapInstance.cpp @@ -3,11 +3,11 @@ #include #include +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/thread/ThreadPool.hpp" #include "openvic-simulation/history/ProvinceHistory.hpp" #include "openvic-simulation/map/MapDefinition.hpp" #include "openvic-simulation/pop/PopValuesFromProvince.hpp" -#include "openvic-simulation/utility/Logger.hpp" -#include "openvic-simulation/utility/ThreadPool.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/map/MapInstance.hpp b/src/openvic-simulation/map/MapInstance.hpp index 61667e1cc..cddedc770 100644 --- a/src/openvic-simulation/map/MapInstance.hpp +++ b/src/openvic-simulation/map/MapInstance.hpp @@ -1,11 +1,11 @@ #pragma once +#include "openvic-simulation/core/object/Date.hpp" #include "openvic-simulation/map/ProvinceDefinition.hpp" #include "openvic-simulation/map/ProvinceInstance.hpp" #include "openvic-simulation/map/State.hpp" #include "openvic-simulation/pathfinding/AStarPathing.hpp" #include "openvic-simulation/pop/PopValuesFromProvince.hpp" -#include "openvic-simulation/types/Date.hpp" namespace OpenVic { struct BuildingTypeManager; @@ -34,7 +34,7 @@ namespace OpenVic { StateManager PROPERTY_REF(state_manager); // TODO - should this be a vector of bools which we resize to the largest enabled canal index? - ordered_set PROPERTY(enabled_canals); + memory::ordered_set PROPERTY(enabled_canals); ArmyAStarPathing PROPERTY_REF(land_pathing); NavyAStarPathing PROPERTY_REF(sea_pathing); @@ -50,11 +50,11 @@ namespace OpenVic { return map_definition; } - constexpr OpenVic::utility::forwardable_span get_province_instances() { + constexpr OpenVic::forwardable_span get_province_instances() { return province_instance_by_definition.get_values(); } - constexpr OpenVic::utility::forwardable_span get_province_instances() const { + constexpr OpenVic::forwardable_span get_province_instances() const { return province_instance_by_definition.get_values(); } ProvinceInstance* get_province_instance_by_identifier(std::string_view identifier); diff --git a/src/openvic-simulation/map/Mapmode.cpp b/src/openvic-simulation/map/Mapmode.cpp index a46b891b8..9a9ab3f64 100644 --- a/src/openvic-simulation/map/Mapmode.cpp +++ b/src/openvic-simulation/map/Mapmode.cpp @@ -1,5 +1,12 @@ #include "Mapmode.hpp" +#include +#include + +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/object/Colour.hpp" #include "openvic-simulation/country/CountryInstance.hpp" #include "openvic-simulation/economy/BuildingType.hpp" #include "openvic-simulation/map/MapDefinition.hpp" @@ -8,8 +15,6 @@ #include "openvic-simulation/map/ProvinceInstance.hpp" #include "openvic-simulation/pop/Culture.hpp" #include "openvic-simulation/pop/Religion.hpp" -#include "openvic-simulation/types/OrderedContainersMath.hpp" -#include "openvic-simulation/types/PopSize.hpp" using namespace OpenVic; using namespace OpenVic::colour_literals; @@ -165,14 +170,14 @@ static constexpr auto get_colour_mapmode( template static constexpr Mapmode::base_stripe_t shaded_mapmode( - ordered_map const& map + memory::ordered_map const& map ) { - const auto largest = get_largest_two_items(map); + const auto largest = memory::get_largest_two_items(map); if (largest.first != map.end()) { const colour_argb_t base_colour = colour_argb_t { largest.first->first->get_colour(), ALPHA_VALUE }; if (largest.second != map.end()) { /* If second largest is at least a third... */ - if (largest.second->second * 3 >= get_total(map)) { + if (largest.second->second * 3 >= memory::get_total(map)) { const colour_argb_t stripe_colour = colour_argb_t { largest.second->first->get_colour(), ALPHA_VALUE }; return { base_colour, stripe_colour }; } @@ -183,7 +188,7 @@ static constexpr Mapmode::base_stripe_t shaded_mapmode( } template -static constexpr auto shaded_mapmode(ordered_map const&(ProvinceInstance::*get_map)() const) { +static constexpr auto shaded_mapmode(memory::ordered_map const&(ProvinceInstance::*get_map)() const) { return [get_map]( MapInstance const& map_instance, ProvinceInstance const& province, CountryInstance const* player_country, ProvinceInstance const* selected_province diff --git a/src/openvic-simulation/map/Mapmode.hpp b/src/openvic-simulation/map/Mapmode.hpp index aaa7e0305..f2021bc5f 100644 --- a/src/openvic-simulation/map/Mapmode.hpp +++ b/src/openvic-simulation/map/Mapmode.hpp @@ -1,9 +1,9 @@ #pragma once -#include "openvic-simulation/types/Colour.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/HasIndex.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/object/Colour.hpp" #include diff --git a/src/openvic-simulation/map/ProvinceDefinition.cpp b/src/openvic-simulation/map/ProvinceDefinition.cpp index c37d6ef76..394838fce 100644 --- a/src/openvic-simulation/map/ProvinceDefinition.cpp +++ b/src/openvic-simulation/map/ProvinceDefinition.cpp @@ -1,11 +1,16 @@ #include "ProvinceDefinition.hpp" +#include + #include +#include "openvic-simulation/core/memory/Format.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/economy/BuildingType.hpp" #include "openvic-simulation/map/MapDefinition.hpp" -#include "openvic-simulation/utility/Containers.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; diff --git a/src/openvic-simulation/map/ProvinceDefinition.hpp b/src/openvic-simulation/map/ProvinceDefinition.hpp index 26c46edef..72a4ede94 100644 --- a/src/openvic-simulation/map/ProvinceDefinition.hpp +++ b/src/openvic-simulation/map/ProvinceDefinition.hpp @@ -2,14 +2,13 @@ #include +#include "openvic-simulation/core/container/HasIdentifierAndColour.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/object/Vector.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/HasIndex.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/types/Vector.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { @@ -78,8 +77,8 @@ namespace OpenVic { std::optional factory; std::optional building_construction; std::optional military_construction; - ordered_map building_position; - fixed_point_map_t building_rotation; + memory::ordered_map building_position; + memory::fixed_point_map_t building_rotation; }; static constexpr index_t NULL_INDEX = 0, MAX_INDEX = std::numeric_limits::max(); diff --git a/src/openvic-simulation/map/ProvinceInstance.cpp b/src/openvic-simulation/map/ProvinceInstance.cpp index 355b51b8f..964e1d837 100644 --- a/src/openvic-simulation/map/ProvinceInstance.cpp +++ b/src/openvic-simulation/map/ProvinceInstance.cpp @@ -390,7 +390,7 @@ void ProvinceInstance::province_tick( PopValuesFromProvince& reusable_pop_values, RandomU32& random_number_generator, IndexedFlatMap& reusable_goods_mask, - utility::forwardable_span< + forwardable_span< memory::vector, VECTORS_FOR_PROVINCE_TICK > reusable_vectors @@ -520,7 +520,7 @@ void ProvinceInstance::initialise_for_new_game( PopValuesFromProvince& reusable_pop_values, RandomU32& random_number_generator, IndexedFlatMap& reusable_goods_mask, - utility::forwardable_span< + forwardable_span< memory::vector, VECTORS_FOR_PROVINCE_TICK > reusable_vectors diff --git a/src/openvic-simulation/map/ProvinceInstance.hpp b/src/openvic-simulation/map/ProvinceInstance.hpp index fac675925..86668278f 100644 --- a/src/openvic-simulation/map/ProvinceInstance.hpp +++ b/src/openvic-simulation/map/ProvinceInstance.hpp @@ -2,6 +2,13 @@ #include +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/container/FlagStrings.hpp" +#include "openvic-simulation/core/container/HasIdentifierAndColour.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" +#include "openvic-simulation/core/memory/Colony.hpp" +#include "openvic-simulation/core/memory/OrderedSet.hpp" +#include "openvic-simulation/core/portable/ForwardableSpan.hpp" #include "openvic-simulation/economy/BuildingInstance.hpp" #include "openvic-simulation/economy/production/ResourceGatheringOperation.hpp" #include "openvic-simulation/military/UnitBranchedGetterMacro.hpp" @@ -9,14 +16,7 @@ #include "openvic-simulation/pop/Pop.hpp" #include "openvic-simulation/pop/PopsAggregate.hpp" #include "openvic-simulation/types/ColonyStatus.hpp" -#include "openvic-simulation/types/FlagStrings.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/HasIndex.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/types/ProvinceLifeRating.hpp" #include "openvic-simulation/types/UnitBranchType.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/ForwardableSpan.hpp" namespace OpenVic { struct BaseIssue; @@ -80,7 +80,7 @@ namespace OpenVic { ModifierSum const& get_owner_modifier_sum() const; CountryInstance* PROPERTY_PTR(controller, nullptr); CountryInstance* PROPERTY_PTR(country_to_report_economy, nullptr); - ordered_set PROPERTY(cores); + memory::ordered_set PROPERTY(cores); // The total/resultant modifier of local effects on this province (global effects come from the province's owner) ModifierSum PROPERTY(modifier_sum); @@ -190,7 +190,7 @@ namespace OpenVic { PopValuesFromProvince& reusable_pop_values, RandomU32& random_number_generator, IndexedFlatMap& reusable_goods_mask, - utility::forwardable_span< + forwardable_span< memory::vector, VECTORS_FOR_PROVINCE_TICK > reusable_vectors @@ -200,7 +200,7 @@ namespace OpenVic { PopValuesFromProvince& reusable_pop_values, RandomU32& random_number_generator, IndexedFlatMap& reusable_goods_mask, - utility::forwardable_span< + forwardable_span< memory::vector, VECTORS_FOR_PROVINCE_TICK > reusable_vectors diff --git a/src/openvic-simulation/map/ProvinceInstanceDeps.hpp b/src/openvic-simulation/map/ProvinceInstanceDeps.hpp index 795e9a223..6c8ff6335 100644 --- a/src/openvic-simulation/map/ProvinceInstanceDeps.hpp +++ b/src/openvic-simulation/map/ProvinceInstanceDeps.hpp @@ -1,6 +1,6 @@ #pragma once -#include "openvic-simulation/utility/ForwardableSpan.hpp" +#include "openvic-simulation/core/portable/ForwardableSpan.hpp" namespace OpenVic { struct BuildingTypeManager; @@ -13,9 +13,9 @@ namespace OpenVic { struct ProvinceInstanceDeps { BuildingTypeManager const& building_type_manager; GameRulesManager const& game_rules_manager; - utility::forwardable_span ideologies; - utility::forwardable_span pop_types; + forwardable_span ideologies; + forwardable_span pop_types; ResourceGatheringOperationDeps const& rgo_deps; - utility::forwardable_span stratas; + forwardable_span stratas; }; } \ No newline at end of file diff --git a/src/openvic-simulation/map/Region.cpp b/src/openvic-simulation/map/Region.cpp index 4e5cb9ecd..e092f1790 100644 --- a/src/openvic-simulation/map/Region.cpp +++ b/src/openvic-simulation/map/Region.cpp @@ -1,7 +1,7 @@ #include "Region.hpp" #include "openvic-simulation/map/ProvinceDefinition.hpp" -#include "openvic-simulation/types/Colour.hpp" +#include "openvic-simulation/core/object/Colour.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/map/Region.hpp b/src/openvic-simulation/map/Region.hpp index 24612d7c1..a31b03ff9 100644 --- a/src/openvic-simulation/map/Region.hpp +++ b/src/openvic-simulation/map/Region.hpp @@ -1,10 +1,12 @@ #pragma once +#include #include #include +#include "openvic-simulation/core/container/HasIdentifierAndColour.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" #include "openvic-simulation/modifier/Modifier.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { struct ProvinceDefinition; diff --git a/src/openvic-simulation/map/State.cpp b/src/openvic-simulation/map/State.cpp index 8f16fcfc0..6cfd998bf 100644 --- a/src/openvic-simulation/map/State.cpp +++ b/src/openvic-simulation/map/State.cpp @@ -1,5 +1,18 @@ #include "State.hpp" +#include +#include +#include + +#include + +#include "openvic-simulation/core/FormatValidate.hpp" +#include "openvic-simulation/core/error/ErrorMacros.hpp" +#include "openvic-simulation/core/memory/Format.hpp" +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/portable/ForwardableSpan.hpp" #include "openvic-simulation/country/CountryInstance.hpp" #include "openvic-simulation/map/MapDefinition.hpp" #include "openvic-simulation/map/MapInstance.hpp" @@ -7,10 +20,6 @@ #include "openvic-simulation/map/Region.hpp" #include "openvic-simulation/pop/Pop.hpp" #include "openvic-simulation/pop/PopType.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/StringUtils.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/ErrorMacros.hpp" using namespace OpenVic; @@ -19,9 +28,9 @@ State::State( ProvinceInstance* new_capital, memory::vector&& new_provinces, colony_status_t new_colony_status, - utility::forwardable_span strata_keys, - utility::forwardable_span pop_type_keys, - utility::forwardable_span ideology_keys + forwardable_span strata_keys, + forwardable_span pop_type_keys, + forwardable_span ideology_keys ) : PopsAggregate { strata_keys, pop_type_keys, ideology_keys }, state_set { new_state_set }, capital { new_capital }, @@ -134,9 +143,9 @@ void StateSet::update_gamestate() { bool StateManager::add_state_set( MapInstance& map_instance, Region const& region, - utility::forwardable_span strata_keys, - utility::forwardable_span pop_type_keys, - utility::forwardable_span ideology_keys + forwardable_span strata_keys, + forwardable_span pop_type_keys, + forwardable_span ideology_keys ) { OV_ERR_FAIL_COND_V_MSG(region.get_is_meta(), false, memory::fmt::format("Cannot use meta region \"{}\" as state template!", region)); OV_ERR_FAIL_COND_V_MSG(region.empty(), false, memory::fmt::format("Cannot use empty region \"{}\" as state template!", region)); @@ -191,9 +200,9 @@ bool StateManager::add_state_set( bool StateManager::generate_states( MapInstance& map_instance, - utility::forwardable_span strata_keys, - utility::forwardable_span pop_type_keys, - utility::forwardable_span ideology_keys + forwardable_span strata_keys, + forwardable_span pop_type_keys, + forwardable_span ideology_keys ) { MapDefinition const& map_definition = map_instance.get_map_definition(); diff --git a/src/openvic-simulation/map/State.hpp b/src/openvic-simulation/map/State.hpp index 50c4957f5..47f281067 100644 --- a/src/openvic-simulation/map/State.hpp +++ b/src/openvic-simulation/map/State.hpp @@ -4,12 +4,12 @@ #include +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/memory/Colony.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/portable/ForwardableSpan.hpp" #include "openvic-simulation/pop/PopsAggregate.hpp" #include "openvic-simulation/types/ColonyStatus.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/ForwardableSpan.hpp" -#include "openvic-simulation/utility/Getters.hpp" namespace OpenVic { struct BaseIssue; @@ -48,9 +48,9 @@ namespace OpenVic { ProvinceInstance* new_capital, memory::vector&& new_provinces, colony_status_t new_colony_status, - utility::forwardable_span strata_keys, - utility::forwardable_span pop_type_keys, - utility::forwardable_span ideology_keys + forwardable_span strata_keys, + forwardable_span pop_type_keys, + forwardable_span ideology_keys ); State(State&&) = delete; State(State const&) = delete; @@ -95,9 +95,9 @@ namespace OpenVic { bool add_state_set( MapInstance& map_instance, Region const& region, - utility::forwardable_span strata_keys, - utility::forwardable_span pop_type_keys, - utility::forwardable_span ideology_keys + forwardable_span strata_keys, + forwardable_span pop_type_keys, + forwardable_span ideology_keys ); public: @@ -106,9 +106,9 @@ namespace OpenVic { * validated by functions that modify it. */ bool generate_states( MapInstance& map_instance, - utility::forwardable_span strata_keys, - utility::forwardable_span pop_type_keys, - utility::forwardable_span ideology_keys + forwardable_span strata_keys, + forwardable_span pop_type_keys, + forwardable_span ideology_keys ); void reset(); diff --git a/src/openvic-simulation/map/TerrainType.cpp b/src/openvic-simulation/map/TerrainType.cpp index 118e4f09b..f55184218 100644 --- a/src/openvic-simulation/map/TerrainType.cpp +++ b/src/openvic-simulation/map/TerrainType.cpp @@ -1,12 +1,16 @@ #include "TerrainType.hpp" +#include #include - +#include + +#include "openvic-simulation/core/FormatValidate.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/modifier/ModifierManager.hpp" -#include "openvic-simulation/types/Colour.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/utility/FormatValidate.hpp" -#include "openvic-simulation/utility/Containers.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; diff --git a/src/openvic-simulation/map/TerrainType.hpp b/src/openvic-simulation/map/TerrainType.hpp index ffeee2a72..fcd917242 100644 --- a/src/openvic-simulation/map/TerrainType.hpp +++ b/src/openvic-simulation/map/TerrainType.hpp @@ -1,10 +1,16 @@ #pragma once +#include +#include + +#include "openvic-simulation/core/container/HasColour.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/modifier/Modifier.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/HasIndex.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { // Using HasColour rather than HasIdentifierAndColour to avoid needing virtual inheritance @@ -53,7 +59,7 @@ namespace OpenVic { struct TerrainTypeManager { private: - using terrain_type_mappings_map_t = ordered_map; + using terrain_type_mappings_map_t = memory::ordered_map; IdentifierRegistry IDENTIFIER_REGISTRY(terrain_type); IdentifierRegistry IDENTIFIER_REGISTRY(terrain_type_mapping); diff --git a/src/openvic-simulation/military/Deployment.cpp b/src/openvic-simulation/military/Deployment.cpp index e9fbe4e04..79c397c4d 100644 --- a/src/openvic-simulation/military/Deployment.cpp +++ b/src/openvic-simulation/military/Deployment.cpp @@ -1,12 +1,22 @@ #include "Deployment.hpp" +#include +#include +#include +#include + +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/string/Utility.hpp" #include "openvic-simulation/dataloader/Dataloader.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/map/MapDefinition.hpp" -#include "openvic-simulation/military/MilitaryManager.hpp" #include "openvic-simulation/military/Leader.hpp" #include "openvic-simulation/military/LeaderTrait.hpp" +#include "openvic-simulation/military/MilitaryManager.hpp" #include "openvic-simulation/military/UnitType.hpp" -#include "openvic-simulation/utility/Containers.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -66,7 +76,7 @@ bool DeploymentManager::load_oob_file( static constexpr std::string_view oob_directory = "history/units/"; const fs::path lookedup_path = - dataloader.lookup_file(StringUtils::append_string_views(oob_directory, history_path), false); + dataloader.lookup_file(OpenVic::append_string_views(oob_directory, history_path), false); if (lookedup_path.empty()) { missing_oob_files.emplace(history_path); diff --git a/src/openvic-simulation/military/Deployment.hpp b/src/openvic-simulation/military/Deployment.hpp index aa5f1e548..aeb38bf7b 100644 --- a/src/openvic-simulation/military/Deployment.hpp +++ b/src/openvic-simulation/military/Deployment.hpp @@ -3,13 +3,14 @@ #include #include +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/memory/StringSet.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" #include "openvic-simulation/military/Leader.hpp" #include "openvic-simulation/military/UnitBranchedGetterMacro.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" #include "openvic-simulation/types/UnitBranchType.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/Getters.hpp" namespace OpenVic { class Dataloader; @@ -95,7 +96,7 @@ namespace OpenVic { struct DeploymentManager { private: IdentifierRegistry IDENTIFIER_REGISTRY(deployment); - string_set_t missing_oob_files; + memory::string_set_t missing_oob_files; public: bool add_deployment( diff --git a/src/openvic-simulation/military/Leader.hpp b/src/openvic-simulation/military/Leader.hpp index 651bc72c4..4c6f977be 100644 --- a/src/openvic-simulation/military/Leader.hpp +++ b/src/openvic-simulation/military/Leader.hpp @@ -2,11 +2,11 @@ #include -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/types/UnitBranchType.hpp" -#include "openvic-simulation/types/UniqueId.hpp" -#include "openvic-simulation/utility/Getters.hpp" namespace OpenVic { struct DeploymentManager; diff --git a/src/openvic-simulation/military/LeaderTrait.hpp b/src/openvic-simulation/military/LeaderTrait.hpp index 50c050197..9a046ec94 100644 --- a/src/openvic-simulation/military/LeaderTrait.hpp +++ b/src/openvic-simulation/military/LeaderTrait.hpp @@ -2,10 +2,10 @@ #include +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/modifier/Modifier.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { struct LeaderTrait : Modifier { diff --git a/src/openvic-simulation/military/UnitInstance.hpp b/src/openvic-simulation/military/UnitInstance.hpp index 025b41723..026fb11a0 100644 --- a/src/openvic-simulation/military/UnitInstance.hpp +++ b/src/openvic-simulation/military/UnitInstance.hpp @@ -3,10 +3,10 @@ #include #include "openvic-simulation/military/UnitType.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/types/UnitBranchType.hpp" -#include "openvic-simulation/types/UniqueId.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/Property.hpp" namespace OpenVic { diff --git a/src/openvic-simulation/military/UnitInstanceGroup.cpp b/src/openvic-simulation/military/UnitInstanceGroup.cpp index de92a57dd..6abfe46fa 100644 --- a/src/openvic-simulation/military/UnitInstanceGroup.cpp +++ b/src/openvic-simulation/military/UnitInstanceGroup.cpp @@ -1,7 +1,18 @@ #include "UnitInstanceGroup.hpp" +#include +#include +#include #include +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/FormatValidate.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/string/Utility.hpp" #include "openvic-simulation/country/CountryInstance.hpp" #include "openvic-simulation/defines/MilitaryDefines.hpp" #include "openvic-simulation/map/MapInstance.hpp" @@ -9,9 +20,7 @@ #include "openvic-simulation/military/Deployment.hpp" #include "openvic-simulation/military/LeaderTrait.hpp" #include "openvic-simulation/pop/Culture.hpp" -#include "openvic-simulation/types/OrderedContainersMath.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/FormatValidate.hpp" +#include "openvic-simulation/types/UnitBranchType.hpp" using namespace OpenVic; @@ -62,14 +71,14 @@ UnitType const* UnitInstanceGroup::get_display_unit_type() const { return nullptr; } - fixed_point_map_t weighted_unit_types; + memory::fixed_point_map_t weighted_unit_types; for (UnitInstance const* unit : units) { UnitType const& unit_type = unit->get_unit_type(); weighted_unit_types[&unit_type] += unit_type.get_weighted_value(); } - return get_largest_item_tie_break( + return memory::get_largest_item_tie_break( weighted_unit_types, [](UnitType const* lhs, UnitType const* rhs) -> bool { return lhs->get_weighted_value() < rhs->get_weighted_value(); @@ -502,7 +511,7 @@ bool UnitInstanceManager::create_leader( connector = " "; } - name_storage = StringUtils::append_string_views(first_name, connector, last_name); + name_storage = OpenVic::append_string_views(first_name, connector, last_name); name = name_storage; } } diff --git a/src/openvic-simulation/military/UnitInstanceGroup.hpp b/src/openvic-simulation/military/UnitInstanceGroup.hpp index b47260d07..c2fe2f863 100644 --- a/src/openvic-simulation/military/UnitInstanceGroup.hpp +++ b/src/openvic-simulation/military/UnitInstanceGroup.hpp @@ -4,13 +4,14 @@ #include +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/memory/Colony.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/military/Leader.hpp" #include "openvic-simulation/military/UnitInstance.hpp" #include "openvic-simulation/military/UnitType.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" #include "openvic-simulation/types/UnitBranchType.hpp" -#include "openvic-simulation/utility/Getters.hpp" -#include "openvic-simulation/utility/Containers.hpp" #include "openvic-simulation/military/UnitBranchedGetterMacro.hpp" //below other imports that undef the macros @@ -169,17 +170,17 @@ namespace OpenVic { // TODO - maps from unique_ids to leader/unit/unit group pointers (one big map or multiple maps?) memory::colony PROPERTY(leaders); - ordered_map PROPERTY(leader_instance_map); + memory::ordered_map PROPERTY(leader_instance_map); memory::colony PROPERTY(regiments); memory::colony PROPERTY(ships); - ordered_map PROPERTY(unit_instance_map); + memory::ordered_map PROPERTY(unit_instance_map); OV_UNIT_BRANCHED_GETTER(get_unit_instances, regiments, ships); memory::colony PROPERTY(armies); memory::colony PROPERTY(navies); - ordered_map PROPERTY(unit_instance_group_map); + memory::ordered_map PROPERTY(unit_instance_group_map); OV_UNIT_BRANCHED_GETTER(get_unit_instance_groups, armies, navies); diff --git a/src/openvic-simulation/military/UnitType.cpp b/src/openvic-simulation/military/UnitType.cpp index 60d640276..f31096b14 100644 --- a/src/openvic-simulation/military/UnitType.cpp +++ b/src/openvic-simulation/military/UnitType.cpp @@ -1,11 +1,20 @@ #include "UnitType.hpp" +#include +#include +#include + +#include +#include + +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" #include "openvic-simulation/country/CountryInstance.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/map/TerrainType.hpp" +#include "openvic-simulation/modifier/ModifierEffectCache.hpp" #include "openvic-simulation/modifier/ModifierManager.hpp" -#include "modifier/ModifierEffectCache.hpp" -#include "types/UnitBranchType.hpp" +#include "openvic-simulation/types/UnitBranchType.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -181,7 +190,7 @@ bool UnitTypeManager::load_unit_type_file( ModifierManager const& modifier_manager, ovdl::v2script::Parser const& parser ) { using namespace std::string_view_literals; - auto type_symbol = parser.find_intern("type"sv); + ovdl::symbol type_symbol = parser.find_intern("type"sv); if (!type_symbol) { spdlog::error_s("type could not be interned."); } @@ -203,7 +212,7 @@ bool UnitTypeManager::load_unit_type_file( UnitType::unit_type_args_t unit_args {}; - static const string_map_t unit_type_map { + static const memory::string_map_t unit_type_map { { "infantry", INFANTRY }, { "cavalry", CAVALRY }, { "support", SUPPORT }, diff --git a/src/openvic-simulation/military/UnitType.hpp b/src/openvic-simulation/military/UnitType.hpp index 30925267e..71264acdf 100644 --- a/src/openvic-simulation/military/UnitType.hpp +++ b/src/openvic-simulation/military/UnitType.hpp @@ -5,14 +5,17 @@ #include +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/economy/GoodDefinition.hpp" #include "openvic-simulation/modifier/Modifier.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/HasIndex.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" #include "openvic-simulation/types/UnitBranchType.hpp" namespace OpenVic { @@ -23,14 +26,14 @@ namespace OpenVic { struct UnitType : HasIdentifier { using icon_t = uint32_t; - using terrain_modifiers_t = ordered_map; + using terrain_modifiers_t = memory::ordered_map; enum struct unit_category_t : uint8_t { INVALID_UNIT_CATEGORY, INFANTRY, CAVALRY, SUPPORT, SPECIAL, BIG_SHIP, LIGHT_SHIP, TRANSPORT }; struct unit_type_args_t { - using terrain_modifier_values_t = ordered_map; + using terrain_modifier_values_t = memory::ordered_map; icon_t icon = 0; unit_category_t unit_category = unit_category_t::INVALID_UNIT_CATEGORY; @@ -41,7 +44,7 @@ namespace OpenVic { fixed_point_t max_strength = 0, default_organisation = 0, maximum_speed = 0, weighted_value = 0, supply_consumption = 0; Timespan build_time; - fixed_point_map_t build_cost, supply_cost; + memory::fixed_point_map_t build_cost, supply_cost; terrain_modifier_values_t terrain_modifier_values; unit_type_args_t() {}; @@ -66,9 +69,9 @@ namespace OpenVic { memory::string PROPERTY(select_sound); const Timespan PROPERTY(build_time); - fixed_point_map_t PROPERTY(build_cost); + memory::fixed_point_map_t PROPERTY(build_cost); const fixed_point_t PROPERTY(supply_consumption); - fixed_point_map_t PROPERTY(supply_cost); + memory::fixed_point_map_t PROPERTY(supply_cost); terrain_modifiers_t PROPERTY(terrain_modifiers); @@ -183,7 +186,7 @@ namespace OpenVic { NodeTools::Callback auto callback ) { using enum unit_branch_t; - static const string_map_t branch_map { + static const memory::string_map_t branch_map { { "land", LAND }, { "naval", NAVAL }, { "sea", NAVAL } }; return NodeTools::expect_mapped_string(branch_map, callback); diff --git a/src/openvic-simulation/military/Wargoal.cpp b/src/openvic-simulation/military/Wargoal.cpp index fb7b88f03..4d8b26740 100644 --- a/src/openvic-simulation/military/Wargoal.cpp +++ b/src/openvic-simulation/military/Wargoal.cpp @@ -1,7 +1,14 @@ #include "Wargoal.hpp" +#include +#include + +#include #include +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" +#include "openvic-simulation/core/object/Date.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" using namespace OpenVic; @@ -118,7 +125,7 @@ bool WargoalTypeManager::load_wargoal_file(ovdl::v2script::Parser const& parser) bool ret = expect_dictionary_keys_and_default( [&modifiers, &identifier](std::string_view key, ast::NodeCPtr value) -> bool { using enum WargoalType::PEACE_MODIFIERS; - static const string_map_t peace_modifier_map { + static const memory::string_map_t peace_modifier_map { { "badboy_factor", BADBOY_FACTOR }, { "prestige_factor", PRESTIGE_FACTOR }, { "peace_cost_factor", PEACE_COST_FACTOR }, diff --git a/src/openvic-simulation/military/Wargoal.hpp b/src/openvic-simulation/military/Wargoal.hpp index 5dc463617..f458a13aa 100644 --- a/src/openvic-simulation/military/Wargoal.hpp +++ b/src/openvic-simulation/military/Wargoal.hpp @@ -1,13 +1,18 @@ #pragma once +#include +#include + #include +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/template/EnumBitfield.hpp" #include "openvic-simulation/scripts/ConditionScript.hpp" #include "openvic-simulation/scripts/EffectScript.hpp" -#include "openvic-simulation/types/EnumBitfield.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/utility/Getters.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { struct WargoalTypeManager; @@ -32,7 +37,7 @@ namespace OpenVic { WAR_SCORE_BATTLE_FACTOR, CONSTRUCTION_SPEED }; - using peace_modifiers_t = fixed_point_map_t; + using peace_modifiers_t = memory::fixed_point_map_t; enum class peace_options_t : uint32_t { NO_PEACE_OPTIONS = 0, diff --git a/src/openvic-simulation/misc/Decision.hpp b/src/openvic-simulation/misc/Decision.hpp index 3ac7f0e44..54da3359c 100644 --- a/src/openvic-simulation/misc/Decision.hpp +++ b/src/openvic-simulation/misc/Decision.hpp @@ -2,7 +2,7 @@ #include "openvic-simulation/scripts/ConditionalWeight.hpp" #include "openvic-simulation/scripts/EffectScript.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" namespace OpenVic { struct DecisionManager; diff --git a/src/openvic-simulation/misc/Event.cpp b/src/openvic-simulation/misc/Event.cpp index 0bdca4449..46e6ef36b 100644 --- a/src/openvic-simulation/misc/Event.cpp +++ b/src/openvic-simulation/misc/Event.cpp @@ -1,11 +1,20 @@ #include "Event.hpp" + #include +#include #include +#include + +#include + +#include +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/string/CharConv.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/politics/BaseIssue.hpp" #include "openvic-simulation/politics/IssueManager.hpp" -#include "openvic-simulation/utility/Containers.hpp" +#include "openvic-simulation/scripts/Condition.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -219,7 +228,7 @@ bool EventManager::load_on_action_file(ast::NodeCPtr root) { [this, &identifier, &weighted_events](std::string_view weight_str, ast::NodeCPtr event_node) -> bool { bool ret = false; uint64_t weight; - std::from_chars_result result = StringUtils::string_to_uint64(weight_str, weight); + std::from_chars_result result = OpenVic::string_to_uint64(weight_str, weight); ret = result.ec == std::errc{}; if (!ret) { spdlog::error_s("Invalid weight {} on action {}", weight_str, identifier); diff --git a/src/openvic-simulation/misc/Event.hpp b/src/openvic-simulation/misc/Event.hpp index 370e57a5f..2b4389514 100644 --- a/src/openvic-simulation/misc/Event.hpp +++ b/src/openvic-simulation/misc/Event.hpp @@ -1,10 +1,16 @@ #pragma once +#include +#include + +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/scripts/ConditionalWeight.hpp" #include "openvic-simulation/scripts/EffectScript.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { struct BaseIssueGroup; @@ -76,7 +82,7 @@ namespace OpenVic { }; struct OnAction : HasIdentifier { - using weight_map_t = ordered_map; + using weight_map_t = memory::ordered_map; private: weight_map_t PROPERTY(weighted_events); diff --git a/src/openvic-simulation/misc/GameAction.cpp b/src/openvic-simulation/misc/GameAction.cpp index 43d41637a..0c098aa66 100644 --- a/src/openvic-simulation/misc/GameAction.cpp +++ b/src/openvic-simulation/misc/GameAction.cpp @@ -1,12 +1,22 @@ #include "GameAction.hpp" -#include +#include +#include +#include +#include +#include +#include + #include +#include #include "openvic-simulation/DefinitionManager.hpp" #include "openvic-simulation/InstanceManager.hpp" -#include "utility/Containers.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/memory/Format.hpp" +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/misc/GameAction.hpp b/src/openvic-simulation/misc/GameAction.hpp index d605ef21d..45a2d7800 100644 --- a/src/openvic-simulation/misc/GameAction.hpp +++ b/src/openvic-simulation/misc/GameAction.hpp @@ -6,7 +6,7 @@ #include #include -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" namespace OpenVic { diff --git a/src/openvic-simulation/misc/GameRulesManager.hpp b/src/openvic-simulation/misc/GameRulesManager.hpp index 237c6fa10..5013d123a 100644 --- a/src/openvic-simulation/misc/GameRulesManager.hpp +++ b/src/openvic-simulation/misc/GameRulesManager.hpp @@ -2,7 +2,7 @@ #include -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/Property.hpp" namespace OpenVic { struct ProvinceInstance; diff --git a/src/openvic-simulation/misc/SimulationClock.cpp b/src/openvic-simulation/misc/SimulationClock.cpp index bee19f013..4344ac008 100644 --- a/src/openvic-simulation/misc/SimulationClock.cpp +++ b/src/openvic-simulation/misc/SimulationClock.cpp @@ -1,6 +1,7 @@ #include "SimulationClock.hpp" #include +#include using namespace OpenVic; diff --git a/src/openvic-simulation/misc/SimulationClock.hpp b/src/openvic-simulation/misc/SimulationClock.hpp index c4c3af0a4..8255e269d 100644 --- a/src/openvic-simulation/misc/SimulationClock.hpp +++ b/src/openvic-simulation/misc/SimulationClock.hpp @@ -1,9 +1,11 @@ #pragma once +#include #include +#include #include -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/Property.hpp" #include diff --git a/src/openvic-simulation/misc/SongChance.hpp b/src/openvic-simulation/misc/SongChance.hpp index 818dc3244..6f1a77791 100644 --- a/src/openvic-simulation/misc/SongChance.hpp +++ b/src/openvic-simulation/misc/SongChance.hpp @@ -1,6 +1,6 @@ #pragma once -#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" #include "openvic-simulation/scripts/ConditionalWeight.hpp" namespace OpenVic { diff --git a/src/openvic-simulation/misc/SoundEffect.hpp b/src/openvic-simulation/misc/SoundEffect.hpp index 628090b98..5688cb660 100644 --- a/src/openvic-simulation/misc/SoundEffect.hpp +++ b/src/openvic-simulation/misc/SoundEffect.hpp @@ -2,9 +2,9 @@ #include -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" namespace OpenVic { class Dataloader; diff --git a/src/openvic-simulation/modifier/Modifier.hpp b/src/openvic-simulation/modifier/Modifier.hpp index 165c444d4..0acb824b5 100644 --- a/src/openvic-simulation/modifier/Modifier.hpp +++ b/src/openvic-simulation/modifier/Modifier.hpp @@ -2,10 +2,10 @@ #include +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/object/Date.hpp" #include "openvic-simulation/modifier/ModifierValue.hpp" #include "openvic-simulation/scripts/ConditionScript.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/utility/Getters.hpp" namespace OpenVic { struct UnitType; diff --git a/src/openvic-simulation/modifier/ModifierEffect.cpp b/src/openvic-simulation/modifier/ModifierEffect.cpp index 13df97bf2..4ac5dfc24 100644 --- a/src/openvic-simulation/modifier/ModifierEffect.cpp +++ b/src/openvic-simulation/modifier/ModifierEffect.cpp @@ -1,6 +1,9 @@ #include "ModifierEffect.hpp" -#include "openvic-simulation/utility/StringUtils.hpp" +#include + +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/string/Utility.hpp" using namespace OpenVic; @@ -37,7 +40,7 @@ memory::string ModifierEffect::target_to_string(target_t target) { } memory::string ModifierEffect::make_default_modifier_effect_localisation_key(std::string_view identifier) { - return "MODIFIER_" + StringUtils::string_toupper(identifier); + return "MODIFIER_" + OpenVic::string_toupper(identifier); } ModifierEffect::ModifierEffect( diff --git a/src/openvic-simulation/modifier/ModifierEffect.hpp b/src/openvic-simulation/modifier/ModifierEffect.hpp index e90e3cd1f..77b3979f8 100644 --- a/src/openvic-simulation/modifier/ModifierEffect.hpp +++ b/src/openvic-simulation/modifier/ModifierEffect.hpp @@ -2,8 +2,8 @@ #include -#include "openvic-simulation/types/EnumBitfield.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" +#include "openvic-simulation/core/template/EnumBitfield.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" namespace OpenVic { struct ModifierManager; diff --git a/src/openvic-simulation/modifier/ModifierEffectCache.hpp b/src/openvic-simulation/modifier/ModifierEffectCache.hpp index 1bd6a2849..0cd5d8edd 100644 --- a/src/openvic-simulation/modifier/ModifierEffectCache.hpp +++ b/src/openvic-simulation/modifier/ModifierEffectCache.hpp @@ -1,8 +1,8 @@ #pragma once -#include "openvic-simulation/types/IndexedFlatMap.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" #include "openvic-simulation/types/UnitBranchType.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/Property.hpp" namespace OpenVic { struct ModifierEffect; diff --git a/src/openvic-simulation/modifier/ModifierManager.cpp b/src/openvic-simulation/modifier/ModifierManager.cpp index 88a6f3091..e1272368c 100644 --- a/src/openvic-simulation/modifier/ModifierManager.cpp +++ b/src/openvic-simulation/modifier/ModifierManager.cpp @@ -1,14 +1,18 @@ #include "ModifierManager.hpp" +#include +#include #include +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/memory/Format.hpp" +#include "openvic-simulation/core/memory/String.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/modifier/Modifier.hpp" #include "openvic-simulation/modifier/ModifierEffect.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; -using namespace OpenVic::StringUtils; using enum ModifierEffect::target_t; diff --git a/src/openvic-simulation/modifier/ModifierManager.hpp b/src/openvic-simulation/modifier/ModifierManager.hpp index d712ad464..e6289ff92 100644 --- a/src/openvic-simulation/modifier/ModifierManager.hpp +++ b/src/openvic-simulation/modifier/ModifierManager.hpp @@ -2,10 +2,13 @@ #include +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/memory/StringSet.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/modifier/Modifier.hpp" #include "openvic-simulation/modifier/ModifierEffectCache.hpp" #include "openvic-simulation/modifier/StaticModifierCache.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" namespace OpenVic { struct ModifierManager { @@ -31,7 +34,7 @@ namespace OpenVic { modifier_effect_registry_t IDENTIFIER_REGISTRY(base_country_modifier_effect); modifier_effect_registry_t IDENTIFIER_REGISTRY(base_province_modifier_effect); modifier_effect_registry_t IDENTIFIER_REGISTRY(terrain_modifier_effect); - case_insensitive_string_set_t complex_modifiers; + memory::case_insensitive_string_set_t complex_modifiers; IdentifierRegistry IDENTIFIER_REGISTRY(event_modifier); IdentifierRegistry IDENTIFIER_REGISTRY(triggered_modifier); diff --git a/src/openvic-simulation/modifier/ModifierSum.cpp b/src/openvic-simulation/modifier/ModifierSum.cpp index 03988fa6c..9d72d6fca 100644 --- a/src/openvic-simulation/modifier/ModifierSum.cpp +++ b/src/openvic-simulation/modifier/ModifierSum.cpp @@ -1,10 +1,14 @@ #include "ModifierSum.hpp" -#include "openvic-simulation/modifier/Modifier.hpp" +#include +#include -#include "openvic-simulation/country/CountryInstance.hpp" -#include "openvic-simulation/map/ProvinceInstance.hpp" -#include "openvic-simulation/utility/Concepts.hpp" +#include "openvic-simulation/core/memory/Format.hpp" +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/template/Concepts.hpp" +#include "openvic-simulation/country/CountryInstance.hpp" // IWYU pragma: keep +#include "openvic-simulation/map/ProvinceInstance.hpp" // IWYU pragma: keep +#include "openvic-simulation/modifier/Modifier.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/modifier/ModifierSum.hpp b/src/openvic-simulation/modifier/ModifierSum.hpp index 6960fdb99..165518813 100644 --- a/src/openvic-simulation/modifier/ModifierSum.hpp +++ b/src/openvic-simulation/modifier/ModifierSum.hpp @@ -1,13 +1,13 @@ #pragma once -#include +#include #include +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/modifier/ModifierValue.hpp" #include "openvic-simulation/modifier/Modifier.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Containers.hpp" +#include "openvic-simulation/modifier/ModifierValue.hpp" namespace OpenVic { struct CountryInstance; diff --git a/src/openvic-simulation/modifier/ModifierValue.cpp b/src/openvic-simulation/modifier/ModifierValue.cpp index f0d519bdf..271e17e63 100644 --- a/src/openvic-simulation/modifier/ModifierValue.cpp +++ b/src/openvic-simulation/modifier/ModifierValue.cpp @@ -1,6 +1,9 @@ #include "ModifierValue.hpp" -#include "openvic-simulation/utility/TslHelper.hpp" +#include + +#include "openvic-simulation/core/container/MutableIterator.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/modifier/ModifierValue.hpp b/src/openvic-simulation/modifier/ModifierValue.hpp index 104d71084..08b975eab 100644 --- a/src/openvic-simulation/modifier/ModifierValue.hpp +++ b/src/openvic-simulation/modifier/ModifierValue.hpp @@ -1,13 +1,14 @@ #pragma once +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/modifier/ModifierEffect.hpp" -#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" namespace OpenVic { struct ModifierValue { friend struct ModifierManager; - using effect_map_t = fixed_point_map_t; + using effect_map_t = memory::fixed_point_map_t; private: effect_map_t PROPERTY(values); diff --git a/src/openvic-simulation/modifier/StaticModifierCache.hpp b/src/openvic-simulation/modifier/StaticModifierCache.hpp index b30ac50eb..4d96bd444 100644 --- a/src/openvic-simulation/modifier/StaticModifierCache.hpp +++ b/src/openvic-simulation/modifier/StaticModifierCache.hpp @@ -2,7 +2,7 @@ #include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/modifier/Modifier.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/Property.hpp" #define COUNTRY_DIFFICULTY_MODIFIER_LIST(F, ID_F) \ F(very_easy_player) \ diff --git a/src/openvic-simulation/pathfinding/AStarPathing.cpp b/src/openvic-simulation/pathfinding/AStarPathing.cpp index 4cac33ae8..3a3e0f0fc 100644 --- a/src/openvic-simulation/pathfinding/AStarPathing.cpp +++ b/src/openvic-simulation/pathfinding/AStarPathing.cpp @@ -1,22 +1,22 @@ #include "openvic-simulation/pathfinding/AStarPathing.hpp" +#include + #include -#include #include #include #include +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/country/CountryInstance.hpp" #include "openvic-simulation/map/MapDefinition.hpp" #include "openvic-simulation/map/MapInstance.hpp" #include "openvic-simulation/map/ProvinceInstance.hpp" #include "openvic-simulation/military/UnitInstanceGroup.hpp" #include "openvic-simulation/pathfinding/PointMap.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/pathfinding/AStarPathing.hpp b/src/openvic-simulation/pathfinding/AStarPathing.hpp index 396f60361..8553f0eb5 100644 --- a/src/openvic-simulation/pathfinding/AStarPathing.hpp +++ b/src/openvic-simulation/pathfinding/AStarPathing.hpp @@ -7,7 +7,7 @@ #include "openvic-simulation/military/UnitType.hpp" #include "openvic-simulation/pathfinding/PathingBase.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" namespace OpenVic { struct MapInstance; diff --git a/src/openvic-simulation/pathfinding/FringePathing.cpp b/src/openvic-simulation/pathfinding/FringePathing.cpp index bdf67e635..52e90d9d5 100644 --- a/src/openvic-simulation/pathfinding/FringePathing.cpp +++ b/src/openvic-simulation/pathfinding/FringePathing.cpp @@ -1,5 +1,6 @@ #include "openvic-simulation/pathfinding/FringePathing.hpp" +#include #include #include @@ -11,9 +12,9 @@ #include #include +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/pathfinding/PointMap.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Containers.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/pathfinding/FringePathing.hpp b/src/openvic-simulation/pathfinding/FringePathing.hpp index 72123ac48..8e68c999d 100644 --- a/src/openvic-simulation/pathfinding/FringePathing.hpp +++ b/src/openvic-simulation/pathfinding/FringePathing.hpp @@ -1,7 +1,7 @@ #pragma once #include "openvic-simulation/pathfinding/PathingBase.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" namespace OpenVic { struct FringePathingNode final : public PathingNodeBase { diff --git a/src/openvic-simulation/pathfinding/PathingBase.hpp b/src/openvic-simulation/pathfinding/PathingBase.hpp index f8ababef5..eee3f823e 100644 --- a/src/openvic-simulation/pathfinding/PathingBase.hpp +++ b/src/openvic-simulation/pathfinding/PathingBase.hpp @@ -4,11 +4,11 @@ #include +#include "openvic-simulation/core/Signal.hpp" +#include "openvic-simulation/core/error/ErrorMacros.hpp" +#include "openvic-simulation/core/memory/Format.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/pathfinding/PointMap.hpp" -#include "openvic-simulation/types/Signal.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/ErrorMacros.hpp" #include #include diff --git a/src/openvic-simulation/pathfinding/PointMap.cpp b/src/openvic-simulation/pathfinding/PointMap.cpp index eb8498b10..514ed67b6 100644 --- a/src/openvic-simulation/pathfinding/PointMap.cpp +++ b/src/openvic-simulation/pathfinding/PointMap.cpp @@ -4,17 +4,15 @@ #include #include #include -#include -#include -#include +#include -#include - -#include "openvic-simulation/types/Vector.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/ErrorMacros.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/error/ErrorMacros.hpp" +#include "openvic-simulation/core/memory/Format.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/object/Vector.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/pathfinding/PointMap.hpp b/src/openvic-simulation/pathfinding/PointMap.hpp index a37536c51..dcc6e1603 100644 --- a/src/openvic-simulation/pathfinding/PointMap.hpp +++ b/src/openvic-simulation/pathfinding/PointMap.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -8,11 +9,11 @@ #include #include -#include "openvic-simulation/types/EnumBitfield.hpp" -#include "openvic-simulation/types/Signal.hpp" -#include "openvic-simulation/types/Vector.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Hash.hpp" +#include "openvic-simulation/core/Hash.hpp" +#include "openvic-simulation/core/Signal.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/object/Vector.hpp" +#include "openvic-simulation/core/template/EnumBitfield.hpp" namespace OpenVic { struct PointMap { @@ -62,8 +63,7 @@ namespace OpenVic { struct SegmentHash { inline constexpr std::size_t operator()(Segment const& segment) const { - return hash_murmur3(hash_murmur3(segment.key.first) << 32) | - hash_murmur3(segment.key.second); + return hash_murmur3(hash_murmur3(segment.key.first) << 32) | hash_murmur3(segment.key.second); } }; diff --git a/src/openvic-simulation/player/PlayerManager.hpp b/src/openvic-simulation/player/PlayerManager.hpp index b873eb6d0..011087fa7 100644 --- a/src/openvic-simulation/player/PlayerManager.hpp +++ b/src/openvic-simulation/player/PlayerManager.hpp @@ -1,7 +1,7 @@ #pragma once -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/memory/String.hpp" namespace OpenVic { struct CountryInstance; diff --git a/src/openvic-simulation/politics/BaseIssue.hpp b/src/openvic-simulation/politics/BaseIssue.hpp index 909b1a4cb..71865de77 100644 --- a/src/openvic-simulation/politics/BaseIssue.hpp +++ b/src/openvic-simulation/politics/BaseIssue.hpp @@ -1,8 +1,12 @@ #pragma once +#include + +#include "openvic-simulation/core/container/HasColour.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" #include "openvic-simulation/modifier/Modifier.hpp" #include "openvic-simulation/politics/Rule.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" namespace OpenVic { struct BaseIssueGroup; diff --git a/src/openvic-simulation/politics/Government.cpp b/src/openvic-simulation/politics/Government.cpp index 2a6a1fe18..3a9562cf8 100644 --- a/src/openvic-simulation/politics/Government.cpp +++ b/src/openvic-simulation/politics/Government.cpp @@ -1,5 +1,14 @@ #include "Government.hpp" +#include +#include +#include +#include + +#include + +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/memory/StringSet.hpp" #include "openvic-simulation/politics/Ideology.hpp" using namespace OpenVic; @@ -83,7 +92,7 @@ bool GovernmentTypeManager::load_government_types_file(IdeologyManager const& id [this, &ideology_manager, &ideologies, government_type_identifier]( std::string_view key, ast::NodeCPtr value ) -> bool { - static const string_set_t reserved_keys = { "election", "duration", "appoint_ruling_party", "flagType" }; + static const memory::string_set_t reserved_keys = { "election", "duration", "appoint_ruling_party", "flagType" }; if (reserved_keys.contains(key)) { return true; } diff --git a/src/openvic-simulation/politics/Government.hpp b/src/openvic-simulation/politics/Government.hpp index 71656d82f..38c8cf0fc 100644 --- a/src/openvic-simulation/politics/Government.hpp +++ b/src/openvic-simulation/politics/Government.hpp @@ -1,10 +1,13 @@ #pragma once -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/HasIndex.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/utility/Containers.hpp" +#include + +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/memory/StringSet.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/Date.hpp" namespace OpenVic { struct Ideology; @@ -36,7 +39,7 @@ namespace OpenVic { struct GovernmentTypeManager { private: IdentifierRegistry IDENTIFIER_REGISTRY(government_type); - string_set_t PROPERTY(flag_types); + memory::string_set_t PROPERTY(flag_types); public: bool add_government_type( diff --git a/src/openvic-simulation/politics/Ideology.cpp b/src/openvic-simulation/politics/Ideology.cpp index 2171d9059..084fe5f64 100644 --- a/src/openvic-simulation/politics/Ideology.cpp +++ b/src/openvic-simulation/politics/Ideology.cpp @@ -1,6 +1,15 @@ #include "Ideology.hpp" -#include "openvic-simulation/types/Colour.hpp" +#include +#include +#include +#include + +#include + +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/object/Date.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; diff --git a/src/openvic-simulation/politics/Ideology.hpp b/src/openvic-simulation/politics/Ideology.hpp index 888138e11..517eccc24 100644 --- a/src/openvic-simulation/politics/Ideology.hpp +++ b/src/openvic-simulation/politics/Ideology.hpp @@ -1,9 +1,16 @@ #pragma once +#include +#include + +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/HasIdentifierAndColour.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/scripts/ConditionalWeight.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/HasIndex.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" namespace OpenVic { struct IdeologyManager; diff --git a/src/openvic-simulation/politics/IssueManager.cpp b/src/openvic-simulation/politics/IssueManager.cpp index 6ad833106..e361457ad 100644 --- a/src/openvic-simulation/politics/IssueManager.cpp +++ b/src/openvic-simulation/politics/IssueManager.cpp @@ -1,7 +1,18 @@ #include "IssueManager.hpp" +#include +#include +#include +#include + +#include + +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/memory/StringSet.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/modifier/ModifierManager.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -277,7 +288,7 @@ bool IssueManager::load_issues_file( return expect_length(add_variable_callback(expected_party_policy_groups))(value); } else { - static const string_set_t uncivilised_reform_groups { + static const memory::string_set_t uncivilised_reform_groups { "economic_reforms", "education_reforms", "military_reforms" }; diff --git a/src/openvic-simulation/politics/IssueManager.hpp b/src/openvic-simulation/politics/IssueManager.hpp index 7754f6ff9..3206ffc75 100644 --- a/src/openvic-simulation/politics/IssueManager.hpp +++ b/src/openvic-simulation/politics/IssueManager.hpp @@ -2,7 +2,7 @@ #include "openvic-simulation/politics/PartyPolicy.hpp" #include "openvic-simulation/politics/Reform.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" #include "BaseIssue.hpp" namespace OpenVic { diff --git a/src/openvic-simulation/politics/NationalFocus.cpp b/src/openvic-simulation/politics/NationalFocus.cpp index f2bc09950..04df51e45 100644 --- a/src/openvic-simulation/politics/NationalFocus.cpp +++ b/src/openvic-simulation/politics/NationalFocus.cpp @@ -4,7 +4,7 @@ #include "openvic-simulation/modifier/ModifierManager.hpp" #include "openvic-simulation/politics/Ideology.hpp" #include "openvic-simulation/pop/PopManager.hpp" -#include "openvic-simulation/utility/FormatValidate.hpp" +#include "openvic-simulation/core/FormatValidate.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -23,8 +23,8 @@ NationalFocus::NationalFocus( Ideology const* new_loyalty_ideology, fixed_point_t new_loyalty_value, fixed_point_t new_encourage_railroads, - fixed_point_map_t&& new_encourage_goods, - fixed_point_map_t&& new_encourage_pop_types, + memory::fixed_point_map_t&& new_encourage_goods, + memory::fixed_point_map_t&& new_encourage_pop_types, ConditionScript&& new_limit ) : Modifier { new_identifier, std::move(new_modifiers), modifier_type_t::NATIONAL_FOCUS }, group { new_group }, @@ -66,8 +66,8 @@ inline bool NationalFocusManager::add_national_focus( Ideology const* loyalty_ideology, fixed_point_t loyalty_value, fixed_point_t encourage_railroads, - fixed_point_map_t&& encourage_goods, - fixed_point_map_t&& encourage_pop_types, + memory::fixed_point_map_t&& encourage_goods, + memory::fixed_point_map_t&& encourage_pop_types, ConditionScript&& limit ) { spdlog::scope scope { fmt::format("national focus {}", identifier) }; @@ -132,8 +132,8 @@ bool NationalFocusManager::load_national_foci_file( Ideology const* loyalty_ideology = nullptr; fixed_point_t loyalty_value = 0; fixed_point_t encourage_railroads = 0; - fixed_point_map_t encourage_goods; - fixed_point_map_t encourage_pop_types; + memory::fixed_point_map_t encourage_goods; + memory::fixed_point_map_t encourage_pop_types; ConditionScript limit { PROVINCE | COUNTRY, PROVINCE | COUNTRY, NO_SCOPE }; diff --git a/src/openvic-simulation/politics/NationalFocus.hpp b/src/openvic-simulation/politics/NationalFocus.hpp index 879b8f38e..f88b21777 100644 --- a/src/openvic-simulation/politics/NationalFocus.hpp +++ b/src/openvic-simulation/politics/NationalFocus.hpp @@ -2,9 +2,9 @@ #include "openvic-simulation/modifier/Modifier.hpp" #include "openvic-simulation/scripts/ConditionScript.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/Property.hpp" namespace OpenVic { struct NationalFocusManager; @@ -31,8 +31,8 @@ namespace OpenVic { Ideology const* PROPERTY(loyalty_ideology); fixed_point_t PROPERTY(loyalty_value); fixed_point_t PROPERTY(encourage_railroads); - fixed_point_map_t PROPERTY(encourage_goods); - fixed_point_map_t PROPERTY(encourage_pop_types); + memory::fixed_point_map_t PROPERTY(encourage_goods); + memory::fixed_point_map_t PROPERTY(encourage_pop_types); ConditionScript PROPERTY(limit); bool parse_scripts(DefinitionManager const& definition_manager); @@ -50,8 +50,8 @@ namespace OpenVic { Ideology const* new_loyalty_ideology, fixed_point_t new_loyalty_value, fixed_point_t new_encourage_railroads, - fixed_point_map_t&& new_encourage_goods, - fixed_point_map_t&& new_encourage_pop_types, + memory::fixed_point_map_t&& new_encourage_goods, + memory::fixed_point_map_t&& new_encourage_pop_types, ConditionScript&& new_limit ); NationalFocus(NationalFocus&&) = default; @@ -81,8 +81,8 @@ namespace OpenVic { Ideology const* loyalty_ideology, fixed_point_t loyalty_value, fixed_point_t encourage_railroads, - fixed_point_map_t&& encourage_goods, - fixed_point_map_t&& encourage_pop_types, + memory::fixed_point_map_t&& encourage_goods, + memory::fixed_point_map_t&& encourage_pop_types, ConditionScript&& limit ); diff --git a/src/openvic-simulation/politics/NationalValue.hpp b/src/openvic-simulation/politics/NationalValue.hpp index e7b5bb0a8..925c9f8cc 100644 --- a/src/openvic-simulation/politics/NationalValue.hpp +++ b/src/openvic-simulation/politics/NationalValue.hpp @@ -1,7 +1,7 @@ #pragma once #include "openvic-simulation/modifier/Modifier.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" namespace OpenVic { struct NationalValueManager; diff --git a/src/openvic-simulation/politics/PartyPolicy.hpp b/src/openvic-simulation/politics/PartyPolicy.hpp index b110f5a2f..a149cf7f7 100644 --- a/src/openvic-simulation/politics/PartyPolicy.hpp +++ b/src/openvic-simulation/politics/PartyPolicy.hpp @@ -1,7 +1,7 @@ #pragma once #include "openvic-simulation/politics/BaseIssue.hpp" -#include "openvic-simulation/types/HasIndex.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" namespace OpenVic { struct PartyPolicy; diff --git a/src/openvic-simulation/politics/PoliticsInstanceManager.hpp b/src/openvic-simulation/politics/PoliticsInstanceManager.hpp index 53451fc85..65c2d13a5 100644 --- a/src/openvic-simulation/politics/PoliticsInstanceManager.hpp +++ b/src/openvic-simulation/politics/PoliticsInstanceManager.hpp @@ -2,9 +2,9 @@ #include -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/IndexedFlatMap.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/Property.hpp" namespace OpenVic { struct InstanceManager; diff --git a/src/openvic-simulation/politics/Rebel.cpp b/src/openvic-simulation/politics/Rebel.cpp index 75cbb331c..8e774f5e5 100644 --- a/src/openvic-simulation/politics/Rebel.cpp +++ b/src/openvic-simulation/politics/Rebel.cpp @@ -1,10 +1,18 @@ #include "Rebel.hpp" +#include #include +#include +#include + +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/memory/Format.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/modifier/ModifierManager.hpp" #include "openvic-simulation/politics/Government.hpp" #include "openvic-simulation/politics/Ideology.hpp" -#include "openvic-simulation/modifier/ModifierManager.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -76,7 +84,7 @@ bool RebelManager::load_rebels_file( ) { spdlog::scope scope { "common/rebel_types.txt" }; - static const string_map_t area_map = { + static const memory::string_map_t area_map = { { "nation", RebelType::area_t::NATION }, { "nation_religion", RebelType::area_t::NATION_RELIGION }, { "nation_culture", RebelType::area_t::NATION_CULTURE }, @@ -86,7 +94,7 @@ bool RebelManager::load_rebels_file( { "all", RebelType::area_t::ALL } }; - static const string_map_t defection_map = { + static const memory::string_map_t defection_map = { { "none", RebelType::defection_t::NONE }, { "culture", RebelType::defection_t::CULTURE }, { "culture_group", RebelType::defection_t::CULTURE_GROUP }, @@ -96,7 +104,7 @@ bool RebelManager::load_rebels_file( { "any", RebelType::defection_t::ANY } }; - static const string_map_t independence_map = { + static const memory::string_map_t independence_map = { { "none", RebelType::independence_t::NONE }, { "culture", RebelType::independence_t::CULTURE }, { "culture_group", RebelType::independence_t::CULTURE_GROUP }, diff --git a/src/openvic-simulation/politics/Rebel.hpp b/src/openvic-simulation/politics/Rebel.hpp index 432f59478..084d76ca3 100644 --- a/src/openvic-simulation/politics/Rebel.hpp +++ b/src/openvic-simulation/politics/Rebel.hpp @@ -1,13 +1,16 @@ #pragma once #include +#include +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/scripts/ConditionalWeight.hpp" #include "openvic-simulation/scripts/EffectScript.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/HasIndex.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" namespace OpenVic { struct GovernmentType; @@ -20,7 +23,7 @@ namespace OpenVic { struct RebelType : HasIndex, HasIdentifier { friend struct RebelManager; - using government_map_t = ordered_map; + using government_map_t = memory::ordered_map; using icon_t = uint16_t; enum class area_t { diff --git a/src/openvic-simulation/politics/Reform.hpp b/src/openvic-simulation/politics/Reform.hpp index 689a4ccd7..a18d24d92 100644 --- a/src/openvic-simulation/politics/Reform.hpp +++ b/src/openvic-simulation/politics/Reform.hpp @@ -3,8 +3,8 @@ #include "openvic-simulation/politics/BaseIssue.hpp" #include "openvic-simulation/scripts/ConditionScript.hpp" #include "openvic-simulation/scripts/EffectScript.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/HasIndex.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" namespace OpenVic { struct ReformGroup; diff --git a/src/openvic-simulation/politics/Rule.cpp b/src/openvic-simulation/politics/Rule.cpp index c3858a0b8..ddb2f64e4 100644 --- a/src/openvic-simulation/politics/Rule.cpp +++ b/src/openvic-simulation/politics/Rule.cpp @@ -1,16 +1,25 @@ #include "Rule.hpp" +#include +#include +#include +#include + +#include "openvic-simulation/core/FormatValidate.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/string/Utility.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/economy/BuildingType.hpp" -#include "openvic-simulation/utility/TslHelper.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/FormatValidate.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; static memory::string make_default_rule_localisation_key(std::string_view identifier) { - return "RULE_" + StringUtils::string_toupper(identifier); + return "RULE_" + OpenVic::string_toupper(identifier); } Rule::Rule(std::string_view new_identifier, rule_group_t new_group, index_t new_index, std::string_view new_localisation_key) @@ -156,7 +165,7 @@ bool RuleManager::setup_rules(BuildingTypeManager const& building_type_manager) using enum Rule::rule_group_t; - static const ordered_map> hardcoded_rules { + static const memory::ordered_map> hardcoded_rules { { ECONOMY, { "build_railway", "build_factory", "expand_factory", "open_factory", "destroy_factory", "pop_build_factory", "pop_expand_factory", "pop_open_factory", "can_subsidise", "factory_priority", "delete_factory_if_no_input", @@ -221,7 +230,7 @@ namespace OpenVic { // so the compiler shuts up (copied from Modifier.cpp) std::ostream& operator<<(std::ostream& stream, RuleSet const& value) { for (auto const& [group, rule_map] : value.rule_groups) { for (auto const& [rule, value] : rule_map) { - stream << rule << ": " << StringUtils::bool_to_yes_no(value) << "\n"; + stream << rule << ": " << bool_to_yes_no(value) << "\n"; } } return stream; diff --git a/src/openvic-simulation/politics/Rule.hpp b/src/openvic-simulation/politics/Rule.hpp index 2c174903a..e9cb5c6e7 100644 --- a/src/openvic-simulation/politics/Rule.hpp +++ b/src/openvic-simulation/politics/Rule.hpp @@ -1,9 +1,12 @@ #pragma once -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/HasIndex.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" +#include +#include + +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" namespace OpenVic { struct RuleManager; @@ -40,8 +43,8 @@ namespace OpenVic { struct RuleSet { friend struct RuleManager; - using rule_map_t = ordered_map; - using rule_group_map_t = ordered_map; + using rule_map_t = memory::ordered_map; + using rule_group_map_t = memory::ordered_map; private: rule_group_map_t PROPERTY(rule_groups); @@ -80,7 +83,7 @@ namespace OpenVic { struct RuleManager { private: IdentifierRegistry IDENTIFIER_REGISTRY(rule); - ordered_map rule_group_sizes; + memory::ordered_map rule_group_sizes; public: bool add_rule(std::string_view identifier, Rule::rule_group_t group, std::string_view localisation_key = {}); diff --git a/src/openvic-simulation/pop/Culture.cpp b/src/openvic-simulation/pop/Culture.cpp index 60c844164..74b9bfcd4 100644 --- a/src/openvic-simulation/pop/Culture.cpp +++ b/src/openvic-simulation/pop/Culture.cpp @@ -1,12 +1,19 @@ #include "Culture.hpp" + +#include +#include +#include #include +#include +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/memory/StringSet.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/country/CountryDefinition.hpp" #include "openvic-simulation/dataloader/Dataloader.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/types/Colour.hpp" -#include "openvic-simulation/utility/StringUtils.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -206,7 +213,7 @@ bool CultureManager::load_culture_file(CountryDefinitionManager const& country_d [this, &country_definition_manager](CultureGroup const& culture_group, ast::NodeCPtr culture_group_value) -> bool { return expect_dictionary( [this, &country_definition_manager, &culture_group](std::string_view key, ast::NodeCPtr value) -> bool { - static const string_set_t reserved_keys = { "leader", "unit", "union", "is_overseas" }; + static const memory::string_set_t reserved_keys = { "leader", "unit", "union", "is_overseas" }; if (reserved_keys.contains(key)) { return true; } @@ -246,7 +253,7 @@ memory::string CultureManager::make_leader_picture_name( return {}; } - return StringUtils::append_string_views(cultural_type, *branch_text, std::to_string(count)); + return OpenVic::append_string_views(cultural_type, *branch_text, fmt::to_string(count)); } memory::string CultureManager::make_leader_picture_path(std::string_view leader_picture_name) { diff --git a/src/openvic-simulation/pop/Culture.hpp b/src/openvic-simulation/pop/Culture.hpp index 84253fc44..3f3c1ac84 100644 --- a/src/openvic-simulation/pop/Culture.hpp +++ b/src/openvic-simulation/pop/Culture.hpp @@ -1,8 +1,18 @@ #pragma once -#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include +#include +#include + +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/HasIdentifierAndColour.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/types/UnitBranchType.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" namespace OpenVic { struct CultureManager; @@ -72,7 +82,7 @@ namespace OpenVic { using general_admiral_picture_count_t = std::pair; // Cultural type string maps to (general picture count, admiral picture count) pair - string_map_t leader_picture_counts; + memory::string_map_t leader_picture_counts; bool _load_culture_group( CountryDefinitionManager const& country_definition_manager, size_t& total_expected_cultures, diff --git a/src/openvic-simulation/pop/Pop.cpp b/src/openvic-simulation/pop/Pop.cpp index 6d0225441..490999ce1 100644 --- a/src/openvic-simulation/pop/Pop.cpp +++ b/src/openvic-simulation/pop/Pop.cpp @@ -4,11 +4,20 @@ #include // IWYU pragma: keep for lambda #include #include +#include +#include #include -#include "openvic-simulation/country/CountryParty.hpp" +#include "openvic-simulation/core/FormatValidate.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/Typedefs.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/country/CountryDefinition.hpp" #include "openvic-simulation/country/CountryInstance.hpp" +#include "openvic-simulation/country/CountryParty.hpp" #include "openvic-simulation/defines/Define.hpp" #include "openvic-simulation/economy/GoodDefinition.hpp" #include "openvic-simulation/economy/GoodInstance.hpp" @@ -16,8 +25,8 @@ #include "openvic-simulation/economy/production/ProductionType.hpp" #include "openvic-simulation/economy/trading/BuyResult.hpp" #include "openvic-simulation/economy/trading/BuyUpToOrder.hpp" -#include "openvic-simulation/economy/trading/MarketSellOrder.hpp" #include "openvic-simulation/economy/trading/MarketInstance.hpp" +#include "openvic-simulation/economy/trading/MarketSellOrder.hpp" #include "openvic-simulation/economy/trading/SellResult.hpp" #include "openvic-simulation/map/ProvinceInstance.hpp" #include "openvic-simulation/modifier/ModifierEffectCache.hpp" @@ -28,14 +37,6 @@ #include "openvic-simulation/pop/PopType.hpp" #include "openvic-simulation/pop/PopValuesFromProvince.hpp" #include "openvic-simulation/pop/Religion.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" -#include "openvic-simulation/types/IndexedFlatMap.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/FormatValidate.hpp" -#include "openvic-simulation/utility/Logger.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" using namespace OpenVic; @@ -93,7 +94,7 @@ void Pop::setup_pop_test_values(IssueManager const& issue_manager) { map.set(key, value); } }; - auto test_weight_ordered = [](ordered_map& map, U const& key, int32_t min, int32_t max) -> void { + auto test_weight_ordered = [](memory::ordered_map& map, U const& key, int32_t min, int32_t max) -> void { if constexpr (std::is_convertible_v || std::is_convertible_v) { const int32_t value = rand() % (max + 1); if (value >= min) { @@ -125,14 +126,14 @@ void Pop::setup_pop_test_values(IssueManager const& issue_manager) { test_weight_ordered(supporter_equivalents_by_issue, reform, 3, 6); } } - rescale_fixed_point_map(supporter_equivalents_by_issue, size); + memory::rescale_fixed_point_map(supporter_equivalents_by_issue, size); if (!vote_equivalents_by_party.empty()) { for (auto& [party, value] : vote_equivalents_by_party) { vote_equivalents_by_party[party] = 0; test_weight_ordered(vote_equivalents_by_party, party, 4, 10); } - rescale_fixed_point_map(vote_equivalents_by_party, size); + memory::rescale_fixed_point_map(vote_equivalents_by_party, size); } /* Returns a fixed point between 0 and max. */ @@ -385,8 +386,8 @@ OV_DO_FOR_ALL_NEED_CATEGORIES(DEFINE_NEEDS_FULFILLED) #undef DEFINE_NEEDS_FULFILLED void Pop::allocate_for_needs( - fixed_point_map_t const& scaled_needs, - utility::forwardable_span money_to_spend_per_good, + memory::fixed_point_map_t const& scaled_needs, + forwardable_span money_to_spend_per_good, memory::vector& reusable_vector, fixed_point_t& weights_sum, fixed_point_t& cash_left_to_spend @@ -455,7 +456,7 @@ void Pop::pop_tick( PopValuesFromProvince const& shared_values, RandomU32& random_number_generator, IndexedFlatMap& reusable_goods_mask, - utility::forwardable_span< + forwardable_span< memory::vector, VECTORS_FOR_POP_TICK > reusable_vectors @@ -475,12 +476,12 @@ void Pop::pop_tick_without_cleanup( PopValuesFromProvince const& shared_values, RandomU32& random_number_generator, IndexedFlatMap& reusable_goods_mask, - utility::forwardable_span< + forwardable_span< memory::vector, VECTORS_FOR_POP_TICK > reusable_vectors ) { - utility::forwardable_span good_keys = reusable_goods_mask.get_keys(); + forwardable_span good_keys = reusable_goods_mask.get_keys(); memory::vector& reusable_vector_0 = reusable_vectors[0]; memory::vector& reusable_vector_1 = reusable_vectors[1]; memory::vector& max_quantity_to_buy_per_good = reusable_vectors[2]; @@ -677,7 +678,7 @@ void Pop::after_buy(void* actor, BuyResult const& buy_result) { if (quantity_left_to_consume <= 0) { \ return; \ } \ - const fixed_point_map_t::const_iterator need_category##it = pop.need_category##_needs.find(&good_definition); \ + const memory::fixed_point_map_t::const_iterator need_category##it = pop.need_category##_needs.find(&good_definition); \ if (need_category##it != pop.need_category##_needs.end()) { \ const fixed_point_t desired_quantity = need_category##it->second; \ fixed_point_t consumed_quantity; \ diff --git a/src/openvic-simulation/pop/Pop.hpp b/src/openvic-simulation/pop/Pop.hpp index a36a80ea4..4e81a1d8e 100644 --- a/src/openvic-simulation/pop/Pop.hpp +++ b/src/openvic-simulation/pop/Pop.hpp @@ -1,15 +1,15 @@ #pragma once +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/object/FixedPoint/Atomic.hpp" +#include "openvic-simulation/core/portable/ForwardableSpan.hpp" #include "openvic-simulation/economy/production/ArtisanalProducer.hpp" #include "openvic-simulation/pop/PopNeedsMacro.hpp" -#include "openvic-simulation/types/fixed_point/Atomic.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" -#include "openvic-simulation/types/IndexedFlatMap.hpp" -#include "openvic-simulation/types/PopSize.hpp" #include "openvic-simulation/types/UnitBranchType.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/ForwardableSpan.hpp" namespace OpenVic { struct BaseIssue; @@ -111,8 +111,8 @@ namespace OpenVic { // added together with automatic weighting based on their relative sizes. Similarly, the province, state and country // equivalents of these distributions will have a total size equal to their total population size. OV_IFLATMAP_PROPERTY(Ideology, fixed_point_t, supporter_equivalents_by_ideology); - fixed_point_map_t PROPERTY(supporter_equivalents_by_issue); - fixed_point_map_t PROPERTY(vote_equivalents_by_party); + memory::fixed_point_map_t PROPERTY(supporter_equivalents_by_issue); + memory::fixed_point_map_t PROPERTY(vote_equivalents_by_party); public: // The values returned by these functions are scaled by pop size, so they must be divided by pop size to get @@ -139,8 +139,8 @@ namespace OpenVic { public: \ fixed_point_t get_##need_category##_needs_fulfilled() const; \ private: \ - fixed_point_map_t PROPERTY(need_category##_needs); /* TODO pool? (if recalculating in UI is acceptable) */ \ - ordered_map PROPERTY(need_category##_needs_fulfilled_goods); + memory::fixed_point_map_t PROPERTY(need_category##_needs); /* TODO pool? (if recalculating in UI is acceptable) */ \ + memory::ordered_map PROPERTY(need_category##_needs_fulfilled_goods); OV_DO_FOR_ALL_NEED_CATEGORIES(NEED_MEMBERS) #undef NEED_MEMBERS @@ -165,8 +165,8 @@ namespace OpenVic { void reserve_needs_fulfilled_goods(); void fill_needs_fulfilled_goods_with_false(); void allocate_for_needs( - fixed_point_map_t const& scaled_needs, - utility::forwardable_span money_to_spend_per_good, + memory::fixed_point_map_t const& scaled_needs, + forwardable_span money_to_spend_per_good, memory::vector& reusable_vector, fixed_point_t& price_inverse_sum, fixed_point_t& cash_left_to_spend @@ -175,7 +175,7 @@ namespace OpenVic { PopValuesFromProvince const& shared_values, RandomU32& random_number_generator, IndexedFlatMap& reusable_goods_mask, - utility::forwardable_span< + forwardable_span< memory::vector, VECTORS_FOR_POP_TICK > reusable_vectors @@ -215,7 +215,7 @@ namespace OpenVic { PopValuesFromProvince const& shared_values, RandomU32& random_number_generator, IndexedFlatMap& reusable_goods_mask, - utility::forwardable_span< + forwardable_span< memory::vector, VECTORS_FOR_POP_TICK > reusable_vectors diff --git a/src/openvic-simulation/pop/PopManager.cpp b/src/openvic-simulation/pop/PopManager.cpp index ef8cedc51..293890a5b 100644 --- a/src/openvic-simulation/pop/PopManager.cpp +++ b/src/openvic-simulation/pop/PopManager.cpp @@ -1,16 +1,28 @@ #include "PopManager.hpp" +#include +#include +#include +#include + #include +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/FormatValidate.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/string/Utility.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/economy/GoodDefinition.hpp" +#include "openvic-simulation/military/UnitType.hpp" +#include "openvic-simulation/modifier/ModifierManager.hpp" #include "openvic-simulation/politics/Ideology.hpp" #include "openvic-simulation/politics/IssueManager.hpp" #include "openvic-simulation/politics/Rebel.hpp" #include "openvic-simulation/pop/Pop.hpp" -#include "openvic-simulation/military/UnitType.hpp" -#include "openvic-simulation/modifier/ModifierManager.hpp" -#include "openvic-simulation/utility/FormatValidate.hpp" -#include "openvic-simulation/utility/Logger.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -72,9 +84,9 @@ bool PopManager::add_pop_type( colour_t colour, Strata* strata, pop_sprite_t sprite, - fixed_point_map_t&& life_needs, - fixed_point_map_t&& everyday_needs, - fixed_point_map_t&& luxury_needs, + memory::fixed_point_map_t&& life_needs, + memory::fixed_point_map_t&& everyday_needs, + memory::fixed_point_map_t&& luxury_needs, PopType::income_type_t life_needs_income_types, PopType::income_type_t everyday_needs_income_types, PopType::income_type_t luxury_needs_income_types, @@ -220,7 +232,7 @@ void PopManager::reserve_pop_types_and_delayed_nodes(size_t size) { static NodeCallback auto expect_needs_income(PopType::income_type_t& types) { using enum PopType::income_type_t; - static const string_map_t income_type_map { + static const memory::string_map_t income_type_map { { "administration", ADMINISTRATION }, { "education", EDUCATION }, { "military", MILITARY } @@ -258,7 +270,7 @@ bool PopManager::load_pop_type_file( colour_t colour = colour_t::null(); Strata* strata = default_strata; pop_sprite_t sprite = 0; - fixed_point_map_t life_needs, everyday_needs, luxury_needs; + memory::fixed_point_map_t life_needs, everyday_needs, luxury_needs; PopType::income_type_t life_needs_income_types = NO_INCOME_TYPE, everyday_needs_income_types = NO_INCOME_TYPE, luxury_needs_income_types = NO_INCOME_TYPE; ast::NodeCPtr rebel_units = nullptr; @@ -502,7 +514,7 @@ bool PopManager::generate_modifiers(ModifierManager& modifier_manager) const { bool has_no_effect = false ) -> void { ret &= modifier_manager.register_base_country_modifier_effect( - effect_cache, StringUtils::append_string_views(strata.get_identifier(), suffix), format, {}, has_no_effect + effect_cache, OpenVic::append_string_views(strata.get_identifier(), suffix), format, {}, has_no_effect ); }; diff --git a/src/openvic-simulation/pop/PopManager.hpp b/src/openvic-simulation/pop/PopManager.hpp index 1d02f26c1..d3f7b89a5 100644 --- a/src/openvic-simulation/pop/PopManager.hpp +++ b/src/openvic-simulation/pop/PopManager.hpp @@ -55,9 +55,9 @@ namespace OpenVic { colour_t new_colour, Strata* strata, pop_sprite_t sprite, - fixed_point_map_t&& life_needs, - fixed_point_map_t&& everyday_needs, - fixed_point_map_t&& luxury_needs, + memory::fixed_point_map_t&& life_needs, + memory::fixed_point_map_t&& everyday_needs, + memory::fixed_point_map_t&& luxury_needs, PopType::income_type_t life_needs_income_types, PopType::income_type_t everyday_needs_income_types, PopType::income_type_t luxury_needs_income_types, diff --git a/src/openvic-simulation/pop/PopType.cpp b/src/openvic-simulation/pop/PopType.cpp index c17778fa2..f0e89a125 100644 --- a/src/openvic-simulation/pop/PopType.cpp +++ b/src/openvic-simulation/pop/PopType.cpp @@ -1,10 +1,13 @@ #include "PopType.hpp" #include +#include +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/country/CountryDefinition.hpp" #include "openvic-simulation/map/ProvinceInstance.hpp" -#include "openvic-simulation/utility/TslHelper.hpp" using namespace OpenVic; @@ -17,9 +20,9 @@ PopType::PopType( index_t new_index, Strata const& new_strata, pop_sprite_t new_sprite, - fixed_point_map_t&& new_life_needs, - fixed_point_map_t&& new_everyday_needs, - fixed_point_map_t&& new_luxury_needs, + memory::fixed_point_map_t&& new_life_needs, + memory::fixed_point_map_t&& new_everyday_needs, + memory::fixed_point_map_t&& new_luxury_needs, income_type_t new_life_needs_income_types, income_type_t new_everyday_needs_income_types, income_type_t new_luxury_needs_income_types, diff --git a/src/openvic-simulation/pop/PopType.hpp b/src/openvic-simulation/pop/PopType.hpp index 0b85cd2b8..663532d94 100644 --- a/src/openvic-simulation/pop/PopType.hpp +++ b/src/openvic-simulation/pop/PopType.hpp @@ -1,14 +1,22 @@ #pragma once +#include +#include +#include +#include + +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/HasIdentifierAndColour.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/template/EnumBitfield.hpp" #include "openvic-simulation/scripts/ConditionalWeight.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/HasIndex.hpp" -#include "openvic-simulation/types/IndexedFlatMap.hpp" -#include "openvic-simulation/types/PopSize.hpp" -#include "openvic-simulation/types/PopSprite.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { struct BaseIssue; @@ -43,17 +51,17 @@ namespace OpenVic { MILITARY = 1 << 2 }; - using rebel_units_t = fixed_point_map_t; + using rebel_units_t = memory::fixed_point_map_t; using poptype_weight_map_t = IndexedFlatMap; using ideology_weight_map_t = IndexedFlatMap; - using issue_weight_map_t = ordered_map; + using issue_weight_map_t = memory::ordered_map; private: Strata const& PROPERTY(strata); const pop_sprite_t PROPERTY(sprite); - fixed_point_map_t PROPERTY(life_needs); - fixed_point_map_t PROPERTY(everyday_needs); - fixed_point_map_t PROPERTY(luxury_needs); + memory::fixed_point_map_t PROPERTY(life_needs); + memory::fixed_point_map_t PROPERTY(everyday_needs); + memory::fixed_point_map_t PROPERTY(luxury_needs); income_type_t PROPERTY(life_needs_income_types); income_type_t PROPERTY(everyday_needs_income_types); income_type_t PROPERTY(luxury_needs_income_types); @@ -93,9 +101,9 @@ namespace OpenVic { index_t new_index, Strata const& new_strata, pop_sprite_t new_sprite, - fixed_point_map_t&& new_life_needs, - fixed_point_map_t&& new_everyday_needs, - fixed_point_map_t&& new_luxury_needs, + memory::fixed_point_map_t&& new_life_needs, + memory::fixed_point_map_t&& new_everyday_needs, + memory::fixed_point_map_t&& new_luxury_needs, income_type_t new_life_needs_income_types, income_type_t new_everyday_needs_income_types, income_type_t new_luxury_needs_income_types, diff --git a/src/openvic-simulation/pop/PopValuesFromProvince.cpp b/src/openvic-simulation/pop/PopValuesFromProvince.cpp index 9b2e4c3f4..4e68bfe21 100644 --- a/src/openvic-simulation/pop/PopValuesFromProvince.cpp +++ b/src/openvic-simulation/pop/PopValuesFromProvince.cpp @@ -9,7 +9,7 @@ #include "openvic-simulation/map/ProvinceInstance.hpp" #include "openvic-simulation/misc/GameRulesManager.hpp" #include "openvic-simulation/pop/PopType.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/pop/PopValuesFromProvince.hpp b/src/openvic-simulation/pop/PopValuesFromProvince.hpp index 3a6ab0676..45fc1d808 100644 --- a/src/openvic-simulation/pop/PopValuesFromProvince.hpp +++ b/src/openvic-simulation/pop/PopValuesFromProvince.hpp @@ -1,8 +1,8 @@ #pragma once -#include "openvic-simulation/types/IndexedFlatMap.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/Property.hpp" namespace OpenVic { struct GameRulesManager; diff --git a/src/openvic-simulation/pop/PopsAggregate.cpp b/src/openvic-simulation/pop/PopsAggregate.cpp index 425d12bf7..b3b65adc9 100644 --- a/src/openvic-simulation/pop/PopsAggregate.cpp +++ b/src/openvic-simulation/pop/PopsAggregate.cpp @@ -1,12 +1,14 @@ #include "PopsAggregate.hpp" +#include + +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/country/CountryDefinition.hpp" #include "openvic-simulation/country/CountryInstance.hpp" #include "openvic-simulation/politics/Ideology.hpp" #include "openvic-simulation/pop/Pop.hpp" #include "openvic-simulation/pop/PopType.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/OrderedContainersMath.hpp" using namespace OpenVic; PopsAggregate::PopsAggregate( @@ -205,7 +207,7 @@ void PopsAggregate::update_parties_for_votes(CountryDefinition const* country_de return; } - auto view = country_definition->get_parties() | std::views::transform( + auto view = country_definition->get_parties() | ranges::views::transform( [](CountryParty const& key) { return std::make_pair(&key, fixed_point_t::_0); } diff --git a/src/openvic-simulation/pop/PopsAggregate.hpp b/src/openvic-simulation/pop/PopsAggregate.hpp index 65f9af0a8..06dcc08f0 100644 --- a/src/openvic-simulation/pop/PopsAggregate.hpp +++ b/src/openvic-simulation/pop/PopsAggregate.hpp @@ -1,12 +1,12 @@ #pragma once -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" -#include "openvic-simulation/types/IndexedFlatMap.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/types/PopSize.hpp" -#include "openvic-simulation/utility/Getters.hpp" -#include "openvic-simulation/utility/reactive/MutableState.hpp" +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/container/IndexedFlatMap.hpp" +#include "openvic-simulation/core/memory/FixedPointMap.hpp" +#include "openvic-simulation/core/memory/OrderedMap.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/reactive/MutableState.hpp" namespace OpenVic { struct BaseIssue; @@ -41,10 +41,10 @@ namespace OpenVic { OV_IFLATMAP_PROPERTY(PopType, pop_size_t, population_by_type); OV_IFLATMAP_PROPERTY(PopType, pop_size_t, unemployed_pops_by_type); OV_IFLATMAP_PROPERTY(Ideology, fixed_point_t, supporter_equivalents_by_ideology); - fixed_point_map_t PROPERTY(supporter_equivalents_by_issue); - fixed_point_map_t PROPERTY(vote_equivalents_by_party); - ordered_map PROPERTY(population_by_culture); - ordered_map PROPERTY(population_by_religion); + memory::fixed_point_map_t PROPERTY(supporter_equivalents_by_issue); + memory::fixed_point_map_t PROPERTY(vote_equivalents_by_party); + memory::ordered_map PROPERTY(population_by_culture); + memory::ordered_map PROPERTY(population_by_religion); protected: PopsAggregate( diff --git a/src/openvic-simulation/pop/Religion.cpp b/src/openvic-simulation/pop/Religion.cpp index 49cd2f4f0..35024ca1d 100644 --- a/src/openvic-simulation/pop/Religion.cpp +++ b/src/openvic-simulation/pop/Religion.cpp @@ -1,6 +1,8 @@ #include "Religion.hpp" -#include "openvic-simulation/types/Colour.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; diff --git a/src/openvic-simulation/pop/Religion.hpp b/src/openvic-simulation/pop/Religion.hpp index 42ee48a1c..979480b46 100644 --- a/src/openvic-simulation/pop/Religion.hpp +++ b/src/openvic-simulation/pop/Religion.hpp @@ -1,8 +1,13 @@ #pragma once +#include +#include + +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/HasIdentifierAndColour.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/object/Colour.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" namespace OpenVic { struct ReligionGroup : HasIdentifier { diff --git a/src/openvic-simulation/research/Invention.hpp b/src/openvic-simulation/research/Invention.hpp index e85b0c64c..e7a3b5413 100644 --- a/src/openvic-simulation/research/Invention.hpp +++ b/src/openvic-simulation/research/Invention.hpp @@ -1,10 +1,13 @@ #pragma once +#include + +#include "openvic-simulation/core/Property.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/memory/OrderedSet.hpp" #include "openvic-simulation/modifier/Modifier.hpp" #include "openvic-simulation/scripts/ConditionalWeight.hpp" -#include "openvic-simulation/types/HasIndex.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" namespace OpenVic { @@ -20,9 +23,9 @@ namespace OpenVic { friend struct InventionManager; //TODO implement limit and chance - using unit_set_t = ordered_set; - using building_set_t = ordered_set; - using crime_set_t = ordered_set; + using unit_set_t = memory::ordered_set; + using building_set_t = memory::ordered_set; + using crime_set_t = memory::ordered_set; private: const bool PROPERTY_CUSTOM_PREFIX(news, is); diff --git a/src/openvic-simulation/research/Technology.hpp b/src/openvic-simulation/research/Technology.hpp index fdb14b2b2..fbe6cb464 100644 --- a/src/openvic-simulation/research/Technology.hpp +++ b/src/openvic-simulation/research/Technology.hpp @@ -1,15 +1,19 @@ #pragma once #include - +#include +#include + +#include "openvic-simulation/Alias.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/HasIndex.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/memory/OrderedSet.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/modifier/Modifier.hpp" #include "openvic-simulation/scripts/ConditionalWeight.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/HasIndex.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/types/UnitVariant.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { struct BuildingType; @@ -47,8 +51,8 @@ namespace OpenVic { friend struct TechnologyManager; using area_index_t = uint8_t; - using unit_set_t = ordered_set; - using building_set_t = ordered_set; + using unit_set_t = memory::ordered_set; + using building_set_t = memory::ordered_set; private: TechnologyArea const& PROPERTY(area); diff --git a/src/openvic-simulation/scripts/Condition.cpp b/src/openvic-simulation/scripts/Condition.cpp index 141556e54..1f03fab09 100644 --- a/src/openvic-simulation/scripts/Condition.cpp +++ b/src/openvic-simulation/scripts/Condition.cpp @@ -1,10 +1,21 @@ #include "Condition.hpp" +#include +#include +#include + +#include + +#include + #include -#include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/DefinitionManager.hpp" -#include "openvic-simulation/utility/Containers.hpp" +#include "openvic-simulation/core/Logger.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/memory/StringSet.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -475,7 +486,7 @@ callback_t ConditionManager::expect_parse_identifier( return callback(identified); \ } __VA_OPT__(else { \ /* TODO: the set is just a placeholder for actual logic */ \ - static const case_insensitive_string_set_t chances { __VA_ARGS__ }; \ + static const memory::case_insensitive_string_set_t chances { __VA_ARGS__ }; \ if (chances.contains(identifier)) { \ return true; \ } \ diff --git a/src/openvic-simulation/scripts/Condition.hpp b/src/openvic-simulation/scripts/Condition.hpp index 7fdbae034..390c28909 100644 --- a/src/openvic-simulation/scripts/Condition.hpp +++ b/src/openvic-simulation/scripts/Condition.hpp @@ -5,10 +5,9 @@ #include #include -#include "openvic-simulation/types/EnumBitfield.hpp" -#include "openvic-simulation/types/HasIdentifier.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/utility/Containers.hpp" +#include "openvic-simulation/core/container/HasIdentifier.hpp" +#include "openvic-simulation/core/container/IdentifierRegistry.hpp" +#include "openvic-simulation/core/template/EnumBitfield.hpp" namespace OpenVic { struct ConditionManager; diff --git a/src/openvic-simulation/scripts/ConditionalWeight.hpp b/src/openvic-simulation/scripts/ConditionalWeight.hpp index dd1a8acd9..a5ad17369 100644 --- a/src/openvic-simulation/scripts/ConditionalWeight.hpp +++ b/src/openvic-simulation/scripts/ConditionalWeight.hpp @@ -1,12 +1,14 @@ #pragma once +#include +#include #include -#include +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/scripts/Condition.hpp" #include "openvic-simulation/scripts/ConditionScript.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { enum class conditional_weight_type_t : uint8_t { diff --git a/src/openvic-simulation/testing/Requirement.hpp b/src/openvic-simulation/testing/Requirement.hpp index 06332b101..6b8f0b9b5 100644 --- a/src/openvic-simulation/testing/Requirement.hpp +++ b/src/openvic-simulation/testing/Requirement.hpp @@ -1,6 +1,6 @@ #pragma once #include -#include "openvic-simulation/utility/Getters.hpp" +#include "openvic-simulation/core/Property.hpp" namespace OpenVic { diff --git a/src/openvic-simulation/testing/TestScript.hpp b/src/openvic-simulation/testing/TestScript.hpp index 2ae2fd73e..2c7b5130e 100644 --- a/src/openvic-simulation/testing/TestScript.hpp +++ b/src/openvic-simulation/testing/TestScript.hpp @@ -1,10 +1,11 @@ #pragma once -#include -#include + +#include #include "openvic-simulation/DefinitionManager.hpp" +#include "openvic-simulation/core/memory/UniquePtr.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" #include "openvic-simulation/testing/Requirement.hpp" -#include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { diff --git a/src/openvic-simulation/testing/Testing.cpp b/src/openvic-simulation/testing/Testing.cpp index 29400aeb1..98ade3184 100644 --- a/src/openvic-simulation/testing/Testing.cpp +++ b/src/openvic-simulation/testing/Testing.cpp @@ -2,8 +2,12 @@ #include +#include + +#include "openvic-simulation/core/memory/String.hpp" +#include "openvic-simulation/core/memory/UniquePtr.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" #include "openvic-simulation/testing/TestScript.hpp" -#include "openvic-simulation/utility/Containers.hpp" using namespace OpenVic; diff --git a/src/openvic-simulation/testing/Testing.hpp b/src/openvic-simulation/testing/Testing.hpp index 27d57afcf..e81945956 100644 --- a/src/openvic-simulation/testing/Testing.hpp +++ b/src/openvic-simulation/testing/Testing.hpp @@ -1,11 +1,12 @@ #pragma once #include -#include #include #include "openvic-simulation/DefinitionManager.hpp" +#include "openvic-simulation/core/memory/UniquePtr.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" #include "openvic-simulation/testing/TestScript.hpp" #include "openvic-simulation/testing/test_scripts/A_001_file_tests.cpp" #include "openvic-simulation/testing/test_scripts/A_002_economy_tests.cpp" diff --git a/src/openvic-simulation/testing/test_scripts/A_001_file_tests.cpp b/src/openvic-simulation/testing/test_scripts/A_001_file_tests.cpp index dbf777f65..e05ade691 100644 --- a/src/openvic-simulation/testing/test_scripts/A_001_file_tests.cpp +++ b/src/openvic-simulation/testing/test_scripts/A_001_file_tests.cpp @@ -1,8 +1,6 @@ -#include "openvic-simulation/DefinitionManager.hpp" +#include "openvic-simulation/core/memory/UniquePtr.hpp" #include "openvic-simulation/testing/TestScript.hpp" -#include "utility/Containers.hpp" - namespace OpenVic { class A_001_file_tests : public TestScript { diff --git a/src/openvic-simulation/types/BuildingLevel.hpp b/src/openvic-simulation/types/BuildingLevel.hpp deleted file mode 100644 index 14f1cdd5e..000000000 --- a/src/openvic-simulation/types/BuildingLevel.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include - -namespace OpenVic { - using building_level_t = int16_t; -} \ No newline at end of file diff --git a/src/openvic-simulation/types/FixedVector.hpp b/src/openvic-simulation/types/FixedVector.hpp deleted file mode 100644 index 3c412b0f0..000000000 --- a/src/openvic-simulation/types/FixedVector.hpp +++ /dev/null @@ -1,198 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "openvic-simulation/utility/Concepts.hpp" -#include "openvic-simulation/utility/Typedefs.hpp" - -namespace OpenVic::_detail { - //fixed capacity + not movable + not copyable - template > - class FixedVector { - private: - using allocator_traits = std::allocator_traits; - const size_t _max_size; - size_t _size; - OV_NO_UNIQUE_ADDRESS Allocator _allocator; - T* const _data_start_ptr; - - public: - using const_reference = T const&; - using difference_type = ptrdiff_t; - using reference = T&; - using size_type = size_t; - using value_type = T; - - constexpr size_t size() const { return _size; } - constexpr size_t capacity() const { return _max_size; } - constexpr size_t max_size() const { return _max_size; } - constexpr T* data() { return _data_start_ptr; } - constexpr T const* data() const { return _data_start_ptr; } - constexpr bool empty() const { return _size == 0; } - - explicit FixedVector(const size_t capacity) - : _max_size(capacity), - _size(0), - _allocator(), - _data_start_ptr(allocator_traits::allocate(_allocator, capacity)) {} - - //Generator (size_t i) -> U (where T is constructable from U) - template - // The generator must NOT return a tuple - requires (!specialization_of>, std::tuple>) - // The type must be constructible from the generator's single return value - && std::constructible_from()(std::declval()))> - FixedVector(size_t capacity, GeneratorTemplateType&& generator) - : _max_size(capacity), - _size(capacity), - _allocator(), - _data_start_ptr(allocator_traits::allocate(_allocator, capacity)) { - for (size_t i = 0; i < capacity; ++i) { - allocator_traits::construct( - _allocator, - begin()+i, - generator(i) - ); - } - } - - //Generator (size_t i) -> std::tuple (where T is constructable from Args) - template - // The generator must return a tuple - requires specialization_of>, std::tuple> - // The tuple must be constructible into a T - && requires(GeneratorTemplateType&& generator) { - { - std::apply( - [](auto&&... args) { - T obj{std::forward(args)...}; - }, - generator(std::declval()) - ) - }; - } - FixedVector(size_t max_size, GeneratorTemplateType&& generator) - : _max_size(max_size), - _size(max_size), - _allocator(), - _data_start_ptr(allocator_traits::allocate(_allocator, max_size)) { - for (size_t i = 0; i < max_size; ++i) { - std::apply( - [this, i](auto&&... args) { - allocator_traits::construct( - _allocator, - begin()+i, - std::forward(args)... - ); - }, - generator(i) - ); - } - } - - FixedVector(const FixedVector&) = delete; - FixedVector& operator=(const FixedVector&) = delete; - FixedVector(FixedVector&&) = delete; - FixedVector& operator=(FixedVector&&) = delete; - - ~FixedVector() { - clear(); - allocator_traits::deallocate(_allocator, _data_start_ptr, _max_size); - } - - using iterator = T*; - using const_iterator = const T*; - - iterator begin() { return _data_start_ptr; } - const_iterator begin() const { return _data_start_ptr; } - const_iterator cbegin() const { return _data_start_ptr; } - - iterator end() { return begin() + _size; } - const_iterator end() const { return begin() + _size; } - const_iterator cend() const { return cbegin() + _size; } - - using reverse_iterator = std::reverse_iterator; - using const_reverse_iterator = std::reverse_iterator; - - reverse_iterator rbegin() { return reverse_iterator(end()); } - const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } - const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); } - - reverse_iterator rend() { return reverse_iterator(begin()); } - const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } - const_reverse_iterator crend() const { return const_reverse_iterator(begin()); } - - T& operator[](size_t index) { - assert(index < _size && "Index out of bounds."); - return _data_start_ptr[index]; - } - const T& operator[](size_t index) const { - assert(index < _size && "Index out of bounds."); - return _data_start_ptr[index]; - } - - T& front() { - assert(!empty()); - return *begin(); - } - const T& front() const { - assert(!empty()); - return *cbegin(); - } - T& back() { - assert(!empty()); - return *(end()-1); - } - const T& back() const { - assert(!empty()); - return *(cend()-1); - } - - template - iterator emplace_back(Args&&... args) { - if (_size >= _max_size) { - return end(); - } - allocator_traits::construct( - _allocator, - begin() + _size, - std::forward(args)... - ); - ++_size; - return end()-1; - } - - void pop_back() { - if (_size > 0) { - allocator_traits::destroy( - _allocator, - end() - 1 - ); - --_size; - } - } - - void clear() { - for (iterator it = end(); it != begin(); ) { - --it; - allocator_traits::destroy( - _allocator, - it - ); - } - _size = 0; - } - }; -} - -#include - -#include "openvic-simulation/utility/MemoryTracker.hpp" - -namespace OpenVic::memory { - template - using FixedVector = _detail::FixedVector>>; -} \ No newline at end of file diff --git a/src/openvic-simulation/types/HasIdentifier.hpp b/src/openvic-simulation/types/HasIdentifier.hpp deleted file mode 100644 index c2738b555..000000000 --- a/src/openvic-simulation/types/HasIdentifier.hpp +++ /dev/null @@ -1,112 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include - -#include "openvic-simulation/types/Colour.hpp" -#include "openvic-simulation/utility/Concepts.hpp" -#include "openvic-simulation/utility/Getters.hpp" - -namespace OpenVic { - constexpr bool valid_basic_identifier_char(char c) { - return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9') || c == '_'; - } - constexpr bool valid_basic_identifier(std::string_view identifier) { - return std::all_of(identifier.begin(), identifier.end(), valid_basic_identifier_char); - } - constexpr std::string_view extract_basic_identifier_prefix(std::string_view identifier) { - size_t len = 0; - while (len < identifier.size() && valid_basic_identifier_char(identifier[len])) { - ++len; - } - return { identifier.data(), len }; - } - - /* - * Base class for objects with a non-empty string identifier. Uniquely named instances of a type derived from this class - * can be entered into an IdentifierRegistry instance. - */ - class HasIdentifier { - /* Not const so it can be moved rather than needing to be copied. */ - memory::string PROPERTY(identifier); - - protected: - HasIdentifier(std::string_view new_identifier): identifier { new_identifier } { - assert(!identifier.empty()); - } - HasIdentifier(HasIdentifier const&) = default; - - public: - HasIdentifier(HasIdentifier&&) = default; - HasIdentifier& operator=(HasIdentifier const&) = delete; - HasIdentifier& operator=(HasIdentifier&&) = delete; - }; - - inline std::ostream& operator<<(std::ostream& stream, HasIdentifier const& obj) { - return stream << obj.get_identifier(); - } - inline std::ostream& operator<<(std::ostream& stream, HasIdentifier const* obj) { - return obj != nullptr ? stream << *obj : stream << ""; - } - - /* - * Base class for objects with associated colour information. - */ - template - class _HasColour { - const ColourT PROPERTY(colour); - - protected: - _HasColour(ColourT new_colour, bool cannot_be_null): colour { new_colour } { - assert(!cannot_be_null || !colour.is_null()); - } - _HasColour(_HasColour const&) = default; - - public: - _HasColour(_HasColour&&) = default; - _HasColour& operator=(_HasColour const&) = delete; - _HasColour& operator=(_HasColour&&) = delete; - }; - - using HasColour = _HasColour; - using HasAlphaColour = _HasColour; - - /* - * Base class for objects with a unique string identifier and associated colour information. - */ - template - class _HasIdentifierAndColour : public HasIdentifier, public _HasColour { - protected: - _HasIdentifierAndColour(std::string_view new_identifier, ColourT new_colour, bool cannot_be_null) - : HasIdentifier { new_identifier }, _HasColour { new_colour, cannot_be_null } {} - _HasIdentifierAndColour(_HasIdentifierAndColour const&) = default; - - public: - _HasIdentifierAndColour(_HasIdentifierAndColour&&) = default; - _HasIdentifierAndColour& operator=(_HasIdentifierAndColour const&) = delete; - _HasIdentifierAndColour& operator=(_HasIdentifierAndColour&&) = delete; - }; - - using HasIdentifierAndColour = _HasIdentifierAndColour; - using HasIdentifierAndAlphaColour = _HasIdentifierAndColour; -} - -template -requires (!OpenVic::has_get_name) -struct fmt::formatter : fmt::formatter { - fmt::format_context::iterator format(T const& has_id, fmt::format_context& ctx) const { - return fmt::formatter::format(has_id.get_identifier(), ctx); - } -}; - -template -requires (!OpenVic::has_get_identifier) -struct fmt::formatter : fmt::formatter { - fmt::format_context::iterator format(T const& has_id, fmt::format_context& ctx) const { - return fmt::formatter::format(has_id.get_name(), ctx); - } -}; \ No newline at end of file diff --git a/src/openvic-simulation/types/IndexedFlatMap.hpp b/src/openvic-simulation/types/IndexedFlatMap.hpp deleted file mode 100644 index f6850ec2d..000000000 --- a/src/openvic-simulation/types/IndexedFlatMap.hpp +++ /dev/null @@ -1,1173 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include - -#include - -#include "openvic-simulation/types/FixedVector.hpp" -#include "openvic-simulation/utility/Concepts.hpp" -#include "openvic-simulation/utility/Getters.hpp" -#include "openvic-simulation/utility/ForwardableSpan.hpp" -#include "openvic-simulation/utility/Logger.hpp" - -#define OV_IFLATMAP_PROPERTY(KEYTYPE, VALUETYPE, NAME) OV_IFLATMAP_PROPERTY_ACCESS(KEYTYPE, VALUETYPE, NAME, private) -#define OV_IFLATMAP_PROPERTY_ACCESS(KEYTYPE, VALUETYPE, NAME, ACCESS) \ - IndexedFlatMap NAME; \ -\ -public: \ - [[nodiscard]] constexpr IndexedFlatMap const& get_##NAME() const { \ - return NAME; \ - } \ - [[nodiscard]] auto get_##NAME(KEYTYPE const& key) const->decltype(OpenVic::_get_property(NAME.at_index(0))); \ - ACCESS: - -namespace OpenVic { - /** - * @brief A dictionary-like type that uses std::vector for contiguous storage, - * providing O(1) access time by directly using an integer index obtained from the KeyType. - * It strictly assumes that keys provided at construction are ordered and continuous - * in their index values. - * @tparam KeyType The type of keys used to access values. This type must provide a - * `size_t getIndex() const` method. - * @tparam ValueType The type of values to be stored in the map. - * - * This class assumes that an integer index can be obtained from the key type - * via its `getIndex()` method. The indices used with this map must be within - * the specified range [min_index, max_index]. - * - * @warning This class stores a `std::span` of the provided keys. This means the - * `IndexedFlatMap` does NOT own the lifetime of the keys. The caller is - * responsible for ensuring that the underlying data (the `std::vector` or array) - * that the `std::span` refers to remains valid and outlives the `IndexedFlatMap` instance. - */ - template - struct IndexedFlatMap { - using keys_span_type = OpenVic::utility::forwardable_span; - using values_vector_type = std::conditional_t< - std::is_move_constructible_v || std::is_copy_constructible_v, - memory::vector, - memory::FixedVector - >; - - private: - values_vector_type values; - keys_span_type keys; //non-owning! - size_t min_index; - size_t max_index; - - /** - * @brief Converts an external key's index to an internal vector index. - * Logs a fatal error if the key's index is out of bounds. - * @param key The key whose index is to be converted. - * @return The internal index, or 0 if out of bounds (after logging error). - */ - constexpr size_t get_internal_index_from_key(KeyType const& key) const { - static_assert(has_get_index); - const size_t index = key.get_index(); - if (index < min_index || index > max_index) { - spdlog::error_s( - "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> attempted to access key with index {} which is outside the map's defined range [{}, {}].", - utility::type_name(), - utility::type_name(), - index, - min_index, - max_index - ); - assert(index >= min_index && index <= max_index); - return 0; - } - return index - min_index; - } - - /** - * @brief Validates that the provided span of keys is ordered and continuous. - * Logs errors if validation fails. - * @param new_keys The span of keys to validate. - * @return True if keys are valid, false otherwise. - */ - static bool validate_new_keys(keys_span_type new_keys) { - static_assert(has_get_index); - if (new_keys.empty()) { - spdlog::warn_s( - "DEVELOPER: OpenVic::IndexedFlatMap<{}, {}> should not be constructed with empty key span.", - utility::type_name(), utility::type_name() - ); - return false; - } - - const size_t min_index = new_keys.front().get_index(); - const size_t max_index = new_keys.back().get_index(); - const size_t expected_capacity = max_index - min_index + 1; - - if (new_keys.size() != expected_capacity) { - spdlog::error_s( - "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> must be constructed with a continuous span of keys with incremental indices. Expected capacity {} but got {} keys.", - utility::type_name(), - utility::type_name(), - expected_capacity, - new_keys.size() - ); - assert(new_keys.size() == expected_capacity); - return false; - } - - for (size_t i = 0; i < new_keys.size(); ++i) { - if (new_keys[i].get_index() != min_index + i) { - spdlog::error_s( - "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> must be constructed with a continuous span of keys with incremental indices. " - "Expected index {} but got {} at position {}.", - utility::type_name(), - utility::type_name(), - min_index + i, - new_keys[i].get_index(), - i - ); - assert(new_keys[i].get_index() == min_index + i); - return false; - } - } - - return true; - } - - //could be rewritten to return iterators for both this and other. - //that would overcomplicate it with const & non-const - template - keys_span_type get_shared_keys(IndexedFlatMap const& other) const { - if (other.get_min_index() >= min_index && other.get_max_index() <= max_index) { - return other.get_keys(); - } - - const size_t min_shared_index = std::max(other.get_min_index(), min_index); - const size_t max_shared_index = std::min(other.get_max_index(), max_index); - - if (min_shared_index > max_shared_index) { - return {}; - } - - const size_t shared_count = 1 + max_shared_index - min_shared_index; - const size_t other_keys_offset = min_shared_index - other.get_min_index(); - return {other.get_keys().data() + other_keys_offset, shared_count}; - } - - /** - * @brief Checks if the 'other' IndexedFlatMap's key span is a subset of 'this' map's key span, - * based purely on index ranges. - * Logs a fatal error if not compatible. - * @param other The other IndexedFlatMap to compare with. - * @return True if compatible as a subset, false otherwise. - */ - template - bool check_subset_span_match(IndexedFlatMap const& other) const { - // Check if 'other's index range is contained within 'this's index range - if (!(other.get_min_index() >= min_index && other.get_max_index() <= max_index)) { - spdlog::error_s( - "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> subset operation requires the right-hand map's index range " - "({}-{}) to be a subset of the left-hand map's index range ({}-{}).", - utility::type_name(), - utility::type_name(), - other.get_min_index(), - other.get_max_index(), - min_index, - max_index - ); - assert(other.get_min_index() >= min_index && other.get_max_index() <= max_index); - return false; - } - - // There is no check for keys.data() being identical as KeyType objects are considered functionally equivalent if their get_index() values match. - return true; - } - - //vector - constexpr IndexedFlatMap() - requires std::is_move_constructible_v || std::is_copy_constructible_v - : values(), keys(), min_index(0), max_index(0) {} - - //FixedVector - constexpr IndexedFlatMap() - requires (!std::is_move_constructible_v) && (!std::is_copy_constructible_v) - : values(0), keys(), min_index(0), max_index(0) {} - - public: - static constexpr IndexedFlatMap create_empty() { - return {}; - } - - /** vector - * @brief Constructs an IndexedFlatMap based on a provided span of ordered and continuous keys - * and a key-based generator. - * The map's range [min_idx, max_idx] is determined by the first and last key in the span. - * All elements are initialized using the `value_generator`. - * @param new_keys A `std::span` of KeyType objects. These keys MUST be - * ordered by their index and continuous (i.e., `new_keys[i+1].getIndex() == new_keys[i].getIndex() + 1`). - * The `IndexedFlatMap` stores a reference to this span; the caller is responsible - * for ensuring the underlying data outlives this map instance. - * @param value_generator A callable that takes a `KeyType const&` and returns a single argument to construct `ValueType`. - * This is used to generate values for each key in the provided span. - */ - template - requires (std::is_move_constructible_v || std::is_copy_constructible_v) - //value_generator(key) doesn't return std:tuple<...> - && (!specialization_of>, std::tuple>) - //ValueType(value_generator(key)) is valid constructor call - && std::constructible_from()(std::declval()))> - IndexedFlatMap( - keys_span_type new_keys, - GeneratorTemplateType value_generator - ) : keys(new_keys), - min_index { new_keys.front().get_index() }, - max_index { new_keys.back().get_index() }, - values() { - static_assert(has_get_index); - if (!validate_new_keys(new_keys)) { - return; - } - - values.reserve(new_keys.size()); - for (KeyType const& key : keys) { - values.emplace_back( - std::forward()(std::declval()))>( - value_generator(key) - ) - ); - } - } - - /** FixedVector - * @brief Constructs an IndexedFlatMap based on a provided span of ordered and continuous keys - * and a key-based generator. - * The map's range [min_idx, max_idx] is determined by the first and last key in the span. - * All elements are initialized using the `value_generator`. - * @param new_keys A `std::span` of KeyType objects. These keys MUST be - * ordered by their index and continuous (i.e., `new_keys[i+1].getIndex() == new_keys[i].getIndex() + 1`). - * The `IndexedFlatMap` stores a reference to this span; the caller is responsible - * for ensuring the underlying data outlives this map instance. - * @param value_generator A callable that takes a `KeyType const&` and returns a single argument to construct `ValueType`. - * This is used to generate values for each key in the provided span. - */ - template - requires (!std::is_move_constructible_v) && (!std::is_copy_constructible_v) - //value_generator(key) doesn't return std:tuple<...> - && (!specialization_of>, std::tuple>) - //ValueType(value_generator(key)) is valid constructor call - && std::constructible_from()(std::declval()))> - IndexedFlatMap( - keys_span_type new_keys, - GeneratorTemplateType value_generator - ) : keys(new_keys), - min_index { new_keys.front().get_index() }, - max_index { new_keys.back().get_index() }, - values(new_keys.size()) { - static_assert(has_get_index); - if (!validate_new_keys(new_keys)) { - return; - } - - for (KeyType const& key : keys) { - values.emplace_back( - std::forward()(std::declval()))>( - value_generator(key) - ) - ); - } - } - - /** vector - * @brief Constructs an IndexedFlatMap based on a provided span of ordered and continuous keys - * and a key-based generator. - * The map's range [min_idx, max_idx] is determined by the first and last key in the span. - * All elements are initialized using the `value_generator`. - * @param new_keys A `std::span` of KeyType objects. These keys MUST be - * ordered by their index and continuous (i.e., `new_keys[i+1].getIndex() == new_keys[i].getIndex() + 1`). - * The `IndexedFlatMap` stores a reference to this span; the caller is responsible - * for ensuring the underlying data outlives this map instance. - * @param value_generator A callable that takes a `KeyType const&` and returns arguments to construct `ValueType`. - * This is used to generate values for each key in the provided span. - */ - template - requires (std::is_move_constructible_v || std::is_copy_constructible_v) - //value_generator(key) returns tuple - && specialization_of>, std::tuple> - //ValueType(...value_generator(key)) is valid constructor call - && requires(GeneratorTemplateType&& value_generator) { { - std::apply( - [](auto&&... args) { - ValueType obj{std::forward(args)...}; - }, - value_generator(std::declval()) - ) - }; } - IndexedFlatMap( - keys_span_type new_keys, - GeneratorTemplateType&& value_generator - ) : keys(new_keys), - min_index { new_keys.front().get_index() }, - max_index { new_keys.back().get_index() }, - values() { - static_assert(has_get_index); - if (!validate_new_keys(new_keys)) { - return; - } - - values.reserve(new_keys.size()); - for (KeyType const& key : keys) { - std::apply( - [this](auto&&... args) { - values.emplace_back(std::forward(args)...); - }, - value_generator(key) - ); - } - } - - /** FixedVector - * @brief Constructs an IndexedFlatMap based on a provided span of ordered and continuous keys - * and a key-based generator. - * The map's range [min_idx, max_idx] is determined by the first and last key in the span. - * All elements are initialized using the `value_generator`. - * @param new_keys A `std::span` of KeyType objects. These keys MUST be - * ordered by their index and continuous (i.e., `new_keys[i+1].getIndex() == new_keys[i].getIndex() + 1`). - * The `IndexedFlatMap` stores a reference to this span; the caller is responsible - * for ensuring the underlying data outlives this map instance. - * @param value_generator A callable that takes a `KeyType const&` and returns arguments to construct `ValueType`. - * This is used to generate values for each key in the provided span. - */ - template - requires (!std::is_move_constructible_v) && (!std::is_copy_constructible_v) - //value_generator(key) returns tuple - && specialization_of>, std::tuple> - //ValueType(...value_generator(key)) is valid constructor call - && requires(GeneratorTemplateType value_generator) { { - std::apply( - [](auto&&... args) { - ValueType obj{std::forward(args)...}; - }, - value_generator(std::declval()) - ) - }; } - IndexedFlatMap( - keys_span_type new_keys, - GeneratorTemplateType value_generator - ) : keys(new_keys), - min_index { new_keys.front().get_index() }, - max_index { new_keys.back().get_index() }, - values(new_keys.size()) { - static_assert(has_get_index); - if (!validate_new_keys(new_keys)) { - return; - } - - for (KeyType const& key : keys) { - std::apply( - [this](auto&&... args) { - values.emplace_back(std::forward(args)...); - }, - value_generator(key) - ); - } - } - - /** vector - * @brief Constructs an IndexedFlatMap based on a provided span of ordered and continuous keys. - * All elements are constructed by passing `KeyType const&` or default-constructed if there is no constructor taking `KeyType const&`. - * @param new_keys A `std::span` of KeyType objects. These keys MUST be - * ordered by their index and continuous (i.e., `new_keys[i+1].getIndex() == new_keys[i].getIndex() + 1`). - * The `IndexedFlatMap` stores a reference to this span; the caller is responsible - * for ensuring the underlying data outlives this map instance. - * - * @note This constructor requires `ValueType` to be `std::default_initializable || std::constructible_from`. - */ - IndexedFlatMap(keys_span_type new_keys) - requires (std::is_move_constructible_v || std::is_copy_constructible_v) - && (std::default_initializable || std::constructible_from) - : keys(new_keys), - min_index { new_keys.front().get_index() }, - max_index { new_keys.back().get_index() }, - values() { - static_assert(has_get_index); - if (!validate_new_keys(new_keys)) { - return; - } - - if constexpr (std::constructible_from) { - values.reserve(new_keys.size()); - for (KeyType const& key : keys) { - values.emplace_back(key); - } - } else { - // Resize and default-construct elements - values.resize(new_keys.size()); - } - } - - /** FixedVector - * @brief Constructs an IndexedFlatMap based on a provided span of ordered and continuous keys. - * All elements are constructed by passing `KeyType const&` or default-constructed if there is no constructor taking `KeyType const&`. - * @param new_keys A `std::span` of KeyType objects. These keys MUST be - * ordered by their index and continuous (i.e., `new_keys[i+1].getIndex() == new_keys[i].getIndex() + 1`). - * The `IndexedFlatMap` stores a reference to this span; the caller is responsible - * for ensuring the underlying data outlives this map instance. - * - * @note This constructor requires `ValueType` to be `std::default_initializable || std::constructible_from`. - */ - IndexedFlatMap(keys_span_type new_keys) - requires (!std::is_move_constructible_v) && (!std::is_copy_constructible_v) - && (std::default_initializable || std::constructible_from) - : keys(new_keys), - min_index { new_keys.front().get_index() }, - max_index { new_keys.back().get_index() }, - values(new_keys.size()) { - static_assert(has_get_index); - if (!validate_new_keys(new_keys)) { - return; - } - - if constexpr (std::constructible_from) { - for (KeyType const& key : keys) { - values.emplace_back(key); - } - } else { - // Resize and default-construct elements - for (KeyType const& key : keys) { - values.emplace_back(); - } - } - } - - /** - * @brief Sets the value associated with a key using copy assignment. - */ - void set(KeyType const& key, ValueType const& value) - requires std::assignable_from { - static_assert(has_get_index); - values[get_internal_index_from_key(key)] = value; - } - - /** - * @brief Sets the value associated with a key using move assignment (via std::swap). - */ - void set(KeyType const& key, ValueType&& value) - requires std::movable { - static_assert(has_get_index); - std::swap( - values[get_internal_index_from_key(key)], - value - ); - } - - constexpr ValueType& at(KeyType const& key) { - static_assert(has_get_index); - return values[get_internal_index_from_key(key)]; - } - - constexpr ValueType const& at(KeyType const& key) const { - static_assert(has_get_index); - return values[get_internal_index_from_key(key)]; - } - - constexpr ValueType& at_index(const size_t index) { - if (index < min_index || index > max_index) { - spdlog::error_s( - "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> attempted to access index {} which is outside the map's defined range [{}, {}].", - utility::type_name(), - utility::type_name(), - index, - min_index, - max_index - ); - assert(index >= min_index && index <= max_index); - } - return values[index - min_index]; - } - - constexpr ValueType const& at_index(const size_t index) const { - if (index < min_index || index > max_index) { - spdlog::error_s( - "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> attempted to access index {} which is outside the map's defined range [{}, {}].", - utility::type_name(), - utility::type_name(), - index, - min_index, - max_index - ); - assert(index >= min_index && index <= max_index); - } - return values[index - min_index]; - } - - constexpr bool contains(KeyType const& key) const { - static_assert(has_get_index); - return contains_index(key.get_index()); - } - - constexpr bool contains_index(const size_t external_index) const { - return external_index >= min_index && external_index <= max_index; - } - - constexpr keys_span_type const& get_keys() const { - return keys; - } - - constexpr OpenVic::utility::forwardable_span get_values() { - return values; - } - - constexpr OpenVic::utility::forwardable_span get_values() const { - return values; - } - - constexpr size_t get_count() const { - return keys.size(); - } - - constexpr bool empty() const { - return keys.empty(); - } - - constexpr size_t get_min_index() const { - return min_index; - } - - constexpr size_t get_max_index() const { - return max_index; - } - - /** - * @brief Fills all elements in the map with a specified value. - * The capacity and index range remain unchanged. - * @param value The value to fill all elements with. - * - * @note This method requires `ValueType` to be copy-constructible and copy-assignable. - */ - void fill(ValueType const& value) - requires std::copy_constructible && std::assignable_from { - values.assign(values.size(), value); - } - - constexpr void copy_values_from(IndexedFlatMap const& other) - requires std::assignable_from { - static_assert(has_get_index); - for (KeyType const& key : get_shared_keys(other)) { - set(key, other.at(key)); - } - } - - /** - * @brief Reinitializes all elements in the map using a provided value generator function. - * This overload is chosen for types that are copyable (have a copy constructor and copy assignment). - * The capacity and index range remain unchanged. - * @param value_generator A callable that takes a `KeyType const&` and - * returns a `ValueType`. This is used to generate a new value for each slot. - * - * @note This method requires `ValueType` to be copy-assignable. - */ - void reinitialize_with_generator(fu2::function value_generator) - requires std::copyable { - for (iterator it = begin(); it < end(); it++) { - auto& [key, value] = *it; - value = value_generator(key); // Copy/Move assignment - } - } - - /** - * @brief Reinitializes all elements in the map using a provided value generator function. - * This overload is chosen for types that are movable but NOT copyable. - * The capacity and index range remain unchanged. - * @param value_generator A callable that takes a `KeyType const&` and - * returns a `ValueType`. This is used to generate a new value for each slot. - * - * @note This method destroys existing elements and constructs new ones in place, - * which is often more efficient for move-only types. - */ - void reinitialize_with_generator(fu2::function value_generator) - requires std::movable && (!std::copyable) { - values.clear(); - for (KeyType const& key : keys) { - // Emplace directly, using move construction if generator returns rvalue - values.emplace_back(value_generator(key)); - } - } - - // --- Mathematical Operators (Valarray-like functionality) --- - - // Unary plus operator - IndexedFlatMap operator+() const { - // Unary plus typically returns a copy of itself - return *this; - } - - // Unary minus operator - IndexedFlatMap operator-() const - requires unary_negatable { - static_assert(has_get_index); - return IndexedFlatMap(keys, [&](KeyType const& key) { - return -this->at(key); - }); - } - - template - IndexedFlatMap operator+(IndexedFlatMap const& other) const - requires addable { - static_assert(has_get_index); - if (!check_subset_span_match(other)) { - return IndexedFlatMap(); - } - - // Create a new map with the same keys as 'this' - return IndexedFlatMap(keys, [&](KeyType const& key) { - if (other.contains(key)) { - // If the key exists in 'other' (i.e., within its min_index/max_index), - // perform the operation. - return this->at(key) + other.at(key); - } else { - // If the key does not exist in 'other' (i.e., outside its min_index/max_index), - // retain the value from 'this'. - return this->at(key); - } - }); - } - - template - IndexedFlatMap operator-(IndexedFlatMap const& other) const - requires subtractable { - static_assert(has_get_index); - if (!check_subset_span_match(other)) { - return IndexedFlatMap(); - } - - return IndexedFlatMap(keys, [&](KeyType const& key) { - if (other.contains(key)) { - return this->at(key) - other.at(key); - } else { - return this->at(key); - } - }); - } - - template - IndexedFlatMap operator*(IndexedFlatMap const& other) const - requires multipliable { - static_assert(has_get_index); - if (!check_subset_span_match(other)) { - return IndexedFlatMap(); - } - - return IndexedFlatMap(keys, [&](KeyType const& key) { - if (other.contains(key)) { - return this->at(key) * other.at(key); - } else { - return this->at(key); - } - }); - } - - template - IndexedFlatMap operator/(IndexedFlatMap const& other) const - requires divisible { - static_assert(has_get_index); - if (!check_subset_span_match(other)) { - return IndexedFlatMap(); - } - - return IndexedFlatMap(keys, [&](KeyType const& key) { - // Add a basic division by zero check for each element - if (other.contains(key)) { - if (other.at(key) == static_cast(0)) { - spdlog::error_s( - "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> division by zero detected at key index {}.", - utility::type_name(), - utility::type_name(), - key.get_index() - ); - assert(other.at(key) != static_cast(0)); - //continue and let it throw - } - return this->at(key) / other.at(key); - } else { - return this->at(key); // Retain original value if key not in 'other' - } - }); - } - - template - IndexedFlatMap divide_handle_zero( - IndexedFlatMap const& other, - fu2::function handle_div_by_zero - ) const requires divisible { - static_assert(has_get_index); - if (!check_subset_span_match(other)) { - return IndexedFlatMap(); - } - - return IndexedFlatMap(keys, [&](KeyType const& key) { - if (other.contains(key)) { - if (other.at(key) == static_cast(0)) { - return handle_div_by_zero( - this->at(key), - other.at(key) - ); - } - return this->at(key) / other.at(key); - } else { - return this->at(key); // Retain original value if key not in 'other' - } - }); - } - - // Binary addition operator (Map + Scalar) - template - IndexedFlatMap operator+(ScalarType const& scalar) const - requires addable { - static_assert(has_get_index); - return IndexedFlatMap(keys, [&](KeyType const& key) { - return this->at(key) + scalar; - }); - } - - // Binary subtraction operator (Map - Scalar) - template - IndexedFlatMap operator-(ScalarType const& scalar) const - requires subtractable { - static_assert(has_get_index); - return IndexedFlatMap(keys, [&](KeyType const& key) { - return this->at(key) - scalar; - }); - } - - // Binary multiplication operator (Map * Scalar) - template - IndexedFlatMap operator*(ScalarType const& scalar) const - requires multipliable { - static_assert(has_get_index); - return IndexedFlatMap(keys, [&](KeyType const& key) { - return this->at(key) * scalar; - }); - } - - // Binary division operator (Map / Scalar) - template - IndexedFlatMap operator/(ScalarType const& scalar) const - requires divisible { - static_assert(has_get_index); - if (scalar == static_cast(0)) { - spdlog::error_s( - "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> division by zero for scalar operation.", - utility::type_name(), - utility::type_name() - ); - assert(scalar != static_cast(0)); - //continue and let it throw - } - return IndexedFlatMap(keys, [&](KeyType const& key) { - return this->at(key) / scalar; - }); - } - - template - IndexedFlatMap& operator+=(IndexedFlatMap const& other) - requires add_assignable { - static_assert(has_get_index); - if (!check_subset_span_match(other)) { - return *this; // Return current state on error - } - - // Iterate only over the keys that are present in 'other' - for (KeyType const& key : other.get_keys()) { - this->at(key) += other.at(key); - } - return *this; - } - - template - IndexedFlatMap& operator-=(IndexedFlatMap const& other) - requires subtract_assignable { - static_assert(has_get_index); - if (!check_subset_span_match(other)) { - return *this; - } - - for (KeyType const& key : other.get_keys()) { - this->at(key) -= other.at(key); - } - return *this; - } - - template - IndexedFlatMap& operator*=(IndexedFlatMap const& other) - requires multiply_assignable { - static_assert(has_get_index); - if (!check_subset_span_match(other)) { - return *this; - } - - for (KeyType const& key : other.get_keys()) { - this->at(key) *= other.at(key); - } - return *this; - } - - template - IndexedFlatMap& operator/=(IndexedFlatMap const& other) - requires divide_assignable { - static_assert(has_get_index); - if (!check_subset_span_match(other)) { - return *this; - } - - for (KeyType const& key : other.get_keys()) { - if (other.at(key) == static_cast(0)) { - spdlog::error_s( - "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> compound division by zero detected at key index {}.", - utility::type_name(), - utility::type_name(), - key.get_index() - ); - assert(other.at(key) != static_cast(0)); - //continue and let it throw - } - this->at(key) /= other.at(key); - } - return *this; - } - - template - IndexedFlatMap& divide_assign_handle_zero( - IndexedFlatMap const& other, - fu2::function handle_div_by_zero - ) requires divide_assignable { - static_assert(has_get_index); - if (!check_subset_span_match(other)) { - return *this; - } - - for (KeyType const& key : other.get_keys()) { - if (other.at(key) == static_cast(0)) { - handle_div_by_zero( - this->at(key), - other.at(key) - ); - } else { - this->at(key) /= other.at(key); - } - } - return *this; - } - - // Compound assignment addition operator (Map += Scalar) - template - IndexedFlatMap& operator+=(ScalarType const& scalar) - requires add_assignable { - for (ValueType& val : values) { - val += scalar; - } - return *this; - } - - // Compound assignment subtraction operator (Map -= Scalar) - template - IndexedFlatMap& operator-=(ScalarType const& scalar) - requires subtract_assignable { - for (ValueType& val : values) { - val -= scalar; - } - return *this; - } - - // Compound assignment multiplication operator (Map *= Scalar) - template - IndexedFlatMap& operator*=(ScalarType const& scalar) - requires multiply_assignable { - for (ValueType& val : values) { - val *= scalar; - } - return *this; - } - - // Compound assignment division operator (Map /= Scalar) - template - IndexedFlatMap& operator/=(ScalarType const& scalar) - requires divide_assignable { - if (scalar == static_cast(0)) { - spdlog::error_s( - "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> compound division by zero for scalar operation.", - utility::type_name(), - utility::type_name() - ); - assert(scalar != static_cast(0)); - //continue and let it throw - } - for (ValueType& val : values) { - val /= scalar; - } - return *this; - } - - template - constexpr IndexedFlatMap& mul_add( - IndexedFlatMap const& other, - ScalarType const& factor - ) requires mul_add_assignable { - static_assert(has_get_index); - for (KeyType const& key : get_shared_keys(other)) { - at(key) += other.at(key) * factor; - } - - return *this; - } - - template - constexpr IndexedFlatMap& mul_add( - IndexedFlatMap const& a, - IndexedFlatMap const& b - ) requires mul_add_assignable { - static_assert(has_get_index); - if (a.get_min_index() != b.get_min_index() || a.get_max_index() != b.get_max_index()) { - spdlog::error_s( - "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> attempted mul_add where a and b don't have the same keys. This is not implemented.", - utility::type_name(), - utility::type_name() - ); - assert(a.get_min_index() == b.get_min_index() && a.get_max_index() == b.get_max_index()); - } - - for (KeyType const& key : get_shared_keys(a)) { - at(key) += a.at(key) * b.at(key); - } - - return *this; - } - - template - constexpr IndexedFlatMap& rescale(ScalarType const& new_total) - requires std::default_initializable - && add_assignable - && multiply_assignable - && divide_assignable { - static_assert(has_get_index); - bool has_any_non_zero_value = false; - const ValueType zero = static_cast(0); - ValueType old_total {}; - for (ValueType const& value : values) { - old_total += value; - has_any_non_zero_value |= value != zero; - } - IndexedFlatMap& this_ref = *this; - if (has_any_non_zero_value) { - this_ref *= new_total; - this_ref /= old_total; - } - return this_ref; - } - - // --- Iterators for Key-Value Pairs --- - template - class BasicIterator { - public: - using value_type = std::pair>; - using difference_type = std::ptrdiff_t; - using reference = value_type; - using iterator_category = std::random_access_iterator_tag; - - private: - std::conditional_t value_it; - typename keys_span_type::iterator key_it; - - public: - // Constructor - BasicIterator( - std::conditional_t val_it, - typename keys_span_type::iterator k_it - ) : value_it(val_it), key_it(k_it) {} - - // Dereference operator - reference operator*() const { - return {*key_it, *value_it}; - } - - // Arrow operator (for member access) - // Note: This returns a proxy object, as std::pair is not a class type. - // For direct member access, it's often better to dereference and use dot operator: (*it).first - // However, for compatibility with some algorithms, a proxy is provided. - struct Proxy { - KeyType const& first; - std::conditional_t second; - // Add operator-> for nested access if needed, e.g., Proxy->first.some_member() - // For now, direct access to first/second is assumed. - }; - Proxy operator->() const { - return {*key_it, *value_it}; - } - - // Pre-increment - BasicIterator& operator++() { - ++value_it; - ++key_it; - return *this; - } - - // Post-increment - BasicIterator operator++(int) { - BasicIterator temp = *this; - ++(*this); - return temp; - } - - // Pre-decrement - BasicIterator& operator--() { - --value_it; - --key_it; - return *this; - } - - // Post-decrement - BasicIterator operator--(int) { - BasicIterator temp = *this; - --(*this); - return temp; - } - - // Random access operators - BasicIterator& operator+=(difference_type n) { - value_it += n; - key_it += n; - return *this; - } - - BasicIterator operator+(difference_type n) const { - BasicIterator temp = *this; - temp += n; - return temp; - } - - BasicIterator& operator-=(difference_type n) { - value_it -= n; - key_it -= n; - return *this; - } - - BasicIterator operator-(difference_type n) const { - BasicIterator temp = *this; - temp -= n; - return temp; - } - - difference_type operator-(BasicIterator const& other) const { - return value_it - other.value_it; - } - - // Comparison operators - bool operator==(BasicIterator const& other) const { - return value_it == other.value_it; - } - - bool operator!=(BasicIterator const& other) const { - return !(*this == other); - } - - bool operator<(BasicIterator const& other) const { - return value_it < other.value_it; - } - - bool operator>(BasicIterator const& other) const { - return value_it > other.value_it; - } - - bool operator<=(BasicIterator const& other) const { - return value_it <= other.value_it; - } - - bool operator>=(BasicIterator const& other) const { - return value_it >= other.value_it; - } - - // Conversion to const_iterator - operator BasicIterator() const { - return BasicIterator(value_it, key_it); - } - }; - - using iterator = BasicIterator; - using const_iterator = BasicIterator; - - // Begin and End methods - // note cbegin & cend for std::span requires c++23 so begin & end are used instead - iterator begin() { - return iterator(values.begin(), keys.begin()); - } - - const_iterator begin() const { - return const_iterator(values.cbegin(), keys.begin()); - } - - const_iterator cbegin() const { - return const_iterator(values.cbegin(), keys.begin()); - } - - iterator end() { - return iterator(values.end(), keys.end()); - } - - const_iterator end() const { - return const_iterator(values.cend(), keys.end()); - } - - const_iterator cend() const { - return const_iterator(values.cend(), keys.end()); - } - }; - - // Non-member binary operators for (Scalar op Map) - template - IndexedFlatMap operator+( - ScalarType const& scalar, - IndexedFlatMap const& map - ) requires addable { - return map + scalar; // Delegate to the member operator - } - - template - IndexedFlatMap operator-( - ValueType const& scalar, - IndexedFlatMap const& map - ) requires subtractable { - static_assert(has_get_index); - // Scalar - Map is not simply map - scalar, so we implement it directly - return IndexedFlatMap(map.get_keys(), [&](KeyType const& key) { - return scalar - map.at(key); - }); - } - - template - IndexedFlatMap operator*( - ScalarType const& scalar, - IndexedFlatMap const& map - ) requires multipliable { - return map * scalar; // Delegate to the member operator - } - - template - IndexedFlatMap operator/( - ScalarType const& scalar, - IndexedFlatMap const& map - ) requires divisible { - static_assert(has_get_index); - return IndexedFlatMap(map.get_keys(), [&](KeyType const& key) { - if (map.at(key) == static_cast(0)) { - spdlog::error_s( - "DEVELOPER: OpenVic::IndexedFlatMap<{},{}> scalar division by zero detected at key index {}.", - utility::type_name(), - utility::type_name(), - key.get_index() - ); - assert(map.at(key) != static_cast(0)); - //continue and let it throw - } - return scalar / map.at(key); - }); - } -} \ No newline at end of file diff --git a/src/openvic-simulation/types/OrderedContainers.hpp b/src/openvic-simulation/types/OrderedContainers.hpp deleted file mode 100644 index 2384887c3..000000000 --- a/src/openvic-simulation/types/OrderedContainers.hpp +++ /dev/null @@ -1,206 +0,0 @@ -#pragma once - -#include -#include - -#include -#include - -#include "openvic-simulation/utility/Concepts.hpp" -#include "openvic-simulation/utility/Containers.hpp" -#include "openvic-simulation/utility/Deque.hpp" -#include "openvic-simulation/utility/StringUtils.hpp" - -#include -#include - -namespace OpenVic { - struct ordered_container_string_hash { - using is_transparent = void; - [[nodiscard]] size_t operator()(char const* txt) const { - return std::hash {}(txt); - } - [[nodiscard]] size_t operator()(std::string_view txt) const { - return std::hash {}(txt); - } - [[nodiscard]] size_t operator()(std::string const& txt) const { - return std::hash {}(txt); - } - [[nodiscard]] size_t operator()(memory::string const& txt) const { - return std::hash {}(txt); - } - }; - - template - struct container_hash : std::hash {}; - - template<> - struct container_hash : ordered_container_string_hash {}; - template<> - struct container_hash : ordered_container_string_hash {}; - template<> - struct container_hash : ordered_container_string_hash {}; - template<> - struct container_hash : ordered_container_string_hash {}; - template - struct container_hash : std::hash {}; - - // Useful for contiguous memory - template< - class Key, class T, class Hash = container_hash, class KeyEqual = std::equal_to<>, - class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, - class Allocator = foonathan::memory::std_allocator, memory::tracker>> - using vector_ordered_map = - tsl::ordered_map, Allocator>, IndexType>; - - // Useful for stable memory addresses (so long as you don't remove or insert values) - template< - class Key, class T, class Hash = container_hash, class KeyEqual = std::equal_to<>, - class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, - class Allocator = foonathan::memory::std_allocator, memory::tracker>> - using deque_ordered_map = - tsl::ordered_map, Allocator>, IndexType>; - - template< - class Key, class T, class Hash = container_hash, class KeyEqual = std::equal_to<>, - class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, - class Allocator = foonathan::memory::std_allocator, memory::tracker>> - using ordered_map = vector_ordered_map; - - // Useful for contiguous memory - template< - class Key, class Hash = container_hash, class KeyEqual = std::equal_to<>, - class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, - class Allocator = foonathan::memory::std_allocator>> - using vector_ordered_set = tsl::ordered_set, IndexType>; - - // Useful for stable memory addresses (so long as you don't remove or insert values) - template< - class Key, class Hash = container_hash, class KeyEqual = std::equal_to<>, - class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, - class Allocator = foonathan::memory::std_allocator>> - using deque_ordered_set = - tsl::ordered_set, IndexType>; - - template< - class Key, class Hash = container_hash, class KeyEqual = std::equal_to<>, - class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, - class Allocator = foonathan::memory::std_allocator>> - using ordered_set = vector_ordered_set; - - template - concept derived_ordered_map = derived_from_specialization_of; - - /* Case-Insensitive Containers */ - struct case_insensitive_string_hash { - using is_transparent = void; - - private: - /* Based on the byte array hashing functions in MSVC's . */ - [[nodiscard]] static constexpr size_t _hash_bytes_case_insensitive(char const* first, size_t count) { - constexpr size_t _offset_basis = 14695981039346656037ULL; - constexpr size_t _prime = 1099511628211ULL; - size_t hash = _offset_basis; - for (size_t i = 0; i < count; ++i) { - hash ^= static_cast(std::tolower(static_cast(first[i]))); - hash *= _prime; - } - return hash; - } - - public: - [[nodiscard]] constexpr size_t operator()(char const* txt) const { - return operator()(std::string_view { txt }); - } - [[nodiscard]] constexpr size_t operator()(std::string_view txt) const { - return _hash_bytes_case_insensitive(txt.data(), txt.length()); - } - [[nodiscard]] constexpr size_t operator()(std::string const& txt) const { - return _hash_bytes_case_insensitive(txt.data(), txt.length()); - } - [[nodiscard]] constexpr size_t operator()(memory::string const& txt) const { - return _hash_bytes_case_insensitive(txt.data(), txt.length()); - } - }; - - struct case_insensitive_string_equal { - using is_transparent = void; - - [[nodiscard]] constexpr bool operator()(std::string_view const& lhs, std::string_view const& rhs) const { - return StringUtils::strings_equal_case_insensitive(lhs, rhs); - } - }; - - // Useful for contiguous memory - template< - class Key, class T, class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, - class Allocator = foonathan::memory::std_allocator, memory::tracker>> - using case_insensitive_vector_ordered_map = vector_ordered_map< - Key, T, case_insensitive_string_hash, case_insensitive_string_equal, RawAllocator, IndexType, Allocator>; - - // Useful for stable memory addresses (so long as you don't remove or insert values) - template< - class Key, class T, class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, - class Allocator = foonathan::memory::std_allocator, memory::tracker>> - using case_insensitive_deque_ordered_map = deque_ordered_map< - Key, T, case_insensitive_string_hash, case_insensitive_string_equal, RawAllocator, IndexType, Allocator>; - - template< - class Key, class T, class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, - class Allocator = foonathan::memory::std_allocator, memory::tracker>> - using case_insensitive_ordered_map = case_insensitive_vector_ordered_map; - - // Useful for contiguous memory - template< - class Key, class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, - class Allocator = foonathan::memory::std_allocator>> - using case_insensitive_vector_ordered_set = vector_ordered_set< - Key, case_insensitive_string_hash, case_insensitive_string_equal, RawAllocator, IndexType, Allocator>; - - // Useful for stable memory addresses (so long as you don't remove or insert values) - template< - class Key, class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, - class Allocator = foonathan::memory::std_allocator>> - using case_insensitive_deque_ordered_set = - deque_ordered_set; - - template< - class Key, class RawAllocator = foonathan::memory::default_allocator, class IndexType = std::uint_least32_t, - class Allocator = foonathan::memory::std_allocator>> - using case_insensitive_ordered_set = case_insensitive_vector_ordered_set; - - struct StringMapCaseSensitive { - using hash = container_hash; - using equal = std::equal_to<>; - }; - struct StringMapCaseInsensitive { - using hash = case_insensitive_string_hash; - using equal = case_insensitive_string_equal; - }; - - /* Intermediate struct that "remembers" Case, instead of just decomposing it into its hash and equal components, - * needed so that templates can deduce the Case with which a type was defined. */ - template typename Container, string_map_case Case, typename... Args> - struct template_case_container_t : Container { - using container_t = Container; - using container_t::container_t; - - using case_t = Case; - }; - - /* Template for map with string keys, supporting search by string_view without creating an intermediate string. */ - template - using template_string_map_t = template_case_container_t; - - template - using string_map_t = template_string_map_t; - template - using case_insensitive_string_map_t = template_string_map_t; - - /* Template for set with string elements, supporting search by string_view without creating an intermediate string. */ - template - using template_string_set_t = template_case_container_t; - - using string_set_t = template_string_set_t; - using case_insensitive_string_set_t = template_string_set_t; -} diff --git a/src/openvic-simulation/types/OrderedContainersMath.hpp b/src/openvic-simulation/types/OrderedContainersMath.hpp deleted file mode 100644 index adf8e568a..000000000 --- a/src/openvic-simulation/types/OrderedContainersMath.hpp +++ /dev/null @@ -1,173 +0,0 @@ -#pragma once - -#include -#include - -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/utility/Concepts.hpp" - -namespace OpenVic { - template - constexpr ordered_map operator-(ordered_map const& rhs) { - ordered_map result {}; - auto view = rhs | std::views::transform( - [](KeyType const& key, ValueType const& value) { - return std::make_pair(key, -value); - } - ); - result.insert(view.begin(), view.end()); - return result; - } - - template - constexpr ordered_map& operator+=( - ordered_map& lhs, - ordered_map const& rhs - ) requires add_assignable { - for (auto const& [key, rhs_value] : rhs) { - lhs[key] += rhs_value; - } - return lhs; - } - - template - constexpr ordered_map& operator-=( - ordered_map& lhs, - ordered_map const& rhs - ) requires subtract_assignable { - for (auto const& [key, rhs_value] : rhs) { - lhs[key] -= rhs_value; - } - return lhs; - } - - template - constexpr ordered_map& operator*=( - ordered_map& lhs, - ordered_map const& rhs - ) requires multiply_assignable { - for (auto const& [key, rhs_value] : rhs) { - lhs[key] *= rhs_value; - } - return lhs; - } - - template - constexpr ordered_map& operator/=( - ordered_map& lhs, - ordered_map const& rhs - ) requires divide_assignable { - for (auto const& [key, rhs_value] : rhs) { - lhs[key] = rhs_value; - } - return lhs; - } - - template - constexpr ordered_map& operator+=( - ordered_map& lhs, - RhsValueType const& rhs - ) requires add_assignable { - for (typename decltype(lhs)::iterator it = lhs.begin(); it != lhs.end(); ++it) { - it.value() += rhs; - } - return lhs; - } - - template - constexpr ordered_map& operator-=( - ordered_map& lhs, - RhsValueType const& rhs - ) requires subtract_assignable { - for (typename decltype(lhs)::iterator it = lhs.begin(); it != lhs.end(); ++it) { - it.value() -= rhs; - } - return lhs; - } - - template - constexpr ordered_map& operator*=( - ordered_map& lhs, - RhsValueType const& rhs - ) requires multiply_assignable { - for (typename decltype(lhs)::iterator it = lhs.begin(); it != lhs.end(); ++it) { - it.value() *= rhs; - } - return lhs; - } - - template - constexpr ordered_map& operator/=( - ordered_map& lhs, - RhsValueType const& rhs - ) requires divide_assignable { - for (auto it = lhs.begin(); it != lhs.end(); ++it) { - it.value() /= rhs; - } - return lhs; - } - - template - constexpr static ValueType get_total( - ordered_map const& map - ) requires std::is_default_constructible_v { - ValueType running_total {}; - for (auto const& [key, value] : map) { - running_total += value; - } - return running_total; - } - - template - constexpr static typename ordered_map::const_iterator get_largest_item( - ordered_map const& map - ) requires std::three_way_comparable_with { - constexpr auto pred = []( - typename ordered_map::value_type const& lhs, - typename ordered_map::value_type const& rhs - ) -> bool { - return lhs.second < rhs.second; - }; - - return std::max_element(map.begin(), map.end(), pred); - } - - /* This function includes a key comparator to choose between entries with equal values. */ - template - constexpr static typename ordered_map::const_iterator get_largest_item_tie_break( - ordered_map const& map, const auto key_pred - ) requires std::three_way_comparable_with { - constexpr auto pred = [key_pred]( - typename ordered_map::value_type const& lhs, - typename ordered_map::value_type const& rhs - ) -> bool { - return lhs.second < rhs.second || (lhs.second == rhs.second && key_pred(lhs.first, rhs.first)); - }; - - return std::max_element(map.begin(), map.end(), pred); - } - - template - constexpr static std::pair< - typename ordered_map::const_iterator, - typename ordered_map::const_iterator - > get_largest_two_items( - ordered_map const& map - ) requires std::three_way_comparable_with { - typename ordered_map::const_iterator largest = map.end(), second_largest = map.end(); - - for (typename ordered_map::const_iterator it = map.begin(); it != map.end(); ++it) { - if (largest == map.end() || it->second > largest->second) { - second_largest = largest; - largest = it; - } else if (second_largest == map.end() || it->second > second_largest->second) { - second_largest = it; - } - } - - return std::make_pair( - std::move(largest), - std::move(second_largest) - ); - } -} \ No newline at end of file diff --git a/src/openvic-simulation/types/PopSize.hpp b/src/openvic-simulation/types/PopSize.hpp deleted file mode 100644 index 6e00eeb04..000000000 --- a/src/openvic-simulation/types/PopSize.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include - -namespace OpenVic { - using pop_size_t = int32_t; -} \ No newline at end of file diff --git a/src/openvic-simulation/types/PopSprite.hpp b/src/openvic-simulation/types/PopSprite.hpp deleted file mode 100644 index 3d086342c..000000000 --- a/src/openvic-simulation/types/PopSprite.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include - -namespace OpenVic { - using pop_sprite_t = uint8_t; -} \ No newline at end of file diff --git a/src/openvic-simulation/types/ProvinceLifeRating.hpp b/src/openvic-simulation/types/ProvinceLifeRating.hpp deleted file mode 100644 index 50f028766..000000000 --- a/src/openvic-simulation/types/ProvinceLifeRating.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include - -namespace OpenVic { - using life_rating_t = int8_t; -} \ No newline at end of file diff --git a/src/openvic-simulation/types/TechnologyUnlockLevel.hpp b/src/openvic-simulation/types/TechnologyUnlockLevel.hpp deleted file mode 100644 index 001462c3c..000000000 --- a/src/openvic-simulation/types/TechnologyUnlockLevel.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include - -namespace OpenVic { - using technology_unlock_level_t = int8_t; -} \ No newline at end of file diff --git a/src/openvic-simulation/types/UniqueId.hpp b/src/openvic-simulation/types/UniqueId.hpp deleted file mode 100644 index 27c1eed91..000000000 --- a/src/openvic-simulation/types/UniqueId.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include - -namespace OpenVic { - using unique_id_t = uint64_t; -} \ No newline at end of file diff --git a/src/openvic-simulation/types/UnitVariant.hpp b/src/openvic-simulation/types/UnitVariant.hpp deleted file mode 100644 index f1711281b..000000000 --- a/src/openvic-simulation/types/UnitVariant.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include - -namespace OpenVic { - using unit_variant_t = uint8_t; -} \ No newline at end of file diff --git a/src/openvic-simulation/utility/CompilerFeatureTesting.hpp b/src/openvic-simulation/utility/CompilerFeatureTesting.hpp deleted file mode 100644 index 8c9f961c8..000000000 --- a/src/openvic-simulation/utility/CompilerFeatureTesting.hpp +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#include - -#include -#include -#include - -namespace OpenVic { - template UnaryFunc> - inline constexpr void try_parallel_for_each(InputIt first, InputIt last, UnaryFunc f) { -#ifndef __cpp_lib_execution - std::for_each(first, last, f); -#else -#if defined(__linux__) || defined(__MINGW32__) - // We cannot use std::execution::par here as its implementation uses exceptions, which we have disabled - std::for_each(first, last, f); -#else - if constexpr (__cpp_lib_execution >= 201603L) { - std::for_each(std::execution::par, first, last, f); - } else { - std::for_each(first, last, f); - } -#endif -#endif - } - - struct _for_each_fn { - template Fun> - constexpr void operator()(Iter first, Iter last, Fun func) const { - try_parallel_for_each(first, last, func); - } - - template> Fun> - constexpr void operator()(Range&& r, Fun f) const { - (*this)(ranges::begin(r), ranges::end(r), std::move(f)); - } - }; - - inline constexpr _for_each_fn parallel_for_each {}; -} diff --git a/src/openvic-simulation/utility/ConstexprIntToStr.hpp b/src/openvic-simulation/utility/ConstexprIntToStr.hpp deleted file mode 100644 index e346ebe96..000000000 --- a/src/openvic-simulation/utility/ConstexprIntToStr.hpp +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace OpenVic::ConstexprIntToStr { - template - constexpr decltype(auto) append_sequence(std::integer_sequence, std::integer_sequence) { - return std::integer_sequence {}; - } - - template - struct string_sequence_to_view; - - template - struct string_sequence_to_view> { - static constexpr decltype(auto) get() { - return std::string_view { c_str }; - } - - static constexpr const char c_str[] { chars..., char {} }; - }; - - template - constexpr decltype(auto) integer_to_string_sequence() { - constexpr auto digits = []() { - return "0123456789abcdefghijklmnopqrstuvwxyz"; - }; - - constexpr std::size_t remainder = value % 10; - constexpr std::size_t next_value = value / 10; - - if constexpr (next_value != 0) { - return append_sequence( - integer_to_string_sequence(), std::integer_sequence {} - ); - } else { - return std::integer_sequence {}; - } - } - template - constexpr std::string_view make_string() { - return string_sequence_to_view())> {}.c_str; - } - - template - constexpr auto generate_itosv_array(std::integer_sequence) { - return std::array { make_string()... }; - } - - // Make array of N string views, countings up from 0 to N - 1 - template - constexpr auto make_itosv_array() { - return generate_itosv_array(std::make_integer_sequence()); - } -} diff --git a/src/openvic-simulation/utility/Containers.hpp b/src/openvic-simulation/utility/Containers.hpp deleted file mode 100644 index eb115a663..000000000 --- a/src/openvic-simulation/utility/Containers.hpp +++ /dev/null @@ -1,292 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include - -#include "openvic-simulation/types/CowPtr.hpp" -#include "openvic-simulation/types/CowVector.hpp" -#include "openvic-simulation/utility/DequeMemory.hpp" -#include "openvic-simulation/utility/MemoryTracker.hpp" - -#include -#include -#include -#include -#include -#include -#include - -namespace OpenVic::memory { - template - tracker make_tracked_allocator( // - RawAllocator&& alloc - ) { -#ifdef DEBUG_ENABLED - return foonathan::memory::make_tracked_allocator( - typename tracker::tracker {}, std::forward(alloc) - ); -#else - return std::forward(alloc); -#endif - } - - template - using allocator_reference = foonathan::memory::allocator_reference; - - template - allocator_reference make_allocator_reference(RawAllocator&& alloc) { - return foonathan::memory::make_allocator_reference(alloc); - } - - template - using allocator_deleter = foonathan::memory::allocator_deleter; - - template - struct will_optimize_deleter_for : std::false_type {}; - - template<> - struct will_optimize_deleter_for : std::true_type {}; - - template<> - struct will_optimize_deleter_for : std::true_type {}; - - template<> - struct will_optimize_deleter_for : std::true_type {}; - -#ifdef DEBUG_ENABLED - template - using unique_ptr = foonathan::memory::unique_ptr>; - - template - using unique_base_ptr = foonathan::memory::unique_base_ptr>; - - template - requires(!std::is_array_v) - unique_ptr> allocate_unique(RawAllocator&& alloc, Args&&... args) { - return foonathan::memory::allocate_unique( - make_tracked_allocator(std::forward(alloc)), std::forward(args)... - ); - } - - template - requires(std::is_array_v) - unique_ptr> allocate_unique(RawAllocator&& alloc, std::size_t size) { - return foonathan::memory::allocate_unique>( - make_tracked_allocator(std::forward(alloc)), size - ); - } -#else - template - using unique_ptr = std::conditional_t< - will_optimize_deleter_for::value, std::unique_ptr, - foonathan::memory::unique_ptr>>; - - template - using unique_base_ptr = std::conditional_t< - will_optimize_deleter_for::value, std::unique_ptr, - foonathan::memory::unique_base_ptr>>; - - template - requires(!std::is_array_v) - unique_ptr> allocate_unique(RawAllocator&& alloc, Args&&... args) { - if constexpr (will_optimize_deleter_for::value) { - return std::make_unique(std::forward(args)...); - } else { - return foonathan::memory::allocate_unique( - make_tracked_allocator(std::forward(alloc)), std::forward(args)... - ); - } - } - - template - requires(std::is_array_v) - unique_ptr> allocate_unique(RawAllocator&& alloc, std::size_t size) { - if constexpr (will_optimize_deleter_for::value) { - return std::make_unique(size); - } else { - return foonathan::memory::allocate_unique>( - make_tracked_allocator(std::forward(alloc)), size - ); - } - } -#endif - - template - requires(!std::is_array_v) - static inline T* allocate_new(RawAllocator&& alloc, Args&&... args) { - allocator_reference alloc_ref = ::OpenVic::memory::make_allocator_reference(alloc); - T* ptr = static_cast(alloc_ref.allocate_node(sizeof(T), alignof(T))); - ::new (ptr) T(std::forward(args)...); - return ptr; - } - - template - requires(std::is_unbounded_array_v) - static inline T* allocate_new(RawAllocator&& alloc, size_t n) { - allocator_reference alloc_ref = ::OpenVic::memory::make_allocator_reference(alloc); - return static_cast(alloc_ref.allocate_node(sizeof(T) * n, alignof(T))); - } - - template - requires(!std::is_array_v) - static inline T* make_new(Args&&... args) { - return allocate_new(make_tracked_allocator(RawAllocator {}), std::forward(args)...); - } - - template - requires(std::is_unbounded_array_v) - static inline T* make_new(size_t n) { - return allocate_new(make_tracked_allocator(RawAllocator {}), n); - } - - template - requires(!std::is_array_v) - static inline void dealloc_delete(RawAllocator&& alloc, T* ptr) { - ptr->~T(); - allocator_reference alloc_ref = ::OpenVic::memory::make_allocator_reference(alloc); - alloc_ref.deallocate(ptr, sizeof(T), alignof(T)); - } - - template - requires(std::is_unbounded_array_v) - static inline void dealloc_delete(RawAllocator&& alloc, T* ptr, size_t n) { - std::destroy_n(ptr, n); - allocator_reference alloc_ref = ::OpenVic::memory::make_allocator_reference(alloc); - alloc_ref.deallocate(ptr, sizeof(T) * n, alignof(T)); - } - - template - requires(!std::is_array_v) - static inline void delete_at(T* ptr) { - dealloc_delete(make_tracked_allocator(RawAllocator {}), ptr); - } - - template - requires(std::is_unbounded_array_v) - static inline void delete_at(T* ptr, size_t n) { - dealloc_delete(make_tracked_allocator(RawAllocator {}), ptr, n); - } - - template - requires(!std::is_array_v) - static inline unique_ptr make_unique(Args&&... args) { - return allocate_unique(RawAllocator {}, std::forward(args)...); - } - - template - requires(std::is_unbounded_array_v) - static inline unique_ptr make_unique(size_t n) { - return allocate_unique(make_tracked_allocator(RawAllocator {}), n); - } - - template - requires(!std::is_array_v) - static inline unique_ptr make_unique_for_overwrite() { - using raw_ptr = std::unique_ptr>>; - - tracker alloc = make_tracked_allocator(RawAllocator {}); - auto memory = alloc.allocate_node(sizeof(T), alignof(T)); - // raw_ptr deallocates memory in case of constructor exception - raw_ptr result(static_cast(memory), { alloc }); - // pass ownership to return value using a deleter that calls destructor - return { result.release(), { alloc } }; - } - - template - requires(std::is_unbounded_array_v) - static inline unique_ptr make_unique_for_overwrite(size_t size) { - using raw_ptr = std::unique_ptr>>; - - tracker alloc = make_tracked_allocator(RawAllocator {}); - auto memory = alloc.allocate_array(size, sizeof(T), alignof(T)); - // raw_ptr deallocates memory in case of constructor exception - raw_ptr result(static_cast(memory), { alloc, size }); - // pass ownership to return value using a deleter that calls destructor - return { result.release(), { alloc, size } }; - } - - template - std::shared_ptr allocate_shared(RawAllocator&& alloc, Args&&... args) { - return foonathan::memory::allocate_shared( - make_tracked_allocator(std::forward(alloc)), std::forward(args)... - ); - } - - template - static inline std::shared_ptr make_shared(Args&&... args) { - return allocate_shared(RawAllocator {}, std::forward(args)...); - } - - template - using vector = foonathan::memory::vector>; - - template - using deque = OpenVic::utility::memory::deque>; - - template - using basic_string = - std::basic_string, foonathan::memory::std_allocator>>; - - template - using string_alloc = basic_string; - - using string = string_alloc<>; - - template - using wstring_alloc = basic_string; - - using wstring = wstring_alloc<>; - - template - using colony = plf::colony>>; - - template> - using stack = std::stack; - - template> - using queue = std::queue; - - template, class RawAllocator = foonathan::memory::default_allocator> - using basic_stringstream = - std::basic_stringstream>>; - - template - using stringstream_alloc = basic_stringstream, RawAllocator>; - template - using wstringstream_alloc = basic_stringstream, RawAllocator>; - - using stringstream = stringstream_alloc<>; - using wstringstream = wstringstream_alloc<>; - - template - using cow_ptr = cow_ptr>>; - - template - using cow_vector = cow_vector>>; - - namespace fmt { - template - using basic_memory_buffer = ::fmt::basic_memory_buffer< - T, ::fmt::inline_buffer_size, foonathan::memory::std_allocator>>; - - inline static memory::string vformat(::fmt::string_view fmt, ::fmt::format_args args) { - memory::fmt::basic_memory_buffer buf {}; - ::fmt::vformat_to(std::back_inserter(buf), fmt, args); - return memory::string(buf.data(), buf.size()); - } - - template - memory::string format(::fmt::string_view fmt, const Args&... args) { - return memory::fmt::vformat(fmt, ::fmt::make_format_args(args...)); - } - } -} diff --git a/src/openvic-simulation/utility/DequeMemory.hpp b/src/openvic-simulation/utility/DequeMemory.hpp deleted file mode 100644 index b5346e168..000000000 --- a/src/openvic-simulation/utility/DequeMemory.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include - -#include "openvic-simulation/utility/Deque.hpp" - -#include -#include - -namespace OpenVic::utility::memory { - template - FOONATHAN_ALIAS_TEMPLATE(deque, OpenVic::utility::deque>); - - template - FOONATHAN_ALIAS_TEMPLATE(deque_scoped_alloc, OpenVic::utility::deque>>); -} diff --git a/tests/src/utility/DualAdjacent.cpp b/tests/src/core/DualAdjacent.cpp similarity index 99% rename from tests/src/utility/DualAdjacent.cpp rename to tests/src/core/DualAdjacent.cpp index 151ae4216..bf591dae0 100644 --- a/tests/src/utility/DualAdjacent.cpp +++ b/tests/src/core/DualAdjacent.cpp @@ -1,10 +1,11 @@ #include #include +#include #include #include -#include "openvic-simulation/utility/Algorithm.hpp" +#include "openvic-simulation/core/Algorithm.hpp" #include "Helper.hpp" // IWYU pragma: keep #include diff --git a/tests/src/types/Signal.cpp b/tests/src/core/Signal.cpp similarity index 99% rename from tests/src/types/Signal.cpp rename to tests/src/core/Signal.cpp index aa8ada241..08a20daf1 100644 --- a/tests/src/types/Signal.cpp +++ b/tests/src/core/Signal.cpp @@ -1,11 +1,11 @@ -#include "openvic-simulation/types/Signal.hpp" +#include "openvic-simulation/core/Signal.hpp" #include #include #include #include -#include "openvic-simulation/types/SpinMutex.hpp" +#include "openvic-simulation/core/thread/SpinMutex.hpp" #include "Helper.hpp" // IWYU pragma: keep #include diff --git a/tests/src/types/FixedVector.cpp b/tests/src/core/container/FixedVector.cpp similarity index 64% rename from tests/src/types/FixedVector.cpp rename to tests/src/core/container/FixedVector.cpp index 6c36f15b1..bdaac11ea 100644 --- a/tests/src/types/FixedVector.cpp +++ b/tests/src/core/container/FixedVector.cpp @@ -1,4 +1,4 @@ -#include "openvic-simulation/types/FixedVector.hpp" +#include "openvic-simulation/core/container/FixedVector.hpp" #include #include @@ -6,57 +6,61 @@ #include #include -using namespace OpenVic::_detail; +using namespace OpenVic; // A simple test type to demonstrate a more complex object. // It has a multi-argument constructor to test emplace_back. struct ComplexType { public: - int index; - std::string name; + int index; + std::string name; - ComplexType(int new_index, std::string name_val) : index {new_index}, name(std::move(name_val)) {} + ComplexType(int new_index, std::string name_val) : index { new_index }, name(std::move(name_val)) {} - // Equality operator for assertions - bool operator==(const ComplexType& other) const { - return index == other.index && name == other.name; - } + // Equality operator for assertions + bool operator==(const ComplexType& other) const { + return index == other.index && name == other.name; + } }; // A type that is explicitly non-copyable and non-movable. struct NonCopyableNonMovable { public: - int value; + int value; - // A simple constructor - NonCopyableNonMovable(int new_value) : value {new_value} {} + // A simple constructor + NonCopyableNonMovable(int new_value) : value { new_value } {} - // Explicitly delete copy and move operations - NonCopyableNonMovable(const NonCopyableNonMovable&) = delete; - NonCopyableNonMovable& operator=(const NonCopyableNonMovable&) = delete; - NonCopyableNonMovable(NonCopyableNonMovable&&) = delete; - NonCopyableNonMovable& operator=(NonCopyableNonMovable&&) = delete; + // Explicitly delete copy and move operations + NonCopyableNonMovable(const NonCopyableNonMovable&) = delete; + NonCopyableNonMovable& operator=(const NonCopyableNonMovable&) = delete; + NonCopyableNonMovable(NonCopyableNonMovable&&) = delete; + NonCopyableNonMovable& operator=(NonCopyableNonMovable&&) = delete; - bool operator==(const NonCopyableNonMovable& other) const { - return value == other.value; - } + bool operator==(const NonCopyableNonMovable& other) const { + return value == other.value; + } }; // A custom type to verify constructors and destructors are called correctly. struct DestructionCounter { public: - static int constructor_count; - static int destructor_count; - - DestructionCounter() { constructor_count++; } - ~DestructionCounter() { destructor_count++; } - - // Must be non-copyable/non-movable to be safe to use in a fixed vector - // and for better testing of in-place construction. - DestructionCounter(const DestructionCounter&) = delete; - DestructionCounter(DestructionCounter&&) = delete; - DestructionCounter& operator=(const DestructionCounter&) = delete; - DestructionCounter& operator=(DestructionCounter&&) = delete; + static int constructor_count; + static int destructor_count; + + DestructionCounter() { + constructor_count++; + } + ~DestructionCounter() { + destructor_count++; + } + + // Must be non-copyable/non-movable to be safe to use in a fixed vector + // and for better testing of in-place construction. + DestructionCounter(const DestructionCounter&) = delete; + DestructionCounter(DestructionCounter&&) = delete; + DestructionCounter& operator=(const DestructionCounter&) = delete; + DestructionCounter& operator=(DestructionCounter&&) = delete; }; // Define the static member variables outside the class. @@ -65,7 +69,7 @@ int DestructionCounter::constructor_count = 0; int DestructionCounter::destructor_count = 0; // A simple test case to check the basic constructor and accessors with a simple type. -TEST_CASE("FixedVector Construction and basic accessors","[FixedVector]") { +TEST_CASE("FixedVector Construction and basic accessors", "[FixedVector]") { constexpr size_t capacity = 5; FixedVector vec(capacity); @@ -77,9 +81,9 @@ TEST_CASE("FixedVector Construction and basic accessors","[FixedVector]") { } // Test the generator constructor with a single value return. -TEST_CASE("FixedVector Generator constructor (single value)","[FixedVector]") { +TEST_CASE("FixedVector Generator constructor (single value)", "[FixedVector]") { constexpr size_t capacity = 3; - FixedVector vec(capacity, [](const size_t i)->auto { + FixedVector vec(capacity, [](const size_t i) -> auto { return std::make_tuple(static_cast(i), std::to_string(i)); }); @@ -88,14 +92,14 @@ TEST_CASE("FixedVector Generator constructor (single value)","[FixedVector]") { REQUIRE(vec.max_size() == capacity); for (size_t i = 0; i < capacity; ++i) { - CHECK(vec[i] == ComplexType{static_cast(i), std::to_string(i)}); + CHECK(vec[i] == ComplexType { static_cast(i), std::to_string(i) }); } } // Test the generator constructor with a tuple return. -TEST_CASE("FixedVector Generator constructor (tuple)","[FixedVector]") { +TEST_CASE("FixedVector Generator constructor (tuple)", "[FixedVector]") { constexpr size_t capacity = 3; - FixedVector vec(capacity, [](const size_t i)->auto { + FixedVector vec(capacity, [](const size_t i) -> auto { return std::make_tuple(static_cast(i), std::to_string(i)); }); @@ -104,41 +108,41 @@ TEST_CASE("FixedVector Generator constructor (tuple)","[FixedVector]") { REQUIRE(vec.max_size() == capacity); for (size_t i = 0; i < capacity; ++i) { - CHECK(vec[i] == ComplexType{static_cast(i), std::to_string(i)}); + CHECK(vec[i] == ComplexType { static_cast(i), std::to_string(i) }); } } // Test emplace_back, pop_back, and clear with a complex type. -TEST_CASE("FixedVector Manipulation (emplace_back, pop_back, clear)","[FixedVector]") { +TEST_CASE("FixedVector Manipulation (emplace_back, pop_back, clear)", "[FixedVector]") { constexpr size_t capacity = 4; FixedVector vec(capacity); // Emplace elements with multiple arguments vec.emplace_back(1, "hello"); REQUIRE(vec.size() == 1); - CHECK(vec.back() == ComplexType{1, "hello"}); + CHECK(vec.back() == ComplexType { 1, "hello" }); // Emplace up to capacity vec.emplace_back(2, "world"); vec.emplace_back(3, "!"); vec.emplace_back(4, "done"); REQUIRE(vec.size() == 4); - CHECK(vec.back() == ComplexType{4, "done"}); + CHECK(vec.back() == ComplexType { 4, "done" }); // Test emplace_back on a full vector (should not change size) auto it = vec.emplace_back(5, "oops"); CHECK(vec.size() == 4); CHECK(it == vec.end()); - CHECK(vec.back() == ComplexType{4, "done"}); // The last element is unchanged + CHECK(vec.back() == ComplexType { 4, "done" }); // The last element is unchanged // Test pop_back vec.pop_back(); REQUIRE(vec.size() == 3); - CHECK(vec.back() == ComplexType{3, "!"}); + CHECK(vec.back() == ComplexType { 3, "!" }); vec.pop_back(); REQUIRE(vec.size() == 2); - CHECK(vec.back() == ComplexType{2, "world"}); + CHECK(vec.back() == ComplexType { 2, "world" }); // Test clear vec.clear(); @@ -151,7 +155,7 @@ TEST_CASE("FixedVector Manipulation (emplace_back, pop_back, clear)","[FixedVect } // Test that accessor methods return references to the same memory location -TEST_CASE("FixedVector Accessor reference consistency","[FixedVector]") { +TEST_CASE("FixedVector Accessor reference consistency", "[FixedVector]") { constexpr size_t capacity = 3; FixedVector vec(capacity); @@ -169,7 +173,7 @@ TEST_CASE("FixedVector Accessor reference consistency","[FixedVector]") { } // Test with a non-copyable and non-movable type to ensure in-place construction. -TEST_CASE("FixedVector Non-copyable, non-movable type","[FixedVector]") { +TEST_CASE("FixedVector Non-copyable, non-movable type", "[FixedVector]") { constexpr size_t capacity = 2; FixedVector vec(capacity); @@ -187,7 +191,7 @@ TEST_CASE("FixedVector Non-copyable, non-movable type","[FixedVector]") { CHECK(&vec.back() == it1); } -TEST_CASE("FixedVector Destruction, Clear, and Refill","[FixedVector]") { +TEST_CASE("FixedVector Destruction, Clear, and Refill", "[FixedVector]") { // Reset the counters for a clean test DestructionCounter::constructor_count = 0; DestructionCounter::destructor_count = 0; @@ -209,7 +213,7 @@ TEST_CASE("FixedVector Destruction, Clear, and Refill","[FixedVector]") { vec.clear(); CHECK(vec.size() == 0); CHECK(DestructionCounter::destructor_count == 2); // All elements should be destructed - + // Test refilling after a clear vec.emplace_back(); CHECK(vec.size() == 1); @@ -218,4 +222,4 @@ TEST_CASE("FixedVector Destruction, Clear, and Refill","[FixedVector]") { // After the scope, the last element should also be destructed CHECK(DestructionCounter::destructor_count == 3); -} \ No newline at end of file +} diff --git a/tests/src/types/RingBuffer.cpp b/tests/src/core/container/RingBuffer.cpp similarity index 99% rename from tests/src/types/RingBuffer.cpp rename to tests/src/core/container/RingBuffer.cpp index 9fa4e434a..ab73be81b 100644 --- a/tests/src/types/RingBuffer.cpp +++ b/tests/src/core/container/RingBuffer.cpp @@ -1,4 +1,4 @@ -#include "openvic-simulation/types/RingBuffer.hpp" +#include "openvic-simulation/core/container/RingBuffer.hpp" #include "Helper.hpp" // IWYU pragma: keep #include diff --git a/tests/src/types/Approx.hpp b/tests/src/core/object/Approx.hpp similarity index 95% rename from tests/src/types/Approx.hpp rename to tests/src/core/object/Approx.hpp index eb7c5202e..2229c2e39 100644 --- a/tests/src/types/Approx.hpp +++ b/tests/src/core/object/Approx.hpp @@ -1,12 +1,10 @@ #pragma once #include -#include -#include #include -#include "openvic-simulation/types/Vector.hpp" -#include "openvic-simulation/utility/Math.hpp" +#include "openvic-simulation/core/Math.hpp" +#include "openvic-simulation/core/object/Vector.hpp" #include "Vector.hpp" // IWYU pragma: keep #include diff --git a/tests/src/types/AtomicFixedPoint.cpp b/tests/src/core/object/AtomicFixedPoint.cpp similarity index 92% rename from tests/src/types/AtomicFixedPoint.cpp rename to tests/src/core/object/AtomicFixedPoint.cpp index fe2296aa2..d0189221b 100644 --- a/tests/src/types/AtomicFixedPoint.cpp +++ b/tests/src/core/object/AtomicFixedPoint.cpp @@ -1,7 +1,7 @@ #include -#include "openvic-simulation/types/fixed_point/Atomic.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/object/FixedPoint/Atomic.hpp" #include "Helper.hpp" // IWYU pragma: keep #include "Numeric.hpp" // IWYU pragma: keep diff --git a/tests/src/types/Colour.cpp b/tests/src/core/object/Colour.cpp similarity index 99% rename from tests/src/types/Colour.cpp rename to tests/src/core/object/Colour.cpp index 206ac93fc..cb32a4d2a 100644 --- a/tests/src/types/Colour.cpp +++ b/tests/src/core/object/Colour.cpp @@ -1,7 +1,5 @@ -#include "openvic-simulation/types/Colour.hpp" +#include "openvic-simulation/core/object/Colour.hpp" -#include -#include #include #include "Colour.hpp" // IWYU pragma: keep diff --git a/tests/src/types/Colour.hpp b/tests/src/core/object/Colour.hpp similarity index 87% rename from tests/src/types/Colour.hpp rename to tests/src/core/object/Colour.hpp index 5768c615d..1f37e0f7a 100644 --- a/tests/src/types/Colour.hpp +++ b/tests/src/core/object/Colour.hpp @@ -1,8 +1,9 @@ #pragma once -#include +#include +#include -#include "openvic-simulation/types/Colour.hpp" +#include "openvic-simulation/core/object/Colour.hpp" #include #include diff --git a/tests/src/types/Date.cpp b/tests/src/core/object/Date.cpp similarity index 99% rename from tests/src/types/Date.cpp rename to tests/src/core/object/Date.cpp index ff8126a11..0b91fc6ee 100644 --- a/tests/src/types/Date.cpp +++ b/tests/src/core/object/Date.cpp @@ -1,7 +1,5 @@ -#include "openvic-simulation/types/Date.hpp" +#include "openvic-simulation/core/object/Date.hpp" -#include -#include #include #include "Helper.hpp" // IWYU pragma: keep diff --git a/tests/src/types/FixedPoint.cpp b/tests/src/core/object/FixedPoint.cpp similarity index 99% rename from tests/src/types/FixedPoint.cpp rename to tests/src/core/object/FixedPoint.cpp index 56809e310..3ac7aff58 100644 --- a/tests/src/types/FixedPoint.cpp +++ b/tests/src/core/object/FixedPoint.cpp @@ -1,9 +1,6 @@ -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" -#include -#include #include -#include #include #include "Approx.hpp" diff --git a/tests/src/types/Numeric.hpp b/tests/src/core/object/Numeric.hpp similarity index 91% rename from tests/src/types/Numeric.hpp rename to tests/src/core/object/Numeric.hpp index d7e2cfd13..fe27ff70c 100644 --- a/tests/src/types/Numeric.hpp +++ b/tests/src/core/object/Numeric.hpp @@ -1,8 +1,9 @@ #pragma once #include +#include -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include #include diff --git a/tests/src/types/Timespan.cpp b/tests/src/core/object/Timespan.cpp similarity index 96% rename from tests/src/types/Timespan.cpp rename to tests/src/core/object/Timespan.cpp index bd2b17455..1a429ac2e 100644 --- a/tests/src/types/Timespan.cpp +++ b/tests/src/core/object/Timespan.cpp @@ -1,8 +1,6 @@ -#include -#include #include -#include "openvic-simulation/types/Date.hpp" +#include "openvic-simulation/core/object/Date.hpp" #include "Helper.hpp" // IWYU pragma: keep #include diff --git a/tests/src/types/Vector.hpp b/tests/src/core/object/Vector.hpp similarity index 96% rename from tests/src/types/Vector.hpp rename to tests/src/core/object/Vector.hpp index 9c23bf5db..f3971e22f 100644 --- a/tests/src/types/Vector.hpp +++ b/tests/src/core/object/Vector.hpp @@ -1,7 +1,7 @@ #pragma once -#include "openvic-simulation/types/Vector.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/object/Vector.hpp" #include "Numeric.hpp" #include diff --git a/tests/src/types/Vector2.cpp b/tests/src/core/object/Vector2.cpp similarity index 98% rename from tests/src/types/Vector2.cpp rename to tests/src/core/object/Vector2.cpp index 154605cc2..b97795f81 100644 --- a/tests/src/types/Vector2.cpp +++ b/tests/src/core/object/Vector2.cpp @@ -1,8 +1,9 @@ #include +#include #include -#include "openvic-simulation/types/Vector.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/object/Vector.hpp" #include "Approx.hpp" #include "Helper.hpp" // IWYU pragma: keep diff --git a/tests/src/types/Vector3.cpp b/tests/src/core/object/Vector3.cpp similarity index 98% rename from tests/src/types/Vector3.cpp rename to tests/src/core/object/Vector3.cpp index daecfa4ea..8b39e6601 100644 --- a/tests/src/types/Vector3.cpp +++ b/tests/src/core/object/Vector3.cpp @@ -1,8 +1,9 @@ #include +#include #include -#include "openvic-simulation/types/Vector.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/object/Vector.hpp" #include "Approx.hpp" #include "Helper.hpp" // IWYU pragma: keep diff --git a/tests/src/types/Vector4.cpp b/tests/src/core/object/Vector4.cpp similarity index 98% rename from tests/src/types/Vector4.cpp rename to tests/src/core/object/Vector4.cpp index 8f7ce9bb8..718787bda 100644 --- a/tests/src/types/Vector4.cpp +++ b/tests/src/core/object/Vector4.cpp @@ -1,8 +1,9 @@ #include +#include #include -#include "openvic-simulation/types/Vector.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/object/Vector.hpp" #include "Approx.hpp" #include "Helper.hpp" // IWYU pragma: keep diff --git a/tests/src/utility/ExtendedMath.hpp b/tests/src/core/random/ExtendedMath.hpp similarity index 100% rename from tests/src/utility/ExtendedMath.hpp rename to tests/src/core/random/ExtendedMath.hpp diff --git a/tests/src/utility/WeightedSampling.cpp b/tests/src/core/random/WeightedSampling.cpp similarity index 59% rename from tests/src/utility/WeightedSampling.cpp rename to tests/src/core/random/WeightedSampling.cpp index 4148d88e1..9ed9af7ce 100644 --- a/tests/src/utility/WeightedSampling.cpp +++ b/tests/src/core/random/WeightedSampling.cpp @@ -1,11 +1,12 @@ -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/WeightedSampling.hpp" - -#include "utility/ExtendedMath.hpp" +#include "openvic-simulation/core/random/WeightedSampling.hpp" +#include #include #include +#include "openvic-simulation/core/object/FixedPoint.hpp" + +#include "core/random/ExtendedMath.hpp" #include #include @@ -16,25 +17,21 @@ constexpr uint32_t max_random_value = std::numeric_limits().max(); TEST_CASE("WeightedSampling limits", "[WeightedSampling]") { const fixed_point_t weights_sum = fixed_point_t::usable_max; - constexpr size_t perfect_divisor = 257; //4294967295 is perfectly divisible by 257 + constexpr size_t perfect_divisor = 257; // 4294967295 is perfectly divisible by 257 std::array weights {}; constexpr uint32_t step_size = max_random_value / weights.size(); weights.fill(weights_sum / weights.size()); for (size_t i = 0; i < weights.size(); ++i) { - CHECK(sample_weighted_index( - i * step_size, - weights, - weights_sum - ) == i); + CHECK(sample_weighted_index(i * step_size, weights, weights_sum) == i); } } TEST_CASE("WeightedSampling weights", "[WeightedSampling]") { - constexpr size_t max_length = 255; //weights_sum = (size^2+size)/2, this needs to be <= fixed_point_t::usable_max + constexpr size_t max_length = 255; // weights_sum = (size^2+size)/2, this needs to be <= fixed_point_t::usable_max std::array weights {}; - + fixed_point_t weights_sum = 0; for (size_t i = 0; i < weights.size(); ++i) { - const fixed_point_t weight = 1+i; + const fixed_point_t weight = 1 + i; weights_sum += weight; weights[i] = weight; } @@ -45,17 +42,9 @@ TEST_CASE("WeightedSampling weights", "[WeightedSampling]") { for (size_t i = 0; i < weights.size(); ++i) { cumulative_weight += weights[i]; const Int96DivisionResult random_value = portable_int96_div_int64( - portable_int64_mult_uint32_96bit( - cumulative_weight.get_raw_value(), - max_random_value - ), - weights_sum.get_raw_value() + portable_int64_mult_uint32_96bit(cumulative_weight.get_raw_value(), max_random_value), weights_sum.get_raw_value() ); - assert(!random_value.quotient_overflow); - CHECK(sample_weighted_index( - static_cast(random_value.quotient), - weights, - weights_sum - ) == i); + REQUIRE_FALSE(random_value.quotient_overflow); + CHECK(sample_weighted_index(static_cast(random_value.quotient), weights, weights_sum) == i); } -} \ No newline at end of file +} diff --git a/tests/src/core/reactive/DerivedState.cpp b/tests/src/core/reactive/DerivedState.cpp new file mode 100644 index 000000000..1df2bca0a --- /dev/null +++ b/tests/src/core/reactive/DerivedState.cpp @@ -0,0 +1,80 @@ +#include "openvic-simulation/core/reactive/DerivedState.hpp" + +#include "openvic-simulation/core/Signal.hpp" +#include "openvic-simulation/core/reactive/DependencyTracker.hpp" +#include "openvic-simulation/core/reactive/MutableState.hpp" + +#include +#include + +using namespace OpenVic; + +TEST_CASE("DerivedState untracked", "[DerivedState-untracked]") { + MutableState mutable_state_a(0); + MutableState mutable_state_b(0); + DerivedState sum( + [&a = static_cast&>(mutable_state_a), + &b = static_cast&>(mutable_state_b)](DependencyTracker& tracker) -> int { + return a.get(tracker) + b.get(tracker); + } + ); + + CHECK(sum.get_untracked() == 0); + mutable_state_a.set(1); + CHECK(sum.get_untracked() == 1); + mutable_state_b.set(2); + CHECK(sum.get_untracked() == 3); +} + +TEST_CASE("DerivedState reactive", "[DerivedState-reactive]") { + MutableState mutable_state_a(0); + MutableState mutable_state_b(0); + DerivedState sum( + [&a = static_cast&>(mutable_state_a), + &b = static_cast&>(mutable_state_b)](DependencyTracker& tracker) -> int { + return a.get(tracker) + b.get(tracker); + } + ); + + int times_marked_dirty = 0; + connection conn = sum.connect([×_marked_dirty]() -> void { + ++times_marked_dirty; + }); + + mutable_state_a.set(1); + CHECK(times_marked_dirty == 0); // starts dirty + CHECK(sum.get_untracked() == 1); // force update + + mutable_state_b.set(2); + CHECK(times_marked_dirty == 1); + + mutable_state_a.set(2); + CHECK(times_marked_dirty == 1); + + CHECK(sum.get_untracked() == 4); + mutable_state_a.set(3); + CHECK(times_marked_dirty == 2); +} + +TEST_CASE("DerivedState untracked in callback", "[DerivedState-untracked-in-callback]") { + MutableState mutable_state_a(0); + MutableState mutable_state_b(0); + DerivedState sum( + [&a = static_cast&>(mutable_state_a), + &b = static_cast&>(mutable_state_b)](DependencyTracker& tracker) -> int { + return a.get(tracker) + b.get(tracker); + } + ); + + connection conn; + int sum_value = sum.get([&sum, &conn, &sum_value](OpenVic::signal<>& sum_changed) mutable -> void { + conn = sum_changed.connect([&sum, &sum_value]() mutable -> void { + sum_value = sum.get_untracked(); + }); + }); + CHECK(sum_value == 0); + mutable_state_a.set(1); + CHECK(sum_value == 1); + mutable_state_a.set(2); + CHECK(sum_value == 2); +} diff --git a/tests/src/utility/reactive/MutableState.cpp b/tests/src/core/reactive/MutableState.cpp similarity index 83% rename from tests/src/utility/reactive/MutableState.cpp rename to tests/src/core/reactive/MutableState.cpp index 4e0463c7f..83cc9a009 100644 --- a/tests/src/utility/reactive/MutableState.cpp +++ b/tests/src/core/reactive/MutableState.cpp @@ -1,4 +1,4 @@ -#include "openvic-simulation/utility/reactive//MutableState.hpp" +#include "openvic-simulation/core/reactive//MutableState.hpp" #include #include @@ -8,7 +8,9 @@ using namespace OpenVic; TEST_CASE("MutableState", "[MutableState]") { int sum = 0; MutableState mutable_state(0); - connection conn = mutable_state.connect([&sum](const int new_value) { sum += new_value; }); + connection conn = mutable_state.connect([&sum](const int new_value) { + sum += new_value; + }); CHECK(sum == 0); mutable_state.set(1); CHECK(sum == 1); @@ -19,4 +21,4 @@ TEST_CASE("MutableState", "[MutableState]") { conn.disconnect(); mutable_state.set(3); CHECK(sum == 5); -} \ No newline at end of file +} diff --git a/tests/src/dataloader/NodeTools.cpp b/tests/src/dataloader/NodeTools.cpp index 6ebd6cf99..f4468a918 100644 --- a/tests/src/dataloader/NodeTools.cpp +++ b/tests/src/dataloader/NodeTools.cpp @@ -1,7 +1,6 @@ #include "openvic-simulation/dataloader/NodeTools.hpp" #include -#include #include #include #include @@ -22,14 +21,15 @@ #include #include -#include "openvic-simulation/types/Colour.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/StringUtils.hpp" +#include "openvic-simulation/core/memory/StringMap.hpp" +#include "openvic-simulation/core/object/Colour.hpp" +#include "openvic-simulation/core/object/Date.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/string/CharConv.hpp" -#include "../types/Colour.hpp" // IWYU pragma: keep #include "Helper.hpp" // IWYU pragma: keep -#include "types/Numeric.hpp" // IWYU pragma: keep +#include "core/object/Colour.hpp" // IWYU pragma: keep +#include "core/object/Numeric.hpp" // IWYU pragma: keep #include #include #include @@ -377,7 +377,7 @@ TEST_CASE("NodeTools expect integer functions", "[NodeTools][NodeTools-expect-fu std::string_view sv = ptr->value().view(); decltype(val) check; - std::from_chars_result result = OpenVic::StringUtils::string_to_uint64(sv, check, base); + std::from_chars_result result = OpenVic::string_to_uint64(sv, check, base); CHECK_IF(result.ec == std::errc {}); else { return false; @@ -559,7 +559,7 @@ TEST_CASE( auto callback = [](FlatValue const* ptr, text_format_t val) -> bool { using enum text_format_t; - static const string_map_t format_map = // + static const memory::string_map_t format_map = // { // { "left", left }, // { "right", right }, @@ -632,7 +632,7 @@ TEST_CASE( std::string_view sv = ptr->value().view(); int64_t check_int; - std::from_chars_result result = StringUtils::string_to_int64(sv, check_int); + std::from_chars_result result = OpenVic::string_to_int64(sv, check_int); CHECK_IF(result.ec == std::errc {}); else { return false; @@ -708,7 +708,7 @@ TEST_CASE("NodeTools expect vector functions", "[NodeTools][NodeTools-expect-fun if constexpr (std::same_as) { return check.from_chars(sv.data(), sv.data() + sv.size()); } else { - return StringUtils::from_chars(sv.data(), sv.data() + sv.size(), check); + return OpenVic::from_chars(sv.data(), sv.data() + sv.size(), check); } }(); CHECK_OR_CONTINUE(result.ec == std::errc {}); @@ -1397,14 +1397,14 @@ TEST_CASE( ) { Ast ast; - static const string_map_t map // + static const memory::string_map_t map // { // { "key_test1", "value_test1"sv }, { "key_test2", "value_test2"sv }, { "key_test3", "value_test3"sv } }; - static auto callback = [](string_map_t const& map, size_t expected_index, std::string_view val) { + static auto callback = [](memory::string_map_t const& map, size_t expected_index, std::string_view val) { CHECK_IF(map.size() > expected_index); else { return false; @@ -1426,7 +1426,8 @@ TEST_CASE( CHECK(NodeTools::expect_mapped_string(map, std::bind_front(callback, map, 1))(map.values_container()[1].first)); CHECK(NodeTools::expect_mapped_string(map, std::bind_front(callback, map, 2))(map.values_container()[2].first)); - static auto callback_false = [](string_map_t const& map, size_t check_index, std::string_view val) { + static auto callback_false = [ // + ](memory::string_map_t const& map, size_t check_index, std::string_view val) { for (auto [index, pair] : map | ranges::views::enumerate) { if (index != check_index) { continue; diff --git a/tests/src/pathfinding/AStarPathing.cpp b/tests/src/pathfinding/AStarPathing.cpp index d73d68978..ea809f1a2 100644 --- a/tests/src/pathfinding/AStarPathing.cpp +++ b/tests/src/pathfinding/AStarPathing.cpp @@ -1,9 +1,9 @@ #include "openvic-simulation/pathfinding/AStarPathing.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/object/Vector.hpp" #include "openvic-simulation/pathfinding/PointMap.hpp" -#include "openvic-simulation/types/Vector.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Containers.hpp" #include "Helper.hpp" #include "pathfinding/Pathing.hpp" diff --git a/tests/src/pathfinding/FringePathing.cpp b/tests/src/pathfinding/FringePathing.cpp index 252330914..91fe4eae3 100644 --- a/tests/src/pathfinding/FringePathing.cpp +++ b/tests/src/pathfinding/FringePathing.cpp @@ -1,9 +1,9 @@ #include "openvic-simulation/pathfinding/FringePathing.hpp" +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" +#include "openvic-simulation/core/object/Vector.hpp" #include "openvic-simulation/pathfinding/PointMap.hpp" -#include "openvic-simulation/types/Vector.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/Containers.hpp" #include "Helper.hpp" #include "pathfinding/Pathing.hpp" diff --git a/tests/src/pathfinding/Pathing.hpp b/tests/src/pathfinding/Pathing.hpp index 61e390b45..01d955094 100644 --- a/tests/src/pathfinding/Pathing.hpp +++ b/tests/src/pathfinding/Pathing.hpp @@ -1,12 +1,13 @@ #pragma once +#include #include -#include +#include +#include "openvic-simulation/core/memory/Vector.hpp" +#include "openvic-simulation/core/object/Vector.hpp" #include "openvic-simulation/pathfinding/PointMap.hpp" -#include "openvic-simulation/types/Vector.hpp" -#include "openvic-simulation/utility/Containers.hpp" #include "Helper.hpp" #include diff --git a/tests/src/pathfinding/PointMap.cpp b/tests/src/pathfinding/PointMap.cpp index 21951a1fb..9f51763a6 100644 --- a/tests/src/pathfinding/PointMap.cpp +++ b/tests/src/pathfinding/PointMap.cpp @@ -1,6 +1,6 @@ #include "openvic-simulation/pathfinding/PointMap.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/core/object/FixedPoint.hpp" #include "Helper.hpp" // IWYU pragma: keep #include diff --git a/tests/src/utility/reactive/DerivedState.cpp b/tests/src/utility/reactive/DerivedState.cpp deleted file mode 100644 index fd08a8c4f..000000000 --- a/tests/src/utility/reactive/DerivedState.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#include "openvic-simulation/types/Signal.hpp" -#include "openvic-simulation/utility/reactive/DependencyTracker.hpp" -#include "openvic-simulation/utility/reactive/DerivedState.hpp" -#include "openvic-simulation/utility/reactive/MutableState.hpp" - -#include -#include - -using namespace OpenVic; - -TEST_CASE("DerivedState untracked", "[DerivedState-untracked]") { - MutableState mutable_state_a(0); - MutableState mutable_state_b(0); - DerivedState sum([ - &a=static_cast&>(mutable_state_a), - &b=static_cast&>(mutable_state_b) - ](DependencyTracker& tracker)->int { - return a.get(tracker) + b.get(tracker); - }); - - CHECK(sum.get_untracked() == 0); - mutable_state_a.set(1); - CHECK(sum.get_untracked() == 1); - mutable_state_b.set(2); - CHECK(sum.get_untracked() == 3); -} - -TEST_CASE("DerivedState reactive", "[DerivedState-reactive]") { - MutableState mutable_state_a(0); - MutableState mutable_state_b(0); - DerivedState sum([ - &a=static_cast&>(mutable_state_a), - &b=static_cast&>(mutable_state_b) - ](DependencyTracker& tracker)->int { - return a.get(tracker) + b.get(tracker); - }); - - int times_marked_dirty = 0; - connection conn = sum.connect([×_marked_dirty]()->void { - ++times_marked_dirty; - }); - - mutable_state_a.set(1); - CHECK(times_marked_dirty == 0); //starts dirty - CHECK(sum.get_untracked() == 1); //force update - - mutable_state_b.set(2); - CHECK(times_marked_dirty == 1); - - mutable_state_a.set(2); - CHECK(times_marked_dirty == 1); - - CHECK(sum.get_untracked() == 4); - mutable_state_a.set(3); - CHECK(times_marked_dirty == 2); -} - -TEST_CASE("DerivedState untracked in callback", "[DerivedState-untracked-in-callback]") { - MutableState mutable_state_a(0); - MutableState mutable_state_b(0); - DerivedState sum([ - &a=static_cast&>(mutable_state_a), - &b=static_cast&>(mutable_state_b) - ](DependencyTracker& tracker)->int { - return a.get(tracker) + b.get(tracker); - }); - - connection conn; - int sum_value = sum.get([&sum,&conn,&sum_value](OpenVic::signal<>& sum_changed) mutable -> void { - conn = sum_changed.connect([&sum,&sum_value]() mutable -> void { - sum_value = sum.get_untracked(); - }); - }); - CHECK(sum_value == 0); - mutable_state_a.set(1); - CHECK(sum_value == 1); - mutable_state_a.set(2); - CHECK(sum_value == 2); -} \ No newline at end of file