Skip to content

Instantly share code, notes, and snippets.

View h0tk3y's full-sized avatar
🐘

Sergey Igushkin h0tk3y

🐘
View GitHub Profile
val mapper = jacksonObjectMapper()
.enable(SerializationFeature.INDENT_OUTPUT)
.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET)
.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT)
.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE)

πanic Protocol

Версия конфигурации

Версией конфигурации или просто версией будем называть число реконфигураций работающей системы с момента начала работы, изначально это ноль. Каждый узел будет хранить последнюю известную ему версию конфигурации, и версия конфигурации будет использоваться в токене.

Узлы

@h0tk3y
h0tk3y / Main.java
Last active August 24, 2025 19:31
Some Java 8 Streams examples
package ru.ifmo.ctddev.igushkin.streamExamples;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
@h0tk3y
h0tk3y / gist:dbb3754e93e044f4e841
Created January 11, 2016 11:36
Maps inversion
public <A, B, C> Map<B, List<Pair<A, C>>> invertMap(Map<A, Map<B, C>> input) {
return input.entrySet().stream()
.flatMap(kv -> kv.getValue().entrySet().stream().map(vs -> new Pair<>(kv.getKey(), vs)))
.collect(Collectors.groupingBy(
it -> it.getValue().getKey(),
Collectors.mapping(
it -> new Pair<>(it.getKey(), it.getValue().getValue()),
Collectors.toList()
))
);
import kotlin.system.measureTimeMillis
/**
* Created by igushs on 2/2/16.
*/
public operator fun <T> Sequence<T>.plus(otherGenerator: () -> Sequence<T>) =
object : Sequence<T> {
private val thisIterator: Iterator<T> by lazy { [email protected]() }
private val otherIterator: Iterator<T> by lazy { otherGenerator().iterator() }
import kotlin.system.measureTimeMillis
/**
* Created by igushs on 2/2/16.
*/
public operator fun <T> Sequence<T>.plus(otherGenerator: () -> Sequence<T>) =
object : Sequence<T> {
private val thisIterator: Iterator<T> by lazy { [email protected]() }
private val otherIterator: Iterator<T> by lazy { otherGenerator().iterator() }
import kotlin.system.measureTimeMillis
/**
* Created by igushs on 2/2/16.
*/
public operator fun <T> Sequence<T>.plus(otherGenerator: () -> Sequence<T>) =
object : Sequence<T> {
private val thisIterator: Iterator<T> by lazy { [email protected]() }
private val otherIterator: Iterator<T> by lazy { otherGenerator().iterator() }
private const val SOURCE_ID = 0
private const val SINK_ID = 1
class IntEmd<TItem>(val distance: ((TItem, TItem) -> Int)? = null)
: HistogramDistance<TItem, Int> {
/** Adds two [TNumeric] items. */
infix fun Int.plus(other: Int) = this + other
/** Subtracts two [TNumeric] items. */

EMD

This is an implementation of Earth Mover's Distance for histograms in Kotlin.

It uses successive shortest path min-cost max-flow solution with SPFA inside and thus is quite fast.

How to use

class Node<T>(val value: T,
val children: Map<T, Node<T>>)
fun <T> buildTree(initialState: T, childrenProducer: (T) -> Set<T>): Node<T> = Node(
initialState,
childrenProducer(initialState).associateBy({ it }, { buildTree(it, childrenProducer) })
)
fun <T> List<T>.withoutItemAt(index: Int) = take(index) + drop(index + 1)