Skip to content

Commit 12d640f

Browse files
committed
Avoid C-style casts to convert iterators
1 parent 016dc98 commit 12d640f

File tree

3 files changed

+79
-75
lines changed

3 files changed

+79
-75
lines changed

c/include/cccl/types.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct cccl_type_info
4545
enum class cccl_op_kind_t
4646
{
4747
stateless = 0,
48-
stateful = 1
48+
stateful = 1
4949
};
5050

5151
struct cccl_op_t
@@ -61,7 +61,7 @@ struct cccl_op_t
6161

6262
enum class cccl_iterator_kind_t
6363
{
64-
pointer = 0,
64+
pointer = 0,
6565
iterator = 1
6666
};
6767

libcudacxx/include/cuda/std/inplace_vector

+73-71
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ public:
10471047
// [containers.sequences.inplace.vector.access], element access
10481048
_CCCL_NODISCARD _LIBCUDACXX_INLINE_VISIBILITY constexpr reference at(const size_type __pos)
10491049
{
1050-
if (this->size() < __pos)
1050+
if (__pos > this->size())
10511051
{
10521052
_CUDA_VSTD::__throw_out_of_range("inplace_vector::at");
10531053
}
@@ -1056,7 +1056,7 @@ public:
10561056

10571057
_CCCL_NODISCARD _LIBCUDACXX_INLINE_VISIBILITY constexpr const_reference at(const size_type __pos) const
10581058
{
1059-
if (this->size() < __pos)
1059+
if (__pos > this->size())
10601060
{
10611061
_CUDA_VSTD::__throw_out_of_range("inplace_vector::at");
10621062
}
@@ -1168,43 +1168,44 @@ public:
11681168
_LIBCUDACXX_INLINE_VISIBILITY constexpr iterator
11691169
insert(const_iterator __cpos, const size_type __count, const _Tp& __value)
11701170
{
1171-
const iterator __pos = (iterator) __cpos;
1172-
const iterator __end = this->end();
1173-
if (this->size() + __count > _Capacity)
1171+
const auto __pos = static_cast<size_type>(__cpos - this->cbegin());
1172+
if (__count > _Capacity - this->size())
11741173
{
11751174
_CUDA_VSTD::__throw_bad_alloc();
11761175
}
1177-
else if (__pos < this->begin() || __end < __pos)
1176+
else if (__pos > this->size())
11781177
{
11791178
_CUDA_VSTD::__throw_out_of_range("inplace_vector::insert(const_iterator, size_type, T)");
11801179
}
11811180

1181+
const iterator __first = this->begin() + __pos;
11821182
if (__count == 0)
11831183
{
1184-
return __pos;
1184+
return __first;
11851185
}
11861186

1187-
if (__pos == __end)
1187+
const iterator __end = this->end();
1188+
if (__pos == this->size())
11881189
{
11891190
this->__uninitialized_fill(__end, __end + __count, __value);
1190-
return __pos;
1191+
return __first;
11911192
}
11921193

1193-
const iterator __middle = __pos + __count;
1194+
const iterator __middle = __first + __count;
11941195
if (__end <= __middle)
11951196
{ // all existing elements are pushed into uninitialized storage
11961197
this->__uninitialized_fill(__end, __middle, __value);
1197-
this->__uninitialized_move(__pos, __end, __middle);
1198-
_CUDA_VSTD::fill(__pos, __end, __value);
1198+
this->__uninitialized_move(__first, __end, __middle);
1199+
_CUDA_VSTD::fill(__first, __end, __value);
11991200
}
12001201
else
12011202
{ // some elements get copied into existing storage
12021203
this->__uninitialized_move(__end - __count, __end, __end);
1203-
_CUDA_VSTD::move_backward(__pos, __end - __count, __end);
1204-
_CUDA_VSTD::fill(__pos, __middle, __value);
1204+
_CUDA_VSTD::move_backward(__first, __end - __count, __end);
1205+
_CUDA_VSTD::fill(__first, __middle, __value);
12051206
}
12061207

1207-
return __pos;
1208+
return __first;
12081209
}
12091210

12101211
_LIBCUDACXX_TEMPLATE(class _Iter)
@@ -1219,99 +1220,99 @@ public:
12191220
emplace_back(*__first);
12201221
}
12211222

1222-
const iterator __pos = (iterator) __cpos;
1223-
_CUDA_VSTD::rotate(__pos, __old_end, this->end());
1224-
return __pos;
1223+
const iterator __res = this->begin() + (__cpos - this->cbegin());
1224+
_CUDA_VSTD::rotate(__res, __old_end, this->end());
1225+
return __res;
12251226
}
12261227

