Speclib  0.1.2
The library for writing better CUDA libraries
sp::TypeList< Ts > Class Template Reference

A compile-time list of types, providing all the usual list primitives. More...

#include <TypeList.hpp>

Public Types

using Tuple = std::tuple< Ts... >
 
template<int I>
using get = __type_pack_element< I, Ts... >
 The type at index I in this list. More...
 
template<typename... Qs>
using prepend = TypeList< Qs..., Ts... >
 A TypeList with Qs... added to the start. More...
 
template<typename... Qs>
using append = TypeList< Ts..., Qs... >
 A TypeList with Qs... added to the end. More...
 
template<int... Is>
using pick = TypeList< __type_pack_element< Is, Ts... >... >
 A TypeList comprised of the elements at the listed indices in this one. More...
 
template<int Start, int End = Size>
using slice = decltype(sliceHelper< Start >(sp::make_int_sequence< End - Start >{}))
 A TypeList comprised of elements in the range [Start, End). More...
 
template<int Ind>
using remove = decltype(removalHelper< Ind >(sp::make_int_sequence< Ind >{}, sp::make_int_sequence< Size - Ind - 1 >{}))
 Remove an element from the list. More...
 
template<int Ind, typename T >
using set = decltype(setHelper< Ind, T >(sp::make_int_sequence< Ind >{}, sp::make_int_sequence< Size - Ind - 1 >{}))
 Set a the list element at index Ind to T. More...
 
template<typename O >
using cat = decltype(catHelper(O{}))
 A TypeList formed by concatenating this one with the given one (O) More...
 
template<int J, typename I >
using insert = typename slice< 0, J >::template append< I >::template cat< slice< J > >
 Insert an element at index J. More...
 
template<template< typename > typename Predicate>
using FindMatchingIndices = decltype(findMatchingIndHelper< Predicate >())
 Find the index of all matching elements. More...
 
template<template< typename > typename Predicate>
using filter = decltype(filterHelper< Predicate, 0 >())
 The types from this list that meet the given predicate. More...
 
template<template< typename > typename Lambda>
using map = TypeList< typename Lambda< Ts >::type... >
 If Lambda is a template struct that exposes one type member called type, this yields a new TypeList with the result of applying Lambda to each element in this list. More...
 
template<template< typename, int > typename Lambda>
using mapi = decltype(mapiImpl< Lambda >(sp::make_int_sequence< Size >{}))
 Indexed functional map: the predicate receives both the element and its index. More...
 
template<typename OtherList >
using merge = decltype(mergeHelper< OtherList >())
 
template<typename OtherList >
using uniqueMerge = decltype(uniqueMergeHelper< OtherList >())
 
template<typename Defer = Deferred>
using sortedUnique = decltype(Defer::template sortedUniqueHelper())
 Get unique elements of a sorted list in linear time. More...
 
template<typename Defer = Deferred>
using sort = decltype(Defer::template sortHelper())
 Get the sorted version of this list using insertion sort. Caution: O(n²). More...
 

Static Public Member Functions

template<typename Needle , int Start = 0, int End = Size>
constexpr static int binarySearch ()
 Find the index of the first value that is > Needle. More...
 

Static Public Attributes

constexpr static int Size = sizeof...(Ts)
 The size of this list. More...
 
constexpr static bool Empty = Size == 0
 
template<template< typename > typename Predicate>
static constexpr bool All = (Predicate<Ts>::value && ...)
 True iff Predicate<T> is true for all T in this list. More...
 
template<template< typename > typename Predicate>
static constexpr bool Any = (Predicate<Ts>::value || ...)
 True iff Predicate<T> is true for any T in this list. More...
 
template<template< typename > typename Predicate>
static constexpr int CountMatching = (Predicate<Ts>::value + ... + 0)
 Count how many elements of the list match the predicate. More...
 
template<template< typename > typename Predicate, int Stride = 1, int From = 0>
constexpr static int find = findHelper<Predicate, Stride, From>()
 
template<typename T , int From = 0>
constexpr static int indexOf = findHelper<sp::tm::TypeEqual<T>::template type, 1, From>()
 
template<typename T , int From = Size - 1>
constexpr static int reverseIndexOf = findHelper<sp::tm::TypeEqual<T>::template type, -1, From>()
 

Detailed Description

template<typename... Ts>
class sp::TypeList< Ts >

A compile-time list of types, providing all the usual list primitives.

A system for manipulating lists of types. Has all the features you'd expect from a convenient list implementation, including functional-programming-esque schemes.

Code that manipulates template type parameter packs can often be simplified using TypeList.

Example

template<typename... Ts>
void example() {
using TL = sp::TypeList<Ts...>;
// Filter the list of types to find which are tensors (of some
// sort or another...)
using Tensors = TL::template filter<sp::is_tensor>;
// Get the 3rd Tensor
using ThirdTensor = Tensors::template get<2>;
ThirdTensor someInstance;
}
A compile-time list of types, providing all the usual list primitives.
Definition: TypeList.hpp:40

