Skip to content

Commit b6d4c3e

Browse files
committed
Rewrite List class
1 parent 8a554f5 commit b6d4c3e

File tree

6 files changed

+38
-99
lines changed

6 files changed

+38
-99
lines changed

src/blocks/listblocks.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,28 @@ bool ListBlocks::categoryVisible() const
3838
Value ListBlocks::addToList(const BlockArgs &args)
3939
{
4040
auto list = std::static_pointer_cast<List>(args.field(LIST)->valuePtr());
41-
list->append(args.input(ITEM)->value());
41+
list->push_back(args.input(ITEM)->value());
4242
return Value();
4343
}
4444

4545
Value ListBlocks::deleteFromList(const BlockArgs &args)
4646
{
4747
auto list = std::static_pointer_cast<List>(args.field(LIST)->valuePtr());
4848
Value value = args.input(INDEX)->value();
49-
int index;
49+
size_t index;
5050
if (value.isString()) {
5151
std::string str = value.toString();
5252
if (str == "last")
53-
index = list->length();
53+
index = list->size();
5454
else if (str == "all") {
5555
list->clear();
5656
return Value();
5757
} else if (str == "random") {
58-
index = list->length() == 0 ? 0 : randint(1, list->length());
58+
index = list->size() == 0 ? 0 : randint<size_t>(1, list->size());
5959
} else
6060
return Value();
6161
} else
62-
index = validateIndex(value.toInt(), list->length());
62+
index = validateIndex(value.toInt(), list->size());
6363
if (index == 0)
6464
return Value();
6565
list->removeAt(index - 1);
@@ -81,14 +81,14 @@ Value ListBlocks::insertToList(const BlockArgs &args)
8181
if (value.isString()) {
8282
std::string str = value.toString();
8383
if (str == "last") {
84-
list->append(args.input(ITEM)->value());
84+
list->push_back(args.input(ITEM)->value());
8585
return Value();
8686
} else if (str == "random") {
87-
index = list->length() == 0 ? 1 : randint(1, list->length());
87+
index = list->size() == 0 ? 1 : randint<size_t>(1, list->size());
8888
} else
8989
return Value();
9090
} else {
91-
index = validateIndex(value.toInt(), list->length());
91+
index = validateIndex(value.toInt(), list->size());
9292
if (index == 0)
9393
return Value();
9494
}
@@ -104,13 +104,13 @@ Value ListBlocks::replaceItemOfList(const BlockArgs &args)
104104
if (value.isString()) {
105105
std::string str = value.toString();
106106
if (str == "last")
107-
index = list->length();
107+
index = list->size();
108108
else if (str == "random") {
109-
index = list->length() == 0 ? 0 : randint(1, list->length());
109+
index = list->size() == 0 ? 0 : randint<size_t>(1, list->size());
110110
} else
111111
return Value();
112112
} else
113-
index = validateIndex(value.toInt(), list->length());
113+
index = validateIndex(value.toInt(), list->size());
114114
if (index == 0)
115115
return Value();
116116
list->replace(index - 1, args.input(ITEM)->value());
@@ -125,13 +125,13 @@ Value ListBlocks::itemOfList(const BlockArgs &args)
125125
if (value.isString()) {
126126
std::string str = value.toString();
127127
if (str == "last")
128-
index = list->length();
128+
index = list->size();
129129
else if (str == "random") {
130-
index = list->length() == 0 ? 0 : randint(1, list->length());
130+
index = list->size() == 0 ? 0 : randint<size_t>(1, list->size());
131131
} else
132132
return "";
133133
} else
134-
index = validateIndex(value.toInt(), list->length());
134+
index = validateIndex(value.toInt(), list->size());
135135
if (index == 0)
136136
return "";
137137
return list->at(index - 1);
@@ -146,7 +146,7 @@ Value ListBlocks::itemNumberInList(const BlockArgs &args)
146146
Value ListBlocks::lengthOfList(const BlockArgs &args)
147147
{
148148
auto list = std::static_pointer_cast<List>(args.field(LIST)->valuePtr());
149-
return list->length();
149+
return list->size();
150150
}
151151

152152
Value ListBlocks::listContainsItem(const BlockArgs &args)
@@ -155,7 +155,7 @@ Value ListBlocks::listContainsItem(const BlockArgs &args)
155155
return list->contains(args.input(ITEM)->value());
156156
}
157157

158-
int ListBlocks::validateIndex(int index, int listLength)
158+
int ListBlocks::validateIndex(size_t index, size_t listLength)
159159
{
160160
if (listLength == 0) {
161161
if (index != 1)

src/blocks/listblocks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class LIBSCRATCHCPP_EXPORT ListBlocks : public IBlockSection
3838
static Value listContainsItem(const BlockArgs &args);
3939

4040
private:
41-
static int validateIndex(int index, int listLength);
41+
static int validateIndex(size_t index, size_t listLength);
4242
};
4343

4444
} // namespace libscratchcpp

src/blocks/operatorblocks.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Value OperatorBlocks::divide(const BlockArgs &args)
5656