12271228
_LIBCUDACXX_TEMPLATE(class _Iter)
12281229
_LIBCUDACXX_REQUIRES(__is_cpp17_forward_iterator<_Iter>::value)
12291230
_LIBCUDACXX_INLINE_VISIBILITY constexpr iterator insert(const_iterator __cpos, _Iter __first, _Iter __last)
12301231
{
1231-
const iterator __pos = (iterator) __cpos;
1232-
const iterator __end = this->end();
1233-
const auto __count = _CUDA_VSTD::distance(__first, __last);
1234-
if (this->size() + __count > _Capacity)
1232+
const auto __pos = static_cast<size_type>(__cpos - this->cbegin());
1233+
const auto __count = static_cast<size_type>(_CUDA_VSTD::distance(__first, __last));
1234+
if (__count > _Capacity - this->size())
12351235
{
12361236
_CUDA_VSTD::__throw_bad_alloc();
12371237
}
1238-
else if (__pos < this->begin() || __end < __pos)
1238+
else if (__pos > this->size())
12391239
{
12401240
_CUDA_VSTD::__throw_out_of_range("inplace_vector::insert(const_iterator, Iter, Iter)");
12411241
}
12421242

1243+
const iterator __res = this->begin() + __pos;
12431244
if (__count == 0)
12441245
{
1245-
return __pos;
1246+
return __res;
12461247
}
12471248

1248-
if (__pos == __end)
1249+
const iterator __end = this->end();
1250+
if (__pos == this->size())
12491251
{
12501252
this->__uninitialized_copy(__first, __last, __end);
1251-
return __pos;
1253+
return __res;
12521254
}
12531255

1254-
const iterator __middle = __pos + __count;
1255-
const auto __to_copy = __end - __pos;
1256+
const iterator __middle = __res + __count;
12561257
if (__end <= __middle)
12571258
{ // all existing elements are pushed into uninitialized storage
1258-
_Iter __imiddle = _CUDA_VSTD::next(__first, __to_copy);
1259+
_Iter __imiddle = _CUDA_VSTD::next(__first, this->size() - __pos);
12591260
this->__uninitialized_copy(__imiddle, __last, __end);
1260-
this->__uninitialized_move(__pos, __end, __middle);
1261-
_CUDA_VSTD::copy(__first, __imiddle, __pos);
1261+
this->__uninitialized_move(__res, __end, __middle);
1262+
_CUDA_VSTD::copy(__first, __imiddle, __res);
12621263
}
12631264
else
12641265
{ // all new elements get copied into existing storage
12651266
this->__uninitialized_move(__end - __count, __end, __end);
1266-
_CUDA_VSTD::move_backward(__pos, __end - __count, __end);
1267-
_CUDA_VSTD::copy(__first, __last, __pos);
1267+
_CUDA_VSTD::move_backward(__res, __end - __count, __end);
1268+
_CUDA_VSTD::copy(__first, __last, __res);
12681269
}
12691270

1270-
return __pos;
1271+
return __res;
12711272
}
12721273

12731274
_LIBCUDACXX_INLINE_VISIBILITY constexpr iterator insert(const_iterator __cpos, initializer_list<_Tp> __ilist)
12741275
{
1275-
const iterator __pos = (iterator) __cpos;
1276-
const iterator __end = this->end();
1277-
const auto __count = __ilist.size();
1278-
if (this->size() + __count > _Capacity)
1276+
const auto __pos = static_cast<size_type>(__cpos - this->cbegin());
1277+
const auto __count = __ilist.size();
1278+
if (__count > _Capacity - this->size())
12791279
{
12801280
_CUDA_VSTD::__throw_bad_alloc();
12811281
}
1282-
else if (__pos < this->begin() || __end < __pos)
1282+
else if (__pos > this->size())
12831283
{
12841284
_CUDA_VSTD::__throw_out_of_range("inplace_vector::insert(const_iterator, initializer_list)");
12851285
}
12861286

1287+
const iterator __res = this->begin() + __pos;
12871288
if (__count == 0)
12881289
{
1289-
return __pos;
1290+
return __res;
12901291
}
12911292

1292-
if (__pos == __end)
1293+
const iterator __end = this->end();
1294+
if (__pos == this->size())
12931295
{
12941296
this->__uninitialized_copy(__ilist.begin(), __ilist.end(), __end);
1295-
return __pos;
1297+
return __res;
12961298
}
12971299

1298-
const iterator __middle = __pos + __count;
1299-
const auto __to_copy = __end - __pos;
1300+
const iterator __middle = __res + __count;
13001301
if (__end <= __middle)
13011302
{ // all existing elements are pushed into uninitialized storage
1302-
auto __imiddel = __ilist.begin() + __to_copy;
1303+
auto __imiddel = __ilist.begin() + this->size() - __pos;
13031304
this->__uninitialized_copy(__imiddel, __ilist.end(), __end);
1304-
this->__uninitialized_move(__pos, __end, __middle);
1305-
_CUDA_VSTD::copy(__ilist.begin(), __imiddel, __pos);
1305+
this->__uninitialized_move(__res, __end, __middle);
1306+
_CUDA_VSTD::copy(__ilist.begin(), __imiddel, __res);
13061307
}
13071308
else
13081309
{ // all new elements get copied into existing storage
13091310
this->__uninitialized_move(__end - __count, __end, __end);
1310-
_CUDA_VSTD::move_backward(__pos, __end - __count, __end);
1311-
_CUDA_VSTD::copy(__ilist.begin(), __ilist.end(), __pos);
1311+
_CUDA_VSTD::move_backward(__res, __end - __count, __end);
1312+
_CUDA_VSTD::copy(__ilist.begin(), __ilist.end(), __res);
13121313
}
13131314

1314-
return __pos;
1315+
return __res;
13151316
}
13161317