Member Typedef Documentation

◆ append

template<typename... Ts>
template<typename... Qs>
using sp::TypeList< Ts >::append = TypeList<Ts..., Qs...>

A TypeList with Qs... added to the end.

◆ cat

template<typename... Ts>
template<typename O >
using sp::TypeList< Ts >::cat = decltype(catHelper(O{}))

A TypeList formed by concatenating this one with the given one (O)

◆ filter

template<typename... Ts>
template<template< typename > typename Predicate>
using sp::TypeList< Ts >::filter = decltype(filterHelper<Predicate, 0>())

The types from this list that meet the given predicate.

It's often useful to do things like:

// Usually this will come from somewhere else
// This is `sp::TypeList<sp::Tensor<float, 2>>`
using Scalars = SomeTypes::filter<sp::is_tensor>;

◆ FindMatchingIndices

template<typename... Ts>
template<template< typename > typename Predicate>
using sp::TypeList< Ts >::FindMatchingIndices = decltype(findMatchingIndHelper<Predicate>())

Find the index of all matching elements.

◆ get

template<typename... Ts>
template<int I>
using sp::TypeList< Ts >::get = __type_pack_element<I, Ts...>

The type at index I in this list.

◆ insert

template<typename... Ts>
template<int J, typename I >
using sp::TypeList< Ts >::insert = typename slice<0, J>::template append<I>::template cat<slice<J> >

Insert an element at index J.

◆ map

template<typename... Ts>
template<template< typename > typename Lambda>
using sp::TypeList< Ts >::map = TypeList<typename Lambda<Ts>::type...>

If Lambda is a template struct that exposes one type member called type, this yields a new TypeList with the result of applying Lambda to each element in this list.

◆ mapi

template<typename... Ts>
template<template< typename, int > typename Lambda>
using sp::TypeList< Ts >::mapi = decltype(mapiImpl<Lambda>(sp::make_int_sequence<Size>{}))

Indexed functional map: the predicate receives both the element and its index.

◆ pick

template<typename... Ts>
template<int... Is>
using sp::TypeList< Ts >::pick = TypeList<__type_pack_element<Is, Ts...>...>

A TypeList comprised of the elements at the listed indices in this one.

◆ prepend

template<typename... Ts>
template<typename... Qs>
using sp::TypeList< Ts >::prepend = TypeList<Qs..., Ts...>

A TypeList with Qs... added to the start.

◆ remove

template<typename... Ts>
template<int Ind>
using sp::TypeList< Ts >::remove = decltype(removalHelper<Ind>(sp::make_int_sequence<Ind>{}, sp::make_int_sequence<Size - Ind - 1>{}))

Remove an element from the list.

◆ set

template<typename... Ts>
template<int Ind, typename T >
using sp::TypeList< Ts >::set = decltype(setHelper<Ind, T>(sp::make_int_sequence<Ind>{}, sp::make_int_sequence<Size - Ind - 1>{}))

Set a the list element at index Ind to T.

◆ slice

template<typename... Ts>
template<int Start, int End = Size>
using sp::TypeList< Ts >::slice = decltype(sliceHelper<Start>(sp::make_int_sequence<End - Start>{}))

A TypeList comprised of elements in the range [Start, End).

◆ sort

template<typename... Ts>
template<typename Defer = Deferred>
using sp::TypeList< Ts >::sort = decltype(Defer::template sortHelper())

Get the sorted version of this list using insertion sort. Caution: O(n²).

◆ sortedUnique

template<typename... Ts>
template<typename Defer = Deferred>
using sp::TypeList< Ts >::sortedUnique = decltype(Defer::template sortedUniqueHelper())

Get unique elements of a sorted list in linear time.

Member Function Documentation

◆ binarySearch()

template<typename... Ts>
template<typename Needle , int Start = 0, int End = Size>
constexpr static int sp::TypeList< Ts >::binarySearch ( )
staticconstexpr

Find the index of the first value that is > Needle.

This assumes that the stored types all have an operator > that works on default-constructed objects of the type (that's how we define ordering on TypeList elements). If no such operator is available, this function can't be used.

Member Data Documentation

◆ All

template<typename... Ts>
template<template< typename > typename Predicate>
constexpr bool sp::TypeList< Ts >::All = (Predicate<Ts>::value && ...)
staticconstexpr

True iff Predicate<T> is true for all T in this list.

◆ Any

template<typename... Ts>
template<template< typename > typename Predicate>
constexpr bool sp::TypeList< Ts >::Any = (Predicate<Ts>::value || ...)
staticconstexpr

True iff Predicate<T> is true for any T in this list.

◆ CountMatching

template<typename... Ts>
template<template< typename > typename Predicate>
constexpr int sp::TypeList< Ts >::CountMatching = (Predicate<Ts>::value + ... + 0)
staticconstexpr

Count how many elements of the list match the predicate.

◆ Size

template<typename... Ts>
constexpr static int sp::TypeList< Ts >::Size = sizeof...(Ts)
staticconstexpr

The size of this list.