Skip to content

Instantly share code, notes, and snippets.

View matklad's full-sized avatar

Alex Kladov matklad

View GitHub Profile
interface I<SELF> {
fun base(): V
fun reversed(): SELF
infix fun add(other: SELF): SELF
}
abstract class Adapter<SELF : I<SELF>>(val base: V) : I<SELF> {
abstract fun ctor(base: V): SELF
final override fun reversed(): SELF = ctor(base.reversed())
@matklad
matklad / loop.cpp
Last active October 14, 2016 15:16
int upper = 0;
for (int i = 0; i < (1 << n); i++) {
if (i == 2^{upper+1}) upper++;
function[i] = function[i ^ (1 << upper)] и что-нибудь ещё.
}
Число единичных бит можно так: f[i] = f[i >> 1] + (i & 1);
Сумма элементов: f[i] = f[i ^ (1 << upper)] + weight[upper]
package org.rust;
import java.util.ArrayList;
import java.util.List;
public class Main {
private static class Base {
}
private static class Derived extends Base {
import heapq
import random
import time
from threading import Thread
def alarm(span, count):
for _ in range(count):
time.sleep(span)
print(span)
@matklad
matklad / bfs.py
Last active November 11, 2016 10:28
def bfs(graph, start):
work = {start}
dist = {}
current_dist = 0
while work:
for u in work:
dist[u] = current_dist
work = {nb for u in work
@matklad
matklad / test.groovy
Last active December 1, 2016 10:17
Laconic test output for gradle
test {
testLogging {
events 'skipped', 'failed'
exceptionFormat = 'full'
}
beforeSuite { suite ->
if (suite.className != null) {
println()
println(suite.className)
abstract class Query {
class Sum {
}
class Add {
}
/**
* Executes long running tasks not faster then once in [delayMillis] and makes sure
* at most one tasks executes at a time. The task itself may take more than [delayMillis]
* to complete. Task may be asynchronous.
*/
private class DebouncingQueue(
private val delayMillis: Int,
parentDisposable: Disposable
) {
private val alarm = Alarm(Alarm.ThreadToUse.POOLED_THREAD, parentDisposable)
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <ctime>
#include <forward_list>
#include <iostream>
#include <list>
#include <unordered_set>
@matklad
matklad / takeWhileInclusive.kt
Created December 24, 2016 21:03
An "inclusive" version of Kotlin's takeWhile
fun <T> Sequence<T>.takeWhileInclusive(pred: (T) -> Boolean): Sequence<T> {
var shouldContinue = true
return takeWhile {
val result = shouldContinue
shouldContinue = pred(it)
result
}
}