Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
I may be slow to respond.

Dan Glastonbury djg

💭
I may be slow to respond.
View GitHub Profile
View bsearch
int* bsearch(int val, int* base, int nelems)
{
if (base == NULL || nelems == 0)
return NULL;
int lo = 0;
int hi = nelems - 1;
for (;;)
{
View bsearch2.c
int binarysearch(DataType t)
/* return (any) position
if t in sorted x[0..n-1] or
-1 if t is not present */
{
int l, u, m;
l = 0;
u = n-1;
while (l <= u) {
m = (l + u) / 2;
View theevil.cpp
// Overloading operator, to allow custom list of objects to be passed
// eg: object obj()[, , , ];
struct list
{
...
list& operator,(list l);
...
};
View selection_sort.cpp
void sort(int* A, int n)
{
for (int i = 0; i < n; ++i)
for (int j = i+1; j < n; ++j)
if (A[j] < A[i])
{
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
View selection_sort2.cpp
void sort2(int* A, int n)
{
for (int i = 0; i < n; ++i)
{
int min = i;
for (int j = i+1; j < n; ++j)
{
if (A[j] < A[min])
min = j;
}
View hack_enum.cpp
enum Flags
{
Flag1 = 0x1,
Flag2 = 0x2,
Flag3 = 0x4,
Flag4 = 0x8
};
Flags operator|(Flags a, Flags b)
{
View sorting.cpp
// Compile with cl /O2 sorting.cpp winmm.lib
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
void exch(int& a, int& b)
{
int tmp = a; a = b; b = tmp;
}
View test.cpp
// One of many test.cpp I found. I think this one was for examining the generated assembly.
#include "xmmintrin.h"
#if 0
struct Vec3
{
float x, y, z;
__forceinline Vec3() {};
__forceinline Vec3(float X, float Y, float Z) : x(X), y(Y), z(Z) {};
__forceinline Vec3 operator*(const Vec3& t) const { return Vec3(x*t.x, y*t.y, z*t.z); };
View test2.cpp
// Yet another test. Also had a .obj along side.
#include <winsock2.h>
int
main()
{
u_short x = 1;
x = htons(x);
return x;
View test3.cpp
// Another test. RGB->YCoCg conversion. Think I discovered a problem with negative
// values being returned on RGB->YCoCg->RGB.
#include <stdio.h>
struct IVect { int x, y, z; };
/*
* [Y ] = [ 1/4 1/2 1/4][R] R, G, B = [0..255] => Y = [0..255], Co, Cg = [-128..127]
* [Co] [ 1/2 0 -1/2][G]
* [Cg] [-1/4 1/2 -1/4][B]
You can’t perform that action at this time.