refactor: use std ranges functions where applicable

This commit is contained in:
Jan
2024-03-24 20:49:15 +01:00
parent 132cccb971
commit 239001e6f2
42 changed files with 251 additions and 295 deletions

View File

@ -844,13 +844,12 @@ namespace IW4
if (techsetDefinition->GetTechniqueByIndex(i, techniqueName))
{
const auto stateBitsForTechnique = GetStateBitsForTechnique(techniqueName);
const auto foundStateBits =
std::find_if(m_state_bits.begin(),
m_state_bits.end(),
[stateBitsForTechnique](const GfxStateBits& s1)
{
return s1.loadBits[0] == stateBitsForTechnique.loadBits[0] && s1.loadBits[1] == stateBitsForTechnique.loadBits[1];
});
const auto foundStateBits = std::ranges::find_if(m_state_bits,
[stateBitsForTechnique](const GfxStateBits& s1)
{
return s1.loadBits[0] == stateBitsForTechnique.loadBits[0]
&& s1.loadBits[1] == stateBitsForTechnique.loadBits[1];
});
if (foundStateBits != m_state_bits.end())
{

View File

@ -403,22 +403,21 @@ namespace IW4
return false;
// Sort args by their update frequency
std::sort(pass.m_arguments.begin(),
pass.m_arguments.end(),
[](const PassShaderArgument& arg1, const PassShaderArgument& arg2)
{
if (arg1.m_update_frequency != arg2.m_update_frequency)
return arg1.m_update_frequency < arg2.m_update_frequency;
std::ranges::sort(pass.m_arguments,
[](const PassShaderArgument& arg1, const PassShaderArgument& arg2)
{
if (arg1.m_update_frequency != arg2.m_update_frequency)
return arg1.m_update_frequency < arg2.m_update_frequency;
if (arg1.m_arg.type != arg2.m_arg.type)
return arg1.m_arg.type < arg2.m_arg.type;
if (arg1.m_arg.type != arg2.m_arg.type)
return arg1.m_arg.type < arg2.m_arg.type;
if (arg1.m_arg.type == MTL_ARG_MATERIAL_VERTEX_CONST || arg1.m_arg.type == MTL_ARG_MATERIAL_PIXEL_CONST
|| arg1.m_arg.type == MTL_ARG_MATERIAL_PIXEL_SAMPLER)
return arg1.m_arg.u.codeSampler < arg2.m_arg.u.codeSampler;
if (arg1.m_arg.type == MTL_ARG_MATERIAL_VERTEX_CONST || arg1.m_arg.type == MTL_ARG_MATERIAL_PIXEL_CONST
|| arg1.m_arg.type == MTL_ARG_MATERIAL_PIXEL_SAMPLER)
return arg1.m_arg.u.codeSampler < arg2.m_arg.u.codeSampler;
return arg1.m_arg.dest < arg2.m_arg.dest;
});
return arg1.m_arg.dest < arg2.m_arg.dest;
});
AllocateVertexDecl();
@ -600,12 +599,11 @@ namespace IW4
size_t& registerOffset,
std::string& errorMessage) const
{
const auto matchingShaderConstant = std::find_if(shaderInfo.m_constants.begin(),
shaderInfo.m_constants.end(),
[argument](const d3d9::ShaderConstant& constant)
{
return constant.m_name == argument.m_argument_name;
});
const auto matchingShaderConstant = std::ranges::find_if(shaderInfo.m_constants,
[argument](const d3d9::ShaderConstant& constant)
{
return constant.m_name == argument.m_argument_name;
});
if (matchingShaderConstant == shaderInfo.m_constants.end())
{
@ -1013,14 +1011,14 @@ namespace IW4
return false;
}
const auto foundDestination = std::find(std::begin(materialStreamDestinationNames), std::end(materialStreamDestinationNames), destination);
const auto foundDestination = std::ranges::find(materialStreamDestinationNames, destination);
if (foundDestination == std::end(materialStreamDestinationNames))
{
errorMessage = "Unknown stream destination";
return false;
}
const auto foundSource = std::find(std::begin(materialStreamSourceNames), std::end(materialStreamSourceNames), source);
const auto foundSource = std::ranges::find(materialStreamSourceNames, source);
if (foundSource == std::end(materialStreamSourceNames))
{
errorMessage = "Unknown stream source";
@ -1187,7 +1185,7 @@ namespace IW4
assert(arg.m_arg.type == MTL_ARG_CODE_PIXEL_SAMPLER);
if (arg.m_arg.type == MTL_ARG_CODE_PIXEL_SAMPLER)
{
const auto customSampler = std::find(std::begin(g_customSamplerSrc), std::end(g_customSamplerSrc), arg.m_arg.u.codeSampler);
const auto customSampler = std::ranges::find(g_customSamplerSrc, arg.m_arg.u.codeSampler);
assert(customSampler != std::end(g_customSamplerSrc));
if (customSampler != std::end(g_customSamplerSrc))
{

View File

@ -64,16 +64,14 @@ bool AssetLoaderVertexDecl::LoadFromRaw(
return false;
}
const auto foundSourceAbbreviation =
std::find(std::begin(materialStreamSourceAbbreviation), std::end(materialStreamSourceAbbreviation), sourceAbbreviation);
const auto foundSourceAbbreviation = std::ranges::find(materialStreamSourceAbbreviation, sourceAbbreviation);
if (foundSourceAbbreviation == std::end(materialStreamSourceAbbreviation))
{
std::cout << "Unknown vertex decl source abbreviation: " << sourceAbbreviation << "\n";
return false;
}
const auto foundDestinationAbbreviation =
std::find(std::begin(materialStreamDestinationAbbreviation), std::end(materialStreamDestinationAbbreviation), destinationAbbreviation);
const auto foundDestinationAbbreviation = std::ranges::find(materialStreamDestinationAbbreviation, destinationAbbreviation);
if (foundDestinationAbbreviation == std::end(materialStreamDestinationAbbreviation))
{
std::cout << "Unknown vertex decl destination abbreviation: " << destinationAbbreviation << "\n";

View File

@ -48,12 +48,11 @@ void AssetLoaderFontIcon::PreprocessRow(std::vector<std::string>& row)
bool AssetLoaderFontIcon::RowIsEmpty(const std::vector<std::string>& row)
{
return std::all_of(row.begin(),
row.end(),
[](const std::string& cell)
{
return cell.empty();
});
return std::ranges::all_of(row,
[](const std::string& cell)
{
return cell.empty();
});
}
bool AssetLoaderFontIcon::ParseInt(int& value, const std::string& str)

View File

@ -27,7 +27,7 @@ namespace
{
std::string soundFilePath(sndAlias->assetFileName);
std::replace(soundFilePath.begin(), soundFilePath.end(), '\\', '/');
std::ranges::replace(soundFilePath, '\\', '/');
for (const auto& droppedPrefix : PREFIXES_TO_DROP)
{
if (soundFilePath.rfind(droppedPrefix, 0) != std::string::npos)

View File

@ -56,12 +56,11 @@ class IPak::Impl : public ObjContainerReferenceable
m_index_entries.push_back(indexEntry);
}
std::sort(m_index_entries.begin(),
m_index_entries.end(),
[](const IPakIndexEntry& entry1, const IPakIndexEntry& entry2)
{
return entry1.key.combinedKey < entry2.key.combinedKey;
});
std::ranges::sort(m_index_entries,
[](const IPakIndexEntry& entry1, const IPakIndexEntry& entry2)
{
return entry1.key.combinedKey < entry2.key.combinedKey;
});
return true;
}

View File

@ -71,12 +71,11 @@ public:
m_stream_mutex.lock();
ChunkBuffer* reservedChunkBuffer;
const auto freeChunkBuffer = std::find_if(m_chunk_buffers.begin(),
m_chunk_buffers.end(),
[](ChunkBuffer* chunkBuffer)
{
return chunkBuffer->m_using_stream == nullptr;
});
const auto freeChunkBuffer = std::ranges::find_if(m_chunk_buffers,
[](ChunkBuffer* chunkBuffer)
{
return chunkBuffer->m_using_stream == nullptr;
});
if (freeChunkBuffer == m_chunk_buffers.end())
{
@ -111,12 +110,11 @@ public:
{
m_stream_mutex.lock();
const auto openStreamEntry = std::find_if(m_open_streams.begin(),
m_open_streams.end(),
[stream](const ManagedStream& managedStream)
{
return managedStream.m_stream == stream;
});
const auto openStreamEntry = std::ranges::find_if(m_open_streams,
[stream](const ManagedStream& managedStream)
{
return managedStream.m_stream == stream;
});
if (openStreamEntry != m_open_streams.end())
{
@ -127,7 +125,7 @@ public:
// Only keep previously allocated chunk buffer if we did not get over the limit of idle chunk buffers
if (m_chunk_buffers.size() > CHUNK_BUFFER_COUNT_IDLE_LIMIT)
{
const auto chunkBufferEntry = std::find(m_chunk_buffers.begin(), m_chunk_buffers.end(), chunkBuffer);
const auto chunkBufferEntry = std::ranges::find(m_chunk_buffers, chunkBuffer);
if (chunkBufferEntry != m_chunk_buffers.end())
{

View File

@ -248,7 +248,7 @@ public:
}
auto iwdFilename = fileName;
std::replace(iwdFilename.begin(), iwdFilename.end(), '\\', '/');
std::ranges::replace(iwdFilename, '\\', '/');
const auto iwdEntry = m_entry_map.find(iwdFilename);

View File

@ -46,12 +46,11 @@ public:
void AddSound(const std::string& soundFilePath, unsigned int soundId, bool looping, bool streamed) override
{
auto itr = std::find_if(this->m_sounds.begin(),
this->m_sounds.end(),
[soundId](SoundBankEntryInfo& entry)
{
return entry.m_sound_id == soundId;
});
auto itr = std::ranges::find_if(this->m_sounds,
[soundId](SoundBankEntryInfo& entry)
{
return entry.m_sound_id == soundId;
});
if (itr == this->m_sounds.end())
{

View File

@ -179,12 +179,11 @@ namespace state_map
const auto tokenValue =
valueToken.m_type == SimpleParserValueType::IDENTIFIER ? valueToken.IdentifierValue() : std::to_string(valueToken.IntegerValue());
const auto referencedValue = std::find_if(var.m_values.begin(),
var.m_values.end(),
[&tokenValue](const StateMapLayoutVarValue& value)
{
return value.m_name == tokenValue;
});
const auto referencedValue = std::ranges::find_if(var.m_values,
[&tokenValue](const StateMapLayoutVarValue& value)
{
return value.m_name == tokenValue;
});
if (referencedValue == var.m_values.end())
throw ParsingException(valueToken.GetPos(), "Not part of the valid values for this var");

View File

@ -40,19 +40,18 @@ void StateMapHandler::ApplyStateMap(const uint32_t* baseStateBits, uint32_t* out
for (auto entryIndex = 0u; entryIndex < m_state_map.m_state_map_entries.size(); entryIndex++)
{
const auto& entry = m_state_map.m_state_map_entries[entryIndex];
const auto matchingRule = std::find_if(entry.m_rules.begin(),
entry.m_rules.end(),
[&vars](const std::unique_ptr<StateMapRule>& rule)
{
const auto matchingCondition = std::find_if(rule->m_conditions.begin(),
rule->m_conditions.end(),
[&vars](std::unique_ptr<ISimpleExpression>& condition)
{
return condition->EvaluateNonStatic(&vars).IsTruthy();
});
const auto matchingRule = std::ranges::find_if(entry.m_rules,
[&vars](const std::unique_ptr<StateMapRule>& rule)
{
const auto matchingCondition =
std::ranges::find_if(rule->m_conditions,
[&vars](std::unique_ptr<ISimpleExpression>& condition)
{
return condition->EvaluateNonStatic(&vars).IsTruthy();
});
return matchingCondition != rule->m_conditions.end();
});
return matchingCondition != rule->m_conditions.end();
});
if (matchingRule != entry.m_rules.end())
ApplyRule(m_state_map_layout.m_entry_layout.m_entries[entryIndex], **matchingRule, outStateBits);
@ -68,12 +67,11 @@ StateMapVars StateMapHandler::BuildVars(const uint32_t* baseStateBits) const
for (const auto& var : m_state_map_layout.m_var_layout.m_vars)
{
const auto baseStateBitField = baseStateBits[var.m_state_bits_index];
const auto matchingValue = std::find_if(var.m_values.begin(),
var.m_values.end(),
[&baseStateBitField](const StateMapLayoutVarValue& value)
{
return (baseStateBitField & value.m_state_bits_mask) == value.m_state_bits_mask;
});
const auto matchingValue = std::ranges::find_if(var.m_values,
[&baseStateBitField](const StateMapLayoutVarValue& value)
{
return (baseStateBitField & value.m_state_bits_mask) == value.m_state_bits_mask;
});
if (matchingValue != var.m_values.end())
result.AddValue(var.m_name, matchingValue->m_name);