aboutsummaryrefslogtreecommitdiff
path: root/src/bitvec.c
diff options
context:
space:
mode:
authorjcid <devnull@localhost>2007-10-07 00:36:34 +0200
committerjcid <devnull@localhost>2007-10-07 00:36:34 +0200
commit93715c46a99c96d6c866968312691ec9ab0f6a03 (patch)
tree573f19ec6aa740844f53a7c0eb7114f04096bf64 /src/bitvec.c
Initial revision
Diffstat (limited to 'src/bitvec.c')
-rw-r--r--src/bitvec.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/bitvec.c b/src/bitvec.c
new file mode 100644
index 00000000..fc308fc6
--- /dev/null
+++ b/src/bitvec.c
@@ -0,0 +1,59 @@
+/*
+ * File: bitvec.c
+ *
+ * Copyright 2001 Jorge Arellano Cid <jcid@dillo.org>
+ *
+ * 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 3 of the License, or
+ * (at your option) any later version.
+ */
+
+/*
+ * A simple ADT for bit arrays
+ */
+
+#include "../dlib/dlib.h"
+#include "bitvec.h"
+
+
+/*
+ * Create a new bitvec with 'num_bits' size
+ */
+bitvec_t *a_Bitvec_new(int num_bits)
+{
+ bitvec_t *bvec = dNew(bitvec_t, 1);
+
+ bvec->vec = dNew0(uchar_t, num_bits/BVEC_SIZE + 1);
+ bvec->len = num_bits;
+ return bvec;
+}
+
+/*
+ * Free a bitvec
+ */
+void a_Bitvec_free(bitvec_t *bvec)
+{
+ if (bvec) {
+ dFree(bvec->vec);
+ dFree(bvec);
+ }
+}
+
+/*
+ * Get a bit
+ */
+int a_Bitvec_get_bit(bitvec_t *bvec, int pos)
+{
+ dReturn_val_if_fail (pos < bvec->len, 0);
+ return (bvec->vec[pos/BVEC_SIZE] & 1 << pos % BVEC_SIZE);
+}
+
+/*
+ * Set a bit
+ */
+void a_Bitvec_set_bit(bitvec_t *bvec, int pos)
+{
+ dReturn_if_fail (pos < bvec->len);
+ bvec->vec[pos/BVEC_SIZE] |= 1 << (pos % BVEC_SIZE);
+}