Skip to content

Instantly share code, notes, and snippets.

View betawaffle's full-sized avatar

Andrew Hodges betawaffle

View GitHub Profile
@betawaffle
betawaffle / gist:448519
Created June 22, 2010 14:21
Is n a prime number?
int is_prime(unsigned n) {
unsigned c;
if (n < 2) return 0;
for (c = 2; c < n; c++) {
if ((n % c) == 0) return 0;
}
return 1;
@betawaffle
betawaffle / trie.cpp
Created August 7, 2010 20:36
Bit Trie WIP (Old)
template <typename T>
class Node
{
unsigned char* mPrefix;
unsigned long mPrefixBits;
Node* mNodes[2];
T* mValue;
public:
@betawaffle
betawaffle / trie.cpp
Created August 8, 2010 22:33
Trie WIP
#include "trie.h"
template <typename T> Trie<T>::Node&
Trie<T>::Node::operator [](char const* aString)
{
Node* node;
unsigned long s, e, i, n;
unsigned char r, l, c, d;
#define X(a,b) typedef a b;
#define u unsigned
#define sT struct
#define R return
X(char,ch)X(void,V)X(ch const*,S)X(u char,C)X(u long,L)X(sT SN{C mO;L mL;C*mP;s\
T SN**mN;}*,N)V nfo(N t,L i,C d){}N nfi(N t,S aS){N no;L s,e,i,n;C r,l,c,d;s=t-\
>mO;e=t->mO+t->mL;i=s/8,r=s%8;n=e/8,l=e%8,l=8-l;for(c=aS[i]&0xFF>>r;i<=n;c=aS[+\
+i]){if(i==n)c>>=l,c<<=l;if(d=c^t->mP[i])nfo(t,i,d),e=t->mO+t->mL,n=e/8,l=e%8,l\
=8-l;}if(c=aS[n],c=='\0')R t;R nfi(t->mN[c>>l-1&0x01],aS+n);}int main(){R 0;}
@betawaffle
betawaffle / gist:536447
Created August 18, 2010 22:54
OpenAnonymous()
#define NUMBERS "0123456789"
#define LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
int
OpenAnonymous(char const* aFormat)
{
size_t i, s;
char* n, c;
int f;
@betawaffle
betawaffle / gist:537841
Created August 19, 2010 13:15
File::Debug()
#define IsReg(s) S_ISREG(s.st_mode)
#define IsDir(s) S_ISDIR(s.st_mode)
#define IsLnk(s) S_ISLNK(s.st_mode)
#define IsBlk(s) S_ISBLK(s.st_mode)
#define IsChr(s) S_ISCHR(s.st_mode)
#define IsFifo(s) S_ISFIFO(s.st_mode)
#define IsSock(s) S_ISSOCK(s.st_mode)
#if _POSIX_MESSAGE_PASSING >= 0
#define IsMsgQ(s) S_TYPEISMQ(s)
@betawaffle
betawaffle / gist:537843
Created August 19, 2010 13:17
File::NextCharacter() WIP
int
File::NextCharacter()
{
unsigned char b, e, s; /* [0-255], [0-8], [0-8] */
int c;
ssize_t i, r;
/*
* b (Byte) : Current byte.
* c (Character) : Current character. (32-bits for Unicode)
@betawaffle
betawaffle / gist:537853
Created August 19, 2010 13:21
Terminal Stuff
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
int
main(void)
{
struct termios iattr;
int ret;
#include <errno.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char* argv[])
{
char const* path;
char const* opts;
size_t
Recv()
{
char* buffer;
size_t length, l, i;
ssize_t n;
length = 512;
buffer = malloc(length);