Skip to content

Instantly share code, notes, and snippets.

public class SingleLock<V> implements Lazy<V> {
private Callable<V> codeBlock;
private V value;
public SingleLock(Callable<V> codeBlock) {
this.codeBlock = codeBlock;
}
@Override
static int rotationPoint(char[] values) {
int start = 0;
int end = values.length - 1;
char startCharacter = values[0];
while (start <= end) {
int mid = (start + end) >>> 1;
if (isRotationPoint(values, mid)) {
static int bruteRotationPoint(char[] values) {
for (int index = 1; index < values.length; index++) {
if (values[index] < values[index - 1]) {
return index;
}
}
return -1;
}
private static int binarySearch(int[] a, int fromIndex, int toIndex,
int key) {
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
int mid = (low + high) >>> 1; // This line had the bug - earlier it was int mid = (low + high) / 2;
int midVal = a[mid];
if (midVal < key)
@ashkrit
ashkrit / gist:ae77ba464b228bf27dd0
Created December 10, 2015 09:25 — forked from debasishg/gist:8172796
A collection of links for streaming algorithms and data structures
  1. General Background and Overview
@Override
public Object[] init() {
Object[] localValue;
int currentSize;
while ((localValue = arrayValue) == null || localValue.length == 0) {
if ((currentSize = size) < 0) {
Thread.yield(); // Array already init
} else if (unsafe.compareAndSwapInt(this, SIZECTL, currentSize, -1)) {
@FunctionalInterface
public interface AutoCodeBlock {
void execute(AutoCloseable closeable) throws IOException;
}
@FunctionalInterface
public interface ExceptionBlock {
void execute() throws Exception;
}
@FunctionalInterface
public interface CodeBlock {
void execute();
}
public static void time(String name, CodeBlock codeBlock) {
long start = System.currentTimeMillis();
codeBlock.execute();
long total = System.currentTimeMillis() - start;
System.out.println(name + " took " + total + " ms");
@FunctionalInterface
public interface CodeBlock {
void execute();
}
@FunctionalInterface
public interface Condition {
boolean test();
}
public void add(T element) {
AutoCompactList<T> slot = slots.get(lastSlotIndex());
if (slot.remainingCapacity() > 0) {
slot.add(element);
} else {
appendNewSlot();
add(element);
}
}