Dillo v3.1.1-111-gd4f56d0d
Loading...
Searching...
No Matches
bitvec.c
Go to the documentation of this file.
1/*
2 * File: bitvec.c
3 *
4 * Copyright 2001 Jorge Arellano Cid <jcid@dillo.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 */
11
16#include "../dlib/dlib.h"
17#include "bitvec.h"
18
19
23bitvec_t *a_Bitvec_new(int num_bits)
24{
25 bitvec_t *bvec = dNew(bitvec_t, 1);
26
27 bvec->vec = dNew0(uchar_t, num_bits/BVEC_SIZE + 1);
28 bvec->len = num_bits;
29 return bvec;
30}
31
36{
37 memset(bvec->vec, 0, sizeof(uchar_t) * bvec->len/BVEC_SIZE + 1);
38}
39
44{
45 if (bvec) {
46 dFree(bvec->vec);
47 dFree(bvec);
48 }
49}
50
54int a_Bitvec_get_bit(bitvec_t *bvec, int pos)
55{
56 dReturn_val_if_fail (pos < bvec->len, 0);
57 return (bvec->vec[pos/BVEC_SIZE] & 1 << pos % BVEC_SIZE);
58}
59
63void a_Bitvec_set_bit(bitvec_t *bvec, int pos)
64{
65 dReturn_if_fail (pos < bvec->len);
66 bvec->vec[pos/BVEC_SIZE] |= 1 << (pos % BVEC_SIZE);
67}
void a_Bitvec_clear(bitvec_t *bvec)
Clear a bitvec.
Definition bitvec.c:35
bitvec_t * a_Bitvec_new(int num_bits)
Create a new bitvec with 'num_bits' size.
Definition bitvec.c:23
void a_Bitvec_set_bit(bitvec_t *bvec, int pos)
Set a bit.
Definition bitvec.c:63
void a_Bitvec_free(bitvec_t *bvec)
Free a bitvec.
Definition bitvec.c:43
int a_Bitvec_get_bit(bitvec_t *bvec, int pos)
Get a bit.
Definition bitvec.c:54
#define BVEC_SIZE
Definition bitvec.h:7
unsigned char uchar_t
Definition d_size.h:17
void dFree(void *mem)
Definition dlib.c:68
#define dReturn_if_fail(expr)
Definition dlib.h:72
#define dNew0(type, count)
Definition dlib.h:51
#define dReturn_val_if_fail(expr, val)
Definition dlib.h:76
#define dNew(type, count)
Definition dlib.h:49
BVEC_TYPE * vec
Definition bitvec.h:10
int len
Definition bitvec.h:11