Skip to content

Instantly share code, notes, and snippets.

void encode(void)
{
int i, j, f1, x, y, r, s, bufferend, c;
for (i = 0; i < N - F; i++) buffer[i] = ' ';
for (i = N - F; i < N * 2; i++) {
if ((c = fgetc(infile)) == EOF) break;
buffer[i] = c; textcount++;
}
bufferend = i; r = N - F; s = 0;
#define EI 11 /* typically 10..13 */
#define EJ 4 /* typically 4..5 */
#define P 1 /* If match length <= P then output one character */
#define N (1 << EI) /* buffer size */
#define F ((1 << EJ) + P) /* lookahead buffer size */
int bit_buffer = 0, bit_mask = 128;
unsigned long codecount = 0, textcount = 0;
unsigned char buffer[N * 2];
FILE *infile, *outfile;
@davidreynolds
davidreynolds / boggle.c
Created June 4, 2012 00:00
Boggle Backtracking Algorithm
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define BIN_SEARCH(dict, size, needle, res, prefix) \
do { \
assert((dict) != NULL); \
assert((size) > 0); \
int l = 0, r = (size)-1; \
@davidreynolds
davidreynolds / simple_snapshot.c
Created August 13, 2011 05:54
Exploring copy-on-write snapshotting of a binary search tree
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#define RWRR S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
#define MAP_RW PROT_READ | PROT_WRITE
@davidreynolds
davidreynolds / level_order.c
Created July 15, 2011 05:12
print binary tree in level order proof-of-concept
/*
* XXX: This code might not work as-is. I'm cutting and pasting the relevant
* parts from a working implementation in my hash tree project that may or may
* not get pushed to github later. That's why I'm putting it here for reference.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/queue.h>
@davidreynolds
davidreynolds / gist:1084019
Created July 15, 2011 03:42
cg_annotate output for hashtree loading
24,522,286 1 1 16,348,190 0 0 0 0 0 while (i < node_count) {
16,348,188 0 0 8,174,094 0 0 8,174,094 0 0 z = (struct rbt_node_s *)pmap;
24,522,282 0 0 8,174,094 0 0 8,174,094 0 0 pmap += sizeof(struct rbt_node_s);
. . . . . . . . .
24,522,282 0 0 16,348,188 0 0 8,174,094 3,064,773 3,064,773 z->strkey = pmap;
57,218,658 1 1 24,522,282 510,781 510,781 8,174,094 0 0 pmap += z->vlen+1;
. . . . . . . . .
24,522,282 0 0 16,348,188 6,962,690 6,962,690 8,174,094 0 0 poff = *(long *)pmap;
24,522,282 0 0 8,174,094 0 0 8,174,094 0 0 pmap += sizeof(long);
24,522,282 1 1 16,348,188 1,021,156 1,021,156
;; Is this a good way to "remove" elements from a list? It just builds a new
;; list after filtering out the element I want removed.
(define (filter-out-fd fd)
;; builds a new list after filtering out fd
(let loop ((li fd-list) (newlist '()))
(if (null? li)
newlist
(if (eq? fd (car li))
(loop (cdr li) newlist)
(define (write-handler fd)
;; epoll tells us to write to socket
(let ((buf (hash-table-ref fd-write-table fd)))
(##net#write fd buf (string-length buf)))
;; write prompt to client after sending buf
(##net#write fd "> " 2)
;; clear out write buffer
(hash-table-set! fd-write-table fd "")
#include <sys/epoll.h>
#include <fcntl.h>
#include <string.h>
#include "chicken.h"
#define MAX_EVENTS 24
extern void SCM_epoll_wait_cb(C_word vec);
;; this function is defined in vector_of_pairs.c and used in this file
(define vector_of_pairs (foreign-safe-lambda void "vector_of_pairs" int))
;; define the scheme callback -- note that this is extern'd in vector_of_pairs.c
(define-external (scm_cb (scheme-object x)) void
(print x))
;; call C function to generate vector of pairs, passing N into it
(vector_of_pairs 10)