Skip to content

Instantly share code, notes, and snippets.

View qrealka's full-sized avatar

Dmitry Loginov qrealka

View GitHub Profile
@qrealka
qrealka / before_greg_test.cpp
Last active November 11, 2019 12:26
test converters for days before 1582 (gergorian calendar)
#include <cstdio>
#include <chrono>
#include <cinttypes>
#include <ctime>
struct YMD
{
int y{};
unsigned m{};
unsigned d{};
@qrealka
qrealka / errors_generator.cpp
Last active October 17, 2019 14:41
generate error messages functions
#include <string>
#include <fmt/format.h>
#include <fmt/printf.h>
namespace details
{
#define ERROR_MESSAGES(X) \
X(DivByZero) \
X(NumOverflow) \
@qrealka
qrealka / concepts_macro.cpp
Created October 16, 2019 11:51
concepts for c++14
// (C) eric niebler
#include <type_traits>
#define CAT_(X, ...) X ## __VA_ARGS__
#define CAT(X, ...) CAT_(X, __VA_ARGS__)
#define RETURN(...) return_t<__VA_ARGS__, std::enable_if_t<RETURN_
#define RETURN_(...) CAT(RETURN_, __VA_ARGS__)>>
#define RETURN_requires
@qrealka
qrealka / strcat17.cpp
Last active October 8, 2019 09:33
c++ 17 one allocation strcat
// fine for string_view or const char*. todo: use std::forward
// from Marco Magdy
template<typename.. Args>
auto strcat(Args... args)
{
const auto total = (... + args.size());
std::string s;
s.reserve(total);
(s += ... += args);
return s;
template <typename InputIt, typename OutputIt>
OutputIt
radix_sort_split(InputIt first, InputIt last, OutputIt output, std::uint64_t bit)
{
std::vector<std::uint64_t> e(std::distance(first, last));
// Count 0s.
std::transform(first, last, e.begin(),
[=] (auto t) { return !(t & (1 << bit)); });
template <typename InputIt, typename OutputIt, typename BinaryOp, typename T, typename Size>
unique_future<OutputIt>
async_inclusive_scan(InputIt first, InputIt last, OutputIt output,BinaryOp op, T init, Size chunk_size)
{
Size const elements = std::distance(first, last);
Size const chunks = (1 + ((elements - 1) / chunk_size)); // Round up.
std::vector<unique_future<T>> sweep;
sweep.reserve(chunks);
@qrealka
qrealka / digit10.h
Created August 28, 2019 09:54
digit coount
// From Andrei Alexandrescu talk
// https://www.facebook.com/notes/facebook-engineering/three-optimization-tips-for-c/10151361643253920/
// Apache License
// Version 2.0, January 2004
**
* Returns the number of digits in the base 10 representation of an
* uint64_t. Useful for preallocating buffers and such. It's also used
* internally, see below. Measurements suggest that defining a
* separate overload for 32-bit integers is not worthwhile.
*/
@qrealka
qrealka / atoi_fast.cpp
Created August 27, 2019 15:50
atoi_fast
#include <cstdlib>
// Andrei Alexandrescu at the C++ and Beyond 2012
class atoi_func
{
public:
atoi_func(): value_() {}
inline int value() const { return value_; }
@qrealka
qrealka / gitclean.sh
Created August 27, 2019 09:44 — forked from ericelliott/gitclean.sh
gitclean.sh - cleans merged/stale branches from origin
git remote prune origin
git branch -r --merged master | egrep -iv '(master|develop)' | sed 's/origin\///g' | xargs -n 1 git push --delete origin
@qrealka
qrealka / cpp11sfinae.cpp
Created August 15, 2019 13:15
cpp11 sfinae
template<class T>
auto serialize_imp(std::ostream& os, T const& obj, int)
-> decltype(os << obj, void())
{
os << obj;
}
template<class T>
auto serialize_imp(std::ostream& os, T const& obj, long)
-> decltype(obj.stream(os), void())