Skip to content

Instantly share code, notes, and snippets.

@matteobertozzi
matteobertozzi / atomic-queue.c
Created May 20, 2012 16:30
Atomic Queue (DWCAS)
/* gcc atomic-queue.c -mcx16 */
/*
Copyright (c) 2011, Matteo Bertozzi
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
/* LRU cache
* gcc -Wall cache.c -lpthread && ./a.out
*/
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
struct cache_entry {
@matteobertozzi
matteobertozzi / balanced-groups.py
Created October 10, 2012 06:18
'Balanced' groups
#!/usr/bin/env python
def balanced_groups(ngroups, items):
items = sorted(items)
wgroups = [0] * ngroups
groups = [[] for _ in xrange(ngroups)]
g = 0
lo = 0
dir = 1
@matteobertozzi
matteobertozzi / varint.c
Created November 6, 2012 17:34
VarInt with with length in the head
/*
* Variable length int encoding. The 1st byte contains the number of bytes left.
* Compared to the 7bit vint-encoding, is worst on small numbers (< 21bit) but
* better with large numbers.
*
* 0: 11110000 -> 4 v7
* 1: 00000000 -> 8 (12) v7 (14)
* 2: 00000000 -> 8 (20) v7 (21)
* 3: 00000000 -> 8 (28) v7 (28)
* 4: 00000000 -> 8 (36) v7 (35)
import java.io.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.*;
public class HBaseFill {
private static void createTable(final HBaseAdmin admin, final byte[] tableName,
final byte[]... families) throws IOException {
@matteobertozzi
matteobertozzi / rwlock.c
Created March 19, 2013 13:17
Read/Write lock
struct rwlock {
volatile uint32_t state;
};
static void __rwlock_init (struct rwlock *lock) {
lock->state = 0;
}
static void __read_lock (struct rwlock *lock) {
unsigned int new_state;
@matteobertozzi
matteobertozzi / typedq.c
Created April 18, 2013 09:25
queue by type?
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
struct node {
struct node *onext;
struct node *cnext;
int type;
int value;
};
@matteobertozzi
matteobertozzi / guitar-scales.py
Last active August 14, 2020 01:14
guitar scales
#!/usr/bin/env python
NOTES = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#']
note_name = lambda n: NOTES[n % len(NOTES)]
SCALES = {
'Major (Ionian)': [2, 2, 1, 2, 2, 2, 1],
'Major Pentatonic': [2, 2, 3, 2, 3],
'Natural minor (Aeolian)' : [2,1,2,2,1,2,2],
@matteobertozzi
matteobertozzi / psql-srv.py
Created November 27, 2013 05:17
postgres "server" wire protocol example
# th30z@u1310:[Desktop]$ psql -h localhost -p 55432
# Password:
# psql (9.1.10, server 0.0.0)
# WARNING: psql version 9.1, server version 0.0.
# Some psql features might not work.
# Type "help" for help.
#
# th30z=> select foo;
# a | b
# ---+---
@matteobertozzi
matteobertozzi / sevugi.py
Last active April 7, 2023 03:00
SvgWriter / Chart
#!/usr/bin/env python
#
# Copyright 2014 Matteo Bertozzi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#