Skip to content

Instantly share code, notes, and snippets.

View amigo421's full-sized avatar
🏠
Working from home

Ernst Maurer amigo421

🏠
Working from home
  • Moscow, Russia
View GitHub Profile
@amigo421
amigo421 / ctype.hpp
Created January 24, 2020 12:48
custom separators, two ways
// getting existing table for ctype<char> specialization
const auto temp = ctype<char>::classic_table();
// create a copy of the table in vector container
vector<ctype<char>::mask> bar(temp, temp + ctype<char>::table_size);
// add/remove stream separators using bitwise arithmetic.
// use char-based indices because ascii codes here are equal to indices
bar[' '] ^= ctype_base::space;
bar['\t'] &= ~(ctype_base::space | ctype_base::cntrl);
bar[':'] |= ctype_base::space;
@amigo421
amigo421 / measure.hpp
Last active September 26, 2020 02:50
wrapper to measure of time execution
/**
usage: std::cout << measure<>::execution(functor(dummy)) << std::endl
*/
template <typename TimeT = std::chrono::nanoseconds> struct measure {
template <typename F, typename... Args>
static typename TimeT::rep execution(F func, Args &&... args) {
auto start = std::chrono::steady_clock::now();
func(std::forward<Args>(args)...);
auto duration = std::chrono::duration_cast<TimeT>(
std::chrono::steady_clock::now() - start);
@amigo421
amigo421 / singleton.hpp
Created January 24, 2020 12:46
singleton template
template <typename T> class singleton {
void operator=(T const &) = delete;
singleton(T const &) = delete;
public:
static T &get_instance() {
static T inst;
return inst;
}
};
@amigo421
amigo421 / checked_iterator_type.hpp
Created January 24, 2020 12:46
checked iterator type
// checking that iterator is forward_itertaor
template <class iter>
using iterator_type_checked =
std::is_same<typename std::iterator_traits<iter>::iterator_category,
std::forward_iterator_tag>;
@amigo421
amigo421 / spawn_task.cpp
Created January 24, 2020 12:45
SpawnTask
template <typename function_type, typename... argument_types>
auto spawn_task(function_type &&f, argument_types &&... args) {
using result_type =
typename std::result_of<function_type(argument_types && ...)>::type;
using packaged_task = std::packaged_task<result_type(argument_types && ...)>;
auto task = packaged_task{std::forward<function_type>(f)};
auto future = task.get_future();
task(args...);
@amigo421
amigo421 / fibonacci.cpp
Created January 24, 2020 12:43
fibonacci compile time
emplate<size_t N>
struct fibonacci : integral_constant<size_t, fibonacci<N-1>{} + fibonacci<N-2>{}> {};
template<> struct fibonacci<1> : integral_constant<size_t,1> {};
template<> struct fibonacci<0> : integral_constant<size_t,0> {};
@amigo421
amigo421 / bfs.h
Created January 24, 2020 12:42
BFS
struct node {
int data;
node *left = nullptr;
node *right = nullptr;
};
void TraversBFS(node *root) {
std::queue<node *> q;
q.push(root);
for (node *n = q.front(); !q.empty(); n = q.front()) {
#include <iostream>
#include <cstring>
#include <algorithm>
#include <iterator>
#include <vector>
#include <queue>
// TODO create tree as an array
auto build_tree(std::size_t n) {
std::vector<int> tree(n);
@amigo421
amigo421 / mem_manage.cpp
Created June 22, 2017 08:59
school example of dynamic memory management
#include <iostream>
#include <algorithm>
#include <iterator>
#include <numeric>
int* array_append(int *arr, unsigned int &size, int value) {
int *new_arr = new int[size + 1];
std::memcpy(new_arr, arr, size * sizeof(*new_arr));
if (size && arr != nullptr)