Skip to content

Instantly share code, notes, and snippets.

View qrealka's full-sized avatar

Dmitry Loginov qrealka

View GitHub Profile
@qrealka
qrealka / web-servers.md
Created January 18, 2020 09:55 — forked from willurd/web-servers.md
Big list of http static server one-liners

Each of these commands will run an ad hoc http static server in your current (or specified) directory, available at http://localhost:8000. Use this power wisely.

Discussion on reddit.

Python 2.x

$ python -m SimpleHTTPServer 8000
@qrealka
qrealka / rand_double.c
Created January 7, 2020 11:02
uniform random float generation
/*-
* Copyright (c) 2014 Taylor R. Campbell
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
@qrealka
qrealka / random.h
Created January 7, 2020 10:45 — forked from Leandros/random.h
C++ Pseudo Random Number Generators
/* Copyright (c) 2018 Arvid Gerstmann. */
/* This code is licensed under MIT license. */
#ifndef AG_RANDOM_H
#define AG_RANDOM_H
class splitmix
{
public:
using result_type = uint32_t;
static constexpr result_type (min)() { return 0; }
@qrealka
qrealka / int_to_bytes.cpp
Created November 16, 2019 07:54
struct binding for bitwise
#include <cstdint>
#include <iostream>
#include <tuple>
#include <type_traits>
template <typename T>
using unsigned_of = std::conditional_t<
(sizeof(T) == 1), uint8_t,
std::conditional_t<(sizeof(T) == 2), uint16_t,
std::conditional_t<(sizeof(T) == 4), uint32_t,
@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);