Skip to content
Open
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
35 changes: 35 additions & 0 deletions examples/named_function_arguments.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "ntuples/full.hh"




template <typename ...Args>
auto variadic_template(Args&& ...args) -> void{
auto t0 = bind_args(
@argument1 = 1,
@argument2 = 15,
@argument3 = nt::required<int>(),
@argument4 = std::string("hello"))(
CPP2_FORWARD(args)...);

std::cout << t0 << std::endl;
}

auto make_ntuple(){
return nt::ntuple{
@a = 1,
@b = 2.5,
@c = std::string("three"),
@d = '4'
};
}

main: () = {
variadic_template( 1, 2.5, @argument4 = std::string("five") , @argument3 = 12 );


nt1 := make_ntuple();

std::cout << nt1 << std::endl;
}

121 changes: 121 additions & 0 deletions include/ntuples/bind_args.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#pragma once
#include "ntuples/ntuples.hh"
#include <optional>
namespace nt
{
namespace __imple__
{

template <typename T>
struct is_ax_name_container
{
private:
template <typename U>
static std::true_type test(const nt::field_name_container<U> *);

static std::false_type test(...);

public:
static constexpr bool value = decltype(test(std::declval<std::remove_cvref_t<T> *>()))::value;
};

template <typename T>
constexpr bool is_ax_name_container_v = is_ax_name_container<T>::value;



template <size_t N, typename NT_T, typename T1, typename... ARGS>
auto bind_args1(NT_T &&t, T1 &&t1, ARGS &&...args)
{
if constexpr (::nt::__imple__::is_ax_name_container_v<T1>)
{
using T1Bare = std::remove_cvref_t<T1>;
if constexpr (T1Bare::template index_of<NT_T>() < N)
{
static_assert(dependent_false<NT_T>::value, "[NTUPLE ERROR] Named argument appears after positional arguments. "
"All named arguments must follow positional ones.");
}
else
{

t[t1].v = t1.v;

}
}

if constexpr (sizeof...(args) == 0)
{
return t;
}
else
{
return ::nt::__imple__::bind_args1<N>(t, std::forward<ARGS>(args)...);
}
}


template <size_t N, typename NT_T, typename T1, typename... ARGS>
auto bind_args0(NT_T &&t, T1 &&t1, ARGS &&...args)
{
if constexpr (is_ax_name_container_v<T1>)
{
return ::nt::__imple__::bind_args1<N>(t, std::forward<T1>(t1), std::forward<ARGS>(args)...);
}
else
{
nt::get_nth<N>(t).v = t1;
if constexpr (sizeof...(args) == 0)
{
return t;
}
else {
return ::nt::__imple__::bind_args0<N + 1>(t, std::forward<ARGS>(args)...);
}
}
}

template<typename T>
void check_optional_has_value(const T&) {
// Do nothing if T is not a specialization of std::optional
}

template<typename T>
void check_optional_has_value(const std::optional<T>& opt) {
if (!opt.has_value()) {
throw std::runtime_error("std::optional is empty");
}
}

}
template <typename... NamedArgs>
auto bind_args(NamedArgs &&...named_args)
{
// Return a lambda that captures named_args
return [&](auto &&...args)
{
auto tup = nt::ntuple(std::forward<NamedArgs>(named_args)...);
if constexpr (sizeof...(args) == 0)
{
constexpr_for<0, tup.__size__ , 1>([&](auto i){
::nt::__imple__::check_optional_has_value(get_nth<i>(tup).v);
});

return tup;
}
else
{

auto tup1 = ::nt::__imple__::bind_args0<0>(std::move(tup), std::forward<decltype(args)>(args)...);

constexpr_for<0, tup1.__size__ , 1>([&](auto i){
::nt::__imple__::check_optional_has_value(get_nth<i>(tup).v);
});
return tup1;
}
};
}
template <typename T>
auto required(){
return std::optional<T>();
}
}
35 changes: 35 additions & 0 deletions include/ntuples/comparators.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once
#include "ntuples/ntuples.hh"


namespace nt::comparators
{

struct on_common_args_t
{

template <typename T1, typename T2>
constexpr static bool __comp__(T1 &&t1, T2 &&t2)
{
bool ret = true;
constexpr_for<0, _Remove_cvref_t<T1>::__size__, 1>(
[&](const auto i)
{
using N_th_T = _Remove_cvref_t<decltype(nt::get_nth<i>(t1))>;
if constexpr (contains_type_v<N_th_T, _Remove_cvref_t<T2>>)
{
ret &= (N_th_T::get(t1) == N_th_T::get(t2));
}
});
return ret;
}

template <typename T1, typename T2>
constexpr bool operator()(T1 &&t1, T2 &&t2) const
{
return __comp__(std::forward<T1>(t1), std::forward<T2>(t2));
}
};
constexpr inline on_common_args_t on_common_args;
}

