Skip to content

Instantly share code, notes, and snippets.

Eugene Hutorny hutorny

View GitHub Profile
View setof.hpp
#include <type_traits>
#if __cplusplus > 201709L
#include <bit>
template<typename T>
constexpr int countr_zero(T val) noexcept { return std::countr_zero(val); }
template<typename T>
constexpr int popcount(T val) noexcept { return std::popcount(val); }
#else
template<typename T, typename = std::enable_if_t<std::is_integral<T>::value,T>>
View hash_fnv1b.hpp
#pragma once
#include <cstdint>
#include <string_view>
namespace fnv1b {
/* FNV-1b hash function with extra rotation
* https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function
*/
template<typename T = std::size_t>
inline T hash(const char* str, std::size_t size) noexcept;
View any_of.hpp
/* constexpr any_of against a list of constants */
/** type_of_list<...>::type - helper structure for determining type of the first item in the list */
template<auto ... List> struct type_of_list;
template<auto List> struct type_of_list<List> { using type = decltype(List); };
template<auto First, auto ... List > struct type_of_list<First, List...> : type_of_list<First> {};
/** type_of<...>::type - determines type of the first item in the list */
template<auto ... List>
using type_of = typename type_of_list<List...>::type;
@hutorny
hutorny / print_iterable.hpp
Created Nov 15, 2018
A c++ template to print any iterable
View print_iterable.hpp
#include <iterator>
#include <utility>
// see live example on http://coliru.stacked-crooked.com/a/591f4db5a008cb5a
template<class Stream, class Vector, class Begin = decltype(std::begin(std::declval<Vector>()))>
inline Stream& operator<<(Stream& stream, const Vector& vect) {
const char* dlm = "";
for(const auto& i : vect) { stream << dlm << i; dlm = ", "; }
return stream;
}
@hutorny
hutorny / enumnames.hpp
Created Jan 21, 2017
Zero-dependency allocation-free mapping enum values to names for C++11
View enumnames.hpp
/** zero-dependency allocation-free mapping enum values to names */
namespace enumnames {
typedef const char* (*name)();
bool match(const char*, const char*) noexcept; /* to be provided by the user */
template<typename T, T K, name V>
struct tuple {
typedef T key_t;
static constexpr T key = K;
@hutorny
hutorny / anotherclass.ccs
Created Apr 4, 2017
Cascaded Configuration Sets for C++1y
View anotherclass.ccs
/************************************************************************/
/* anotherclass.ccs definition of configuration options and sets for */
/* MyAnotherClass */
/************************************************************************/
#include <ccs>
#pragma once
class MyAnotherClass;
namespace configuration {
template<>
struct Configuration<MyAnotherClass, Default> {
@hutorny
hutorny / miculog.config
Created Mar 24, 2017
Minimalistic C++11 logger with per-user-class configuration
View miculog.config
/**
* This file is intended for miculog per-class configuration
*/
/* remove this message from your own copy of miculog.config */
#pragma message "Default miculog configuration is in use"
/* add forward declaration of your classes here,
* embrace classes in namespaces, as needed
@hutorny
hutorny / list.hpp
Created Mar 23, 2017
Simple doubly linked list imlementation
View list.hpp
/* simple doubly linked list imlementation */
template<typename T>
class list {
public:
inline T* pfront() noexcept { return head ? &head->data : nullptr; }
inline T* pback() noexcept { return tail ? &tail->data : nullptr; }
template<typename ... Args>
inline T* construct(Args ... __args) noexcept {
return &append(new item(__args ...))->data;
}
View assign_array.hpp
/** assign - assigns a value to all array elements
* usage: short arr[77]; assign(arr,88);
* License: MIT */
template<typename T, size_t N, typename V>
static inline void assign(T (&a)[N], V v) noexcept {
for(size_t i=0; i<N; ++i) a[i] = v;
}
View container_of.hpp
/* container_of, inspired by AndrewTsao */
template <class T, typename M>
constexpr T* container_of(M *m, const M T::*p) noexcept {
return reinterpret_cast<T*>(reinterpret_cast<char*>(m)
- reinterpret_cast<const ptrdiff_t>(
&(static_cast<const T *>(nullptr)->*p)));
}
You can’t perform that action at this time.