Skip to content

Instantly share code, notes, and snippets.

View robinst's full-sized avatar

Robin Stocker robinst

View GitHub Profile
@robinst
robinst / thread_pool_with_worker_reuse.rs
Created October 12, 2016 00:25
Rust thread pool with worker reuse for tasks that have an expensive initialization
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};
use std::sync::{Arc, Mutex};
use std::thread;
fn main() {
let pool = Arc::new(Pool::new());
let mut handles = Vec::new();
@robinst
robinst / MergeBaseForkPoint.java
Created February 12, 2016 04:28
`git merge-base --fork-point` using JGit (not optimal)
public static Optional<RevCommit> findForkPoint(Repository repository, String base, String tip) throws IOException {
try (RevWalk walk = new RevWalk(repository)) {
RevCommit tipCommit = walk.lookupCommit(repository.resolve(tip));
List<ReflogEntry> reflog = repository.getReflogReader(base).getReverseEntries();
if (reflog.isEmpty()) {
return Optional.empty();
}
// The `<=` is deliberate because we want to check both new and old IDs for the oldest entry
for (int i = 0; i <= reflog.size(); i++) {
ObjectId id = i < reflog.size() ? reflog.get(i).getNewId() : reflog.get(i - 1).getOldId();
@robinst
robinst / gist:95e9222aac69484483c4
Created June 10, 2015 08:25
commonmark.js test input
a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a_ a
@robinst
robinst / VarargWithArray.java
Created October 1, 2012 10:45
VarargWithArray dangerous
import java.util.Arrays;
public class VarargWithArray {
public static void main(String[] args) {
int[] ints = {1, 2, 3};
MyList l = new MyList(ints);
ints[0] = 666;
System.out.println(l);
}
@robinst
robinst / jgit-checkout-dir.java
Created August 13, 2012 08:20
JGit checkout of directory
@Test
public void testCheckoutOfDirectoryShouldBeRecursive() throws Exception {
File a = writeTrashFile("dir/a.txt", "A");
File b = writeTrashFile("dir/sub/b.txt", "B");
git.add().addFilepattern("dir").call();
write(a, "modified");
write(b, "modified");
git.checkout().addPath("dir").call();
@robinst
robinst / structural-types.scala
Created May 12, 2011 12:51
Duck Typing in Scala
class Duck {
def quack = println("The duck quacks")
def walk = println("The duck walks")
}
class Dog {
def quack = println("The dog quacks (barks)")
def walk = println("The dog walks")
}
// The information whether head,index,merge iterators are currently
// pointing to file/folder/non-existing is encoded into this variable.
//
// To decode write down ffMask in hexadecimal form. The last digit
// represents the state for the merge iterator, the second last the
// state for the index iterator and the third last represents the state
// for the head iterator. The hexadecimal constant "F" stands for
// "file",
// an "D" stands for "directory" (tree), and a "0" stands for
// non-exisiting