Skip to content

Commit

Permalink
Fixed most Wsign-conversion warnings
Browse files Browse the repository at this point in the history
Also a few code style changes
  • Loading branch information
OleErikPeistorpet committed Jun 2, 2024
1 parent f119f1c commit c9d877a
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 72 deletions.
4 changes: 2 additions & 2 deletions auxi/dynarray_detail.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace oel::_detail
struct DebugAllocationHeader
{
std::uintptr_t id;
size_t nObjects;
ptrdiff_t nObjects;
};

inline constexpr DebugAllocationHeader headerNoAllocation{};
Expand All @@ -42,7 +42,7 @@ namespace oel::_detail
inline bool HasValidIndex(const T * arrayElem, const DebugAllocationHeader & h)
{
auto index = arrayElem - reinterpret_cast<const T *>(&h + 1);
return static_cast<size_t>(index) < h.nObjects;
return static_cast<size_t>(index) < static_cast<size_t>(h.nObjects);
}

template< typename Alloc, typename Ptr >
Expand Down
69 changes: 34 additions & 35 deletions dynarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ is_trivially_relocatable<Alloc> specify_trivial_relocate(dynarray<T, Alloc>);

//! Overloads generic unordered_erase(RandomAccessContainer &, Integral) (in range_algo.h)
template< typename T, typename A > inline
void unordered_erase(dynarray<T, A> & d, size_t index) { d.unordered_erase(d.begin() + index); }
void unordered_erase(dynarray<T, A> & d, ptrdiff_t index) { d.unordered_erase(d.begin() + index); }

#if OEL_MEM_BOUND_DEBUG_LVL
inline namespace debug
Expand Down Expand Up @@ -206,9 +206,9 @@ class dynarray

[[nodiscard]] bool empty() const noexcept { return _m.data == _m.end; }

size_type size() const noexcept { return _m.end - _m.data; }
size_type size() const noexcept { return static_cast<size_t>(_m.end - _m.data); }

size_type capacity() const noexcept { return _m.reservEnd - _m.data; }
size_type capacity() const noexcept { return static_cast<size_t>(_m.reservEnd - _m.data); }

constexpr size_type max_size() const noexcept { return _alloTrait::max_size(_m) - _allocateWrap::sizeForHeader; }

Expand Down Expand Up @@ -242,15 +242,21 @@ class dynarray
T & back() noexcept { return *_detail::MakeDynarrIter (_m, _m.end - 1); }
const T & back() const noexcept { return *_detail::MakeDynarrIter<const T *>(_m, _m.end - 1); }

T & operator[](size_type index) noexcept { OEL_ASSERT(index < size()); return _m.data[index]; }
const T & operator[](size_type index) const noexcept { OEL_ASSERT(index < size()); return _m.data[index]; }

T & at(size_type index) OEL_ALWAYS_INLINE
{
const auto & cSelf = *this;
return const_cast<T &>(cSelf.at(index));
}
const T & at(size_type index) const;

T & operator[](size_type index) noexcept { OEL_ASSERT(index < size()); return _m.data[index]; }
const T & operator[](size_type index) const noexcept { OEL_ASSERT(index < size()); return _m.data[index]; }
const T & at(size_type index) const
{
if (index < size()) // would be unsafe with signed size_type
return _m.data[index];
else
_detail::OutOfRange::raise("Bad index dynarray::at");
}

friend bool operator==(const dynarray & left, const dynarray & right)
{
Expand Down Expand Up @@ -290,7 +296,7 @@ class dynarray
: ::oel::_detail::DynarrBase<value_type *>{}, _usedAlloc_7KQw{std::move(a)} {
}

_memOwner(_memOwner && other) noexcept
constexpr _memOwner(_memOwner && other) noexcept
: ::oel::_detail::DynarrBase<value_type *>{other}, _usedAlloc_7KQw{std::move(other)}
{
other.reservEnd = other.end = other.data = nullptr;
Expand All @@ -299,7 +305,10 @@ class dynarray
~_memOwner()
{
if (data)
::oel::_detail::DebugAllocateWrapper<_usedAlloc_7KQw, value_type *>::dealloc(*this, data, reservEnd - data);
{
::oel::_detail::DebugAllocateWrapper<_usedAlloc_7KQw, value_type *>
::dealloc(*this, data, static_cast<size_t>(reservEnd - data));
}
}
}
_m; // the only non-static data member
Expand Down Expand Up @@ -328,9 +337,9 @@ class dynarray
}


