flatten 20260225

This commit is contained in:
Timothy Prepscius
2026-02-25 12:39:24 -05:00
commit fa54be052a
315 changed files with 49791 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
// TJP COPYRIGHT HEADER
#pragma once
namespace tjp::core {
template<typename T, typename S>
struct Sortable
{
T t;
S s;
bool operator<(const Sortable &rhs) const
{
return s < rhs.s;
}
} ;
template<typename T, typename F>
struct SortableFunc
{
T t;
bool operator<(const SortableFunc &rhs) const
{
return f(t) < f(rhs.t);
}
} ;
template<typename T, typename S>
struct SortablePtr
{
T t;
S *s;
bool operator<(const SortablePtr &rhs) const
{
return *s < *rhs.s;
}
} ;
} // namespace

View File

@@ -0,0 +1,27 @@
// TJP COPYRIGHT HEADER
#include <tjp/core/testing/catch.hpp>
#include "../call_with_pairs.hpp"
namespace tjp::core {
namespace {
SCENARIO("core::call_with_pairs")
{
WHEN("a function is called with pairs")
{
int c = 0;
auto f = [&](auto a, auto b)
{
return c += a * b;
};
call_with_pairs(f, 1, 2, 3, 4, 5, 6);
REQUIRE(c == 44);
}
}
}
} // namespace

View File

@@ -0,0 +1,43 @@
// TJP COPYRIGHT HEADER
#pragma once
#include <utility>
#include <tuple>
#include <type_traits>
#include <functional> // for std::invoke
namespace tjp::core {
template<typename F, typename ObjOrFirstArg, typename... Args>
constexpr void call_with_pairs(F&& f, ObjOrFirstArg&& obj_or_first, Args&&... args) {
constexpr bool is_member = std::is_member_function_pointer_v<std::decay_t<F>>;
if constexpr (is_member) {
static_assert(sizeof...(Args) % 2 == 0,
"Member function call_with_pairs: arguments after object must be in pairs");
} else {
static_assert((sizeof...(Args)+1) % 2 == 0,
"Non-member call_with_pairs: all arguments must be in pairs");
}
if constexpr (is_member) {
auto tup = std::forward_as_tuple(std::forward<Args>(args)...);
[&]<std::size_t... I>(std::index_sequence<I...>) {
(std::invoke(f,
std::forward<ObjOrFirstArg>(obj_or_first),
std::get<2*I>(tup),
std::get<2*I+1>(tup)), ...);
}(std::make_index_sequence<sizeof...(Args) / 2>{});
} else {
auto tup = std::forward_as_tuple(std::forward<ObjOrFirstArg>(obj_or_first),
std::forward<Args>(args)...);
[&]<std::size_t... I>(std::index_sequence<I...>) {
(std::invoke(f,
std::get<2*I>(tup),
std::get<2*I+1>(tup)), ...);
}(std::make_index_sequence<(sizeof...(Args) + 1) / 2>{});
}
}
} // namespace

View File

@@ -0,0 +1,57 @@
// TJP COPYRIGHT HEADER
#pragma once
#include <algorithm>
#include <vector>
#include <tjp/core/type_traits/is_comparable.hpp>
#include <optional>
#include <tjp/core/random/Random.h>
namespace tjp {
namespace core {
template<typename T, typename F, typename F1>
auto find_get_if_random_transform (T &c, F &&f, F1 &&transform)
{
std::vector<typename T::const_iterator> items;
for (auto i=c.begin(); i!=c.end(); ++i)
{
if (f(*i))
items.push_back(i);
}
using value_type = decltype(transform(*items.front()));
if (items.empty())
return std::optional<value_type>();
auto index = StandardRandom.nextInt<size_t>(0,items.size()-1);
return std::optional<value_type>(transform(*items[index]));
}
template<typename T, typename F>
auto find_get_if_random (T &c, F &&f)
{
std::vector<typename T::const_iterator> items;
for (auto i=c.begin(); i!=c.end(); ++i)
{
if (f(*i))
items.push_back(i);
}
using value_type = typename T::value_type;
if (items.empty())
return std::optional<value_type>();
auto index = StandardRandom.nextInt<size_t>(0,items.size()-1);
return std::optional<value_type>(*items[index]);
}
} // namespace utilities
} // namespace

View File

@@ -0,0 +1,17 @@
// TJP COPYRIGHT HEADER
// https://stackoverflow.com/questions/12030538/calling-a-function-for-each-variadic-template-argument-and-an-array
#pragma once
namespace tjp {
namespace core {
template <class F, class... Args>
void for_each_argument(F f, Args&&... args) {
[](...){}((f(std::forward<Args>(args)), 0)...);
}
} // namespace core
} // namespace

View File

@@ -0,0 +1,16 @@
// TJP COPYRIGHT HEADER
#pragma once
namespace tjp::core {
template<typename T>
T reverse_return(T &&c)
{
for (auto i=0; i<c.size()/2; ++i)
std::swap(c[i], c[c.size()-(i+1)]);
return c;
}
} // namespace

View File

@@ -0,0 +1,14 @@
// TJP COPYRIGHT HEADER
#pragma once
namespace tjp::core {
template<typename T, typename U>
void xor_inplace(T &t, U u)
{
for (auto &v : t)
v ^= u;
}
} // namespace