Skip to content

Instantly share code, notes, and snippets.

View peteb's full-sized avatar

Peter Backman peteb

View GitHub Profile
@peteb
peteb / filter.cpp
Created November 9, 2010 16:03
Implements a simple filter function in C++
#include <vector>
#include <iostream>
#include <functional>
#include <iterator>
template <class Functor>
class Not
{
public:
Not(Functor & f) : func(f) {}
@peteb
peteb / ostream_cast_iterator.cpp
Created November 16, 2010 13:00
An iterator that casts the input data to another type. Note: std::transform should probably be used in most cases
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <iterator>
template <class T, class C, class charT=char, class traits=std::char_traits<charT> >
class ostream_cast_iterator :
public std::iterator<std::output_iterator_tag, void, void, void, void>
{
@peteb
peteb / gist:964164
Created May 10, 2011 09:28
C++ templates for vector classes (vec3, color4, float2, etc)
#include <iostream>
#include <ctime>
#include <iterator>
// old stuff
template<typename DataType, int NumComponents, typename Derived>
struct composite_base {
typedef DataType value_type;
static const int num_components = NumComponents;
@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);
@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: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 / 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: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) {
nums = (1..3).to_a
nums.zip(nums.drop(1)).map { |_, x2| x2 } #=> [2, 3, nil]
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