@@ -16,14 +16,14 @@ class Monitor;
1616class ListPrivate ;
1717
1818/* ! \brief The List class represents a Scratch list. */
19- class LIBSCRATCHCPP_EXPORT List
20- : public veque::veque<Value>
21- , public Entity
19+ class LIBSCRATCHCPP_EXPORT List : public Entity
2220{
2321 public:
2422 List (const std::string &id, const std::string &name);
2523 List (const List &) = delete ;
2624
25+ ~List ();
26+
2727 const std::string &name ();
2828 void setName (const std::string &name);
2929
@@ -33,24 +33,103 @@ class LIBSCRATCHCPP_EXPORT List
3333 Monitor *monitor () const ;
3434 void setMonitor (Monitor *monitor);
3535
36- long indexOf (const Value &value) const ;
37- bool contains (const Value &value) const ;
36+ /* ! Returns the list size. */
37+ inline size_t size () const { return m_dataPtr->size (); }
38+
39+ /* ! Returns true if the list is empty. */
40+ inline bool empty () const { return m_dataPtr->empty (); }
41+
42+ /* ! Returns the index of the given item. */
43+ inline size_t indexOf (const ValueData &value) const
44+ {
45+ for (size_t i = 0 ; i < m_dataPtr->size (); i++) {
46+ if (value_equals (&m_dataPtr->operator [](i), &value))
47+ return i;
48+ }
49+
50+ return -1 ;
51+ }
52+
53+ /* ! Returns the index of the given item. */
54+ inline size_t indexOf (const Value &value) const { return indexOf (value.data ()); }
55+
56+ /* ! Returns true if the list contains the given item. */
57+ inline bool contains (const ValueData &value) const { return (indexOf (value) != -1 ); }
58+
59+ /* ! Returns true if the list contains the given item. */
60+ inline bool contains (const Value &value) const { return contains (value.data ()); }
61+
62+ /* ! Clears the list. */
63+ inline void clear ()
64+ {
65+ for (ValueData &v : *m_dataPtr)
66+ value_free (&v);
67+
68+ m_dataPtr->clear ();
69+ }
70+
71+ /* ! Appends an item. */
72+ inline void append (const ValueData &value)
73+ {
74+ m_dataPtr->push_back (ValueData ());
75+ value_init (&m_dataPtr->back ());
76+ value_assign_copy (&m_dataPtr->back (), &value);
77+ }
78+
79+ /* ! Appends an item. */
80+ inline void append (const Value &value) { append (value.data ()); }
81+
82+ /* ! Appends an empty item and returns the reference to it. Can be used for custom initialization. */
83+ inline ValueData &appendEmpty ()
84+ {
85+ m_dataPtr->push_back (ValueData ());
86+ value_init (&m_dataPtr->back ());
87+ return m_dataPtr->back ();
88+ }
3889
3990 /* ! Removes the item at index. */
40- void removeAt (int index) { erase (begin () + index); }
91+ inline void removeAt (size_t index)
92+ {
93+ assert (index >= 0 && index < size ());
94+ value_free (&m_dataPtr->operator [](index));
95+ m_dataPtr->erase (m_dataPtr->begin () + index);
96+ }
97+
98+ /* ! Inserts an item at index. */
99+ inline void insert (size_t index, const ValueData &value)
100+ {
101+ assert (index >= 0 && index <= size ());
102+ m_dataPtr->insert (m_dataPtr->begin () + index, ValueData ());
103+ value_init (&m_dataPtr->operator [](index));
104+ value_assign_copy (&m_dataPtr->operator [](index), &value);
105+ }
41106
42107 /* ! Inserts an item at index. */
43- void insert (int index, const Value &value) { veque<Value>::insert (begin () + index, value); }
108+ inline void insert (size_t index, const Value &value) { insert (index, value.data ()); }
109+
110+ /* ! Replaces the item at index. */
111+ inline void replace (size_t index, const ValueData &value)
112+ {
113+ assert (index >= 0 && index < size ());
114+ value_assign_copy (&m_dataPtr->operator [](index), &value);
115+ }
44116
45117 /* ! Replaces the item at index. */
46- void replace (int index, const Value &value) { at (index) = value; }
118+ inline void replace (size_t index, const Value &value) { replace (index, value.data ()); }
119+
120+ inline ValueData &operator [](size_t index)
121+ {
122+ assert (index >= 0 && index < size ());
123+ return m_dataPtr->operator [](index);
124+ }
47125
48126 std::string toString () const ;
49127
50128 std::shared_ptr<List> clone ();
51129
52130 private:
53131 spimpl::unique_impl_ptr<ListPrivate> impl;
132+ veque::veque<ValueData> *m_dataPtr = nullptr ; // NOTE: accessing through pointer is faster! (from benchmarks)
54133};
55134
56135} // namespace libscratchcpp
0 commit comments