Skip to content

Instantly share code, notes, and snippets.

@sekrasoft
sekrasoft / delayed-localStorage.user.js
Last active January 9, 2019 20:43
Подменяет localStorage Storage-подобным объектом с сохранением данных в настоящем хранилище.
// ==UserScript==
// @name Отложенная запись в localStorage
// @include http://*/*
// @include https://*/*
// @version 1.0.0
// @grant none
// ==/UserScript==
(function(){
@sekrasoft
sekrasoft / comparators.c
Created March 9, 2016 16:41
Мощь выделения компараторов из алгоритма сортировки
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct computer {
char* name;
int frequency;
int memory;
};
#include <stdio.h>
#include <stdlib.h>
#define TEST_RAND_MAX 0xff
#define MAX 129
#define CYCLES 10000
int test_rand() {
return rand() & 0xff; // 0..255
}
#include <stdio.h>
#include <stdlib.h>
typedef double number;
typedef number* numbers;
typedef size_t index;
typedef number (*function)(number);
numbers map(function f, numbers xs, size_t size) {
numbers ys = malloc(size * sizeof(number));
#include <stdio.h>
#include <stdlib.h>
void swap(void* a, void* b, size_t size) {
char tmp;
for(size_t i = 0; i < size; ++i) {
tmp = ((char*)a)[i];
((char*)a)[i] = ((char*)b)[i];
((char*)b)[i] = tmp;
@sekrasoft
sekrasoft / church.h
Last active August 10, 2016 11:44
Compile-time Church numerals (C++98)
// Church numerals
namespace Church {
// zero = \f x -> x
template <template <typename> class f, typename x> struct Zero {
typedef x value;
};
// succ = \n -> \f x -> f (n f x)
template <template <template <typename> class, typename> class n> struct Succ {
@sekrasoft
sekrasoft / church.h
Last active August 10, 2016 11:44
Compile-time Church numerals (C++11)
// Church numerals
namespace Church {
// zero = \f x -> x
template <template <typename> class f, typename x>
using Zero = x;
// succ = \n -> \f x -> f (n f x)
template <template <template <typename> class, typename> class n> struct Succ {
template <template <typename> class f, typename x>
#include <stdio.h>
#include <stdlib.h>
#define TO_STRING1(X) #X
#define TO_STRING(X) TO_STRING1(X)
#define LINE TO_STRING(__LINE__)
#define malloc(N) log_p(malloc(log_s(N, "CALLING MALLOC @ " \
LINE)), "CALLED MALLOC @ " LINE)
#define free(p) free(log_p(p, "FREE @ " LINE))
@sekrasoft
sekrasoft / README.md
Created October 18, 2017 18:22
Декомпозиция кортежей без std::tie и C++17

Декомпозиция кортежей в декларативном стиле:

my_tuple t(1, 2.0, 'x'); // кортеж произвольного типа my_tuple
int a; float b; char c;  // переменные, в которые хотим распаковать
decomposed(t) = a, b, c; // присваивание (задом наперёд, т.к. C++ не даёт реализовать "=" снаружи класса,
                         //               самая удачная из возможных "прямая" нотация - std::tie(a, b, c) = ...)

Для работы требуется задать правила декомпозиции (в данном случае - класса my_tuple):

DECOMPOSER_RULE(my_tuple, 0, int, const my_tuple& t, t.a)

@sekrasoft
sekrasoft / ndarray.cpp
Created March 24, 2020 18:16
Multidimentional array wrapper
#include <iostream>
#include <cassert>
template <typename T, size_t dim>
class NDArray: protected NDArray<T, dim - 1> {
public:
template <typename T1, typename... D>
NDArray (T1* data, size_t size, D...sizes): NDArray<T, dim - 1>(data, sizes...), size(size) {}
NDArray<T, dim - 1> operator [] (size_t i) {
assert(i >= 0 && i < size);