added Array::upsert(), Array::ensert().

enhanced ScopedPtr
This commit is contained in:
2015-03-10 06:25:15 +00:00
parent 8eadd34b9d
commit 15d76c28a1
4 changed files with 120 additions and 21 deletions

View File

@ -284,6 +284,12 @@ public:
qse_size_t insert (qse_size_t index, const T& value)
{
// Unlike insert() in RedBlackTree and HashList,
// it inserts an item when index exists in the existing array.
// It is because array allows duplicate items.
// RedBlckTree::insert() and HashList::insert() return failure
// if existing item exists.
if (index >= this->capacity)
{
// the position to add the element is beyond the
@ -339,6 +345,22 @@ public:
return index;
}
qse_size_t upsert (qse_size_t index, const T& value)
{
if (index < this->count)
return this->update (index, value);
else
return this->insert (index, value);
}
qse_size_t ensert (qse_size_t index, const T& value)
{
if (index < this->count)
return index; // no update
else
return this->insert (index, value);
}
void remove (qse_size_t index)
{
this->remove (index, index);
@ -409,7 +431,9 @@ public:
if (purge_buffer && this->buffer)
{
QSE_ASSERT (this->count <= 0);
QSE_ASSERT (this->capacity > 0);
::operator delete (this->buffer, this->getMmgr());
this->capacity = 0;
this->buffer = QSE_NULL;

View File

@ -193,4 +193,24 @@ void* operator new[] (qse_size_t size, QSE::Mmgr* mmgr);
void operator delete[] (void* ptr, QSE::Mmgr* mmgr);
#endif
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)
/////////////////////////////////
// Customized deleter for ScopedPtr
template <typename T>
struct ScopedPtrMmgrDeleter
{
void operator() (T* ptr, void* arg)
{
ptr->~T ();
::operator delete (ptr, (QSE::Mmgr*)arg);
}
};
/////////////////////////////////
QSE_END_NAMESPACE(QSE)
/////////////////////////////////
#endif