Skip to content

Instantly share code, notes, and snippets.

@basp1
basp1 / SuperLU_API.cpp
Last active August 29, 2015 14:21
simple API to SuperLU
#include "slu_ddefs.h"
#include <vector>
struct Interal_SuperLU
{
superlu_options_t options;
SuperLUStat_t stat;
std::vector<int> etree;
@basp1
basp1 / refal.pl
Last active July 31, 2016 11:23
simple (in 8 lines) Refal like pattern matching in Prolog
prefix([], [X|Xs], [X|Xs]).
prefix([X|Prefix], [X|List], Rest) :- prefix(Prefix, List, Rest).
refal([X | Pat], [X|Xs]) :- refal(Pat, Xs).
refal([e(X) | Pat], Xs) :- prefix(X, Xs, Rest), refal(Pat, Rest).
refal([e(X)], X).
refal([s(X) | Pat], [X|Xs]) :- refal(Pat, Xs).
refal([], []).
%% usage example
@basp1
basp1 / h.ijs
Last active August 29, 2015 14:21
dummy key-value list in J language
cocurrent 'H'
fst =: [: > 0&{
snd =: [: > 1&{
nop =: ''"_
pos =: 1: i.~ [: ; (([: # ]) = [: +/ (] = [: fst [) :: 0:) each
add =: ;~
unsafe_get =: [: snd ([: > [) {~ pos
get =: (unsafe_get <) :: nop
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <uv.h>
void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
{
buf->base = (char*)malloc(suggested_size);
buf->len = suggested_size;
}
@basp1
basp1 / cholupdate_norm.m
Last active August 29, 2015 14:24
rank-one update with normalization
% H = A'*A;
% D = diag(1 ./ sqrt(diag(H)));
% L = chol(D*H*D)';
% cholupd_norm(L, A_new_row, H)
%% http://math.stackexchange.com/a/1345569/61331
function [Lx] = cholupdate_norm(L, x, H)
n = 1 ./ sqrt(diag(H));
D = diag(n);
nx = 1 ./ sqrt(diag(H + x*x'));
@basp1
basp1 / cas-stack.lisp
Last active November 20, 2022 16:15
lock-free stack in common lisp
(defpackage :cas-stack
(:use :common-lisp)
(:export
:make-stack
:stack-push
:stack-pop))
(in-package :cas-stack)
;; https://github.com/zerth/cl-cas-queue/blob/master/cas-queue.lisp
(defun last-set-bit (value)
(if (zerop value)
-1
(let ((shifted-value (ash value -32)))
(if (not (zerop shifted-value))
(+ 32 (last-set-bit shifted-value))
(flet ((stepf (x b)
(if (>= value (ash 1 x))
(progn
(setf value (ash value (- x)))
import java.nio.ByteBuffer
object LEB128 {
def write(buffer: ByteBuffer, offset: Int, longValue: Long): Int = {
var writtenBytes: Int = 0
var value = longValue
if (value.abs <= 63) {
if (value < 0) {
value = value.abs
@basp1
basp1 / FindAllPaths.cs
Last active November 3, 2020 01:41
Find all paths between 2 graph nodes (iterative depth first search)
using System;
using System.Collections.Generic;
namespace CircuitSelector
{
public static class Graph
{
// use LinkedHashSet from http://stackoverflow.com/a/31419199/1312837 with some additions
public static List<List<int>> FindAllPaths(AdjacentMatrix adj, int begin, int end)
{
@basp1
basp1 / ensure.h
Created February 28, 2017 09:12
ensure macro
#pragma once
#include <exception>
#define DEBUG_STRINGIFY(x) #x
#define DEBUG_TO_STRING(x) DEBUG_STRINGIFY(x)
#define DEBUG_LOCATION "file: " __FILE__ ". line: " DEBUG_TO_STRING(__LINE__)
#define THROW_EXCEPTION(MESSAGE) throw std::exception(MESSAGE)