-
-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move is_contiguous_iterator to type_support, support array iterators
- Loading branch information
1 parent
8531572
commit a43f622
Showing
8 changed files
with
139 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
libs/core/type_support/include/hpx/type_support/is_contiguous_iterator.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) 2007-2022 Hartmut Kaiser | ||
// | ||
// SPDX-License-Identifier: BSL-1.0 | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#pragma once | ||
|
||
#include <array> | ||
#include <iterator> | ||
#include <string> | ||
#include <type_traits> | ||
#include <utility> | ||
#include <vector> | ||
|
||
namespace hpx::traits { | ||
/////////////////////////////////////////////////////////////////////////// | ||
// Iterators are contiguous if they are pointers (without concepts we have | ||
// no generic way of determining whether an iterator is contiguous) | ||
|
||
namespace detail { | ||
|
||
template <typename Iter> | ||
using iter_value_type_t = | ||
typename std::iterator_traits<Iter>::value_type; | ||
|
||
template <typename T, typename Enable = void> | ||
struct is_known_contiguous_iterator : std::false_type | ||
{ | ||
}; | ||
|
||
template <typename Iter> | ||
struct is_known_contiguous_iterator<Iter, | ||
std::enable_if_t<!std::is_array_v<Iter>>> | ||
: std::bool_constant< | ||
std::is_same_v< // for std::vector | ||
typename std::vector<iter_value_type_t<Iter>>::iterator, | ||
Iter> || | ||
std::is_same_v<typename std::vector< | ||
iter_value_type_t<Iter>>::const_iterator, | ||
Iter> || // for std::array | ||
std::is_same_v< | ||
typename std::array<iter_value_type_t<Iter>, 1>::iterator, | ||
Iter> || | ||
std::is_same_v<typename std::array<iter_value_type_t<Iter>, | ||
1>::const_iterator, | ||
Iter> || // for std::string | ||
std::is_same_v<typename std::string::iterator, Iter> || | ||
std::is_same_v<typename std::string::const_iterator, Iter>> | ||
{ | ||
}; | ||
} // namespace detail | ||
|
||
template <typename Iter, | ||
bool not_known_contiguous_iterator = | ||
// When _GLIBCXX_DEBUG is defined vectors are contiguous, but the iterators | ||
// are not plain pointers. | ||
#if defined(_GLIBCXX_DEBUG) | ||
false | ||
#else | ||
detail::is_known_contiguous_iterator<Iter>::value | ||
#endif | ||
> | ||
struct is_contiguous_iterator : std::is_pointer<Iter>::type | ||
{ | ||
}; | ||
|
||
template <typename Iter> | ||
struct is_contiguous_iterator<Iter, true> : std::true_type | ||
{ | ||
}; | ||
|
||
template <typename Iter> | ||
using is_contiguous_iterator_t = | ||
typename is_contiguous_iterator<Iter>::type; | ||
|
||
template <typename Iter> | ||
inline constexpr bool is_contiguous_iterator_v = | ||
is_contiguous_iterator<Iter>::value; | ||
} // namespace hpx::traits |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
libs/core/type_support/tests/unit/is_contiguous_iterator.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2023 Isidoros Tsaousis-Seiras | ||
// | ||
// SPDX-License-Identifier: BSL-1.0 | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#include <array> | ||
#include <cassert> | ||
#include <vector> | ||
|
||
#include <hpx/type_support/is_contiguous_iterator.hpp> | ||
|
||
using hpx::traits::is_contiguous_iterator_v; | ||
|
||
// std::vector<int>::iterator is a contiguous iterator | ||
static_assert(is_contiguous_iterator_v<std::vector<int>::iterator>); | ||
static_assert(is_contiguous_iterator_v<std::vector<int>::const_iterator>); | ||
// reverse_iterator is not a contiguous iterator | ||
static_assert(!is_contiguous_iterator_v<std::vector<int>::reverse_iterator>); | ||
static_assert( | ||
!is_contiguous_iterator_v<std::vector<int>::const_reverse_iterator>); | ||
|
||
// std::array<int, 4>::iterator is a contiguous iterator | ||
static_assert(is_contiguous_iterator_v<std::array<int, 4>::iterator>); | ||
static_assert(is_contiguous_iterator_v<std::array<int, 4>::const_iterator>); | ||
// reverse_iterator is not a contiguous iterator | ||
static_assert(!is_contiguous_iterator_v<std::array<int, 4>::reverse_iterator>); | ||
static_assert( | ||
!is_contiguous_iterator_v<std::array<int, 4>::const_reverse_iterator>); | ||
|
||
// pointers are contiguous iterators | ||
static_assert(is_contiguous_iterator_v<int*>); | ||
static_assert(is_contiguous_iterator_v<int const*>); | ||
static_assert(is_contiguous_iterator_v<int (*)[]>); | ||
static_assert(is_contiguous_iterator_v<int const (*)[]>); | ||
static_assert(is_contiguous_iterator_v<int (*)[4]>); | ||
static_assert(is_contiguous_iterator_v<int const (*)[4]>); | ||
|
||
// arrays are not contiguous iterators | ||
static_assert(!is_contiguous_iterator_v<int[]>); | ||
static_assert(!is_contiguous_iterator_v<int[4]>); | ||
static_assert(!is_contiguous_iterator_v<int const[]>); | ||
static_assert(!is_contiguous_iterator_v<int const[4]>); | ||
|
||
// std::string::iterator is a contiguous iterator | ||
static_assert(is_contiguous_iterator_v<std::string::iterator>); | ||
static_assert(is_contiguous_iterator_v<std::string::const_iterator>); | ||
|
||
int main(int, char*[]) {} |