Skip to content

Commit

Permalink
fixing some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
KRM7 committed Sep 15, 2024
1 parent 3e07056 commit 5b27685
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/small_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#ifndef SMALL_VECTOR_SMALL_VECTOR_HPP
#define SMALL_VECTOR_SMALL_VECTOR_HPP

#include <array>
#include <algorithm>
#include <functional>
#include <iterator>
Expand All @@ -13,7 +12,6 @@
#include <type_traits>
#include <utility>
#include <stdexcept>
#include <cstring>
#include <cstdlib>
#include <cstddef>
#include <cassert>
Expand Down Expand Up @@ -200,7 +198,7 @@ namespace detail
{
if (!std::is_constant_evaluated())
{
std::memset(first, 0, sizeof(T) * (last - first));
std::fill((char*)first, (char*)last, 0);
return;
}
}
Expand Down Expand Up @@ -233,7 +231,8 @@ namespace detail
{
if (!std::is_constant_evaluated())
{
std::memcpy(first, std::to_address(src_first), sizeof(T) * (last - first));
char* src = (char*)std::to_address(src_first);
std::copy(src, src + sizeof(T) * (last - first), (char*)first);
return;
}
}
Expand All @@ -253,7 +252,7 @@ namespace detail
{
if (!std::is_constant_evaluated())
{
std::memcpy(dest, first, sizeof(T) * (last - first));
std::copy((char*)first, (char*)last, (char*)dest);
return;
}
}
Expand All @@ -273,7 +272,7 @@ namespace detail
{
if (!std::is_constant_evaluated())
{
std::memcpy(dest, first, sizeof(T) * (last - first));
std::copy((char*)first, (char*)last, (char*)dest);
return;
}
}
Expand Down Expand Up @@ -337,15 +336,15 @@ namespace detail
constexpr small_vector_buffer() noexcept {};
constexpr ~small_vector_buffer() noexcept {};

constexpr auto begin() noexcept { return data_.data(); }
constexpr auto begin() const noexcept { return data_.data(); }
constexpr auto begin() noexcept { return std::begin(data_); }
constexpr auto begin() const noexcept { return std::begin(data_); }

constexpr auto end() noexcept { return data_.data() + Size; }
constexpr auto end() const noexcept { return data_.data() + Size; }
constexpr auto end() noexcept { return std::end(data_); }
constexpr auto end() const noexcept { return std::end(data_); }

constexpr std::size_t size() const noexcept { return Size; }
constexpr std::size_t size() const noexcept { return std::size(data_); }
private:
union { std::array<T, Size> data_; };
union { T data_[Size]; };
};


Expand Down Expand Up @@ -427,8 +426,6 @@ class small_vector
constexpr small_vector(Iter src_first, Iter src_last, const A& allocator = A()) :
alloc_(allocator)
{
if (src_first == src_last) return;

const auto src_len = std::distance(src_first, src_last);
allocate_n(src_len);
detail::scope_exit guard{ [&] { deallocate(); } };
Expand Down Expand Up @@ -472,9 +469,9 @@ class small_vector
}
else
{
std::swap(first_, other.first_);
std::swap(last_, other.last_);
std::swap(last_alloc_, other.last_alloc_);
first_ = std::exchange(other.first_, other.buffer_.begin());
last_ = std::exchange(other.last_, other.buffer_.begin());
last_alloc_ = std::exchange(other.last_alloc_, other.buffer_.end());
}
}

Expand Down
35 changes: 35 additions & 0 deletions test/small_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ TEST_CASE("small_vector(nullptr, nullptr)", "[constructor]")
const small_vector<int> vec(static_cast<int*>(nullptr), static_cast<int*>(nullptr));

REQUIRE(vec.empty());
REQUIRE(vec.capacity() != 0);
}

