Skip to content

Commit

Permalink
tuple_index
Browse files Browse the repository at this point in the history
  • Loading branch information
deepgrace committed Apr 23, 2024
1 parent 19a1c2c commit 83689a8
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
## Overview
```cpp
#include <cstdint>
#include <monster.hpp>

using namespace monster;
Expand Down
30 changes: 25 additions & 5 deletions Tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,13 +646,28 @@ int main(int argc, char* argv[])
constexpr std::tuple<int, double, short> third(6, 7, 8);
constexpr auto t_ = tuple_cat_unique(first, second, third);

static_assert(std::get<0>(t_) == 1);
static_assert(std::get<1>(t_) == 2);
is_identical<std::get<0>(t_), 1>();
is_identical<std::get<1>(t_), 2>();

static_assert(std::get<2>(t_) == 3);
static_assert(std::get<3>(t_) == 5);
is_identical<std::get<2>(t_), 3>();
is_identical<std::get<3>(t_), 5>();

static_assert(std::get<4>(t_) == 7);
is_identical<std::get<4>(t_), 7>();

int i = 2022;
double j = 11.05;

std::tuple<short, char> t1;
std::tuple<int&, std::tuple<double&>, char> t2(i, std::tie(j), 'X');

is_identical<tuple_index<char>(t1), 1>();
is_identical<tuple_index<short>(t1), 0>();

is_identical<tuple_index<int>(t2), 3>();
is_identical<tuple_index<int&, 0>(t2), 0>();

is_identical<tuple_index<int, 1>(t2), 0>();
is_identical<tuple_index<int&, 1>(t2), 0>();

auto a_ = std::make_tuple(1, 2, 3);
auto b_ = std::make_tuple("one", "two", "three");
Expand Down Expand Up @@ -1098,6 +1113,11 @@ int main(int argc, char* argv[])
is_identical<value_index_v<6, std::integer_sequence<int, 1, -2, 0, 3, 6, 5>>, 4>();
is_identical<value_index_v<7, std::integer_sequence<int, 1, -2, 0, 3, 6, 5>>, 6>();

using namespace std::literals;

is_identical<search_index(1, 0, 1, 2, 3), 1>();
is_identical<search_index("key"sv, "seq"sv, "value"sv, "id"sv), 3>();

is_identical<find_v<char, std::tuple<float, char, double, int, char>>, 1>();
is_identical<find_v<c_6, std::integer_sequence<int, 3, -2, 6, 3, 6, 5>>, 2>();

Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dst=/tmp
path=example

flags=(-I include -m64 -std=c++23 -s -Wall -O3)
executables=(arity curry tensor object_pool is_lambda stream ycombinator sort_tuple memoized_invoke tuple_algorithm compiler_detector loop_unroll)
executables=(arity curry tensor object_pool is_lambda stream ycombinator sort_tuple unique_tuple memoized_invoke tuple_algorithm compiler_detector loop_unroll)

for bin in ${executables[@]}; do
g++ "${flags[@]}" -o ${dst}/${bin} ${path}/${bin}.cpp
Expand Down
4 changes: 3 additions & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ set(LOOP_UNROLL loop_unroll)
set(STREAM stream)
set(IS_LAMBDA is_lambda)
set(SORT_TUPLE sort_tuple)
set(UNIQUE_TUPLE unique_tuple)
set(YCOMBINATOR ycombinator)
set(MEMOIZED_INVOKE memoized_invoke)
set(TUPLE_ALGORITHM tuple_algorithm)
Expand All @@ -44,6 +45,7 @@ add_executable(${LOOP_UNROLL} loop_unroll.cpp)
add_executable(${STREAM} stream.cpp)
add_executable(${IS_LAMBDA} is_lambda.cpp)
add_executable(${SORT_TUPLE} sort_tuple.cpp)
add_executable(${UNIQUE_TUPLE} unique_tuple.cpp)
add_executable(${YCOMBINATOR} ycombinator.cpp)
add_executable(${MEMOIZED_INVOKE} memoized_invoke.cpp)
add_executable(${TUPLE_ALGORITHM} tuple_algorithm.cpp)
Expand All @@ -52,4 +54,4 @@ add_executable(${COMPILER_DETECTOR} compiler_detector.cpp)
target_link_libraries(${THREAD_POOL} pthread)

install(TARGETS ${ARITY} ${CURRY} ${TENSOR} ${MONSTER} ${OVERVIEW} ${OBJECT_POOL} ${THREAD_POOL} ${YCOMBINATOR} ${LOOP_UNROLL} DESTINATION ${PROJECT_SOURCE_DIR}/bin)
install(TARGETS ${STREAM} ${IS_LAMBDA} ${SORT_TUPLE} ${MEMOIZED_INVOKE} ${TUPLE_ALGORITHM} ${COMPILER_DETECTOR} DESTINATION ${PROJECT_SOURCE_DIR}/bin)
install(TARGETS ${STREAM} ${IS_LAMBDA} ${SORT_TUPLE} ${UNIQUE_TUPLE} ${MEMOIZED_INVOKE} ${TUPLE_ALGORITHM} ${COMPILER_DETECTOR} DESTINATION ${PROJECT_SOURCE_DIR}/bin)
30 changes: 25 additions & 5 deletions example/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,13 +644,28 @@ int main(int argc, char* argv[])
constexpr std::tuple<int, double, short> third(6, 7, 8);
constexpr auto t_ = tuple_cat_unique(first, second, third);

static_assert(std::get<0>(t_) == 1);
static_assert(std::get<1>(t_) == 2);
is_identical<std::get<0>(t_), 1>();
is_identical<std::get<1>(t_), 2>();

static_assert(std::get<2>(t_) == 3);
static_assert(std::get<3>(t_) == 5);
is_identical<std::get<2>(t_), 3>();
is_identical<std::get<3>(t_), 5>();

static_assert(std::get<4>(t_) == 7);
is_identical<std::get<4>(t_), 7>();

int i = 2022;
double j = 11.05;

std::tuple<short, char> t1;
std::tuple<int&, std::tuple<double&>, char> t2(i, std::tie(j), 'X');

is_identical<tuple_index<char>(t1), 1>();
is_identical<tuple_index<short>(t1), 0>();

is_identical<tuple_index<int>(t2), 3>();
is_identical<tuple_index<int&, 0>(t2), 0>();

is_identical<tuple_index<int, 1>(t2), 0>();
is_identical<tuple_index<int&, 1>(t2), 0>();

auto a_ = std::make_tuple(1, 2, 3);
auto b_ = std::make_tuple("one", "two", "three");
Expand Down Expand Up @@ -1096,6 +1111,11 @@ int main(int argc, char* argv[])
is_identical<value_index_v<6, std::integer_sequence<int, 1, -2, 0, 3, 6, 5>>, 4>();
is_identical<value_index_v<7, std::integer_sequence<int, 1, -2, 0, 3, 6, 5>>, 6>();

using namespace std::literals;

is_identical<search_index(1, 0, 1, 2, 3), 1>();
is_identical<search_index("key"sv, "seq"sv, "value"sv, "id"sv), 3>();

is_identical<find_v<char, std::tuple<float, char, double, int, char>>, 1>();
is_identical<find_v<c_6, std::integer_sequence<int, 3, -2, 6, 3, 6, 5>>, 2>();

Expand Down
1 change: 1 addition & 0 deletions example/overview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

// g++ -I include -m64 -std=c++23 -s -Wall -O3 -o /tmp/overview example/overview.cpp

#include <cstdint>
#include <monster.hpp>

using namespace monster;
Expand Down
41 changes: 37 additions & 4 deletions include/monster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1730,7 +1730,7 @@ namespace monster
template <template <typename, typename> typename F, auto N, typename T, typename U>
inline constexpr auto rcall_v = typev<rcall<F, N, T, U>>;

template <template <typename, typename> typename F, auto M, auto N, typename T, typename U = T>
template <template <typename ...> typename F, auto M, auto N, typename T, typename U = T>
inline constexpr auto unary_v = typev<unary<F, M, N, T, U>>;

template <template <typename ...> typename F, int N, int B, int E, bool>
Expand Down Expand Up @@ -2522,15 +2522,15 @@ namespace monster
template <template <typename, typename> typename F, typename T, auto B = 0, auto E = sizeof_t_v<T>>
using adjacent_difference_t = typeof_t<adjacent_difference<F, T, B, E>>;

template <template <typename, typename> typename comparator, auto i, auto j, typename T, typename U = T>
template <template <typename ...> typename comparator, auto i, auto j, typename T, typename U = T>
struct predicate : unary<comparator, i, j, T, U>
{
};

template <template <typename, typename> typename comparator, auto i, auto j, typename T, typename U = T>
template <template <typename ...> typename comparator, auto i, auto j, typename T, typename U = T>
using predicate_t = typeof_t<predicate<comparator, i, j, T, U>>;

template <template <typename, typename> typename comparator, auto i, auto j, typename T, typename U = T>
template <template <typename ...> typename comparator, auto i, auto j, typename T, typename U = T>
inline constexpr auto predicate_v = typev<predicate_t<comparator, i, j, T, U>>;

template <template <typename, typename> typename F, typename T, auto B = 0, auto E = sizeof_t_v<T>>
Expand Down Expand Up @@ -4708,6 +4708,39 @@ namespace monster
return T == U ? 1 : valueindex<T, Args...>() + 1;
}

template <bool strip, typename... Args>
constexpr size_t typeindex()
{
return index_of_apply<std::is_same, std::conditional_t<strip, std::remove_cvref_t<Args>, Args>...>() - 1;
}

template <typename T, bool strip = 0, typename... Args>
constexpr decltype(auto) tuple_index(std::tuple<Args...>& t) noexcept
{
return typeindex<strip, T, Args...>();
}

template <typename T, bool strip = 0, typename... Args>
constexpr decltype(auto) tuple_index(std::tuple<Args...>&& t) noexcept
{
return typeindex<strip, T, Args...>();
}

template <typename T, bool strip = 0, typename... Args>
constexpr decltype(auto) tuple_index(const std::tuple<Args...>& t) noexcept
{
return typeindex<strip, T, Args...>();
}

template <typename S, typename T, typename... Args>
constexpr size_t search_index(S&& s, T&& t, Args&&... args)
{
if constexpr(sizeof...(Args) == 0)
return s != t;
else
return s == t ? 0 : search_index(std::forward<S>(s), std::forward<Args>(args)...) + 1;
}

template <typename T, typename U>
struct type_index;

Expand Down
2 changes: 1 addition & 1 deletion include/version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* time a set of code changes is merged to the master branch.
*/

#define MONSTER_VERSION_NUMBER 310
#define MONSTER_VERSION_NUMBER 311
#define MONSTER_VERSION_STRING "Monster/" MONSTER_STRINGIZE(MONSTER_VERSION_NUMBER)

#endif

0 comments on commit 83689a8

Please sign in to comment.