Skip to content

Instantly share code, notes, and snippets.

View asterite's full-sized avatar

Ary Borenszweig asterite

  • NoRedInk
  • Buenos Aires, Argentina
View GitHub Profile
@asterite
asterite / SExpression parser
Created October 14, 2010 17:40
An S-Expression lexer/parser. Later i found out with regexps it's much faster to code and it actually works faster.
#!/usr/bin/ruby
class Lexer
def initialize(str)
@str = str
@p = 0
end
def next_token
while true
@asterite
asterite / query_count.rb
Created November 3, 2010 14:22
For Ruby on Rails. Put it in your config/initializers folder to print the number of queries executed in a controller action, and also raise an exception if that count exceeds a maximum
module QueryCount
Max = 10
module MysqlAdapterExtensions
def self.included(base)
base.alias_method_chain :execute, :review
end
def execute_with_review(sql, *args)
if Thread.current[:query_count] && ['select', 'insert', 'update', 'delete'].include?(sql[0 .. 6].downcase.strip)
@asterite
asterite / require_memory_tree.rb
Created September 9, 2011 13:40
Require memory tree
$current = {:path => "*ROOT*", :children => [], :mem => 2 ** 30}
if GC.respond_to?(:enable_stats)
module RequireTracking
def require(*args)
node = {:path => args, :children => []}
$current[:children] << node
prev, $current = $current, node
start_allocated_size = GC.allocated_size
@asterite
asterite / pathfinding.php
Created September 9, 2011 16:47
Path finding
<?
// Why do we need the nodes?
//$nodes = json_decode('[{"x":3,"y":6,"id":0},{"x":1,"y":1,"id":1},{"x":3,"y":1,"id":2},{"x":3,"y":4,"id":3},{"x":5,"y":1,"id":4},{"x":6,"y":1,"id":5},{"x":4,"y":3,"id":6},{"x":1,"y":6,"id":7},{"x":4,"y":6,"id":8},{"x":6,"y":6,"id":9},{"x":3,"y":9,"id":10},{"x":6,"y":11,"id":11}]');
$paths = json_decode('[[0,1],[0,2],[0,3],[3,2],[3,6],[6,4],[6,5],[3,9],[9,9],[9,10],[10,0],[10,11],[11,10],[10,8],[8,3],[3,1]]');
// Convert paths to a hash: from -> [to]
$newPaths = array();
foreach($paths as $path) {
if (!$newPaths[$path[0]]) $newPaths[$path[0]] = array();
array_push($newPaths[$path[0]], $path[1]);
@asterite
asterite / search.php
Created September 12, 2011 15:38
My searching algorithm
<?
class Dictionary {
function __construct($filename) {
$this->filename = $filename;
$this->sortedFilename = "${filename}.sorted";
}
function search($search) {
if (!file_exists($this->sortedFilename)) {
$this->sortWords();
@asterite
asterite / tree_to_list.php
Created September 12, 2011 19:22
My linked list
<?
class Tree {
function add($value) {
if (!isset($this->value)) {
$this->value = $value;
return;
}
if ($value < $this->value) {
@asterite
asterite / crypto.php
Created September 13, 2011 13:10
My crypto algorithm
<?
/*
* Encrypts any number into a 10-digit cypher.
* Oh, 10 here means 10 in a random base.
*/
class Encrypter
{
function encrypt($num)
{
$str = "";
@asterite
asterite / compress.php
Created September 13, 2011 14:27
My compression algorithm
<?
class Compressor {
function compress($input, $output) {
$input = fopen($input, 'r');
$output = fopen($output, 'w');
while($line = fread($input, 8)) {
$lineLength = strlen($line);
for($i = 0; $i < $lineLength; $i += 8) {
$n = 0;
@asterite
asterite / segfault.ll
Created September 25, 2011 21:36
llvm segfault
%Bar = type { i32 }
%Foo = type { %Bar* }
@.str5 = private constant [4 x i8] c"%d\0A\00", align 1
declare i32 @printf(i8* nocapture, ...) nounwind
define i32 @main() ssp {
entry:
%Bar.i.i.i.i = alloca %Bar, align 8
@asterite
asterite / rwanda.rb
Created March 6, 2012 18:29
Import Rwanda sites for InSTEDD's Resource Map
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require File.expand_path('../../config/environment', __FILE__)
require 'csv'
filename = File.expand_path('../FOSA-Table.csv', __FILE__)
rows = CSV.read filename
rows = rows[1 .. -1]