Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport of some concepts from util to C++17 #1741

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark/BenchmarkExamples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ConfigOptions : public BenchmarkInterface {
auto numSigns =
manager.addOption("num-signs", "The number of street signs.",
&numberOfStreetSigns_, 10000);
manager.addValidator([](const int& num) { return num >= 0; },
manager.addValidator([](const int& num) -> bool { return num >= 0; },
"The number of street signs must be at least 0!",
"Negative numbers, or floating point numbers, are not "
"allowed for the configuration option \"num-signs\".",
Expand Down
4 changes: 2 additions & 2 deletions benchmark/infrastructure/BenchmarkMeasurementContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ class ResultTable : public BenchmarkMetadataGetter {
@param row, column Which table entry to read. Starts with `(0,0)`.
*/
template <ad_utility::SameAsAnyTypeIn<EntryType> T>
T getEntry(const size_t row, const size_t column) const {
CPP_template(typename T)(requires ad_utility::SameAsAnyTypeIn<T, EntryType>) T
getEntry(const size_t row, const size_t column) const {
AD_CONTRACT_CHECK(row < numRows() && column < numColumns());
static_assert(!ad_utility::isSimilar<T, std::monostate>);

Expand Down
12 changes: 6 additions & 6 deletions benchmark/infrastructure/BenchmarkToJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ provided translation function for the vector entries.
@tparam TranslationFunction Has to be a function, that takes `VectorType`
and returns a `nlohmann:json` object.
*/
template <typename VectorType, ad_utility::InvocableWithExactReturnType<
nlohmann::ordered_json, VectorType>
TranslationFunction>
static nlohmann::json transformIntoJsonArray(
const std::vector<VectorType>& vec,
TranslationFunction translationFunction) {
CPP_template(typename VectorType, typename TranslationFunction)(
requires ad_utility::InvocableWithExactReturnType<
TranslationFunction, nlohmann::ordered_json,
VectorType>) static nlohmann::json
transformIntoJsonArray(const std::vector<VectorType>& vec,
TranslationFunction translationFunction) {
/*
Without explicit instantiation, `nlohmann::nlohmann::ordered_json` is not
guaranteed, to always interpret a `push_back` correctly. For instance,
Expand Down
2 changes: 2 additions & 0 deletions src/backports/concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
#ifdef QLEVER_CPP_17
#define QL_CONCEPT_OR_NOTHING(...)
#define QL_CONCEPT_OR_TYPENAME(...) typename
#define CPP_and_def CPP_and_sfinae_def
#else
#define QL_CONCEPT_OR_NOTHING(...) __VA_ARGS__
#define QL_CONCEPT_OR_TYPENAME(...) __VA_ARGS__
#define CPP_and_def CPP_and
#endif

// The namespace `ql::concepts` includes concepts that are contained in the
Expand Down
6 changes: 4 additions & 2 deletions src/engine/idTable/IdTableRow.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,10 @@ class RowReference
// The `cbegin` and `cend` functions are implicitly inherited from `Base`.

// __________________________________________________________________________
template <ad_utility::SimilarTo<RowReference> R>
friend void swap(R&& a, R&& b) requires(!isConst) {
CPP_template(typename R)(
requires ad_utility::SimilarTo<RowReference, R>) friend void swap(R&& a,
R&& b)
requires(!isConst) {
return Base::swapImpl(AD_FWD(a), AD_FWD(b));
}

Expand Down
28 changes: 24 additions & 4 deletions src/global/Pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <vector>

#include "global/Id.h"
#include "backports/concepts.h"
#include "util/ExceptionHandling.h"
#include "util/File.h"
#include "util/Generator.h"
Expand Down Expand Up @@ -71,6 +72,27 @@ struct CompactStringVectorWriter;

}

#ifdef QLEVER_CPP_17
template <typename T, typename = void>
struct IsIteratorOfIterator : std::false_type {};

template <typename T>
struct IsIteratorOfIterator<
T, std::void_t<decltype(*(T::iterator::value_type::begin()))>>
: std::true_type {};

template <typename T, typename DataType>
CPP_concept IteratorOfIterator =
(IsIteratorOfIterator<T>::value &&
ad_utility::SimilarTo<decltype(*(T::iterator::value_type::begin())),
DataType>);
#else
template <typename T, typename DataType>
concept IteratorOfIterator = requires(T t) {
{ *(t.begin()->begin()) } -> ad_utility::SimilarTo<DataType>;
};
#endif

/**
* @brief Stores a list of variable length data of a single type (e.g.
* c-style strings). The data is stored in a single contiguous block
Expand Down Expand Up @@ -102,10 +124,8 @@ class CompactVectorOfStrings {
* @brief Fills this CompactVectorOfStrings with input.
* @param The input from which to build the vector.
*/
template <typename T>
requires requires(T t) {
{ *(t.begin()->begin()) } -> ad_utility::SimilarTo<data_type>;
} void build(const T& input) {
CPP_template(typename T)(
requires IteratorOfIterator<T, data_type>) void build(const T& input) {
// Also make room for the end offset of the last element.
_offsets.reserve(input.size() + 1);
size_t dataSize = 0;
Expand Down
15 changes: 8 additions & 7 deletions src/index/CompressedRelation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,13 +752,14 @@ size_t CompressedRelationReader::getResultSizeOfScan(
}

// ____________________________________________________________________________
IdTable CompressedRelationReader::getDistinctColIdsAndCountsImpl(
ad_utility::InvocableWithConvertibleReturnType<
Id, const CompressedBlockMetadata::PermutedTriple&> auto idGetter,
const ScanSpecification& scanSpec,
const std::vector<CompressedBlockMetadata>& allBlocksMetadata,
const CancellationHandle& cancellationHandle,
const LocatedTriplesPerBlock& locatedTriplesPerBlock) const {
CPP_template_def(typename IdGetter)(
requires ad_utility::InvocableWithConvertibleReturnType<
IdGetter, Id, const CompressedBlockMetadata::PermutedTriple&>) IdTable
CompressedRelationReader::getDistinctColIdsAndCountsImpl(
IdGetter idGetter, const ScanSpecification& scanSpec,
const std::vector<CompressedBlockMetadata>& allBlocksMetadata,
const CancellationHandle& cancellationHandle,
const LocatedTriplesPerBlock& locatedTriplesPerBlock) const {
// The result has two columns: one for the distinct `Id`s and one for their
// counts.
IdTableStatic<2> table(allocator_);
Expand Down
15 changes: 8 additions & 7 deletions src/index/CompressedRelation.h
Original file line number Diff line number Diff line change
Expand Up @@ -746,13 +746,14 @@ class CompressedRelationReader {

// The common implementation for `getDistinctCol0IdsAndCounts` and
// `getCol1IdsAndCounts`.
IdTable getDistinctColIdsAndCountsImpl(
ad_utility::InvocableWithConvertibleReturnType<
Id, const CompressedBlockMetadata::PermutedTriple&> auto idGetter,
const ScanSpecification& scanSpec,
const std::vector<CompressedBlockMetadata>& allBlocksMetadata,
const CancellationHandle& cancellationHandle,
const LocatedTriplesPerBlock& locatedTriplesPerBlock) const;
CPP_template(typename IdGetter)(
requires ad_utility::InvocableWithConvertibleReturnType<
IdGetter, Id, const CompressedBlockMetadata::PermutedTriple&>) IdTable
getDistinctColIdsAndCountsImpl(
IdGetter idGetter, const ScanSpecification& scanSpec,
const std::vector<CompressedBlockMetadata>& allBlocksMetadata,
const CancellationHandle& cancellationHandle,
const LocatedTriplesPerBlock& locatedTriplesPerBlock) const;
};

// TODO<joka921>
Expand Down
4 changes: 2 additions & 2 deletions src/index/StringSortComparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class LocaleManager {
using U8String = std::basic_string<uint8_t>;
using U8StringView = std::basic_string_view<uint8_t>;

template <ad_utility::SimilarToAny<U8String, U8StringView> T>
class SortKeyImpl {
CPP_template(typename T)(requires ad_utility::SimilarToAny<
T, U8String, U8StringView>) class SortKeyImpl {
public:
SortKeyImpl() = default;
explicit SortKeyImpl(U8StringView sortKey) : sortKey_(sortKey) {}
Expand Down
6 changes: 4 additions & 2 deletions src/util/Algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ bool contains_if(const Container& container, const Predicate& predicate) {
* @param destination Vector& to which to append
* @param source Vector&& to append
*/
template <typename T, ad_utility::SimilarTo<std::vector<T>> U>
void appendVector(std::vector<T>& destination, U&& source) {
CPP_template(typename T, typename U)(
requires ad_utility::SimilarTo<
std::vector<T>, U>) void appendVector(std::vector<T>& destination,
U&& source) {
destination.insert(destination.end(),
ad_utility::makeForwardingIterator<U>(source.begin()),
ad_utility::makeForwardingIterator<U>(source.end()));
Expand Down
25 changes: 15 additions & 10 deletions src/util/CancellationHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,14 @@ class CancellationHandle {
/// in the console that would otherwise be triggered by the watchdog.
/// NOTE: The parameter state is expected to be one of `CHECK_WINDOW_MISSED`
/// or `WAITING_FOR_CHECK`, otherwise it will violate the correctness check.
void pleaseWatchDog(CancellationState state,
ad_utility::source_location location,
const ad_utility::InvocableWithConvertibleReturnType<
std::string_view> auto& stageInvocable)
CPP_template(typename StateFunc)(
requires ad_utility::InvocableWithConvertibleReturnType<
StateFunc,
std::string_view>) void pleaseWatchDog(CancellationState state,
ad_utility::source_location
location,
const StateFunc&
stageInvocable)
requires WatchDogEnabled {
using DurationType =
std::remove_const_t<decltype(DESIRED_CANCELLATION_CHECK_INTERVAL)>;
Expand Down Expand Up @@ -215,12 +219,13 @@ class CancellationHandle {
/// nothing otherwise. If `WatchDogEnabled` is true, this will log a warning
/// if this check is not called frequently enough. It will contain the
/// filename and line of the caller of this method.
template <ad_utility::InvocableWithConvertibleReturnType<std::string_view>
Func = decltype(detail::printNothing)>
AD_ALWAYS_INLINE void throwIfCancelled(
[[maybe_unused]] ad_utility::source_location location =
ad_utility::source_location::current(),
const Func& stageInvocable = detail::printNothing) {
CPP_template(typename Func = decltype(detail::printNothing))(
requires ad_utility::InvocableWithConvertibleReturnType<Func,
std::string_view>)
AD_ALWAYS_INLINE void throwIfCancelled(
[[maybe_unused]] ad_utility::source_location location =
ad_utility::source_location::current(),
const Func& stageInvocable = detail::printNothing) {
if constexpr (CancellationEnabled) {
auto state = cancellationState_.load(std::memory_order_relaxed);
if (state == CancellationState::NOT_CANCELLED) [[likely]] {
Expand Down
Loading
Loading