Skip to content

Instantly share code, notes, and snippets.

@Groxx
Groxx / Groxx's Simple Queue in C minor.c
Created February 21, 2010 05:17
Simplest complete queue implementation I've yet seen in C.
/*
Implementation by Groxx, with absolutely no guarantees, so don't complain to me if it breaks stuff.
Feel free to use it for awesome, as needed. Apply liberally, and induce vomiting if you ingest large doses.
Note from #0d15eb: BREAKING CHANGE FROM #1d1057: this is now a generic storage, storing pointers to data. Manage your memory accordingly.
Note: now stores the "next" pointer prior to processing, allowing you to process and pop the first item in the list in one pass without losing your place in the queue (and possibly other shenanigans, this one was just handy for my uses so I changed it).
*/
typedef void * queue_data_type;
struct queue_item {
queue_data_type contents;
@Groxx
Groxx / AppleScript Identity Fail.scpt
Created February 22, 2010 18:47
Serious weirdness in AppleScript with the "contents" property.
tell application "AppleScript Editor"
set d to document 1
set str to "set d to document 1 of AppleScript Editor, and watch the shenanigans.
"
set str to str & ("d is equal to d == " & (d is equal to d) as text) & "
"
set str to (str & "d is equal to (contents of d) == " & (d is equal to (contents of d)) as text) & "
"
set str to str & "d is equal to (contents of (contents of d)) == " & (d is equal to (contents of (contents of d))) as text
# This code
array = [0,1,2,3,4,5,6,7,8,9]
array[3..7] = array[3..7].reverse
puts array.inspect
# Results in this output
# [0, 1, 2, 7, 6, 5, 4, 3, 8, 9]
@Groxx
Groxx / Prefix-Based Array Slicing.rb
Created April 29, 2010 04:53
Grab a portion of an array where the item number / index number has the same left-most digits as are passed.
class Array
# Returns a portion of an array in which the left-most numbers
# match the prefix. If 0 (or less) is passed, matches all
# single-digit numbers (same as [0..8]).
#
# === Warning
# This deals with the n-th items, <i><b>NOT</b></i> the n-th
# <i>indexes</i> (ie, it's 1 based, not 0 based), because it
# serves my needs currently, and allows zero to match all
<html>
<body>
google
gmail.com
youtube.com
blogspot.com
google-analytics.com/urchin.js
google-analytics.com/ga.js
googlesyndication.com
show_ads.js
Time.now # Tue Aug 17 21:37:22 -0500 2010
class Time
def self.now # "self." makes it class-level - ie, you can call "Time." to use.
"WIN"
end
end
Time.now # "WIN"
@Groxx
Groxx / WebDatabase.js
Created November 1, 2010 03:16
Core functionality of HTML5 WebDatabases
var db = openDatabase("short_name" // actual database name. Opens existing or creates.
, "1" // version number. *Must* be correct.
, "a long descriptiony thing" // I do not know where this is useful. Nothing seems to see it...
, 10 * 1024*1024); // for 10 megabytes
// also: db.readTransaction(); identical to below, but read-only access to the database.
db.transaction(function(tx){
tx.executeSql("select * from your_table where aColumn = ?"
, ["aParameter"] // ? + [data] guards against injection, and is the *only* safe way to do so. One data item per ?, in order.
, function(tx, results) { // success callback
@Groxx
Groxx / API.js
Created November 8, 2010 03:18
Fairly simple lacuna API caller + batch capabilities. Part of a much larger project.
var api = {
calls: 0
, requests: 0
, buildRequest: function buildRequest(methodName, args) {
if (args === undefined){args = [];}
var request = {jsonrpc: "2.0", method: methodName, params: []};
// Build a request based on a method.
// First non-value in args is assumed to be a success callback (so a fail callback can be specified without a success callback. just pass null.)
// Second function (and all subsequent) in args is assumed to be an error callback
gcc simvm.c
time ./a.out
857419840
real 0m0.374s
user 0m0.359s
sys 0m0.003s
------
.cstring
LC0:
.ascii "%d\12\0"
.text
.align 4,0x90
.globl _vm
_vm:
LFB14:
pushq %rbp
LCFI0: