Last active
August 29, 2015 14:03
-
-
Save andersonsp/0b8a01bff24ef5829aec to your computer and use it in GitHub Desktop.
Simple utils, algorithms and data structures implemented in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <limit.h> /* for CHAR_BIT */ | |
#include <stdint.h> /* for uint32_t */ | |
typedef uint32_t word_t; | |
enum { BITS_PER_WORD = sizeof(word_t) * CHAR_BIT }; | |
#define WORD_OFFSET(b) ((b) / BITS_PER_WORD) | |
#define BIT_OFFSET(b) ((b) % BITS_PER_WORD) | |
void set_bit(word_t *words, int n) { | |
words[WORD_OFFSET(n)] |= (1 << BIT_OFFSET(n)); | |
} | |
void clear_bit(word_t *words, int n) { | |
words[WORD_OFFSET(n)] &= ~(1 << BIT_OFFSET(n)); | |
} | |
int get_bit(word_t *words, int n) { | |
word_t bit = words[WORD_OFFSET(n)] & (1 << BIT_OFFSET(n)); | |
return bit != 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
/* Debugger */ | |
#define note(S, ...) fprintf(stderr, \ | |
"\x1b[1m(%s:%d, %s)\x1b[0m\n \x1b[1m\x1b[90mnote:\x1b[0m " S "\n", \ | |
__FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__) | |
#define warn(S, ...) fprintf(stderr, \ | |
"\x1b[1m(%s:%d, %s)\x1b[0m\n \x1b[1m\x1b[33mwarning:\x1b[0m " S "\n", \ | |
__FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__) | |
#define errn(S, ...) fprintf(stderr, \ | |
"\x1b[1m(%s:%d, %s)\x1b[0m\n \x1b[1m\x1b[31merror:\x1b[0m " S "\n", \ | |
__FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); exit(1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* $Id: refcount.c,v 1.6 2009/11/06 23:40:38 haguenad Exp $ */ | |
#include <assert.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
typedef struct list list; | |
struct list { | |
int refcount; | |
void *value; | |
list *next; | |
}; | |
list *list_new (void *car, list *cdr); | |
void *list_car (list *l); | |
list *list_cdr (list *l); | |
void list_incr (list *l); | |
void list_decr (list *l); | |
void *cmalloc(size_t size) { | |
void *r = malloc(size); | |
assert(r != NULL); | |
return r; | |
} | |
list *list_new (void *car, list *cdr) { | |
list *l = cmalloc(sizeof *l); | |
l->value = car; | |
l->next = cdr; | |
l->refcount = 1; | |
return l; | |
} | |
void *list_car (list *l) { | |
return l->value; | |
} | |
list *list_cdr (list *l) { | |
return l->next; | |
} | |
void list_incr (list *l) { | |
++l->refcount; | |
} | |
void list_decr (list *l) { | |
if (l == NULL) { | |
return; | |
} | |
if (--l->refcount == 0) { | |
list_decr(l->next); | |
free(l); | |
} | |
} | |
list *list_map(void *(f)(void *), list *l) { | |
if (l == NULL) { | |
return l; | |
} else { | |
return list_new(f(list_car(l)), list_map(f, list_cdr(l))); | |
} | |
} | |
void list_print(void (p)(void *), list *l) { | |
int fst = 1; | |
putchar('['); | |
for (; l != NULL; l = list_cdr(l), fst = 0) { | |
if (!fst) { | |
putchar(','); | |
} | |
p(list_car(l)); | |
} | |
putchar(']'); | |
} | |
/* callbacks */ | |
void print_intp(void *elem) { | |
int *n = elem; | |
printf("%d", *n); | |
} | |
void print_listintp(void *elem) { | |
list *l = elem; | |
list_print(print_intp, l); | |
} | |
void *list_decrv(void *l) { | |
list_decr(l); | |
return NULL; | |
} | |
int main(void) { | |
int a = 1, b = 2, c = 7, d = 11; | |
{ | |
/* Liste d'entiers */ | |
list *l = list_new(&a, list_new(&b, list_new(&c, NULL))); | |
list_print(print_intp, l); | |
putchar('\n'); | |
list_decr(l); | |
} | |
{ | |
/* Liste de listes d'entiers */ | |
list *l = list_new(list_new(&a, NULL), | |
list_new(NULL, | |
list_new(list_new(&b, list_new(&c, NULL)), | |
list_new(list_new(&d, NULL), | |
NULL)))); | |
list_print(print_listintp, l); | |
putchar('\n'); | |
list_decr(list_map(list_decrv, l)); | |
list_decr(l); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment