[Mono-list] [PATCH] bitset doc
Dennis Haney
davh@davh.dk
02 Jun 2002 01:31:42 +0200
--=-=-=
I wrote some interface docs for bit set.
Was this unecessary for such a easy interface... Well, could you guess
that if you used mono_bitset_mem_new it is not freed even if
mono_bitset_free is called on it?
I found two places in the code where it could slip an errorcheck and
thus segv, corrected and tests added. Plus I corrected one test to
actually check return values were correct and deleted useless output.
[OT]:
Does anyone know why my messages seem to take hours to make it to the
list? And why does mailman fail to send acknowledgements that it
received my messages even when I said it should do so?
--
Dennis
use Inline C => qq{void p(char*g){printf("Just Another %s Hacker\n",g);}};p("Perl");
--=-=-=
Content-Disposition: attachment; filename=monobitset-doc.diff
Content-Description: bitset doc
Index: monobitset.c
===================================================================
RCS file: /mono/mono/mono/utils/monobitset.c,v
retrieving revision 1.5
diff -u -b -B -r1.5 monobitset.c
--- monobitset.c 1 Jun 2002 08:27:27 -0000 1.5
+++ monobitset.c 1 Jun 2002 23:25:29 -0000
@@ -17,11 +17,6 @@
guint32 data [MONO_ZERO_LEN_ARRAY];
};
-/*
- * Return the number of bytes required to hold the bitset.
- * Useful to allocate it on the stack or with mempool.
- * Use with mono_bitset_mem_new ().
- */
guint32
mono_bitset_alloc_size (guint32 max_size, guint32 flags) {
guint32 real_size = (max_size + BITS_PER_CHUNK - 1) / BITS_PER_CHUNK;
@@ -212,7 +207,7 @@
int bit;
int result, i;
- if (pos == -1) {
+ if (pos <= -1) {
j = 0;
bit = -1;
} else {
@@ -238,7 +233,7 @@
mono_bitset_find_last (MonoBitSet *set, gint pos) {
int j, bit, result, i;
- if (pos == -1)
+ if (pos <= -1)
pos = set->size - 1;
j = pos / BITS_PER_CHUNK;
@@ -359,7 +354,7 @@
return error;
error++;
- g_print("should be 33: %d\n", mono_bitset_find_first (set1, 0));
+ //g_print("should be 33: %d\n", mono_bitset_find_first (set1, 0));
if (mono_bitset_find_first (set1, 0) != 33)
return error;
@@ -369,6 +364,11 @@
return error;
error++;
+ /* test 5 */
+ if (mono_bitset_find_first (set1, -100) != 33)
+ return error;
+ error++;
+
if (mono_bitset_find_last (set1, -1) != 33)
return error;
error++;
@@ -377,10 +377,15 @@
return error;
error++;
+ if (mono_bitset_find_last (set1, -100) != 33)
+ return error;
+ error++;
+
if (mono_bitset_find_last (set1, 34) != 33)
return error;
error++;
+ /* test 10 */
if (!mono_bitset_test (set1, 33))
return error;
error++;
@@ -389,7 +394,6 @@
return error;
error++;
- /* test 10 */
set2 = mono_bitset_clone (set1, 0);
if (mono_bitset_count (set2) != 1)
return error;
@@ -405,6 +409,7 @@
return error;
error++;
+ /* test 15 */
set3 = mono_bitset_clone (set2, 0);
mono_bitset_union (set3, set1);
if (mono_bitset_count (set3) != (mono_bitset_size (set3) - 1))
@@ -431,17 +436,31 @@
count = 0;
for (i = mono_bitset_find_first (set4, -1); i != -1; i = mono_bitset_find_first (set4, i)) {
count ++;
- g_print ("count got: %d at %d\n", count, i);
+ switch (count) {
+ case 1:
+ if (i != 0)
+ return error;
+ break;
+ case 2:
+ if (i != 1)
+ return error;
+ break;
+ case 3:
+ if (i != 10)
+ return error;
+ break;
+ }
+ //g_print ("count got: %d at %d\n", count, i);
}
if (count != 3)
return error;
error++;
- g_print ("count passed\n");
if (mono_bitset_find_first (set4, -1) != 0)
return error;
error++;
+ /* 20 */
mono_bitset_set (set4, 31);
if (mono_bitset_find_first (set4, 10) != 31)
return error;
Index: monobitset.h
===================================================================
RCS file: /mono/mono/mono/utils/monobitset.h,v
retrieving revision 1.3
diff -u -b -B -r1.3 monobitset.h
--- monobitset.h 31 May 2002 09:47:59 -0000 1.3
+++ monobitset.h 1 Jun 2002 23:25:29 -0000
@@ -10,53 +10,204 @@
MONO_BITSET_DONT_FREE = 1
};
+/*
+ * mono_bitset_alloc_size:
+ * @max_size: The numer of bits you want to hold
+ * @flags: unused
+ *
+ * Return the number of bytes required to hold the bitset.
+ * Useful to allocate it on the stack or with mempool.
+ * Use with mono_bitset_mem_new ().
+ */
guint32 mono_bitset_alloc_size (guint32 max_size, guint32 flags);
+/*
+ * mono_bitset_new:
+ * @max_size: The numer of bits you want to hold
+ * @flags: bitfield of flags
+ *
+ * Return a malloc'ed bitset of size max_size.
+ */
MonoBitSet* mono_bitset_new (guint32 max_size, guint32 flags);
+/*
+ * mono_bitset_mem_new:
+ * @mem: The location the bitset is stored
+ * @max_size: The numer of bits you want to hold
+ * @flags: bitfield of flags
+ *
+ * Return mem, which is now a initialized bitset of size max_size. Is
+ * not freed even if called with mono_bitset_free.
+ */
MonoBitSet* mono_bitset_mem_new (gpointer mem, guint32 max_size, guint32 flags);
+/*
+ * mono_bitset_free:
+ * @set: bitset ptr to free
+ *
+ * Free bitset unless flags have MONO_BITSET_DONT_FREE set.
+ */
void mono_bitset_free (MonoBitSet *set);
+/*
+ * mono_bitset_set:
+ * @set: bitset ptr
+ * @pos: set bit at this pos
+ *
+ * Set bit at pos 'pos', counted from 0. g_return_if_fail is called if
+ * pos out of bounds.
+ */
void mono_bitset_set (MonoBitSet *set, guint32 pos);
+/*
+ * mono_bitset_test:
+ * @set: bitset ptr
+ * @pos: test bit at this pos
+ *
+ * Test bit at pos 'pos', counted from 0. g_return_if_fail is called if
+ * pos out of bounds.
+ */
int mono_bitset_test (MonoBitSet *set, guint32 pos);
+/*
+ * mono_bitset_clear:
+ * @set: bitset ptr
+ * @pos: unset bit at this pos
+ *
+ * Unset bit at pos 'pos', counted from 0. g_return_if_fail is called if
+ * pos out of bounds.
+ */
void mono_bitset_clear (MonoBitSet *set, guint32 pos);
+/*
+ * mono_bitset_clear_all:
+ * @set: bitset ptr
+ *
+ * Unset all bits.
+ */
void mono_bitset_clear_all (MonoBitSet *set);
+/*
+ * mono_bitset_invert:
+ * @set: bitset ptr
+ *
+ * Flip all bits.
+ */
void mono_bitset_invert (MonoBitSet *set);
+/*
+ * mono_bitset_size:
+ * @set: bitset ptr
+ *
+ * return number of bits this bitset can hold.
+ */
guint32 mono_bitset_size (MonoBitSet *set);
+/*
+ * mono_bitset_count:
+ * @set: bitset ptr
+ *
+ * return number of bits that is set.
+ */
guint32 mono_bitset_count (MonoBitSet *set);
/*
- * Find the first bit set _after_ (not including) pos.
+ * mono_bitset_find_first:
+ * @set: bitset ptr
+ * @pos: pos to search _after_ (not including)
+ *
+ * return pos of first set bit after pos. If pos < 0 begin search
+ * from start. Return -1 if all unset. Return -1 and use
+ * g_return_val_if_fail if pos out of bounds.
*/
int mono_bitset_find_first (MonoBitSet *set, gint pos);
-/* Equivalent to find_first (set, -1) but faster */
+
+/*
+ * mono_bitset_find_start:
+ * @set: bitset ptr
+ *
+ * Equivalent to find_first (set, -1) but faster
+ */
int mono_bitset_find_start (MonoBitSet *set);
/*
- * Find the first bit set _before_ (not including) pos.
- * Use -1 to start from the end.
+ * mono_bitset_find_last:
+ * @set: bitset ptr
+ * @pos: pos to search _before_ (not including)
+ *
+ * return pos of first set bit before pos. If pos < 0 search is
+ * started from the end. Return -1 if all unset. Return -1 and use
+ * g_return_val_if_fail if pos out of bounds.
*/
int mono_bitset_find_last (MonoBitSet *set, gint pos);
+/*
+ * mono_bitset_clone:
+ * @set: bitset ptr to clone
+ * @new_size: number of bits the cloned bitset can hold
+ *
+ * Return a malloc'ed close bitset of size new_size.
+ */
MonoBitSet* mono_bitset_clone (MonoBitSet *set, guint32 new_size);
+/*
+ * mono_bitset_copyto:
+ * @src: bitset ptr to copy from
+ * @dest: bitset ptr to copy to
+ *
+ * Copy one bitset to another. g_return_if_fail is called if dest is
+ * smaller than src.
+ */
void mono_bitset_copyto (MonoBitSet *src, MonoBitSet *dest);
+/*
+ * mono_bitset_union:
+ * @dest: bitset ptr to hold union
+ * @src: bitset ptr to copy
+ *
+ * Make union of one bitset and another, src is
+ * untouched. g_return_if_fail is called if src is smaller than dest.
+ */
void mono_bitset_union (MonoBitSet *dest, MonoBitSet *src);
+/*
+ * mono_bitset_intersection:
+ * @dest: bitset ptr to hold intersection
+ * @src: bitset ptr to copy
+ *
+ * Make intersection of one bitset and another, src is
+ * untouched. g_return_if_fail is called if src is smaller than dest.
+ */
void mono_bitset_intersection (MonoBitSet *dest, MonoBitSet *src);
+/*
+ * mono_bitset_sub:
+ * @dest: bitset ptr to hold bitset - src
+ * @src: bitset ptr to copy
+ *
+ * Unset all bits in dest, which are set in src, src is
+ * untouched. g_return_if_fail is called if src is smaller than dest.
+ */
void mono_bitset_sub (MonoBitSet *dest, MonoBitSet *src);
+/*
+ * mono_bitset_equal:
+ * @src: bitset ptr
+ * @src1: bitset ptr
+ *
+ * return TRUE if their size are the same and the same bits are set in both bitsets.
+ */
gboolean mono_bitset_equal (MonoBitSet *src, MonoBitSet *src1);
+/*
+ * mono_bitset_foreach:
+ * @set: bitset ptr
+ * @func: Function to call for every set bit
+ * @data: pass this as second arg to func
+ *
+ * Calls func for every bit set in bitset. Argument 1 is the number of
+ * the bit set, argument 2 is data
+ */
void mono_bitset_foreach (MonoBitSet *set, MonoBitSetFunc func, gpointer data);
#endif /* __MONO_BITSET_H__ */
--=-=-=--