Skip to content

Commit c7501c7

Browse files
authored
Merge pull request #413 from scratchcpp/rewrite_find_methods
Rewrite find methods
2 parents 57390da + 3589652 commit c7501c7

File tree

3 files changed

+79
-93
lines changed

3 files changed

+79
-93
lines changed

src/engine/internal/engine.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -751,24 +751,22 @@ std::shared_ptr<Broadcast> Engine::broadcastAt(int index) const
751751

752752
int Engine::findBroadcast(const std::string &broadcastName) const
753753
{
754-
int i = 0;
755-
for (auto broadcast : m_broadcasts) {
756-
if (broadcast->name() == broadcastName)
757-
return i;
758-
i++;
759-
}
760-
return -1;
754+
auto it = std::find_if(m_broadcasts.begin(), m_broadcasts.end(), [broadcastName](std::shared_ptr<Broadcast> broadcast) { return broadcast->name() == broadcastName; });
755+
756+
if (it == m_broadcasts.end())
757+
return -1;
758+
else
759+
return it - m_broadcasts.begin();
761760
}
762761

763762
int Engine::findBroadcastById(const std::string &broadcastId) const
764763
{
765-
int i = 0;
766-
for (auto broadcast : m_broadcasts) {
767-
if (broadcast->id() == broadcastId)
768-
return i;
769-
i++;
770-
}
771-
return -1;
764+
auto it = std::find_if(m_broadcasts.begin(), m_broadcasts.end(), [broadcastId](std::shared_ptr<Broadcast> broadcast) { return broadcast->id() == broadcastId; });
765+
766+
if (it == m_broadcasts.end())
767+
return -1;
768+
else
769+
return it - m_broadcasts.begin();
772770
}
773771

774772
void Engine::addGreenFlagScript(std::shared_ptr<Block> hatBlock)
@@ -852,13 +850,14 @@ Target *Engine::targetAt(int index) const
852850

853851
int Engine::findTarget(const std::string &targetName) const
854852
{
855-
int i = 0;
856-
for (auto target : m_targets) {
857-
if ((target->isStage() && targetName == "_stage_") || (!target->isStage() && target->name() == targetName))
858-
return i;
859-
i++;
860-
}
861-
return -1;
853+
auto it = std::find_if(m_targets.begin(), m_targets.end(), [targetName](std::shared_ptr<Target> target) {
854+
return ((target->isStage() && targetName == "_stage_") || (!target->isStage() && target->name() == targetName));
855+
});
856+
857+
if (it == m_targets.end())
858+
return -1;
859+
else
860+
return it - m_targets.begin();
862861
}
863862

864863
void Engine::moveSpriteToFront(Sprite *sprite)

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. */

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)