Skip to content

Commit 6e55221

Browse files
committed
Target: Rewrite find methods
1 parent b7a1b59 commit 6e55221

File tree

1 file changed

+47
-58
lines changed

1 file changed

+47
-58
lines changed

src/scratch/target.cpp

Lines changed: 47 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,23 @@ std::shared_ptr<Variable> Target::variableAt(int index) const
6161
/*! Returns the index of the variable with the given name. */
6262
int Target::findVariable(const std::string &variableName) const
6363
{
64-
int i = 0;
65-
for (auto var : impl->variables) {
66-
if (var->name() == variableName)
67-
return i;
68-
i++;
69-
}
70-
return -1;
64+
auto it = std::find_if(impl->variables.begin(), impl->variables.end(), [variableName](std::shared_ptr<Variable> variable) { return variable->name() == variableName; });
65+
66+
if (it == impl->variables.end())
67+
return -1;
68+
else
69+
return it - impl->variables.begin();
7170
}
7271

7372
/*! Returns the index of the variable with the given ID. */
7473
int Target::findVariableById(const std::string &id) const
7574
{
76-
int i = 0;
77-
for (auto var : impl->variables) {
78-
if (var->id() == id)
79-
return i;
80-
i++;
81-
}
82-
return -1;
75+
auto it = std::find_if(impl->variables.begin(), impl->variables.end(), [id](std::shared_ptr<Variable> variable) { return variable->id() == id; });
76+
77+
if (it == impl->variables.end())
78+
return -1;
79+
else
80+
return it - impl->variables.begin();
8381
}
8482

8583
/*! Returns the list of Scratch lists. */
@@ -114,25 +112,23 @@ std::shared_ptr<List> Target::listAt(int index) const
114112
/*! Returns the index of the list with the given name. */
115113
int Target::findList(const std::string &listName) const
116114
{
117-
int i = 0;
118-
for (auto list : impl->lists) {
119-
if (list->name() == listName)
120-
return i;
121-
i++;
122-
}
123-
return -1;
115+
auto it = std::find_if(impl->lists.begin(), impl->lists.end(), [listName](std::shared_ptr<List> list) { return list->name() == listName; });
116+
117+
if (it == impl->lists.end())
118+
return -1;
119+
else
120+
return it - impl->lists.begin();
124121
}
125122

126123
/*! Returns the index of the list with the given ID. */
127124
int Target::findListById(const std::string &id) const
128125
{
129-
int i = 0;
130-
for (auto list : impl->lists) {
131-
if (list->id() == id)
132-
return i;
133-
i++;
134-
}
135-
return -1;
126+
auto it = std::find_if(impl->lists.begin(), impl->lists.end(), [id](std::shared_ptr<List> list) { return list->id() == id; });
127+
128+
if (it == impl->lists.end())
129+
return -1;
130+
else
131+
return it - impl->lists.begin();
136132
}
137133

138134
/*! Returns the list of blocks. */
@@ -177,13 +173,12 @@ int Target::findBlock(const std::string &id) const
177173
if (Target *source = dataSource())
178174
return source->findBlock(id);
179175

180-
int i = 0;
181-
for (auto block : impl->blocks) {
182-
if (block->id() == id)
183-
return i;
184-
i++;
185-
}
186-
return -1;
176+
auto it = std::find_if(impl->blocks.begin(), impl->blocks.end(), [id](std::shared_ptr<Block> block) { return block->id() == id; });
177+
178+
if (it == impl->blocks.end())
179+
return -1;
180+
else
181+
return it - impl->blocks.begin();
187182
}
188183

189184
/*! Returns list of all "when green flag clicked" blocks. */
@@ -242,16 +237,12 @@ int Target::findComment(const std::string &id) const
242237
if (Target *source = dataSource())
243238
return source->findComment(id);
244239

245-
int i = 0;
240+
auto it = std::find_if(impl->comments.begin(), impl->comments.end(), [id](std::shared_ptr<Comment> comment) { return comment->id() == id; });
246241

247-
for (auto comment : impl->comments) {
248-
if (comment->id() == id)
249-
return i;
250-
251-
i++;
252-
}
253-
254-
return -1;
242+
if (it == impl->comments.end())
243+
return -1;
244+
else
245+
return it - impl->comments.begin();
255246
}
256247

257248
/*! Returns the index of the current costume. */
@@ -320,13 +311,12 @@ int Target::findCostume(const std::string &costumeName) const
320311
if (Target *source = dataSource())
321312
return source->findCostume(costumeName);
322313

323-
int i = 0;
324-
for (auto costume : impl->costumes) {
325-
if (costume->name() == costumeName)
326-
return i;
327-
i++;
328-
}
329-
return -1;
314+
auto it = std::find_if(impl->costumes.begin(), impl->costumes.end(), [costumeName](std::shared_ptr<Costume> costume) { return costume->name() == costumeName; });
315+
316+
if (it == impl->costumes.end())
317+
return -1;
318+
else
319+
return it - impl->costumes.begin();
330320
}
331321

332322
/*! Returns the list of sounds. */
@@ -376,13 +366,12 @@ int Target::findSound(const std::string &soundName) const
376366
if (Target *source = dataSource())
377367
return source->findSound(soundName);
378368

379-
int i = 0;
380-
for (auto sound : impl->sounds) {
381-
if (sound->name() == soundName)
382-
return i;
383-
i++;
384-
}
385-
return -1;
369+
auto it = std::find_if(impl->sounds.begin(), impl->sounds.end(), [soundName](std::shared_ptr<Sound> sound) { return sound->name() == soundName; });
370+
371+
if (it == impl->sounds.end())
372+
return -1;
373+
else
374+
return it - impl->sounds.begin();
386375
}
387376

388377
/*! Returns the layer number. */

0 commit comments

Comments
 (0)