/*
 * bitset.h - Bit sets
 *
 * Copyright 2012 by Werner Almesberger
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#ifndef BITSET_H
#define	BITSET_H

#include <stdint.h>

#include "bitset.h"


struct bitset {
	uint64_t v;
};


static inline void bitset_zero(struct bitset *b)
{
	b->v = 0;
}


static inline void bitset_set(struct bitset *b, int n)
{
	b->v |= (uint64_t) 1 << n;
}


static inline void bitset_clear(struct bitset *b, int n)
{
	b->v &= ~((uint64_t) 1 << n);
}


static inline int bitset_get(const struct bitset *b, int n)
{
	return !!(b->v & ((uint64_t) 1 << n));
}


static inline int bitset_empty(const struct bitset *b)
{
	return !b->v;
}


static inline int bitset_first(const struct bitset *b)
{
	int i;

	for (i = 0; i != sizeof(b->v)*8; i++)
		if (b->v & ((uint64_t) 1 << i))
			return i;
	return -1;
}


static inline int bitset_last(const struct bitset *b)
{
	int i = sizeof(b->v)*8;

	while (--i)
		if (b->v & ((uint64_t) 1 << i))
			break;
	return i;
}


static inline int bitset_common(const struct bitset *a, const struct bitset *b)
{
	return (a->v & b->v) != 0;
}

#endif /* !BITSET_H */