Skip to content

Instantly share code, notes, and snippets.

View mtao's full-sized avatar

Michael Tao mtao

View GitHub Profile
@mtao
mtao / splicer.cpp
Last active October 28, 2017 05:28
eigen matrix interleave by rows
#include <Eigen/Dense>
#include <tuple>
#include <utility>
#include <iostream>
template <typename... Args, int... N>
auto _interleave(std::integer_sequence<int,N...>, const Args&... args) {
using namespace Eigen;
constexpr static int S = sizeof...(Args);
using Scalar = typename std::tuple_element<0,std::tuple<Args...>>::type::Scalar;
@mtao
mtao / loops.cpp
Created October 13, 2017 13:32
simple case to remind myself how for loops work
#include <iostream>
int main() {
constexpr static int N = 20;
for(int i = 0; i < N; ++i) {
std::cout << i << ", ";
}
std::cout << std::endl;
@mtao
mtao / binding_raii.cpp
Last active October 11, 2017 23:15
a tool to enable raii bind/release combos when some classes can have additional data. most annoying challenge was to make it so classes with no extra data didn't have to handle things themselves.
#include <cstddef>
#include <utility>
#include <iostream>
template <typename Obj>
struct bind_enabled {
public:
const Obj& derived() const {return *static_cast<const Obj*>(this);}
@mtao
mtao / optional_extra_data.cpp
Created October 10, 2017 20:42
test case to have little containers with extra arguments in some cases
#include <cstddef>
#include <utility>
#include <iostream>
template <typename Obj>
using optional_data_type = decltype(std::declval<Obj>().optional());
template <typename Obj>
struct Serializer {
public:
@mtao
mtao / range_for_from_it.cpp
Created October 4, 2017 17:44
a small wrapper to enable ranged for loops.
template <typename TokType>
struct range {
public:
range(const TokType& a, const TokType& b): m_begin(a), m_end(b) {}
TokType begin() const { return m_begin;}
TokType end() const { return m_end;}
private:
const TokType& m_begin,m_end;
};
@mtao
mtao / contiguous_bin_tree.hpp
Last active September 30, 2017 17:21
contiguous binary tree implementation someone was curious to see
#ifndef CONTIGUOUS_BINARY_TREE_HPP
#define CONTIGUOUS_BINARY_TREE_HPP
#include <cmath>
#include <vector>
template <typename T>
struct CBinaryTree: public std::vector<T> {
public:
using BaseType = std::vector<T>;
using BaseType::operator[];
@mtao
mtao / eigen_hdk_conversion.hpp
Created September 22, 2017 20:06
bad naming scheme and should probably use a more general template than Eigen::VexctorX?, but this does what i ened for now
#ifndef EIGEN_HDK_CONVERSION_HPP
#define EIGEN_HDK_CONVERSION_HPP
#include <UT/UT_SparseMatrix.h>
#include <Eigen/Sparse>
template <typename T, int Options = Eigen::ColMajor, typename StorageIndex>
UT_SparseMatrixELLT<T> eigen_to_hdk_ellt(const Eigen::SparseMatrix<T,Options,StorageIndex>& A) {
UT_SparseMatrixELLT<T> B(A.rows(), A.nonZeros());
int nz;
@mtao
mtao / unrolling_variadic_templates.cpp
Created September 20, 2017 20:33
a silly mechanism for experimenting with variadic templates. I'm hoping there's a way to do tihs without helper functions?
#include <array>
#include <iostream>
#include <utility>
#include <iterator>
template <int N, typename T, typename... Types>
struct NthPackTerm {
using type = typename NthPackTerm<N-1,Types...>::type;
};
@mtao
mtao / call_until_not_nullptr.hpp
Created September 15, 2017 20:41
Code to try multiple attempts at creating a single object
#include <iostream>
template <typename F, typename... Fs>
auto call_until_not_nullptr(F&& f, Fs&&... fs) -> decltype(f()) {
if(auto v = f()) {
return v;
} else {
if constexpr(sizeof...(fs) != 0) {
return call_until_not_nullptr(std::forward<Fs>(fs)...);
#ifndef CYCLIC_LIST_HPP
#define CYCLIC_LIST_HPP
#include <list>
namespace detail {
template <typename Iterable>
struct cyclic_iterator {
public: