Skip to content

Commit 135db57

Browse files
committed
algorithms/containers: annotate sort, stable_sort, and partitioned_vector with preconditions
Add HPX_PRE(first <= last) to hpx::sort and hpx::stable_sort (both sequential and parallel overloads). Both algorithms already enforce std::random_access_iterator via static_assert, so <= is always valid. Add HPX_PRE(pos < size()) to partitioned_vector::operator[] (const and non-const). Mirrors the existing small_vector annotation. Signed-off-by: Sanchit2662 <sanchit2662@gmail.com>
1 parent 3fc42f0 commit 135db57

4 files changed

Lines changed: 10 additions & 3 deletions

File tree

components/containers/partitioned_vector/include/hpx/components/containers/partitioned_vector/partitioned_vector_decl.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <hpx/config.hpp>
1313
#include <hpx/assert.hpp>
14+
#include <hpx/contracts.hpp>
1415
#include <hpx/components/client_base.hpp>
1516
#include <hpx/distribution_policies/container_distribution_policy.hpp>
1617
#include <hpx/distribution_policies/explicit_container_distribution_policy.hpp>
@@ -506,6 +507,7 @@ namespace hpx {
506507
/// this proxy instance is used.
507508
///
508509
segmented::detail::vector_value_proxy<T, Data> operator[](size_type pos)
510+
HPX_PRE(pos < size())
509511
{
510512
return segmented::detail::vector_value_proxy<T, Data>(*this, pos);
511513
}
@@ -521,7 +523,7 @@ namespace hpx {
521523
/// \note This function does not return a reference to the actual
522524
/// element but a copy of its value.
523525
///
524-
T operator[](size_type pos) const
526+
T operator[](size_type pos) const HPX_PRE(pos < size())
525527
{
526528
return get_value(launch::sync, pos);
527529
}

libs/core/algorithms/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ add_hpx_module(
279279
hpx_concepts
280280
hpx_concurrency
281281
hpx_config
282+
hpx_contracts
282283
hpx_coroutines
283284
hpx_datastructures
284285
hpx_errors

libs/core/algorithms/include/hpx/parallel/algorithms/sort.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ namespace hpx {
152152
#include <hpx/config.hpp>
153153
#include <hpx/algorithms/traits/projected.hpp>
154154
#include <hpx/assert.hpp>
155+
#include <hpx/contracts.hpp>
155156
#include <hpx/modules/async_local.hpp>
156157
#include <hpx/modules/concepts.hpp>
157158
#include <hpx/modules/execution.hpp>
@@ -394,6 +395,7 @@ namespace hpx {
394395
// clang-format on
395396
friend void tag_fallback_invoke(
396397
hpx::sort_t, RandomIt first, RandomIt last, Comp comp = Comp())
398+
HPX_PRE(first <= last)
397399
{
398400
static_assert(std::random_access_iterator<RandomIt>,
399401
"Requires a random access iterator.");
@@ -416,7 +418,7 @@ namespace hpx {
416418
// clang-format on
417419
friend parallel::util::detail::algorithm_result_t<ExPolicy>
418420
tag_fallback_invoke(hpx::sort_t, ExPolicy&& policy, RandomIt first,
419-
RandomIt last, Comp comp = Comp())
421+
RandomIt last, Comp comp = Comp()) HPX_PRE(first <= last)
420422
{
421423
static_assert(std::random_access_iterator<RandomIt>,
422424
"Requires a random access iterator.");

libs/core/algorithms/include/hpx/parallel/algorithms/stable_sort.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ namespace hpx {
150150

151151
#include <hpx/config.hpp>
152152
#include <hpx/algorithms/traits/projected.hpp>
153+
#include <hpx/contracts.hpp>
153154
#include <hpx/modules/concepts.hpp>
154155
#include <hpx/modules/execution.hpp>
155156
#include <hpx/modules/executors.hpp>
@@ -285,7 +286,7 @@ namespace hpx {
285286
)
286287
// clang-format on
287288
friend void tag_fallback_invoke(hpx::stable_sort_t, RandomIt first,
288-
RandomIt last, Comp comp = Comp())
289+
RandomIt last, Comp comp = Comp()) HPX_PRE(first <= last)
289290
{
290291
static_assert(std::random_access_iterator<RandomIt>,
291292
"Requires a random access iterator.");
@@ -310,6 +311,7 @@ namespace hpx {
310311
friend hpx::parallel::util::detail::algorithm_result_t<ExPolicy>
311312
tag_fallback_invoke(hpx::stable_sort_t, ExPolicy&& policy,
312313
RandomIt first, RandomIt last, Comp comp = Comp())
314+
HPX_PRE(first <= last)
313315
{
314316
static_assert(std::random_access_iterator<RandomIt>,
315317
"Requires a random access iterator.");

0 commit comments

Comments
 (0)