Skip to content

Instantly share code, notes, and snippets.

View whyrusleeping's full-sized avatar

Whyrusleeping whyrusleeping

  • Mana
  • The Metaverse
View GitHub Profile
@whyrusleeping
whyrusleeping / for shortcut
Created October 5, 2012 19:53
My favorite Pre-processor hackery
#include <stdio.h>
#define DO(n) for(int i = 0; i < n; i++)
int main()
{
int arr[10];
DO(10)
arr[i] = 2 * i;
@whyrusleeping
whyrusleeping / for shortcut
Created October 6, 2012 00:55 — forked from travisperson/for shortcut
My favorite Pre-processor hackery
#include <stdio.h>
#define to ; i <=
#define DO(range) for(int i = range; i++)
int main()
{
int arr[10];
DO(0 to 9)
#include <stdio.h>
#define FILL(arr,n,eq) for(int i = 0; i < n; i++) arr[i] = eq;
#define to ; i <=
#define DO(range) for(int i = range; i++)
int main()
{
int myArray[30];
@whyrusleeping
whyrusleeping / example.c
Created October 22, 2012 16:46
Bit Fields and Unions
#include <stdio.h>
typedef struct a
{
unsigned short q : 4;
unsigned short w : 2;
unsigned short e : 4;
unsigned short r : 6;
}A;
try
{
mainsock = dynamic_cast<whatever>;
if(!mainsock)
throw ERROR_CODE;
}
catch(int er_code)
{
}
@whyrusleeping
whyrusleeping / kbNonblock.c
Created October 30, 2012 21:49
Non Blocking Keyboard Input (C++)
#include <termios.h>
#include <stdlib.h>
void RestoreKeyboardBlocking(struct termios *initial_settings)
{
tcsetattr(0, TCSANOW, initial_settings);
}
void SetKeyboardNonBlock(struct termios *initial_settings)
{
@whyrusleeping
whyrusleeping / revised algorithm in c
Created December 1, 2012 21:55
fuzzy string comp
public const float SCALE = 1.5;
public double compStr(string a, string b)
{
return (double)lcSubString2(a, b)/ Math.Pow((a.Length > b.Length ? a.Length : b.Length), SCALE);
}
public int lcSubString2(string _a, string _b)
{
@whyrusleeping
whyrusleeping / compins.cpp
Created December 3, 2012 07:32
Implicit Compilation (aka, shell scripting fun)
#if 0
#!/bin/sh
g++ compins.cpp -o compins
./compins
exit
#endif
#include <iostream>
using std::cout;
@whyrusleeping
whyrusleeping / gist:4201095
Created December 4, 2012 05:52
Huffman tree in java, kinda
import java.io.InputStream;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;
public class HuffmanTree {
private Map<Character, Integer> counts;
@whyrusleeping
whyrusleeping / gist:4575613
Created January 19, 2013 22:30
Go Mergesort
func merge(arr []int, start, mid, end int) {
var sorted = make([]int, (end - start) + 1)
sortPos := 0
leftPos := start
rightPos := mid + 1
for ;sortPos < len(sorted); {
if !(leftPos > mid || rightPos > end) {
if arr[leftPos] < arr[rightPos] {
sorted[sortPos] = arr[leftPos]