Skip to content

Commit

Permalink
use small_vector in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
KRM7 committed Sep 18, 2024
1 parent a12b17a commit 6032958
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 73 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ jobs:
fail-fast: false
matrix:
build-type: [ Release, RelWithDebInfo ]
platform: [ x64 ]
platform: [ x64, Win32 ]
generator: [ "Visual Studio 17 2022" ]
compiler: [
{ name: msvc, toolset: v143 },
{ name: clang, toolset: ClangCL }
]
build-shared: [ "ON", "OFF" ]
exclude:
- platform: Win32
build-type: Release

defaults:
run:
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ else() # GNU style compiler interface
# gcc specific options
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
# -Warray-bounds, -Wstringop-overflow, -Wstringop-overread are regular false positives since g++-12
set(GAPP_WARN_FLAGS "${GAPP_WARN_FLAGS} -Wlogical-op -Wno-array-bounds -Wno-stringop-overflow -Wno-stringop-overread -Wno-free-nonheap-object")
set(GAPP_WARN_FLAGS "${GAPP_WARN_FLAGS} -Wlogical-op -Wno-array-bounds -Wno-stringop-overflow -Wno-stringop-overread")
endif()
# clang specific options
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
Expand Down
2 changes: 1 addition & 1 deletion src/utility/dynamic_bitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ namespace gapp::detail
}

private:
small_vector<block_type> blocks_;
small_vector<block_type, 4> blocks_;
size_type size_ = 0;

constexpr size_type find_first_one() const noexcept
Expand Down
4 changes: 2 additions & 2 deletions src/utility/rng.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,12 +475,12 @@ namespace gapp::rng
template<std::integral IntType>
small_vector<IntType> sampleUnique(IntType lbound, IntType ubound, size_t count)
{
const size_t range_len = detail::range_length(lbound, ubound);
const std::uint64_t range_len = detail::range_length(lbound, ubound);

GAPP_ASSERT(ubound >= lbound);
GAPP_ASSERT(range_len >= count);

const bool select_many = (count > 0.6 * range_len);
const bool select_many = (count >= std::uint64_t(0.6 * range_len));
const bool huge_range = (range_len >= 65536);

if (huge_range) [[unlikely]] return rng::sampleUniqueSet(lbound, ubound, count);
Expand Down
5 changes: 1 addition & 4 deletions src/utility/small_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,10 +890,7 @@ namespace gapp
}

private:
static constexpr std::size_t alignment = std::max(alignof(T), detail::cache_line_size);

alignas(alignment)
GAPP_NO_UNIQUE_ADDRESS detail::small_vector_buffer<T, Size> buffer_;
detail::small_vector_buffer<T, Size> buffer_;
pointer first_ = nullptr;
pointer last_ = nullptr;
pointer last_alloc_ = nullptr;
Expand Down
31 changes: 9 additions & 22 deletions src/utility/utility.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* Copyright (c) 2022 Krisztián Rugási. Subject to the MIT License. */

#ifndef GA_UTILITY_UTILITY_HPP
#define GA_UTILITY_UTILITY_HPP
#ifndef GAPP_UTILITY_UTILITY_HPP
#define GAPP_UTILITY_UTILITY_HPP

#include <vector>
#include <concepts>
#include <type_traits>
#include <cassert>
#include <cstdint>
#include <cstddef>


Expand Down Expand Up @@ -76,9 +77,9 @@


#if defined(__GNUC__) || defined(__clang__)
# define GAPP_NON_NULL __attribute__((nonnull))
# define GAPP_MAY_ALIAS __attribute__((may_alias))
#else
# define GAPP_NON_NULL
# define GAPP_MAY_ALIAS
#endif


Expand Down Expand Up @@ -178,27 +179,13 @@ namespace gapp::detail
temp.swap(vec);
}

// returns true if the signs of the parameters are the same
template<std::signed_integral T>
constexpr bool same_sign(T left, T right) noexcept
{
return (left ^ right) >= 0;
}

// returns the length of the range [low, high) without overflow
// returns the length of the range [low, high] without overflow
template<std::integral T>
constexpr size_t range_length(T low, T high) noexcept
constexpr std::uint64_t range_length(T low, T high) noexcept
{
GAPP_ASSERT(low <= high);

if constexpr (std::is_unsigned_v<T>)
{
return high - low;
}
else
{
return same_sign(low, high) ? high - low : size_t(-(low + 1)) + high + 1;
}
return std::uint64_t(high) - std::uint64_t(low);
}

template<std::integral T>
Expand Down Expand Up @@ -233,4 +220,4 @@ namespace gapp::detail

} // namespace gapp::detail

#endif // !GA_UTILITY_UTILITY_HPP
#endif // !GAPP_UTILITY_UTILITY_HPP
41 changes: 0 additions & 41 deletions test/unit/algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,6 @@ static constexpr auto always_false = [](auto) { return false; };

using namespace gapp;

TEST_CASE("next_mod", "[algorithm]")
{
REQUIRE(detail::next_mod(0, 3) == 1);
REQUIRE(detail::next_mod(1, 3) == 2);
REQUIRE(detail::next_mod(2, 3) == 0);
}

