Skip to content

Commit

Permalink
code smell 4, use ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
payetvin committed Jun 17, 2024
1 parent 72ed904 commit 1fa74d6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/libs/antares/study/include/antares/study/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Parameters final
** \return True if the settings have been loaded, false if at least one error has occured
*/
bool loadFromFile(const AnyString& filename,
StudyVersion& version,
const StudyVersion& version,
const StudyLoadOptions& options);

/*!
Expand Down
6 changes: 3 additions & 3 deletions src/libs/antares/study/parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ void Parameters::resetSeeds()
// For retro-compatibility, the wind ts-generator should produce the
// same results than before 3.8.
// It must have the same seed than before
auto increment = (unsigned)antaresSeedIncrement;
auto s = (unsigned)antaresSeedDefaultValue;
auto increment = antaresSeedIncrement;
auto s = antaresSeedDefaultValue;

seed[seedTsGenWind] = s;
// The same way for all others
Expand Down Expand Up @@ -1984,7 +1984,7 @@ void Parameters::saveToINI(IniFile& ini) const
}

bool Parameters::loadFromFile(const AnyString& filename,
StudyVersion& version,
const StudyVersion& version,
const StudyLoadOptions& options)
{
// Loading the INI file
Expand Down
40 changes: 19 additions & 21 deletions src/libs/antares/study/parts/hydro/allocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,30 +170,28 @@ bool HydroAllocation::loadFromFile(const AreaName& referencearea,
clear();

IniFile ini;
if (fs::exists(filename) && ini.open(filename))
{
if (!ini.empty())
{
ini.each(
[this](const IniFile::Section& section)
{
for (auto* p = section.firstProperty; p; p = p->next)
{
double coeff = p->value.to<double>();
if (!Utils::isZero(coeff))
{
AreaName areaname = p->key;
areaname.toLower();
pValues[areaname] = coeff;
}
}
});
}
}
else
if (!fs::exists(filename) || !ini.open(filename))
{
pValues[referencearea] = 1.0;
return true;
}

if (ini.empty())
return true;

ini.each([this](const IniFile::Section& section)
{
for (auto* p = section.firstProperty; p; p = p->next)
{
double coeff = p->value.to<double>();
if (!Utils::isZero(coeff))
{
AreaName areaname = p->key;
areaname.toLower();
pValues[areaname] = coeff;
}
}
});
return true;
}

Expand Down
31 changes: 10 additions & 21 deletions src/libs/antares/study/parts/short-term-storage/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ namespace Antares::Data::ShortTermStorage
{
bool STStorageInput::validate() const
{
return std::all_of(storagesByIndex.cbegin(),
storagesByIndex.cend(),
[](auto& cluster) { return cluster.validate(); });
return std::ranges::all_of(storagesByIndex, [](auto& cluster)
{ return cluster.validate(); });
}

bool STStorageInput::createSTStorageClustersFromIniFile(const fs::path& path)
Expand Down Expand Up @@ -101,39 +100,29 @@ bool STStorageInput::saveToFolder(const std::string& folder) const
IniFile ini;

logs.debug() << "saving file " << pathIni;
std::for_each(storagesByIndex.cbegin(),
storagesByIndex.cend(),
[&ini](auto& storage) { return storage.saveProperties(ini); });
std::ranges::for_each(storagesByIndex, [&ini](auto& storage)
{ return storage.saveProperties(ini); });

return ini.save(pathIni);
}

bool STStorageInput::saveDataSeriesToFolder(const std::string& folder) const
{
Yuni::IO::Directory::Create(folder);
return std::all_of(storagesByIndex.cbegin(),
storagesByIndex.cend(),
[&folder](auto& storage)
{ return storage.saveSeries(folder + SEP + storage.id); });
return std::ranges::all_of(storagesByIndex, [&folder](auto& storage)
{ return storage.saveSeries(folder + SEP + storage.id); });
}

std::size_t STStorageInput::count() const
{
return std::count_if(storagesByIndex.begin(),
storagesByIndex.end(),
[](const STStorageCluster& st) { return st.properties.enabled; });
return std::ranges::count_if(storagesByIndex, [](const STStorageCluster& st)
{ return st.properties.enabled; });
}

uint STStorageInput::removeDisabledClusters()
{
const auto& it = std::remove_if(storagesByIndex.begin(),
storagesByIndex.end(),
[](const auto& c) { return !c.enabled(); });

uint disabledCount = std::distance(it, storagesByIndex.end());
storagesByIndex.erase(it, storagesByIndex.end());

return disabledCount;
return std::erase_if(storagesByIndex, [](const auto& c)
{ return !c.enabled(); });
}

} // namespace Antares::Data::ShortTermStorage

0 comments on commit 1fa74d6

Please sign in to comment.