Skip to content

Instantly share code, notes, and snippets.

View djg's full-sized avatar
💭
I may be slow to respond.

Dan Glastonbury djg

💭
I may be slow to respond.
View GitHub Profile
int* bsearch(int val, int* base, int nelems)
{
if (base == NULL || nelems == 0)
return NULL;
int lo = 0;
int hi = nelems - 1;
for (;;)
{
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;
// Overloading operator, to allow custom list of objects to be passed
// eg: object obj()[, , , ];
struct list
{
...
list& operator,(list l);
...
};
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;
}
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;
}
enum Flags
{
Flag1 = 0x1,
Flag2 = 0x2,
Flag3 = 0x4,
Flag4 = 0x8
};
Flags operator|(Flags a, Flags b)
{
// 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;
}
// 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); };
// Yet another test. Also had a .obj along side.
#include <winsock2.h>
int
main()
{
u_short x = 1;
x = htons(x);
return x;
// 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]