TEMPLATE_TEST_CASE("small_vector(initializer_list)", "[constructor]", TrivialType, NonTrivialType, NonDefaultConstructibleType)
Expand Down Expand Up @@ -244,6 +245,7 @@ TEMPLATE_TEST_CASE("small_vector(const small_vector&)", "[constructor]", Trivial

REQUIRE(vec.size() == source.size());
REQUIRE(vec == source);
REQUIRE(vec.capacity() != 0);
}

TEMPLATE_TEST_CASE("small_vector(const small_vector&, Alloc)", "[constructor]", TrivialType, NonTrivialType, NonDefaultConstructibleType)
Expand All @@ -256,6 +258,7 @@ TEMPLATE_TEST_CASE("small_vector(const small_vector&, Alloc)", "[constructor]",

REQUIRE(vec.size() == source.size());
REQUIRE(vec == source);
REQUIRE(vec.capacity() != 0);
}

TEMPLATE_TEST_CASE("small_vector(small_vector&&)", "[constructor]", TrivialType, NonTrivialType, NonDefaultConstructibleType)
Expand All @@ -269,6 +272,12 @@ TEMPLATE_TEST_CASE("small_vector(small_vector&&)", "[constructor]", TrivialType,

REQUIRE(vec.size() == source_copy.size());
REQUIRE(vec == source_copy);

REQUIRE(source.empty());
REQUIRE(source.capacity() != 0);

source.push_back(TestType{ 11 });
REQUIRE(source.size() == 1);
}

TEMPLATE_TEST_CASE("small_vector<MoveOnlyType>(small_vector&&)", "[constructor]", MoveOnlyType)
Expand All @@ -282,6 +291,12 @@ TEMPLATE_TEST_CASE("small_vector<MoveOnlyType>(small_vector&&)", "[constructor]"

REQUIRE(vec.size() == source_copy.size());
REQUIRE(vec == source_copy);

REQUIRE(source.empty());
REQUIRE(source.capacity() != 0);

source.push_back(TestType{ 11 });
REQUIRE(source.size() == 1);
}

//-----------------------------------//
Expand Down Expand Up @@ -381,6 +396,11 @@ TEMPLATE_TEST_CASE("operator=(small_vector&&)", "[assignment]", TrivialType, Non

REQUIRE(dest.size() == src_copy.size());
REQUIRE(dest == src_copy);

REQUIRE(source.capacity() != 0);

source = dest;
REQUIRE(source == dest);
}

TEMPLATE_TEST_CASE("operator=(initializer_list)", "[assignment]", TrivialType, NonTrivialType, NonDefaultConstructibleType)
Expand Down Expand Up @@ -516,6 +536,11 @@ TEMPLATE_TEST_CASE("swap", "[modifiers]", TrivialType, NonTrivialType)

REQUIRE(right == old_left);
REQUIRE(left == old_right);

swap(left, right);

REQUIRE(left == old_left);
REQUIRE(right == old_right);
}

TEMPLATE_TEST_CASE("push_back(const T&)", "[modifiers]", TrivialType, NonTrivialType, NonDefaultConstructibleType)
Expand Down Expand Up @@ -960,6 +985,11 @@ TEMPLATE_TEST_CASE("propagate_on_move_assignment", "[allocators]", TrivialType,

REQUIRE(dest.size() == src_copy.size());
REQUIRE(dest == src_copy);

REQUIRE(source.capacity() != 0);

source = dest;
REQUIRE(source == dest);
}

TEMPLATE_TEST_CASE("propagate_on_swap", "[allocators]", TrivialType, NonTrivialType)
Expand All @@ -977,4 +1007,9 @@ TEMPLATE_TEST_CASE("propagate_on_swap", "[allocators]", TrivialType, NonTrivialT

REQUIRE(right == old_left);
REQUIRE(left == old_right);

swap(left, right);

REQUIRE(left == old_left);
REQUIRE(right == old_right);
}

0 comments on commit 5b27685

Please sign in to comment.