1 #ifndef SimTK_SimTKCOMMON_STABLEARRAY_H_
2 #define SimTK_SimTKCOMMON_STABLEARRAY_H_
59 explicit StableArray(
size_t z,
const T& ival=T()) : stuff(z), nOccupiedSlots(z) {
60 for (
size_t i=0; i<z; ++i) stuff[i] =
new T(ival);
65 for (
size_t i=0; i<s.
size(); ++i)
66 if (!s.
empty(i)) initializeEmptyElement(i, s[i]);
74 for (
size_t i=0; i<s.
size(); ++i)
75 if (!s.
empty(i)) initializeEmptyElement(i, s[i]);
84 assert(i < stuff.
size());
88 size_t nItems()
const {
return nOccupiedSlots;}
94 void resize(
size_t newz,
const T& ival=T()) {
95 const size_t oldz = stuff.
size();
97 for (
size_t i=newz; i < oldz; ++i)
98 eraseElementIfNecessary(i);
101 for (
size_t i=0; i < newz; ++i)
102 initializeElementIfNecessary(i,ival);
107 for (
size_t i=0; i < stuff.
size(); ++i)
108 eraseElementIfNecessary(i);
126 eraseOccupiedElement(stuff.
size()-1);
133 assert(i <= stuff.
size());
135 else initializeEmptyElement(i,t);
141 for (
size_t i=0; i<
size(); ++i)
142 if (
empty(i))
return i;
152 assert(i < stuff.
size());
153 for (; i < stuff.
size() && !stuff[i]; ++i);
174 else eraseElementIfNecessary(i);
180 assert(firstItem < stuff.
size());
181 return *stuff[firstItem];
185 assert(firstItem < stuff.
size());
186 return *stuff[firstItem];
195 assert(i < stuff.
size() && stuff[i]);
199 assert(i < stuff.
size() && stuff[i]);
204 size_t nOccupiedSlots;
210 void eraseOccupiedElement(
size_t i) {
211 assert(i < stuff.
size() && stuff[i]);
212 delete stuff[i]; stuff[i]=0; --nOccupiedSlots;
215 void initializeEmptyElement(
size_t i,
const T& t) {
216 assert(i < stuff.
size() && !stuff[i]);
217 stuff[i] =
new T(t); ++nOccupiedSlots;
220 void eraseElementIfNecessary(
size_t i) {
221 assert(i < stuff.
size());
222 if (stuff[i]) eraseOccupiedElement(i);
225 void initializeElementIfNecessary(
size_t i,
const T& t) {
226 assert(i < stuff.
size());
227 if (!stuff[i]) initializeEmptyElement(i,t);
234 #endif // SimTK_SimTKCOMMON_STABLEARRAY_H_
const T & operator[](size_t i) const
Definition: StableArray.h:194
size_type size() const
Return the current number of elements stored in this array.
Definition: Array.h:2037
void erase(size_t i)
Definition: StableArray.h:172
StableArray(const StableArray &s)
Definition: StableArray.h:64
size_t nItems() const
Definition: StableArray.h:88
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
void pop_back()
Definition: StableArray.h:124
void resize(size_t newz, const T &ival=T())
Definition: StableArray.h:94
void clear()
Definition: StableArray.h:106
size_t insert(const T &t)
Definition: StableArray.h:160
const T & back() const
Return a const reference to the last element in this array, which must not be empty.
Definition: Array.h:2297
bool empty(size_t i) const
Definition: StableArray.h:83
This file defines the Array_ class and related support classes including base classes ArrayViewC...
bool empty() const
Definition: StableArray.h:82
size_t size() const
Definition: StableArray.h:87
void push_back(const T &t)
Definition: StableArray.h:115
size_t findNextItem(size_t i)
Definition: StableArray.h:151
StableArray is like std::vector (or SimTK::Array_) but more stable in two ways: ...
Definition: StableArray.h:54
size_t findFreeSlot() const
Definition: StableArray.h:138
void pop_back()
Remove the last element from this array, which must not be empty.
Definition: Array.h:2411
T & front()
Definition: StableArray.h:183
const T & back() const
Definition: StableArray.h:191
StableArray(size_t z, const T &ival=T())
Definition: StableArray.h:59
void insertAt(size_t i, const T &t)
Definition: StableArray.h:132
Mandatory first inclusion for any Simbody source or header file.
void resize(size_type n)
Change the size of this Array, preserving all the elements that will still fit, and default construct...
Definition: Array.h:2053
T & operator[](size_t i)
Definition: StableArray.h:198
bool empty() const
Return true if there are no elements currently stored in this array.
Definition: Array.h:2042
void push_back(const T &value)
This method increases the size of the Array by one element at the end and initializes that element by...
Definition: Array.h:2359
~StableArray()
Definition: StableArray.h:80
const T & front() const
Definition: StableArray.h:178
T & back()
Definition: StableArray.h:192
StableArray & operator=(const StableArray &s)
Definition: StableArray.h:71
StableArray()
Definition: StableArray.h:56