Skip to content

Instantly share code, notes, and snippets.

View peteb's full-sized avatar

Peter Backman peteb

View GitHub Profile
#!/usr/bin/env ruby
require 'erb'
require 'open3'
# == Code generation ====================================================================================
def scenario_multi_call(callsite_count)
ERB.new(<<-HERE).result(binding)
#include "continuable/continuable.hpp"
int hash(const char *data) {
const char *pos = data;
int bit_num = 1;
int xored = 0;
while (*pos) {
int value = (int)*pos << bit_num;
int msb = value >> 15;
value = (value & 0x7FFF) | msb;
#include <iostream>
int main() {
std::cout << &1["strump"] << std::endl;
}
def esc(text)
text.upcase
end
def esc_sub(text, patterns)
patterns.each do |pattern, replacement|
if text =~ /^(.*)#{pattern}(.*)$/
return [esc_sub($1, patterns), replacement, esc_sub($2, patterns)].join
end
end
nums = (1..3).to_a
nums.zip(nums.drop(1)).map { |_, x2| x2 } #=> [2, 3, nil]
@peteb
peteb / gist:6779698
Created October 1, 2013 14:50
Useful method for grouping items in an array
@implementation NSArray (FunctionalKit)
- (NSDictionary *)collectWithBlock:(id (^)(id value)) valueIdentifier {
NSMutableDictionary *dict = [NSMutableDictionary new];
for (id object in self) {
id ident = valueIdentifier(object);
NSMutableArray *array = [dict objectForKey:ident];
if (!array) {
@peteb
peteb / gist:2509025
Created April 27, 2012 13:05
Double-linked list with just one link field. xor-trick.
#include <stdio.h>
#include <inttypes.h>
typedef struct node {
void *link;
int value;
} node_t;
#define LINK(prev, next) (void *)((uintptr_t)(prev) ^ (uintptr_t)(next))
@peteb
peteb / gist:2509014
Created April 27, 2012 13:04
Burrows-Wheeler Transform. Should optimize it sometime...
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
namespace {
std::string rotl(const std::string &s) {
return s.substr(1) + s.at(0);
}
}
@peteb
peteb / mktest
Created November 29, 2011 15:26
Aliases for mkdir && cd. Quite common usage that is finally improved!
alias mkc "mkdir \!^ && cd \!^"
alias mkt 'cd `~/bin/mktest`'
@peteb
peteb / gist:1100155
Created July 22, 2011 19:05
Embedding C in Anita
<%
#include <stdio.h>
GLOBAL_SYM(add);
%>
val my_printer = fun(a) => <%|a|
printf("--> %p\n", a);
//RET(string_object("en katt"));
object_t params = array_object(1);