13171318
# if _CCCL_STD_VER >= 2017 && !defined(_CCCL_COMPILER_MSVC_2017)
@@ -1329,7 +1330,7 @@ public:
13291330
emplace_back(*__first);
13301331
}
13311332

1332-
const iterator __pos = (iterator) __cpos;
1333+
const auto __pos = this->begin() + static_cast<size_type>(__cpos - this->cbegin());
13331334
_CUDA_VSTD::rotate(__pos, __old_end, this->end());
13341335
return __pos;
13351336
}
@@ -1369,30 +1370,30 @@ public:
13691370
template <class... _Args>
13701371
_LIBCUDACXX_INLINE_VISIBILITY constexpr iterator emplace(const_iterator __cpos, _Args&&... __args)
13711372
{
1372-
const iterator __pos = (iterator) __cpos;
1373-
const iterator __end = this->end();
1373+
const auto __pos = static_cast<size_type>(__cpos - this->cbegin());
13741374
if (this->size() == _Capacity)
13751375
{
13761376
_CUDA_VSTD::__throw_bad_alloc();
13771377
}
1378-
else if (__pos < this->begin() || __end < __pos)
1378+
else if (__pos > this->size())
13791379
{
13801380
_CUDA_VSTD::__throw_out_of_range("inplace_vector::emplace(const_iterator, Args...)");
13811381
}
13821382

1383-
if (__pos == __end)
1383+
const iterator __res = this->begin() + __pos;
1384+
if (__pos == this->size())
13841385
{
13851386
this->unchecked_emplace_back(_CUDA_VSTD::forward<_Args>(__args)...);
1386-
}
1387-
else
1388-
{
1389-
_Tp __temp{_CUDA_VSTD::forward<_Args>(__args)...};
1390-
this->unchecked_emplace_back(_CUDA_VSTD::move(*(__end - 1)));
1391-
_CUDA_VSTD::move_backward(__pos, __end - 1, __end);
1392-
*__pos = _CUDA_VSTD::move(__temp);
1387+
return __res;
13931388
}
13941389

1395-
return __pos;
1390+
const iterator __end = this->end();
1391+
_Tp __temp{_CUDA_VSTD::forward<_Args>(__args)...};
1392+
this->unchecked_emplace_back(_CUDA_VSTD::move(*(__end - 1)));
1393+
_CUDA_VSTD::move_backward(__res, __end - 1, __end);
1394+
*__res = _CUDA_VSTD::move(__temp);
1395+
1396+
return __res;
13961397
}
13971398

13981399
template <class... _Args>
@@ -1532,21 +1533,22 @@ public:
15321533
_LIBCUDACXX_INLINE_VISIBILITY constexpr iterator
15331534
erase(const_iterator __cpos) noexcept(_CCCL_TRAIT(is_nothrow_move_assignable, _Tp))
15341535
{
1535-
const iterator __pos = (iterator) __cpos;
1536-
const iterator __end = this->end();
1537-
if (__pos == __end)
1536+
const auto __pos = static_cast<size_type>(__cpos - this->cbegin());
1537+
if (__pos > this->size())
15381538
{
1539-
return __pos;
1539+
_CUDA_VSTD_NOVERSION::terminate();
15401540
}
15411541

1542-
if (this->size() == 0 || __pos < this->begin() || __end < __pos)
1542+
const iterator __res = this->begin() + __pos;
1543+
if (__pos == this->size())
15431544
{
1544-
_CUDA_VSTD_NOVERSION::terminate();
1545+
return __res;
15451546
}
15461547

1547-
_CUDA_VSTD::move(__pos + 1, __end, __pos);
1548+
const iterator __end = this->end();
1549+
_CUDA_VSTD::move(__res + 1, __end, __res);
15481550
this->__destroy(__end - 1, __end);
1549-
return __pos;
1551+
return __res;
15501552
}
15511553

15521554
_LIBCUDACXX_INLINE_VISIBILITY constexpr iterator

thrust/thrust/system/cuda/detail/scan.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ _CCCL_HOST_DEVICE OutputIt inclusive_scan_n_impl(
7171
cudaError_t status;
7272

7373
// Negative number of items are normalized to `0`
74-
if(thrust::detail::is_negative(num_items)){
74+
if (thrust::detail::is_negative(num_items))
75+
{
7576
num_items = 0;
7677
}
7778

@@ -126,7 +127,8 @@ _CCCL_HOST_DEVICE OutputIt exclusive_scan_n_impl(
126127
cudaError_t status;
127128

128129
// Negative number of items are normalized to `0`
129-
if(thrust::detail::is_negative(num_items)){
130+
if (thrust::detail::is_negative(num_items))
131+
{
130132
num_items = 0;
131133
}
132134

0 commit comments

Comments
 (0)