Compare commits
2 Commits
bf5be00c12
...
dba6bb5800
Author | SHA1 | Date | |
---|---|---|---|
dba6bb5800 | |||
40a456e54a |
1
deps/ox/src/ox/std/test/CMakeLists.txt
vendored
1
deps/ox/src/ox/std/test/CMakeLists.txt
vendored
@ -17,6 +17,7 @@ add_test("[ox/std] String" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "String")
|
||||
add_test("[ox/std] SmallMap" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "SmallMap")
|
||||
add_test("[ox/std] SmallMap2" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "SmallMap2")
|
||||
add_test("[ox/std] Vector" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "Vector")
|
||||
add_test("[ox/std] Vector::shrink_to_fit" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "Vector::shrink_to_fit")
|
||||
add_test("[ox/std] HashMap" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "HashMap")
|
||||
add_test("[ox/std] HeapMgr" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest malloc)
|
||||
add_test("[ox/std] Serialize-Int" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "Serialize-Int")
|
||||
|
30
deps/ox/src/ox/std/test/tests.cpp
vendored
30
deps/ox/src/ox/std/test/tests.cpp
vendored
@ -237,6 +237,36 @@ OX_CLANG_NOWARN_END
|
||||
return ox::Error(0);
|
||||
}
|
||||
},
|
||||
{
|
||||
"Vector::shrink_to_fit",
|
||||
[] {
|
||||
{
|
||||
ox::Vector<ox::IString<8>> v;
|
||||
v.reserve(50);
|
||||
v.emplace_back("asdf");
|
||||
v.emplace_back("aoeu");
|
||||
auto const origData = v.data();
|
||||
v.shrink_to_fit();
|
||||
oxExpect(v[0], "asdf");
|
||||
oxExpect(v[1], "aoeu");
|
||||
oxExpect(v.capacity(), 2u);
|
||||
oxAssert(origData != v.data(), "shrink_to_fit did not create a new allocation");
|
||||
}
|
||||
{
|
||||
ox::Vector<ox::IString<8>> v;
|
||||
v.reserve(2);
|
||||
v.emplace_back("asdf");
|
||||
v.emplace_back("aoeu");
|
||||
auto const origData = v.data();
|
||||
v.shrink_to_fit();
|
||||
oxExpect(v[0], "asdf");
|
||||
oxExpect(v[1], "aoeu");
|
||||
oxExpect(v.capacity(), 2u);
|
||||
oxAssert(origData == v.data(), "shrink_to_fit inappropriately created a new allocation");
|
||||
}
|
||||
return ox::Error{};
|
||||
}
|
||||
},
|
||||
{
|
||||
"SmallMap",
|
||||
[] {
|
||||
|
21
deps/ox/src/ox/std/vector.hpp
vendored
21
deps/ox/src/ox/std/vector.hpp
vendored
@ -313,6 +313,8 @@ class Vector: detail::VectorAllocator<T, Allocator, SmallVectorSize> {
|
||||
|
||||
constexpr void reserve(std::size_t cap) noexcept(useNoexcept);
|
||||
|
||||
constexpr void shrink_to_fit() noexcept(useNoexcept);
|
||||
|
||||
private:
|
||||
constexpr void reserveInsert(
|
||||
std::size_t cap, std::size_t pos, std::size_t offset = 1) noexcept(useNoexcept);
|
||||
@ -341,6 +343,7 @@ constexpr Vector<T, SmallVectorSize, Allocator>::Vector(std::size_t size) noexce
|
||||
|
||||
template<typename T, std::size_t SmallVectorSize, typename Allocator>
|
||||
constexpr Vector<T, SmallVectorSize, Allocator>::Vector(std::initializer_list<T> list) noexcept {
|
||||
reserve(list.size());
|
||||
for (auto &item : list) {
|
||||
emplace_back(std::move(item));
|
||||
}
|
||||
@ -675,6 +678,24 @@ constexpr void Vector<T, SmallVectorSize, Allocator>::reserve(std::size_t cap) n
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T, std::size_t SmallVectorSize, typename Allocator>
|
||||
constexpr void Vector<T, SmallVectorSize, Allocator>::shrink_to_fit() noexcept(useNoexcept) {
|
||||
if (m_size == m_cap) {
|
||||
return;
|
||||
}
|
||||
const auto oldItems = m_items;
|
||||
const auto oldCap = m_cap;
|
||||
m_cap = m_size;
|
||||
this->allocate(&m_items, m_size);
|
||||
if (oldItems) { // move over old items
|
||||
for (std::size_t i = 0; i < m_size; ++i) {
|
||||
std::construct_at(&m_items[i], std::move(oldItems[i]));
|
||||
oldItems[i].~T();
|
||||
}
|
||||
this->deallocate(oldItems, oldCap);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T, std::size_t SmallVectorSize, typename Allocator>
|
||||
constexpr void Vector<T, SmallVectorSize, Allocator>::reserveInsert(
|
||||
std::size_t cap,
|
||||
|
Loading…
Reference in New Issue
Block a user