Merge pull request #20449 from hyarion/refactor/cpp20-contains

Avoid using `count` and `std::find` where `contains` and `isEmpty` express intent better
This commit is contained in:
Chris Hennes
2025-05-05 10:31:33 -05:00
committed by GitHub
43 changed files with 118 additions and 103 deletions

View File

@@ -91,7 +91,7 @@ bool Command::has(const std::string& attr) const
{
std::string a(attr);
boost::to_upper(a);
return Parameters.count(a) > 0;
return Parameters.contains(a);
}
std::string Command::toGCode(int precision, bool padzero) const

View File

@@ -281,7 +281,7 @@ PyObject* CommandPy::getCustomAttributes(const char* attr) const
if (satt.length() == 1) {
if (isalpha(satt[0])) {
boost::to_upper(satt);
if (getCommandPtr()->Parameters.count(satt)) {
if (getCommandPtr()->Parameters.contains(satt)) {
return PyFloat_FromDouble(getCommandPtr()->Parameters[satt]);
}
Py_INCREF(Py_None);

View File

@@ -189,7 +189,7 @@ bool ImpExpDxfRead::OnReadBlock(const std::string& name, int flags)
// and don't want to be complaining about unhandled entity types.
// Note that if it *is* for a hatch we could actually import it and use it to draw a hatch.
}
else if (Blocks.count(name) > 0) {
else if (Blocks.contains(name)) {
ImportError("Duplicate block name '%s'\n", name);
}
else {

View File

@@ -2689,7 +2689,7 @@ bool CDxfRead::ReadSection()
}
void CDxfRead::ProcessLayerReference(CDxfRead* object, void* target)
{
if (object->Layers.count(object->m_record_data) == 0) {
if (!object->Layers.contains(object->m_record_data)) {
object->ImportError("First reference to missing Layer '%s'", object->m_record_data);
// Synthesize the Layer so we don't get the same error again.
// We need to take copies of the string arguments because MakeLayer uses them as move

View File

@@ -92,7 +92,7 @@ MaterialLibrary::getMaterialTree(const std::shared_ptr<Materials::MaterialFilter
for (auto& itp : list) {
if (!itp.isEmpty()) {
// Add the folder only if it's not already there
if (node->count(itp) == 0) {
if (!node->contains(itp)) {
auto mapPtr = std::make_shared<
std::map<QString, std::shared_ptr<MaterialTreeNode>>>();
std::shared_ptr<MaterialTreeNode> child =
@@ -125,7 +125,7 @@ MaterialLibrary::getMaterialTree(const std::shared_ptr<Materials::MaterialFilter
// auto node = materialTree;
// for (auto& itp : list) {
// // Add the folder only if it's not already there
// if (node->count(itp) == 0) {
// if (!node->contains(itp)) {
// std::shared_ptr<std::map<QString, std::shared_ptr<MaterialTreeNode>>>
// mapPtr = std::make_shared<
// std::map<QString, std::shared_ptr<MaterialTreeNode>>>();

View File

@@ -87,7 +87,7 @@ ModelLibrary::getModelTree(ModelFilter filter) const
std::shared_ptr<std::map<QString, std::shared_ptr<ModelTreeNode>>> node = modelTree;
for (auto& itp : list) {
// Add the folder only if it's not already there
if (node->count(itp) == 0) {
if (!node->contains(itp)) {
auto mapPtr =
std::make_shared<std::map<QString, std::shared_ptr<ModelTreeNode>>>();
std::shared_ptr<ModelTreeNode> child = std::make_shared<ModelTreeNode>();

View File

@@ -163,7 +163,7 @@ void ModelLoader::dereference(const QString& uuid,
auto childProperties = childYaml[childBase];
for (auto it = childProperties.begin(); it != childProperties.end(); it++) {
std::string name = it->first.as<std::string>();
if (exclude.count(QString::fromStdString(name)) == 0) {
if (!exclude.contains(QString::fromStdString(name))) {
// showYaml(it->second);
if (!parentProperties[name]) {
parentProperties[name] = it->second;
@@ -261,7 +261,7 @@ void ModelLoader::addToTree(std::shared_ptr<ModelEntry> model,
auto yamlProperties = yamlModel[base];
for (auto it = yamlProperties.begin(); it != yamlProperties.end(); it++) {
std::string propName = it->first.as<std::string>();
if (exclude.count(QString::fromStdString(propName)) == 0) {
if (!exclude.contains(QString::fromStdString(propName))) {
// showYaml(it->second);
auto yamlProp = yamlProperties[propName];
auto propDisplayName = yamlValue(yamlProp, "DisplayName", "");
@@ -304,7 +304,7 @@ void ModelLoader::addToTree(std::shared_ptr<ModelEntry> model,
}
auto key = std::pair<QString, QString>(uuid, QString::fromStdString(propName));
if (inheritances->count(key) > 0) {
if (inheritances->contains(key)) {
property.setInheritance((*inheritances)[key]);
}

View File

@@ -404,7 +404,7 @@ void MaterialSave::onSelectModel(const QItemSelection& selected, const QItemSele
_filename = QString(ui->editFilename->text()); // No filename by default
auto model = static_cast<QStandardItemModel*>(ui->treeMaterials->model());
QModelIndexList indexes = selected.indexes();
if (indexes.count() == 0) {
if (indexes.isEmpty()) {
_selectedPath = QStringLiteral("/") + _libraryName;
_selectedFull = _selectedPath;
_selectedUUID = QString();

View File

@@ -151,6 +151,10 @@ int TopoShapeCache::Ancestry::count() const
return shapes.Extent();
}
bool TopoShapeCache::Ancestry::empty() const
{
return shapes.IsEmpty();
}
TopoShapeCache::TopoShapeCache(const TopoDS_Shape& tds)
: shape(tds.Located(TopLoc_Location()))

View File

@@ -114,6 +114,7 @@ public:
int find(const TopoDS_Shape& parent, const TopoDS_Shape& subShape);
TopoDS_Shape find(const TopoDS_Shape& parent, int index);
int count() const;
bool empty() const;
friend TopoShapeCache;
};

View File

@@ -860,7 +860,7 @@ void TopoShape::mapSubElementForShape(const TopoShape& other, const char* op)
for (auto type : types) {
auto& shapeMap = _cache->getAncestry(type);
auto& otherMap = other._cache->getAncestry(type);
if ((shapeMap.count() == 0) || (otherMap.count() == 0)) {
if ((shapeMap.empty()) || (otherMap.empty())) {
continue;
}
@@ -1450,7 +1450,7 @@ TopoShape& TopoShape::makeShapeWithElementMap(const TopoDS_Shape& shape,
continue;
}
auto& otherMap = incomingShape._cache->getAncestry(info.type);
if (otherMap.count() == 0) {
if (otherMap.empty()) {
continue;
}
for (int i = 1; i <= otherMap.count(); i++) {

View File

@@ -952,7 +952,7 @@ void TaskAttacher::updateListOfModes()
//first up, remember currently selected mode.
eMapMode curMode = mmDeactivated;
auto sel = ui->listOfModes->selectedItems();
if (sel.count() > 0)
if (!sel.isEmpty())
curMode = modesInList[ui->listOfModes->row(sel[0])];
//obtain list of available modes:
@@ -1054,7 +1054,7 @@ void TaskAttacher::selectMapMode(eMapMode mmode) {
Attacher::eMapMode TaskAttacher::getActiveMapMode()
{
auto sel = ui->listOfModes->selectedItems();
if (sel.count() > 0)
if (!sel.isEmpty())
return modesInList[ui->listOfModes->row(sel[0])];
else {
if (this->lastSuggestResult.message == SuggestResult::srOK)

View File

@@ -1105,7 +1105,7 @@ void TaskSketcherConstraints::changeFilteredVisibility(bool show, ActionTarget t
processItem = !item->isHidden();
}
else if (target == ActionTarget::Selected) {
if (std::ranges::find(selecteditems, item) != selecteditems.end())
if (selecteditems.contains(item))
processItem = true;
}

View File

@@ -851,7 +851,7 @@ void ColorPickerPopup::showEvent(QShowEvent *)
}
if (!foundSelected) {
if (items.count() == 0)
if (items.isEmpty())
setFocus();
else
widgetAt[0][0]->setFocus();