diff --git a/include/xo/object2/DArray.hpp b/include/xo/object2/DArray.hpp index 8d7248e..282e0e5 100644 --- a/include/xo/object2/DArray.hpp +++ b/include/xo/object2/DArray.hpp @@ -112,6 +112,13 @@ namespace xo { **/ bool resize(size_type new_size) noexcept; + /** reduce array capacity to current array size + * + * note: with X1Collector, capacity is reduced but memory not recycled + * until next collection + **/ + void shrink_to_fit() noexcept; + ///@} /** @defgroup darray-conversion-operators conversion operators **/ ///@{ diff --git a/src/object2/DArray.cpp b/src/object2/DArray.cpp index 31ced92..459b459 100644 --- a/src/object2/DArray.cpp +++ b/src/object2/DArray.cpp @@ -68,7 +68,8 @@ namespace xo { } bool - DArray::push_back(obj elt) noexcept { + DArray::push_back(obj elt) noexcept + { if (size_ >= capacity_) { return false; } else { @@ -85,7 +86,8 @@ namespace xo { } bool - DArray::resize(size_type new_z) noexcept { + DArray::resize(size_type new_z) noexcept + { if (new_z >= capacity_) { return false; } else if (new_z > size_) { @@ -97,6 +99,14 @@ namespace xo { return true; } + void + DArray::shrink_to_fit() noexcept + { + if (capacity_ > size_) { + this->capacity_ = size_; + } + } + // printing support bool