Skip to content

Commit e74ebba

Browse files
committed
Optimize inputs and fields
* assign IDs to inputs and fields using maps
1 parent 0261dc4 commit e74ebba

File tree

6 files changed

+44
-28
lines changed

6 files changed

+44
-28
lines changed

src/engine/blockargs.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ std::shared_ptr<Input> BlockArgs::input(const char *name) const
5656
}
5757

5858
/*! Returns the input with the given ID. */
59-
std::shared_ptr<Input> BlockArgs::input(int id) const
59+
Input *BlockArgs::input(int id) const
6060
{
61-
return m_block->inputAt(m_block->findInputById(id));
61+
return m_block->findInputById(id);
6262
}
6363

6464
/*! Returns the field with the given name. */
@@ -74,9 +74,9 @@ std::shared_ptr<Field> BlockArgs::field(const char *name) const
7474
}
7575

7676
/*! Returns the field with the given ID. */
77-
std::shared_ptr<Field> BlockArgs::field(int id) const
77+
Field *BlockArgs::field(int id) const
7878
{
79-
return m_block->fieldAt(m_block->findFieldById(id));
79+
return m_block->findFieldById(id);
8080
}
8181

8282
} // namespace libscratchcpp

src/engine/blockargs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ class LIBSCRATCHCPP_EXPORT BlockArgs
2828

2929
std::shared_ptr<Input> input(const std::string &name) const;
3030
std::shared_ptr<Input> input(const char *name) const;
31-
std::shared_ptr<Input> input(int id) const;
31+
Input *input(int id) const;
3232

3333
std::shared_ptr<Field> field(const std::string &name) const;
3434
std::shared_ptr<Field> field(const char *name) const;
35-
std::shared_ptr<Field> field(int id) const;
35+
Field *field(int id) const;
3636

3737
private:
3838
Target *m_target = nullptr;

src/engine/engine.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ void Engine::compile()
5858
field->setSpecialValueId(section->resolveFieldValue(field->value().toString()));
5959
}
6060
}
61+
62+
block->updateInputMap();
63+
block->updateFieldMap();
6164
}
6265
}
6366
}

src/scratch/block.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,20 @@ int Block::findInput(const std::string &inputName) const
133133
return -1;
134134
}
135135

136-
/*! Returns the index of the input with the given ID. */
137-
int Block::findInputById(int id)
136+
/*! Returns the input with the given ID. */
137+
Input *Block::findInputById(int id) const
138138
{
139-
int i = 0;
140-
for (auto input : m_inputs) {
141-
if (input->inputId() == id)
142-
return i;
143-
i++;
144-
}
145-
return -1;
139+
if (m_inputMap.count(id) == 1)
140+
return m_inputMap.at(id);
141+
return nullptr;
142+
}
143+
144+
/*! Updates the map that assigns input IDs to input indexes. Used internally by Engine. */
145+
void Block::updateInputMap()
146+
{
147+
m_inputMap.clear();
148+
for (auto input : m_inputs)
149+
m_inputMap[input->inputId()] = input.get();
146150
}
147151

148152
/*! Returns the list of fields. */
@@ -177,15 +181,19 @@ int Block::findField(const std::string &fieldName) const
177181
}
178182

179183
/*! Returns the index of the field with the given ID. */
180-
int Block::findFieldById(int id) const
184+
Field *Block::findFieldById(int id) const
181185
{
182-
int i = 0;
183-
for (auto field : m_fields) {
184-
if (field->fieldId() == id)
185-
return i;
186-
i++;
187-
}
188-
return -1;
186+
if (m_fieldMap.count(id) == 1)
187+
return m_fieldMap.at(id);
188+
return nullptr;
189+
}
190+
191+
/*! Updates the map that assigns input IDs to input indexes. Used internally by Engine. */
192+
void Block::updateFieldMap()
193+
{
194+
m_fieldMap.clear();
195+
for (auto field : m_fields)
196+
m_fieldMap[field->fieldId()] = field.get();
189197
}
190198

191199
/*! Returns true if this is a shadow block. */

src/scratch/block.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "ientity.h"
88
#include "input.h"
99
#include <memory>
10+
#include <map>
1011
#include <string>
1112

1213
namespace libscratchcpp
@@ -40,13 +41,15 @@ class LIBSCRATCHCPP_EXPORT Block : public IEntity
4041
int addInput(std::shared_ptr<Input> input);
4142
std::shared_ptr<Input> inputAt(int index) const;
4243
int findInput(const std::string &inputName) const;
43-
int findInputById(int id);
44+
Input *findInputById(int id) const;
45+
void updateInputMap();
4446

4547
std::vector<std::shared_ptr<Field>> fields() const;
4648
int addField(std::shared_ptr<Field> field);
4749
std::shared_ptr<Field> fieldAt(int index) const;
4850
int findField(const std::string &fieldName) const;
49-
int findFieldById(int id) const;
51+
Field *findFieldById(int id) const;
52+
void updateFieldMap();
5053

5154
bool shadow() const;
5255
void setShadow(bool newShadow);
@@ -66,7 +69,9 @@ class LIBSCRATCHCPP_EXPORT Block : public IEntity
6669
std::shared_ptr<Block> m_parent = nullptr;
6770
std::string m_parentId;
6871
std::vector<std::shared_ptr<Input>> m_inputs;
72+
std::unordered_map<int, Input *> m_inputMap;
6973
std::vector<std::shared_ptr<Field>> m_fields;
74+
std::unordered_map<int, Field *> m_fieldMap;
7075
bool m_shadow = false;
7176
bool m_topLevel = false;
7277
Engine *m_engine = nullptr;

src/scratch/runningscript.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ void RunningScript::moveToSubstack(const BlockArgs &args, int inputId)
104104
std::shared_ptr<Block> RunningScript::getSubstack(const BlockArgs &args, int inputId) const
105105
{
106106
auto block = args.block();
107-
int index = block->findInputById(inputId);
108-
if (index != -1) {
109-
auto ret = block->inputAt(index)->valueBlock();
107+
auto input = block->findInputById(inputId);
108+
if (input) {
109+
auto ret = input->valueBlock();
110110
if (ret)
111111
return ret;
112112
}

0 commit comments

Comments
 (0)