Skip to content

Commit 3589652

Browse files
committed
Block: Rewrite find methods
1 parent 6e55221 commit 3589652

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/scratch/block.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,12 @@ std::shared_ptr<Input> Block::inputAt(int index) const
186186
/*! Returns the index of the input with the given name. */
187187
int Block::findInput(const std::string &inputName) const
188188
{
189-
int i = 0;
190-
for (auto input : impl->inputs) {
191-
if (input->name() == inputName)
192-
return i;
193-
i++;
194-
}
195-
return -1;
189+
auto it = std::find_if(impl->inputs.begin(), impl->inputs.end(), [inputName](std::shared_ptr<Input> input) { return input->name() == inputName; });
190+
191+
if (it == impl->inputs.end())
192+
return -1;
193+
else
194+
return it - impl->inputs.begin();
196195
}
197196

198197
/*! Returns the input with the given ID. */
@@ -248,13 +247,12 @@ std::shared_ptr<Field> Block::fieldAt(int index) const
248247
/*! Returns the index of the field with the given name. */
249248
int Block::findField(const std::string &fieldName) const
250249
{
251-
int i = 0;
252-
for (auto field : impl->fields) {
253-
if (field->name() == fieldName)
254-
return i;
255-
i++;
256-
}
257-
return -1;
250+
auto it = std::find_if(impl->fields.begin(), impl->fields.end(), [fieldName](std::shared_ptr<Field> field) { return field->name() == fieldName; });
251+
252+
if (it == impl->fields.end())
253+
return -1;
254+
else
255+
return it - impl->fields.begin();
258256
}
259257

260258
/*! Returns the index of the field with the given ID. */

0 commit comments

Comments
 (0)