Skip to content

Instantly share code, notes, and snippets.

Bryce Nyeggen bnyeggen

Block or report user

Report or block bnyeggen

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@bnyeggen
bnyeggen / Crypt.java
Created Nov 29, 2015
Stream based en/decryption in Java 7
View Crypt.java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
@bnyeggen
bnyeggen / TombstoneBinarySearch.java
Created Oct 20, 2015
Binary search that considers magic value as not present
View TombstoneBinarySearch.java
public class TombstoneBinarySearch {
public static int binarySearch(long[] a, int fromIndex, int toIndex, long key, long tombstone) {
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
final int origMid = mid;
long midVal = a[mid];
@bnyeggen
bnyeggen / shade.xml
Created Aug 28, 2014
Maven uberjar via maven-shade-plugin
View shade.xml
<plugin>
<!-- uberjar via "mvn package shade:shade" -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
@bnyeggen
bnyeggen / MMapper.java
Last active Jul 29, 2019
Mmap more than 2GB of a file in Java
View MMapper.java
import java.io.RandomAccessFile;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.channels.FileChannel;
import sun.nio.ch.FileChannelImpl;
import sun.misc.Unsafe;
@SuppressWarnings("restriction")
public class MMapper {
@bnyeggen
bnyeggen / IntFreeList.java
Last active Aug 29, 2015
Lockfree quasi-queue of integers.
View IntFreeList.java
package com.nyeggen.strintmap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
/** Lockfree quasi-queue of integers. Conceptually there is a queue of integers
* at the head, followed by a stream of integers from n to Integer.MAX_VALUE.
* add() places at the end of the queue; get() retrieves first from the queue,
* and then from the increasing stream. The stream is implemented with an
* AtomicInteger; continuing beyond Integer.MAX_VALUE will cycle back down to
@bnyeggen
bnyeggen / upgradablelock.go
Last active Jul 25, 2019
Upgradable read -> write locks in Go
View upgradablelock.go
package main
import (
"fmt"
"runtime"
"sync"
)
type UpgradableLock struct {
uglMutex sync.RWMutex
@bnyeggen
bnyeggen / geoull.cl
Created Dec 16, 2013
Geospatial recommendation kernel
View geoull.cl
/*
OpenCL code for a geospatial recommender. The underlying algorithm is:
- Each user has a set of XY points
- Similarity between user A and user B is defined as the inverse mean minimum distance to each of user A's points, from any of user B's points. This is not symmetric. E.g., if user A has points at (0,0) and (10,10), and user B has a point at (4,4), similarity of B to A will be 1 / ((sqrt(4^2 + 4^2) + sqrt(6^2 + 6^2)) / 2), and similarity of A to B will be 1 / (sqrt(4^2 + 4^2)). It is possible to define other similarity measures along the same lines (e.g. inverse mean minimum squared distance) that will give different results. We're all about the heuristics.
- For evaluating the predicted strength at a new XY location for a given main user,
- Calculate an inverse-square dropoff from every point (as if one was calculating gravity in an N-body simulation). Again, one could use some other exponent with better empirical foundation. This can be optimized by excluding points beyond a certa
@bnyeggen
bnyeggen / core.clj
Last active Dec 27, 2015
Running a Compojure app with a main function via java -jar
View core.clj
(ns myproject.core
(:use compojure.core)
(:require [compojure.route :as route]
[compojure.handler :as handler]
[ring.adapter.jetty :as ring-jetty])
(:gen-class))
(defroutes my-routes
(GET "/" [] "<h1>Hello World</h1>")
(route/not-found "<h1>Page not found</h1>"))
@bnyeggen
bnyeggen / RandomElementProvider.java
Created Oct 6, 2013
Threadsafe pseudo-shuffle - each caller gets a random element of the backing array, with the guarantee that each element will be selected at most once. The tradeoff is that as we approach all elements selected, we hit a slowdown as we're forced to scan for the remaining elements. For that reason, this is best suited to situations where we need a…
View RandomElementProvider.java
import java.util.Random;
import java.util.concurrent.atomic.AtomicIntegerArray;
public class RandomElementProvider {
//Flag value - not valid as an element of the backing array.
public static final int INVALID = Integer.MIN_VALUE;
//After this many failed random selections, devolve to a scan.
//Corresponds to being able to handle on average (1 - 1/RETRY_THRESHOLD) of elements
//being selected already before scanning
public static final int RETRY_THRESHOLD = 4;
@bnyeggen
bnyeggen / hinttype.clj
Created Feb 2, 2013
Macro-generated deftype with type-hinted fields.
View hinttype.clj
(defmacro hinttype
[type-name & hint-symbols]
`(deftype ~type-name
[~@(for [s hint-symbols]
(with-meta (gensym "a")
{:tag (case s
:int 'int
:long 'long
:float 'float
:double 'double
You can’t perform that action at this time.