5757
Value OperatorBlocks::pickRandom(const BlockArgs &args)
5858
{
59-
return randint(args.input(FROM)->value().toDouble(), args.input(TO)->value().toDouble());
59+
return randint<double>(args.input(FROM)->value().toDouble(), args.input(TO)->value().toDouble());
6060
}
6161

6262
Value OperatorBlocks::lessThan(const BlockArgs &args)

src/internal/scratch3reader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bool Scratch3Reader::load()
4949
auto list = std::make_shared<List>(it.key(), listInfo[0]);
5050
auto arr = listInfo[1];
5151
for (auto item : arr)
52-
list->append(jsonToValue(item));
52+
list->push_back(jsonToValue(item));
5353
target->addList(list);
5454
}
5555

src/scratch/list.cpp

Lines changed: 7 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -24,90 +24,30 @@ void List::setName(const std::string &name)
2424
m_name = name;
2525
}
2626

27-
/*! Returns the list of items. */
28-
std::deque<Value> List::items() const
29-
{
30-
return m_items;
31-
}
32-
33-
/*! Returns the item at index. */
34-
const Value &List::at(int index) const
35-
{
36-
return m_items[index];
37-
}
38-
3927
/*! Returns the index of the given item. */
40-
int List::indexOf(const Value &value) const
28+
size_t List::indexOf(const Value &value) const
4129
{
42-
auto it = std::find(m_items.begin(), m_items.end(), value);
30+
auto it = std::find(begin(), end(), value);
4331

44-
if (it != m_items.end())
45-
return it - m_items.begin();
32+
if (it != end())
33+
return it - begin();
4634
else
4735
return -1;
4836
}
4937

50-
/*! Returns the number of items in the list. */
51-
int List::size() const
52-
{
53-
return m_items.size();
54-
}
55-
56-
/*! \copydoc size() */
57-
int List::length() const
58-
{
59-
return size();
60-
}
61-
62-
/*! \copydoc size() */
63-
int List::count() const
64-
{
65-
return size();
66-
}
67-
6838
/*! Returns true if the list contains the given item. */
6939
bool List::contains(const Value &value) const
7040
{
7141
return (indexOf(value) != -1);
7242
}
7343

74-
/*! Removes all items from the list. */
75-
void List::clear()
76-
{
77-
m_items.clear();
78-
}
79-
80-
/*! Removes the item at index. */
81-
void List::removeAt(int index)
82-
{
83-
m_items.erase(m_items.begin() + index);
84-
}
85-
86-
/*! Adds an item. */
87-
void List::append(const Value &value)
88-
{
89-
m_items.push_back(value);
90-
}
91-
92-
/*! Inserts an item at index. */
93-
void List::insert(int index, const Value &value)
94-
{
95-
m_items.insert(m_items.begin() + index, value);
96-
}
97-
98-
/*! Replaces the item at index. */
99-
void List::replace(int index, const Value &value)
100-
{
101-
m_items[index] = value;
102-
}
103-
10444
/*! Joins the list items with spaces. */
10545
std::string List::toString() const
10646
{
10747
std::string ret;
108-
for (int i = 0; i < m_items.size(); i++) {
109-
ret.append(m_items[i].toString());
110-
if (i + 1 < m_items.size())
48+
for (int i = 0; i < size(); i++) {
49+
ret.append(at(i).toString());
50+
if (i + 1 < size())
11151
ret.push_back(' ');
11252
}
11353
return ret;

src/scratch/list.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ namespace libscratchcpp
1111
{
1212

1313
/*! \brief The List class represents a Scratch list. */
14-
class LIBSCRATCHCPP_EXPORT List : public IEntity
14+
class LIBSCRATCHCPP_EXPORT List
15+
: public std::deque<Value>
16+
, public IEntity
1517
{
1618
public:
1719
List(std::string id, std::string name);
@@ -20,25 +22,22 @@ class LIBSCRATCHCPP_EXPORT List : public IEntity
2022
std::string name();
2123
void setName(const std::string &name);
2224

23-
std::deque<Value> items() const;
24-
const Value &at(int index) const;
25-
int indexOf(const Value &value) const;
26-
int size() const;
27-
int length() const;
28-
int count() const;
25+
size_t indexOf(const Value &value) const;
2926
bool contains(const Value &value) const;
3027

31-
void clear();
32-
void removeAt(int index);
33-
void append(const Value &value);
34-
void insert(int index, const Value &value);
35-
void replace(int index, const Value &value);
28+
/*! Removes the item at index. */
29+
inline void removeAt(int index) { erase(begin() + index); }
30+
31+
/*! Inserts an item at index. */
32+
inline void insert(int index, const Value &value) { std::deque<Value>::insert(begin() + index, value); }
33+
34+
/*! Replaces the item at index. */
35+
inline void replace(int index, const Value &value) { at(index) = value; }
3636

3737
std::string toString() const;
3838

3939
private:
4040
std::string m_name;
41-
std::deque<Value> m_items;
4241
};
4342

4443
} // namespace libscratchcpp

0 commit comments

Comments
 (0)