one-file-projects/bandmatrix.c

64 lines
1.3 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <assert.h>
#define GET_CLOCK() ((float)clock() / CLOCKS_PER_SEC)
typedef uint32_t Node;
typedef struct {
uint8_t* edges;
uint32_t const node_count;
} Bandmatrix;
Bandmatrix bm_Create(uint32_t node_count) {
uint8_t* edges = malloc(sizeof(uint8_t) * node_count * node_count);
for(uint32_t i = 0; i < node_count*node_count; i++) {
edges[i] = 0;
}
return (Bandmatrix){ edges, node_count };
}
void bm_AddEdge(Bandmatrix b, Node nodefrom, Node nodeto) {
b.edges[nodefrom * b.node_count + nodeto] = 1;
}
void bm_RemoveEdge(Bandmatrix b, Node nodefrom, Node nodeto) {
b.edges[nodefrom * b.node_count + nodeto] = 0;
}
int bm_TestEdge(Bandmatrix b, Node nodefrom, Node nodeto) {
return b.edges[nodefrom * b.node_count + nodeto];
}
void bm_Free(Bandmatrix b) {
free(b.edges);
}
int main( int argc, char *argv[] ) {
Bandmatrix b = bm_Create(1000);
float start, end;
start = GET_CLOCK();
for(Node i = 0; i < 1000; i++) {
bm_AddEdge(b,i,i);
for(Node j = i+1; j < i+49 && j < 1000; j++) {
bm_AddEdge(b,i,j);
bm_AddEdge(b,j,i);
}
}
end = GET_CLOCK();
printf("%.3f msecs\n", (end-start) * 1e3);
bm_Free(b);
return 0;
}