Skip to content

Instantly share code, notes, and snippets.

Here's an example of an algebraic approach to bitwise operations. I'm intentionally choosing something that is obvious from the programmer's point of view to see what it corresponds to algebraically.

We know that bitwise rotation of an n-bit word x can be done with left/shift shifts:

(x << 1) | (x >> (n-1)) = (x << 1) ^ (x >> (n-1)).

Algebraically, this means that the rotation operator C satisfies C = L + R^(n-1). Since C is a rotation it must satisfy C^n = 1, i.e. if we rotate n times we should end up where we started. This corresponds to the identity (L + R^(n-1))^n = 1.

@pervognsen
pervognsen / BTree.cpp
Created April 24, 2016 20:40
A B+-tree implementation with find, insert and delete in 176 lines of code.
enum { BMAX = 32, BMIN = BMAX / 2, BHEIGHT = 6 };
struct BNode {
uint32_t length;
Key keys[BMAX];
union {
BNode *children[BMAX];
Value values[BMAX];
};
};
anonymous
anonymous / bb_enum_reflection.h
Created November 23, 2015 21:58
#pragma once
// http://stackoverflow.com/questions/11761703/overloading-macro-on-number-of-arguments/11763277
// http://codecraft.co/2014/11/25/variadic-macros-tricks/
#define _BB_GET_NTH_ARG(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
#define BB_COUNT_VARARGS(...) _BB_GET_NTH_ARG("ignored", ##__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#define _BB_ENUM_10(x, ...) idx_ ##x, _BB_ENUM_9(__VA_ARGS__)\
One way to generate barycentric coordinates from a vertex stream is to use gl_VertexID. This requires that your vertices are ordered in the vertex buffer in a regular fashion as gl_VertexID is the vertex position in the buffer, so vertex optimization using an index buffer will likely break this.
Step 1 is to generate a vec3 in the vertex shader:
//VS
out vec3 ex_BarycentricCoords;
void main(void)
{
anonymous
anonymous / gist:7918813
Created December 11, 2013 21:31
A vertex buffer conceptually encodes a chunk of voxel data
which has some finite x/y extent, but fully covers z (where
is height).
Ignorning chunks which are at same x/y of the chunk viewer
is in, it's possible to skip two of the four n/s/e/w faces
in the chunk. (I.e. if the chunk is to the NW of viewer,
the N and W faces of the chunk cannot be visible.)
If we sort the faces in the index buffer to separate each
@rygorous
rygorous / glx.cpp
Created December 9, 2013 01:28
GLX
GLuint glx_compile_shader_source( GLenum type, char const * source )
{
if ( !source )
return 0;
// explicitly copy the source together so we can dump it as one string
size_t source_len = strlen( source );
char * fused_source = new char[shader_header_len + source_len + 1];
strcpy( fused_source, shader_header );
strcpy( fused_source + shader_header_len, source );
@ssylvan
ssylvan / rh_hash_table.hpp
Last active January 12, 2023 04:52
Quick'n'dirty Robin Hood hash table implementation. Note, I have implemented this algorithm before, with tons of tests etc. But *this* code was written specifically for the blog post at http://sebastiansylvan.com/post/robin-hood-hashing-should-be-your-default-hash-table-implementation/, it has not been extensively tested so there may be bugs (an…
#define USE_ROBIN_HOOD_HASH 1
#define USE_SEPARATE_HASH_ARRAY 1
template<class Key, class Value>
class hash_table
{
static const int INITIAL_SIZE = 256;
static const int LOAD_FACTOR_PERCENT = 90;
struct elem
@jsimmons
jsimmons / config.lua
Created September 14, 2010 08:32
Mongrel2 config loading, in lua
local chat_demo = Handler {
send_spec = 'tcp://127.0.0.1:9999';
send_ident = '8b7c7833-0932-4d9a-92c3-3b8c06d9b855';
recv_spec = 'tcp://127.0.0.1:9998';
recv_ident = '';
}
local chat_demo_dir = Dir {
base = 'static/chatdemo/';
index_file = 'index.html';