size_type _unusedCapacity() const
auto _spareCapacity() const
{
return _m.reservEnd - _m.end;
return static_cast<size_type>(_m.reservEnd - _m.end);
}

size_type _calcCapUnchecked(size_type const newSize) const
Expand Down Expand Up @@ -375,13 +384,13 @@ class dynarray
{
if constexpr (allocator_can_realloc<allocator_type>)
{
T *const p = _allocateWrap::realloc(_m, _m.data, newCap);
auto const p = _allocateWrap::realloc(_m, _m.data, newCap);
_m.data = p;
_m.end = p + oldSize;
_m.reservEnd = p + newCap;
}
else
{ T *const newData = _allocateWrap::allocate(_m, newCap);
{ auto const newData = _allocateWrap::allocate(_m, newCap);
_m.end = _detail::Relocate(_m.data, oldSize, newData);
_resetData(newData, newCap);
}
Expand Down Expand Up @@ -445,14 +454,14 @@ class dynarray
while (dest != dLast)
{
*dest = *src_;
++src_; ++dest;
++dest; ++src_;
}
return src_;
};
T * newEnd;
if (capacity() < count)
{
T *const newData = _allocateChecked(count);
auto const newData = _allocateChecked(count);
// Old elements might hold some limited resource, probably good to destroy them before constructing new
_detail::Destroy(_m.data, _m.end);
_resetData(newData, count);
Expand Down Expand Up @@ -509,7 +518,7 @@ class dynarray
template< typename InputIter >
InputIter _doAppend(InputIter src, size_type const count)
{
if (_unusedCapacity() < count)
if (_spareCapacity() < count)
_growBy(count);

if constexpr (can_memmove_with<T *, InputIter>)
Expand All @@ -520,7 +529,7 @@ class dynarray
}
else
{ T *__restrict dest = _m.end;
T *const dLast = dest + count;
auto const dLast = dest + count;
OEL_TRY_
{
while (dest != dLast)
Expand Down Expand Up @@ -634,7 +643,7 @@ typename dynarray<T, Alloc>::iterator

size_t const bytesAfterPos{sizeof(T) * (_m.end - pPos)};
T * dLast;
if (_unusedCapacity() >= count)
if (_spareCapacity() >= count)
{
dLast = pPos + count;
// Relocate elements to make space, leaving [pos, pos + count) uninitialized (conceptually)
Expand Down Expand Up @@ -797,10 +806,10 @@ void dynarray<T, Alloc>::shrink_to_fit()
template< typename T, typename Alloc >
inline void dynarray<T, Alloc>::append(size_type count, const T &__restrict val)
{
if (_unusedCapacity() < count)
if (_spareCapacity() < count)
_growBy(count);

T *const pos = _m.end;
auto const pos = _m.end;
_uninitFill::template call< _detail::ForwardT<const T &> >(pos, pos + count, _m, val);

_debugSizeUpdater guard{_m};
Expand All @@ -821,7 +830,7 @@ inline void dynarray<T, Alloc>::pop_back() noexcept
template< typename T, typename Alloc >
void dynarray<T, Alloc>::erase_to_end(iterator first) noexcept
{
T *const newEnd = to_pointer_contiguous(first);
T *const newEnd{to_pointer_contiguous(first)};
OEL_ASSERT(_m.data <= newEnd and newEnd <= _m.end);

_detail::Destroy(newEnd, _m.end);
Expand Down Expand Up @@ -856,12 +865,12 @@ typename dynarray<T, Alloc>::iterator dynarray<T, Alloc>::erase(iterator pos) _
{
_debugSizeUpdater guard{_m};

T *const ptr = to_pointer_contiguous(pos);
T *const ptr{to_pointer_contiguous(pos)};
OEL_ASSERT(_m.data <= ptr and ptr < _m.end);
if constexpr (is_trivially_relocatable<T>::value)
{
ptr-> ~T();
T *const next = ptr + 1;
auto const next = ptr + 1;
std::memmove( // relocate [pos + 1, end) to [pos, end - 1)
static_cast<void *>(ptr),
static_cast<const void *>(next),
Expand All @@ -880,8 +889,8 @@ typename dynarray<T, Alloc>::iterator dynarray<T, Alloc>::erase(iterator first,
{
_debugSizeUpdater guard{_m};

T * dest = to_pointer_contiguous(first);
const T *const pLast = to_pointer_contiguous(last);
T * dest{to_pointer_contiguous(first)};
const T *const pLast{to_pointer_contiguous(last)};
OEL_ASSERT(_m.data <= dest and dest <= pLast and pLast <= _m.end);

if constexpr (is_trivially_relocatable<T>::value)
Expand All @@ -904,16 +913,6 @@ typename dynarray<T, Alloc>::iterator dynarray<T, Alloc>::erase(iterator first,
}


template< typename T, typename Alloc >
const T & dynarray<T, Alloc>::at(size_type i) const
{
if (i < size()) // would be unsafe with signed size_type
return _m.data[i];
else
_detail::OutOfRange::raise("Bad index dynarray::at");
}


template< typename InputRange, typename Alloc = allocator<> >
explicit dynarray(InputRange &&, Alloc = {})
-> dynarray<
Expand Down
16 changes: 8 additions & 8 deletions unit_test/dynarray_construct_assignop_swap_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ TEST_F(dynarrayConstructTest, constructRangeNoCopyAssign)

TEST_F(dynarrayConstructTest, constructForwardRangeNoSize)
{
for (size_t const n : {0, 1, 59})
for (auto const n : {0u, 1u, 59u})
{
std::forward_list<int> li(n, -6);
dynarray<int> d(li);
Expand Down Expand Up @@ -373,12 +373,12 @@ void testConstructMoveElements()
g_allocCount.clear();
T::clearCount();
// not propagating, not equal, cannot steal the memory
for (auto const na : {0, 1, 101})
for (auto const na : {0u, 1u, 101u})
{
using Alloc = StatefulAllocator<T, false, false>;
dynarray<T, Alloc> a(reserve, na, Alloc{1});

for (int i = 0; i < na; ++i)
for (unsigned i{}; i < na; ++i)
a.emplace_back(i + 0.5);

auto const capBefore = a.capacity();
Expand All @@ -397,8 +397,8 @@ void testConstructMoveElements()
EXPECT_EQ(ssize(a) + ssize(b), T::nConstructions - T::nDestruct);

EXPECT_EQ(capBefore, a.capacity());
ASSERT_EQ(na, ssize(b));
for (int i = 0; i < na; ++i)
ASSERT_EQ(na, b.size());
for (unsigned i{}; i < na; ++i)
EXPECT_TRUE(b[i].hasValue() and *b[i] == i + 0.5);
}
EXPECT_EQ(T::nConstructions, T::nDestruct);
Expand Down Expand Up @@ -471,8 +471,8 @@ void testAssignMoveElements()
for (auto const nb : {0, 1, 2})
{
using Alloc = StatefulAllocator<T, false, false>;
dynarray<T, Alloc> a(reserve, na, Alloc{1});
dynarray<T, Alloc> b(reserve, nb, Alloc{2});
dynarray<T, Alloc> a(reserve, as_unsigned(na), Alloc{1});
dynarray<T, Alloc> b(reserve, as_unsigned(nb), Alloc{2});

ASSERT_FALSE(a.get_allocator() == b.get_allocator());

Expand All @@ -497,7 +497,7 @@ void testAssignMoveElements()

EXPECT_EQ(capBefore, a.capacity());
ASSERT_EQ(na, ssize(b));
for (int i = 0; i < na; ++i)
for (unsigned i{}; i < as_unsigned(na); ++i)
EXPECT_TRUE(b[i].hasValue() and *b[i] == i + 0.5);
}
EXPECT_EQ(T::nConstructions, T::nDestruct);
Expand Down
16 changes: 8 additions & 8 deletions unit_test/dynarray_mutate_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ TEST_F(dynarrayTest, assignNonForwardRange)
decltype(das) copyDest;

copyDest.assign(view::counted(das.cbegin(), 2));
copyDest.assign( view::counted(begin(das), das.size()) );
copyDest.assign( view::counted(begin(das), ssize(das)) );

EXPECT_TRUE(das == copyDest);

Expand Down Expand Up @@ -394,7 +394,7 @@ TEST_F(dynarrayTest, appendNonForwardRange)
EXPECT_EQ(it, end);
EXPECT_EQ(3u, dest.size());
for (int i = 0; i < 3; ++i)
EXPECT_EQ(i + 1, dest[i]);
EXPECT_EQ( i + 1, dest[oel::as_unsigned(i)] );
}
#endif

Expand All @@ -413,7 +413,7 @@ TEST_F(dynarrayTest, insertRTrivial)
dest.emplace_back(1);
dest.emplace_back(2);

dest.insert_range(dest.begin() + insertOffset, toInsert);
dest.insert_range(dest.begin() + oel::as_signed(insertOffset), toInsert);

EXPECT_TRUE(dest.size() == initSize + toInsert.size());
for (size_t i = 0; i < toInsert.size(); ++i)
Expand Down Expand Up @@ -453,7 +453,7 @@ TEST_F(dynarrayTest, insertR)
if (countThrow < toInsert.size())
{
#if OEL_HAS_EXCEPTIONS
TrivialRelocat::countToThrowOn = countThrow;
TrivialRelocat::countToThrowOn = oel::as_signed(countThrow);
EXPECT_THROW( dest.insert_range(dest.begin() + insertOffset, toInsert), TestException );
#endif
EXPECT_TRUE(initSize <= dest.size() and dest.size() <= initSize + countThrow);
Expand Down Expand Up @@ -500,7 +500,7 @@ TEST_F(dynarrayTest, emplace)
{ TrivialRelocat::countToThrowOn = -1;
g_allocCount.countToThrowOn = -1;

dynarrayTrackingAlloc<TrivialRelocat> dest(oel::reserve, nReserve);
dynarrayTrackingAlloc<TrivialRelocat> dest(oel::reserve, oel::as_unsigned(nReserve));
dest.emplace(dest.begin(), firstVal);
dest.emplace(dest.begin(), secondVal);

Expand All @@ -519,7 +519,7 @@ TEST_F(dynarrayTest, emplace)
{ dest.emplace(dest.begin() + insertOffset);

EXPECT_EQ(initSize + 1, ssize(dest));
EXPECT_FALSE( dest.at(insertOffset).hasValue() );
EXPECT_FALSE( dest.at(oel::as_unsigned(insertOffset)).hasValue() );
}
if (insertOffset == 0)
{
Expand Down Expand Up @@ -961,9 +961,9 @@ TEST_F(dynarrayTest, misc)
dest0.reserve(1);
dest0 = daSrc;

dest0.append( view::counted(daSrc.cbegin(), daSrc.size()) );
dest0.append( view::counted(daSrc.cbegin(), ssize(daSrc)) );
dest0.append(view::counted(fASrc, 2));
auto srcEnd = dest0.append( view::counted(begin(dequeSrc), dequeSrc.size()) );
auto srcEnd = dest0.append( view::counted(begin(dequeSrc), oel::ssize(dequeSrc)) );
EXPECT_TRUE(end(dequeSrc) == srcEnd);

dynarray<size_t> dest1;
Expand Down
6 changes: 3 additions & 3 deletions unit_test/range_algo_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@

namespace view = oel::view;

TEST(rangeTest, eraseUnstable)
TEST(rangeTest, unorderedErase)
{
std::deque<std::string> d{"aa", "bb", "cc"};

oel::unordered_erase(d, 1);
oel::unordered_erase(d, 1u);
EXPECT_EQ(2U, d.size());
EXPECT_EQ("cc", d.back());
oel::unordered_erase(d, 1);
oel::unordered_erase(d, 1u);
EXPECT_EQ(1U, d.size());
EXPECT_EQ("aa", d.front());
}
Expand Down
4 changes: 2 additions & 2 deletions unit_test/util_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ TEST(utilTest, makeUnique)
EXPECT_TRUE(ps[0].empty());
EXPECT_TRUE(ps[1].empty());

auto a = oel::make_unique_for_overwrite<int[]>(5);
for (int i = 0; i < 5; ++i)
auto a = oel::make_unique_for_overwrite<size_t[]>(5);
for (size_t i{}; i < 5u; ++i)
a[i] = i;
}

Expand Down
4 changes: 2 additions & 2 deletions unit_test/view_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ TEST(viewTest, viewCounted)
#endif
{
oel::dynarray<int> i{1, 2};
auto test = view::counted(i.begin(), i.size());
auto test = view::counted(i.begin(), ssize(i));
EXPECT_EQ(i.size(), test.size());
EXPECT_EQ(1, test[0]);
EXPECT_EQ(2, test[1]);
Expand Down Expand Up @@ -148,7 +148,7 @@ constexpr auto multBy2(StdArrInt2 a)
{ constexpr auto operator()(int i) const { return 2 * i; }
} mult2{};
auto v = view::transform(a, mult2);
std::ptrdiff_t i{};
size_t i{};
for (auto val : v)
res[i++] = val;

Expand Down
Loading

0 comments on commit c9d877a

Please sign in to comment.