1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
Handling of unsigned char arrays as large bitmaps.
19
API limitations (or, rather asserted safety assumptions,
20
to encourage correct programming)
22
* the internal size is a set of 32 bit words
23
* the number of bits specified in creation can be any number > 0
24
* there are THREAD safe versions of most calls called bitmap_lock_*
25
many of those are not used and not compiled normally but the code
26
already exist for them in an #ifdef:ed part. These can only be used
27
if THREAD was specified in bitmap_init
30
Make assembler THREAD safe versions of these using test-and-set instructions
32
Original version created by Sergei Golubchik 2001 - 2004.
33
New version written and test program added and some changes to the interface
34
was made by Mikael Ronström 2005, with assistance of Tomas Ulin and Mats
38
#include "mysys_priv.h"
39
#include <mysys/my_bitmap.h>
40
#include <mystrings/m_string.h>
41
#include <mysys/my_bit.h>
43
void create_last_word_mask(MY_BITMAP *map)
45
/* Get the number of used bits (1..8) in the last byte */
46
unsigned int const used= 1U + ((map->n_bits-1U) & 0x7U);
49
Create a mask with the upper 'unused' bits set and the lower 'used'
50
bits clear. The bits within each byte is stored in big-endian order.
52
unsigned char const mask= (~((1 << used) - 1)) & 255;
55
The first bytes are to be set to zero since they represent real bits
56
in the bitvector. The last bytes are set to 0xFF since they represent
57
bytes not used by the bitvector. Finally the last byte contains bits
58
as set by the mask above.
60
unsigned char *ptr= (unsigned char*)&map->last_word_mask;
62
map->last_word_ptr= map->bitmap + no_words_in_map(map)-1;
63
switch (no_bytes_in_map(map) & 3) {
65
map->last_word_mask= UINT32_MAX;
69
map->last_word_mask= UINT32_MAX;
74
map->last_word_mask= 0;
79
map->last_word_mask= 0U;
86
static inline void bitmap_lock(MY_BITMAP *map)
89
pthread_mutex_lock(map->mutex);
92
static inline void bitmap_unlock(MY_BITMAP *map)
95
pthread_mutex_unlock(map->mutex);
99
bool bitmap_init(MY_BITMAP *map, my_bitmap_map *buf, uint32_t n_bits, bool thread_safe)
103
uint32_t size_in_bytes= bitmap_buffer_size(n_bits);
107
size_in_bytes= ALIGN_SIZE(size_in_bytes);
108
extra= sizeof(pthread_mutex_t);
111
if (!(buf= (my_bitmap_map*) malloc(size_in_bytes+extra)))
115
map->mutex= (pthread_mutex_t *) ((char*) buf + size_in_bytes);
116
pthread_mutex_init(map->mutex, MY_MUTEX_INIT_FAST);
121
assert(thread_safe == 0);
126
create_last_word_mask(map);
127
bitmap_clear_all(map);
132
void bitmap_free(MY_BITMAP *map)
137
pthread_mutex_destroy(map->mutex);
138
free((char*) map->bitmap);
146
test if bit already set and set it if it was not (thread unsafe method)
149
bitmap_fast_test_and_set()
158
bool bitmap_fast_test_and_set(MY_BITMAP *map, uint32_t bitmap_bit)
160
unsigned char *value= ((unsigned char*) map->bitmap) + (bitmap_bit / 8);
161
unsigned char bit= 1 << ((bitmap_bit) & 7);
162
unsigned char res= (*value) & bit;
169
test if bit already set and set it if it was not (thread safe method)
172
bitmap_fast_test_and_set()
174
bitmap_bit bit number
181
bool bitmap_test_and_set(MY_BITMAP *map, uint32_t bitmap_bit)
184
assert(map->bitmap && bitmap_bit < map->n_bits);
186
res= bitmap_fast_test_and_set(map, bitmap_bit);
192
test if bit already set and clear it if it was set(thread unsafe method)
195
bitmap_fast_test_and_set()
204
bool bitmap_fast_test_and_clear(MY_BITMAP *map, uint32_t bitmap_bit)
206
unsigned char *byte= (unsigned char*) map->bitmap + (bitmap_bit / 8);
207
unsigned char bit= 1 << ((bitmap_bit) & 7);
208
unsigned char res= (*byte) & bit;
214
bool bitmap_test_and_clear(MY_BITMAP *map, uint32_t bitmap_bit)
217
assert(map->bitmap && bitmap_bit < map->n_bits);
219
res= bitmap_fast_test_and_clear(map, bitmap_bit);
225
uint32_t bitmap_set_next(MY_BITMAP *map)
229
if ((bit_found= bitmap_get_first(map)) != MY_BIT_NONE)
230
bitmap_set_bit(map, bit_found);
235
void bitmap_set_prefix(MY_BITMAP *map, uint32_t prefix_size)
237
uint32_t prefix_bytes, prefix_bits, d;
238
unsigned char *m= (unsigned char *)map->bitmap;
240
assert(map->bitmap &&
241
(prefix_size <= map->n_bits || prefix_size == UINT32_MAX));
242
set_if_smaller(prefix_size, map->n_bits);
243
if ((prefix_bytes= prefix_size / 8))
244
memset(m, 0xff, prefix_bytes);
246
if ((prefix_bits= prefix_size & 7))
247
*m++= (1 << prefix_bits)-1;
248
if ((d= no_bytes_in_map(map)-prefix_bytes))
253
bool bitmap_is_prefix(const MY_BITMAP *map, uint32_t prefix_size)
255
uint32_t prefix_bits= prefix_size & 0x7, res;
256
unsigned char *m= (unsigned char*)map->bitmap;
257
unsigned char *end_prefix= m+prefix_size/8;
259
assert(m && prefix_size <= map->n_bits);
260
end= m+no_bytes_in_map(map);
262
while (m < end_prefix)
266
*map->last_word_ptr&= ~map->last_word_mask; /*Clear bits*/
268
if (prefix_bits && *m++ != (1 << prefix_bits)-1)
280
bool bitmap_is_set_all(const MY_BITMAP *map)
282
my_bitmap_map *data_ptr= map->bitmap;
283
my_bitmap_map *end= map->last_word_ptr;
284
*map->last_word_ptr |= map->last_word_mask;
285
for (; data_ptr <= end; data_ptr++)
286
if (*data_ptr != 0xFFFFFFFF)
292
bool bitmap_is_clear_all(const MY_BITMAP *map)
294
my_bitmap_map *data_ptr= map->bitmap;
296
if (*map->last_word_ptr & ~map->last_word_mask)
298
end= map->last_word_ptr;
299
for (; data_ptr < end; data_ptr++)
305
/* Return true if map1 is a subset of map2 */
307
bool bitmap_is_subset(const MY_BITMAP *map1, const MY_BITMAP *map2)
309
my_bitmap_map *m1= map1->bitmap, *m2= map2->bitmap, *end;
311
assert(map1->bitmap && map2->bitmap &&
312
map1->n_bits==map2->n_bits);
314
end= map1->last_word_ptr;
315
*map1->last_word_ptr &= ~map1->last_word_mask;
316
*map2->last_word_ptr &= ~map2->last_word_mask;
319
if ((*m1++) & ~(*m2++))
325
/* True if bitmaps has any common bits */
327
bool bitmap_is_overlapping(const MY_BITMAP *map1, const MY_BITMAP *map2)
329
my_bitmap_map *m1= map1->bitmap, *m2= map2->bitmap, *end;
331
assert(map1->bitmap && map2->bitmap &&
332
map1->n_bits==map2->n_bits);
334
end= map1->last_word_ptr;
335
*map1->last_word_ptr &= ~map1->last_word_mask;
336
*map2->last_word_ptr &= ~map2->last_word_mask;
339
if ((*m1++) & (*m2++))
346
void bitmap_intersect(MY_BITMAP *map, const MY_BITMAP *map2)
348
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end;
349
uint32_t len= no_words_in_map(map), len2 = no_words_in_map(map2);
351
assert(map->bitmap && map2->bitmap);
353
end= to+cmin(len,len2);
354
*map2->last_word_ptr&= ~map2->last_word_mask; /*Clear last bits in map2*/
368
Set/clear all bits above a bit.
372
map RETURN The bitmap to change.
373
from_byte The bitmap buffer byte offset to start with.
374
use_bit The bit value (1/0) to use for all upper bits.
377
You can only set/clear full bytes.
378
The function is meant for the situation that you copy a smaller bitmap
379
to a bigger bitmap. Bitmap lengths are always multiple of eigth (the
380
size of a byte). Using 'from_byte' saves multiplication and division
381
by eight during parameter passing.
387
void bitmap_set_above(MY_BITMAP *map, uint32_t from_byte, uint32_t use_bit)
389
unsigned char use_byte= use_bit ? 0xff : 0;
390
unsigned char *to= (unsigned char *)map->bitmap + from_byte;
391
unsigned char *end= (unsigned char *)map->bitmap + (map->n_bits+7)/8;
398
void bitmap_subtract(MY_BITMAP *map, const MY_BITMAP *map2)
400
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end;
401
assert(map->bitmap && map2->bitmap &&
402
map->n_bits==map2->n_bits);
404
end= map->last_word_ptr;
411
void bitmap_union(MY_BITMAP *map, const MY_BITMAP *map2)
413
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end;
415
assert(map->bitmap && map2->bitmap &&
416
map->n_bits==map2->n_bits);
417
end= map->last_word_ptr;
424
void bitmap_xor(MY_BITMAP *map, const MY_BITMAP *map2)
426
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end= map->last_word_ptr;
427
assert(map->bitmap && map2->bitmap &&
428
map->n_bits==map2->n_bits);
434
void bitmap_invert(MY_BITMAP *map)
436
my_bitmap_map *to= map->bitmap, *end;
439
end= map->last_word_ptr;
446
uint32_t bitmap_bits_set(const MY_BITMAP *map)
448
unsigned char *m= (unsigned char*)map->bitmap;
449
unsigned char *end= m + no_bytes_in_map(map);
453
*map->last_word_ptr&= ~map->last_word_mask; /*Reset last bits to zero*/
455
res+= my_count_bits_uint16(*m++);
460
void bitmap_copy(MY_BITMAP *map, const MY_BITMAP *map2)
462
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end;
464
assert(map->bitmap && map2->bitmap &&
465
map->n_bits==map2->n_bits);
466
end= map->last_word_ptr;
472
uint32_t bitmap_get_first_set(const MY_BITMAP *map)
474
unsigned char *byte_ptr;
476
my_bitmap_map *data_ptr, *end= map->last_word_ptr;
479
data_ptr= map->bitmap;
480
*map->last_word_ptr &= ~map->last_word_mask;
482
for (i=0; data_ptr <= end; data_ptr++, i++)
486
byte_ptr= (unsigned char*)data_ptr;
487
for (j=0; ; j++, byte_ptr++)
493
if (*byte_ptr & (1 << k))
494
return (i*32) + (j*8) + k;
504
uint32_t bitmap_get_first(const MY_BITMAP *map)
506
unsigned char *byte_ptr;
508
my_bitmap_map *data_ptr, *end= map->last_word_ptr;
511
data_ptr= map->bitmap;
512
*map->last_word_ptr|= map->last_word_mask;
514
for (i=0; data_ptr <= end; data_ptr++, i++)
516
if (*data_ptr != 0xFFFFFFFF)
518
byte_ptr= (unsigned char*)data_ptr;
519
for (j=0; ; j++, byte_ptr++)
521
if (*byte_ptr != 0xFF)
525
if (!(*byte_ptr & (1 << k)))
526
return (i*32) + (j*8) + k;
536
uint32_t bitmap_lock_set_next(MY_BITMAP *map)
540
bit_found= bitmap_set_next(map);
546
void bitmap_lock_clear_bit(MY_BITMAP *map, uint32_t bitmap_bit)
549
assert(map->bitmap && bitmap_bit < map->n_bits);
550
bitmap_clear_bit(map, bitmap_bit);
556
bool bitmap_lock_is_prefix(const MY_BITMAP *map, uint32_t prefix_size)
559
bitmap_lock((MY_BITMAP *)map);
560
res= bitmap_is_prefix(map, prefix_size);
561
bitmap_unlock((MY_BITMAP *)map);
566
void bitmap_lock_set_all(MY_BITMAP *map)
574
void bitmap_lock_clear_all(MY_BITMAP *map)
577
bitmap_clear_all(map);
582
void bitmap_lock_set_prefix(MY_BITMAP *map, uint32_t prefix_size)
585
bitmap_set_prefix(map, prefix_size);
590
bool bitmap_lock_is_clear_all(const MY_BITMAP *map)
593
bitmap_lock((MY_BITMAP *)map);
594
res= bitmap_is_clear_all(map);
595
bitmap_unlock((MY_BITMAP *)map);
600
bool bitmap_lock_is_set_all(const MY_BITMAP *map)
603
bitmap_lock((MY_BITMAP *)map);
604
res= bitmap_is_set_all(map);
605
bitmap_unlock((MY_BITMAP *)map);
610
bool bitmap_lock_is_set(const MY_BITMAP *map, uint32_t bitmap_bit)
613
assert(map->bitmap && bitmap_bit < map->n_bits);
614
bitmap_lock((MY_BITMAP *)map);
615
res= bitmap_is_set(map, bitmap_bit);
616
bitmap_unlock((MY_BITMAP *)map);
621
bool bitmap_lock_is_subset(const MY_BITMAP *map1, const MY_BITMAP *map2)
624
bitmap_lock((MY_BITMAP *)map1);
625
bitmap_lock((MY_BITMAP *)map2);
626
res= bitmap_is_subset(map1, map2);
627
bitmap_unlock((MY_BITMAP *)map2);
628
bitmap_unlock((MY_BITMAP *)map1);
633
bool bitmap_lock_cmp(const MY_BITMAP *map1, const MY_BITMAP *map2)
637
assert(map1->bitmap && map2->bitmap &&
638
map1->n_bits==map2->n_bits);
639
bitmap_lock((MY_BITMAP *)map1);
640
bitmap_lock((MY_BITMAP *)map2);
641
res= bitmap_cmp(map1, map2);
642
bitmap_unlock((MY_BITMAP *)map2);
643
bitmap_unlock((MY_BITMAP *)map1);
648
void bitmap_lock_intersect(MY_BITMAP *map, const MY_BITMAP *map2)
651
bitmap_lock((MY_BITMAP *)map2);
652
bitmap_intersect(map, map2);
653
bitmap_unlock((MY_BITMAP *)map2);
658
void bitmap_lock_subtract(MY_BITMAP *map, const MY_BITMAP *map2)
661
bitmap_lock((MY_BITMAP *)map2);
662
bitmap_subtract(map, map2);
663
bitmap_unlock((MY_BITMAP *)map2);
668
void bitmap_lock_union(MY_BITMAP *map, const MY_BITMAP *map2)
671
bitmap_lock((MY_BITMAP *)map2);
672
bitmap_union(map, map2);
673
bitmap_unlock((MY_BITMAP *)map2);
683
Number of set bits in the bitmap.
685
uint32_t bitmap_lock_bits_set(const MY_BITMAP *map)
688
bitmap_lock((MY_BITMAP *)map);
690
res= bitmap_bits_set(map);
691
bitmap_unlock((MY_BITMAP *)map);
701
Number of first unset bit in the bitmap or MY_BIT_NONE if all bits are set.
703
uint32_t bitmap_lock_get_first(const MY_BITMAP *map)
706
bitmap_lock((MY_BITMAP*)map);
707
res= bitmap_get_first(map);
708
bitmap_unlock((MY_BITMAP*)map);
713
uint32_t bitmap_lock_get_first_set(const MY_BITMAP *map)
716
bitmap_lock((MY_BITMAP*)map);
717
res= bitmap_get_first_set(map);
718
bitmap_unlock((MY_BITMAP*)map);
723
void bitmap_lock_set_bit(MY_BITMAP *map, uint32_t bitmap_bit)
725
assert(map->bitmap && bitmap_bit < map->n_bits);
727
bitmap_set_bit(map, bitmap_bit);
732
void bitmap_lock_flip_bit(MY_BITMAP *map, uint32_t bitmap_bit)
734
assert(map->bitmap && bitmap_bit < map->n_bits);
736
bitmap_flip_bit(map, bitmap_bit);
742
uint32_t get_rand_bit(uint32_t bitsize)
744
return (rand() % bitsize);
747
bool test_set_get_clear_bit(MY_BITMAP *map, uint32_t bitsize)
749
uint32_t i, test_bit;
750
uint32_t no_loops= bitsize > 128 ? 128 : bitsize;
751
for (i=0; i < no_loops; i++)
753
test_bit= get_rand_bit(bitsize);
754
bitmap_set_bit(map, test_bit);
755
if (!bitmap_is_set(map, test_bit))
757
bitmap_clear_bit(map, test_bit);
758
if (bitmap_is_set(map, test_bit))
763
printf("Error in set bit, bit %u, bitsize = %u", test_bit, bitsize);
766
printf("Error in clear bit, bit %u, bitsize = %u", test_bit, bitsize);
770
bool test_flip_bit(MY_BITMAP *map, uint32_t bitsize)
772
uint32_t i, test_bit;
773
uint32_t no_loops= bitsize > 128 ? 128 : bitsize;
774
for (i=0; i < no_loops; i++)
776
test_bit= get_rand_bit(bitsize);
777
bitmap_flip_bit(map, test_bit);
778
if (!bitmap_is_set(map, test_bit))
780
bitmap_flip_bit(map, test_bit);
781
if (bitmap_is_set(map, test_bit))
786
printf("Error in flip bit 1, bit %u, bitsize = %u", test_bit, bitsize);
789
printf("Error in flip bit 2, bit %u, bitsize = %u", test_bit, bitsize);
793
bool test_operators(MY_BITMAP *, uint32_t)
798
bool test_get_all_bits(MY_BITMAP *map, uint32_t bitsize)
802
if (!bitmap_is_set_all(map))
804
if (!bitmap_is_prefix(map, bitsize))
806
bitmap_clear_all(map);
807
if (!bitmap_is_clear_all(map))
809
if (!bitmap_is_prefix(map, 0))
811
for (i=0; i<bitsize;i++)
812
bitmap_set_bit(map, i);
813
if (!bitmap_is_set_all(map))
815
for (i=0; i<bitsize;i++)
816
bitmap_clear_bit(map, i);
817
if (!bitmap_is_clear_all(map))
821
printf("Error in set_all, bitsize = %u", bitsize);
824
printf("Error in clear_all, bitsize = %u", bitsize);
827
printf("Error in bitmap_is_set_all, bitsize = %u", bitsize);
830
printf("Error in bitmap_is_clear_all, bitsize = %u", bitsize);
833
printf("Error in set_all through set_prefix, bitsize = %u", bitsize);
836
printf("Error in clear_all through set_prefix, bitsize = %u", bitsize);
840
bool test_compare_operators(MY_BITMAP *map, uint32_t bitsize)
842
uint32_t i, j, test_bit1, test_bit2, test_bit3,test_bit4;
843
uint32_t no_loops= bitsize > 128 ? 128 : bitsize;
844
MY_BITMAP map2_obj, map3_obj;
845
MY_BITMAP *map2= &map2_obj, *map3= &map3_obj;
846
my_bitmap_map map2buf[1024];
847
my_bitmap_map map3buf[1024];
848
bitmap_init(&map2_obj, map2buf, bitsize, false);
849
bitmap_init(&map3_obj, map3buf, bitsize, false);
850
bitmap_clear_all(map2);
851
bitmap_clear_all(map3);
852
for (i=0; i < no_loops; i++)
854
test_bit1=get_rand_bit(bitsize);
855
bitmap_set_prefix(map, test_bit1);
856
test_bit2=get_rand_bit(bitsize);
857
bitmap_set_prefix(map2, test_bit2);
858
bitmap_intersect(map, map2);
859
test_bit3= test_bit2 < test_bit1 ? test_bit2 : test_bit1;
860
bitmap_set_prefix(map3, test_bit3);
861
if (!bitmap_cmp(map, map3))
863
bitmap_clear_all(map);
864
bitmap_clear_all(map2);
865
bitmap_clear_all(map3);
866
test_bit1=get_rand_bit(bitsize);
867
test_bit2=get_rand_bit(bitsize);
868
test_bit3=get_rand_bit(bitsize);
869
bitmap_set_prefix(map, test_bit1);
870
bitmap_set_prefix(map2, test_bit2);
871
test_bit3= test_bit2 > test_bit1 ? test_bit2 : test_bit1;
872
bitmap_set_prefix(map3, test_bit3);
873
bitmap_union(map, map2);
874
if (!bitmap_cmp(map, map3))
876
bitmap_clear_all(map);
877
bitmap_clear_all(map2);
878
bitmap_clear_all(map3);
879
test_bit1=get_rand_bit(bitsize);
880
test_bit2=get_rand_bit(bitsize);
881
test_bit3=get_rand_bit(bitsize);
882
bitmap_set_prefix(map, test_bit1);
883
bitmap_set_prefix(map2, test_bit2);
884
bitmap_xor(map, map2);
885
test_bit3= test_bit2 > test_bit1 ? test_bit2 : test_bit1;
886
test_bit4= test_bit2 < test_bit1 ? test_bit2 : test_bit1;
887
bitmap_set_prefix(map3, test_bit3);
888
for (j=0; j < test_bit4; j++)
889
bitmap_clear_bit(map3, j);
890
if (!bitmap_cmp(map, map3))
892
bitmap_clear_all(map);
893
bitmap_clear_all(map2);
894
bitmap_clear_all(map3);
895
test_bit1=get_rand_bit(bitsize);
896
test_bit2=get_rand_bit(bitsize);
897
test_bit3=get_rand_bit(bitsize);
898
bitmap_set_prefix(map, test_bit1);
899
bitmap_set_prefix(map2, test_bit2);
900
bitmap_subtract(map, map2);
901
if (test_bit2 < test_bit1)
903
bitmap_set_prefix(map3, test_bit1);
904
for (j=0; j < test_bit2; j++)
905
bitmap_clear_bit(map3, j);
907
if (!bitmap_cmp(map, map3))
909
bitmap_clear_all(map);
910
bitmap_clear_all(map2);
911
bitmap_clear_all(map3);
912
test_bit1=get_rand_bit(bitsize);
913
bitmap_set_prefix(map, test_bit1);
915
bitmap_set_all(map3);
916
for (j=0; j < test_bit1; j++)
917
bitmap_clear_bit(map3, j);
918
if (!bitmap_cmp(map, map3))
920
bitmap_clear_all(map);
921
bitmap_clear_all(map3);
925
printf("intersect error bitsize=%u,size1=%u,size2=%u", bitsize,
926
test_bit1,test_bit2);
929
printf("union error bitsize=%u,size1=%u,size2=%u", bitsize,
930
test_bit1,test_bit2);
933
printf("xor error bitsize=%u,size1=%u,size2=%u", bitsize,
934
test_bit1,test_bit2);
937
printf("subtract error bitsize=%u,size1=%u,size2=%u", bitsize,
938
test_bit1,test_bit2);
941
printf("invert error bitsize=%u,size=%u", bitsize,
946
bool test_count_bits_set(MY_BITMAP *map, uint32_t bitsize)
948
uint32_t i, bit_count=0, test_bit;
949
uint32_t no_loops= bitsize > 128 ? 128 : bitsize;
950
for (i=0; i < no_loops; i++)
952
test_bit=get_rand_bit(bitsize);
953
if (!bitmap_is_set(map, test_bit))
955
bitmap_set_bit(map, test_bit);
959
if (bit_count==0 && bitsize > 0)
961
if (bitmap_bits_set(map) != bit_count)
965
printf("No bits set bitsize = %u", bitsize);
968
printf("Wrong count of bits set, bitsize = %u", bitsize);
972
bool test_get_first_bit(MY_BITMAP *map, uint32_t bitsize)
974
uint32_t i, test_bit;
975
uint32_t no_loops= bitsize > 128 ? 128 : bitsize;
976
for (i=0; i < no_loops; i++)
978
test_bit=get_rand_bit(bitsize);
979
bitmap_set_bit(map, test_bit);
980
if (bitmap_get_first_set(map) != test_bit)
983
bitmap_clear_bit(map, test_bit);
984
if (bitmap_get_first(map) != test_bit)
986
bitmap_clear_all(map);
990
printf("get_first_set error bitsize=%u,prefix_size=%u",bitsize,test_bit);
993
printf("get_first error bitsize= %u, prefix_size= %u",bitsize,test_bit);
997
bool test_get_next_bit(MY_BITMAP *map, uint32_t bitsize)
999
uint32_t i, j, test_bit;
1000
uint32_t no_loops= bitsize > 128 ? 128 : bitsize;
1001
for (i=0; i < no_loops; i++)
1003
test_bit=get_rand_bit(bitsize);
1004
for (j=0; j < test_bit; j++)
1005
bitmap_set_next(map);
1006
if (!bitmap_is_prefix(map, test_bit))
1008
bitmap_clear_all(map);
1012
printf("get_next error bitsize= %u, prefix_size= %u", bitsize,test_bit);
1016
bool test_prefix(MY_BITMAP *map, uint32_t bitsize)
1018
uint32_t i, j, test_bit;
1019
uint32_t no_loops= bitsize > 128 ? 128 : bitsize;
1020
for (i=0; i < no_loops; i++)
1022
test_bit=get_rand_bit(bitsize);
1023
bitmap_set_prefix(map, test_bit);
1024
if (!bitmap_is_prefix(map, test_bit))
1026
bitmap_clear_all(map);
1027
for (j=0; j < test_bit; j++)
1028
bitmap_set_bit(map, j);
1029
if (!bitmap_is_prefix(map, test_bit))
1031
bitmap_set_all(map);
1032
for (j=bitsize - 1; ~(j-test_bit); j--)
1033
bitmap_clear_bit(map, j);
1034
if (!bitmap_is_prefix(map, test_bit))
1036
bitmap_clear_all(map);
1040
printf("prefix1 error bitsize = %u, prefix_size = %u", bitsize,test_bit);
1043
printf("prefix2 error bitsize = %u, prefix_size = %u", bitsize,test_bit);
1046
printf("prefix3 error bitsize = %u, prefix_size = %u", bitsize,test_bit);
1051
bool do_test(uint32_t bitsize)
1054
my_bitmap_map buf[1024];
1055
if (bitmap_init(&map, buf, bitsize, false))
1057
printf("init error for bitsize %d", bitsize);
1060
if (test_set_get_clear_bit(&map,bitsize))
1062
bitmap_clear_all(&map);
1063
if (test_flip_bit(&map,bitsize))
1065
bitmap_clear_all(&map);
1066
if (test_operators(&map,bitsize))
1068
bitmap_clear_all(&map);
1069
if (test_get_all_bits(&map, bitsize))
1071
bitmap_clear_all(&map);
1072
if (test_compare_operators(&map,bitsize))
1074
bitmap_clear_all(&map);
1075
if (test_count_bits_set(&map,bitsize))
1077
bitmap_clear_all(&map);
1078
if (test_get_first_bit(&map,bitsize))
1080
bitmap_clear_all(&map);
1081
if (test_get_next_bit(&map,bitsize))
1083
if (test_prefix(&map,bitsize))
1094
for (i= 1; i < 4096; i++)
1096
printf("Start test for bitsize=%u\n",i);
1107
will build the bitmap tests and ./test_bitmap will execute it