15 changes: 15 additions & 0 deletions include/ntuples/constexpr_for.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

namespace nt{

template <auto Start, auto End, auto Inc, class F>
constexpr void constexpr_for(F &&f)
{
if constexpr (Start < End)
{
f(std::integral_constant<decltype(Start), Start>());
constexpr_for<Start + Inc, End, Inc>(f);
}
}

}
131 changes: 131 additions & 0 deletions include/ntuples/dataframe.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#pragma once
#include "ntuples/ntuples.hh"

namespace nt{




template <typename... Ts>
struct dataframe : nt::base_maker_t<nt::_Remove_cvref_t<Ts>, nt::ax_type2<std::vector<Ts>, typename Ts::struct_maker>>...
{

template <typename T2>
decltype(auto) operator[](const nt::field_name_container<T2> &t)
{
return nt::field_name_container<T2>::get(*this);
}

auto operator[](size_t i)
{
using ret_t = nt::ntuple<_Remove_cvref_t<Ts> &...>;
return ret_t{
get<Ts>()[i]...};
}

auto operator[](size_t i) const
{
using ret_t = nt::ntuple<const Ts &...>;
return ret_t{
get<Ts>()[i]...};
}

template <typename T>
void push_back(const T &t)
{

[](auto...) {}(Ts::get(*this).emplace_back(Ts::get(t))...);
}

template <typename... T>
void emplace_back(T &&...t)
{
static_assert(sizeof...(t) == sizeof...(Ts), "\n==============missmatched amount of arguments=================\n");
[](auto...) {}(T::get(*this).emplace_back(std::forward<T>(t))...);
}

template <typename T1>
decltype(auto) get() const
{
return T1::get(*this);
}

template <typename T1>
decltype(auto) get()
{
return T1::get(*this);
}

auto size() const
{
auto size = _Remove_cvref_t<NthTypeOf<0, Ts...>>::get(*this).size();
return size;
}

template <int N>
static constexpr auto get_nth_type()
{

return get_ax_name_container(NthTypeOf<N, _Remove_cvref_t<Ts>...>{});
}
friend std::ostream &operator<<(std::ostream &out, const dataframe &self)
{
out << "|";

constexpr_for<0, sizeof...(Ts), 1>([&](auto ntuple_index)
{
static const auto x = self.template get_nth_type<ntuple_index>();
out << " ";
out << std::setw(5) << x.get_name();
out << " |"; });

out << "\n";
out << "|";
constexpr_for<0, sizeof...(Ts), 1>([&](auto i)
{
out << std::setw(5) << "-------|";
});
out << "\n";
auto size = self.size();
for (int i = 0; i < size; ++i)
{
auto current_element = self[i];
out << "|";
constexpr_for<0, sizeof...(Ts), 1>([&](auto ntuple_index)
{
static const auto x = self.template get_nth_type<ntuple_index>();
out << " ";
out << std::setw(5) << x.get(current_element).v;
out << " |"; });
out << "\n";
}

return out;
}
};

template <typename... Ts>
dataframe(Ts &&...ts) -> dataframe<_Remove_cvref_t<Ts>...>;

template <typename T>
struct dataframe_maker
{
};

template <typename... T>
struct dataframe_maker<ntuple<T...>>
{
using type = dataframe<_Remove_cvref_t<T>...>;
};

template <typename F>
auto fill_dataframe(int index, F &&f)
{
typename dataframe_maker<decltype(f(0))>::type ret;
for (int i = 0; i < index; ++i)
{
ret.push_back(f(i));
}
return ret;
}
}
33 changes: 33 additions & 0 deletions include/ntuples/full.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once



#include "ntuples/ntuples.hh"

#include "ntuples/ntuples.hh"
#include "ntuples/macro_nt_new_field.hh"
#include "ntuples/std_adapter.hh"




#include "ntuples/generic_algorithms.hh"
#include "ntuples/macro_nt_field.hh"
#include "ntuples/macro_comperator.hh"
#include "ntuples/macro_groupby.hh"
#include "ntuples/comparators.hh"
#include "ntuples/span.hh"
#include "ntuples/nt_erased.hh"
#include "ntuples/join.hh"
#include "ntuples/constexpr_for.hh"

#include "ntuples/vector_frame.hh"



#include "ntuples/bind_args.hh"
#include "ntuples/nt_vector_erased.hh"

#include "ntuples/groupby1.hh"

#include "ntuples/range.hh"
Loading