Skip to content

Instantly share code, notes, and snippets.

@yohhoy
yohhoy / SpliteratorDump.java
Created September 11, 2014 00:11
Java8 StreamAPI Spliterator characteristics
import java.util.*;
import java.util.stream.*;
public class SpliteratorDump {
static String c2s(String hdr, int c) {
StringBuilder sb = new StringBuilder(hdr + ":");
if ((c & Spliterator.CONCURRENT) != 0) sb.append(" CONCURRENT");
if ((c & Spliterator.DISTINCT) != 0) sb.append(" DISTINCT");
if ((c & Spliterator.IMMUTABLE) != 0) sb.append(" IMMUTABLE");
if ((c & Spliterator.NONNULL) != 0) sb.append(" NONNULL");
@yohhoy
yohhoy / mt_queue0.cpp
Last active August 29, 2015 14:06
MT-safe queue implementation
// mutex + busy-loop
#include <queue>
#include <thread>
#include <mutex>
class mt_queue {
static const int capacity = 10;
std::queue<int> q_;
std::mutex mtx_;
public:
@yohhoy
yohhoy / mt_queue_deadlock1.cpp
Last active August 29, 2015 14:06
MT-safe queue/Dead-lock senario
//
// Dead-lock senario under wrong implementation (capacity == 1)
//
// Precondition:
// 1-Procuder thread(P1) and 2-Consumer threads(C1,C2)
// Thread status = Running->Blocked->Waiting->Running
//
// Step:
// (queue q_ is empty)
// C1: call pop() and blocked at cv_.wait() [C1: Running->Blocked]
@yohhoy
yohhoy / gist:191757eaa0fe8dfa39e8
Last active August 29, 2015 14:06
Dead-lock demonstration with condition_variable::notify_one()/pthread_cond_signal()
//
// Dead-lock demonstration with condition_variable::notify_one()
//
#include <utility>
#include <mutex>
#include <condition_variable>
template<typename T>
class mt_slot {
T slot_;
@yohhoy
yohhoy / ffmpeg_tb.md
Last active April 10, 2023 07:36
time-base in FFmpeg

https://www.ffmpeg.org/doxygen/trunk/dump_8c_source.html#l00454

  • fps = st->avg_frame_rate
  • tbr = st->r_frame_rate
  • tbn = st->time_base
  • tbc = st->codec->time_base

AVStream::avg_frame_rate

Average framerate.

  • demuxing: May be set by libavformat when creating the stream or in avformat_find_stream_info().
  • muxing: May be set by the caller before avformat_write_header().
@yohhoy
yohhoy / sort_by.cpp
Created October 5, 2014 08:28
sort by data member in class
#include <algorithm>
#include <iterator>
#include <tuple>
template <
typename Iterator, // RandomAccessIterator
typename... Ts,
typename U = typename std::iterator_traits<Iterator>::value_type>
void sort_by(Iterator begin, Iterator end, Ts U::*... pm)
{
@yohhoy
yohhoy / SortLexicographicOrder.java
Last active December 29, 2017 11:20
java.util.Comparator in Java8
import java.util.*;
public class SortLexicographicOrder {
public static void main(String[] args) {
Person[] people = {
new Person("C", "F", 30),
new Person("B", "M", 40),
new Person("D", "M", 20),
new Person("B", "F", 40),
new Person("A", "F", 20)
@yohhoy
yohhoy / kill_ptree.cs
Created October 17, 2014 07:12
kill process tree(process group) on WIndows/C#
void KillProcessTree(System.Diagnostics.Process process)
{
string taskkill = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "taskkill.exe");
using (var procKiller = new System.Diagnostics.Process()) {
procKiller.StartInfo.FileName = taskkill;
procKiller.StartInfo.Arguments = string.Format("/PID {0} /T /F", process.Id);
procKiller.StartInfo.CreateNoWindow = true;
procKiller.StartInfo.UseShellExecute = false;
procKiller.Start();
procKiller.WaitForExit(1000); // wait 1sec
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CancellationException;
public class CompletableFutureExample {
private static CompletableFuture<Optional<String>> applyTaskAsync(String s) {
return CompletableFuture.completedFuture(s)
import java.util.stream.IntStream;
public class StreamCollectFailure {
public static void main(String[] args) {
int[] points = { 80, 95, 70, 60 };
double ave = IntStream.of(points).average().orElse(0.0);
double var = IntStream.of(points).collect(
() -> new Double(0.0),
(r,n) -> { r += Math.pow(n - ave, 2); },