TEST_CASE("prev_mod", "[algorithm]")
{
REQUIRE(detail::prev_mod(0, 3) == 2);
REQUIRE(detail::prev_mod(1, 3) == 0);
REQUIRE(detail::prev_mod(2, 3) == 1);
}

TEST_CASE("increment_mod", "[algorithm]")
{
int n = 0;

detail::increment_mod(n, 3);
REQUIRE(n == 1);

detail::increment_mod(n, 3);
REQUIRE(n == 2);

detail::increment_mod(n, 3);
REQUIRE(n == 0);
}

TEST_CASE("decrement_mod", "[algorithm]")
{
int n = 0;

detail::decrement_mod(n, 3);
REQUIRE(n == 2);

detail::decrement_mod(n, 3);
REQUIRE(n == 1);

detail::decrement_mod(n, 3);
REQUIRE(n == 0);
}

TEST_CASE("index_vector", "[algorithm]")
{
Expand Down
1 change: 0 additions & 1 deletion test/unit/small_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ TEST_CASE("small_vector_size", "[object_layout][!mayfail]") // fails under clang
STATIC_REQUIRE(std::is_standard_layout_v<small_vector<int>>);

CHECK(sizeof(small_vector<int>) == detail::cache_line_size);
CHECK(alignof(small_vector<int>) == detail::cache_line_size);
}


Expand Down
97 changes: 97 additions & 0 deletions test/unit/utility.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* Copyright (c) 2024 Krisztián Rugási. Subject to the MIT License. */

#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_template_test_macros.hpp>
#include "utility/utility.hpp"
#include <type_traits>
#include <limits>
#include <cstdint>

using namespace gapp;
using namespace gapp::detail;


TEMPLATE_TEST_CASE("range_length_signed", "[utility]", std::int8_t, std::int16_t, std::int32_t, std::int64_t)
{
using IntType = TestType;
using UIntType = std::make_unsigned_t<IntType>;

constexpr IntType small = std::numeric_limits<IntType>::min();
constexpr IntType large = std::numeric_limits<IntType>::max();

STATIC_REQUIRE(range_length(IntType{ 0 }, IntType{ 0 }) == 0);
STATIC_REQUIRE(range_length(IntType{ -1 }, IntType{ -1 }) == 0);
STATIC_REQUIRE(range_length(IntType{ 1 }, IntType{ 1 }) == 0);

STATIC_REQUIRE(range_length(IntType{ 0 }, IntType{ 1 }) == 1);
STATIC_REQUIRE(range_length(IntType{ -1 }, IntType{ 0 }) == 1);

STATIC_REQUIRE(range_length(IntType{ -1 }, IntType{ 1 }) == 2);

STATIC_REQUIRE(range_length(small, small) == 0);
STATIC_REQUIRE(range_length(large, large) == 0);

STATIC_REQUIRE(range_length(small, IntType{ 0 }) == (UIntType(large) + 1));
STATIC_REQUIRE(range_length(IntType{ 0 }, large) == UIntType(large));
STATIC_REQUIRE(range_length(small, large) == std::numeric_limits<UIntType>::max());
}

TEMPLATE_TEST_CASE("range_length_unsigned", "[utility]", std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t)
{
using IntType = TestType;

constexpr IntType small = std::numeric_limits<IntType>::min();
constexpr IntType large = std::numeric_limits<IntType>::max();

STATIC_REQUIRE(range_length(IntType{ 0 }, IntType{ 0 }) == 0);
STATIC_REQUIRE(range_length(IntType{ 1 }, IntType{ 1 }) == 0);

STATIC_REQUIRE(range_length(IntType{ 0 }, IntType{ 1 }) == 1);

STATIC_REQUIRE(range_length(small, small) == 0);
STATIC_REQUIRE(range_length(large, large) == 0);

STATIC_REQUIRE(range_length(small, large) == large);
}

TEST_CASE("next_mod", "[algorithm]")
{
REQUIRE(detail::next_mod(0, 3) == 1);
REQUIRE(detail::next_mod(1, 3) == 2);
REQUIRE(detail::next_mod(2, 3) == 0);
}

TEST_CASE("prev_mod", "[algorithm]")
{
REQUIRE(detail::prev_mod(0, 3) == 2);
REQUIRE(detail::prev_mod(1, 3) == 0);
REQUIRE(detail::prev_mod(2, 3) == 1);
}

TEST_CASE("increment_mod", "[algorithm]")
{
int n = 0;

detail::increment_mod(n, 3);
REQUIRE(n == 1);

detail::increment_mod(n, 3);
REQUIRE(n == 2);

detail::increment_mod(n, 3);
REQUIRE(n == 0);
}

TEST_CASE("decrement_mod", "[algorithm]")
{
int n = 0;

detail::decrement_mod(n, 3);
REQUIRE(n == 2);

detail::decrement_mod(n, 3);
REQUIRE(n == 1);

detail::decrement_mod(n, 3);
REQUIRE(n == 0);
}

0 comments on commit 6032958

Please sign in to comment.