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 uchar 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 __attribute__((unused)))
89
pthread_mutex_lock(map->mutex);
92
static inline void bitmap_unlock(MY_BITMAP *map __attribute__((unused)))
95
pthread_mutex_unlock(map->mutex);
99
bool bitmap_init(MY_BITMAP *map, my_bitmap_map *buf, uint n_bits,
100
bool thread_safe __attribute__((unused)))
104
uint size_in_bytes= bitmap_buffer_size(n_bits);
108
size_in_bytes= ALIGN_SIZE(size_in_bytes);
109
extra= sizeof(pthread_mutex_t);
112
if (!(buf= (my_bitmap_map*) my_malloc(size_in_bytes+extra, MYF(MY_WME))))
116
map->mutex= (pthread_mutex_t *) ((char*) buf + size_in_bytes);
117
pthread_mutex_init(map->mutex, MY_MUTEX_INIT_FAST);
122
assert(thread_safe == 0);
127
create_last_word_mask(map);
128
bitmap_clear_all(map);
133
void bitmap_free(MY_BITMAP *map)
138
pthread_mutex_destroy(map->mutex);
139
my_free((char*) map->bitmap, MYF(0));
147
test if bit already set and set it if it was not (thread unsafe method)
150
bitmap_fast_test_and_set()
159
bool bitmap_fast_test_and_set(MY_BITMAP *map, uint bitmap_bit)
161
uchar *value= ((uchar*) map->bitmap) + (bitmap_bit / 8);
162
uchar bit= 1 << ((bitmap_bit) & 7);
163
uchar res= (*value) & bit;
170
test if bit already set and set it if it was not (thread safe method)
173
bitmap_fast_test_and_set()
175
bitmap_bit bit number
182
bool bitmap_test_and_set(MY_BITMAP *map, uint bitmap_bit)
185
assert(map->bitmap && bitmap_bit < map->n_bits);
187
res= bitmap_fast_test_and_set(map, bitmap_bit);
193
test if bit already set and clear it if it was set(thread unsafe method)
196
bitmap_fast_test_and_set()
205
bool bitmap_fast_test_and_clear(MY_BITMAP *map, uint bitmap_bit)
207
uchar *byte= (uchar*) map->bitmap + (bitmap_bit / 8);
208
uchar bit= 1 << ((bitmap_bit) & 7);
209
uchar res= (*byte) & bit;
215
bool bitmap_test_and_clear(MY_BITMAP *map, uint bitmap_bit)
218
assert(map->bitmap && bitmap_bit < map->n_bits);
220
res= bitmap_fast_test_and_clear(map, bitmap_bit);
226
uint bitmap_set_next(MY_BITMAP *map)
230
if ((bit_found= bitmap_get_first(map)) != MY_BIT_NONE)
231
bitmap_set_bit(map, bit_found);
236
void bitmap_set_prefix(MY_BITMAP *map, uint prefix_size)
238
uint prefix_bytes, prefix_bits, d;
239
uchar *m= (uchar *)map->bitmap;
241
assert(map->bitmap &&
242
(prefix_size <= map->n_bits || prefix_size == UINT32_MAX));
243
set_if_smaller(prefix_size, map->n_bits);
244
if ((prefix_bytes= prefix_size / 8))
245
memset(m, 0xff, prefix_bytes);
247
if ((prefix_bits= prefix_size & 7))
248
*m++= (1 << prefix_bits)-1;
249
if ((d= no_bytes_in_map(map)-prefix_bytes))
254
bool bitmap_is_prefix(const MY_BITMAP *map, uint prefix_size)
256
uint prefix_bits= prefix_size & 0x7, res;
257
uchar *m= (uchar*)map->bitmap;
258
uchar *end_prefix= m+prefix_size/8;
260
assert(m && prefix_size <= map->n_bits);
261
end= m+no_bytes_in_map(map);
263
while (m < end_prefix)
267
*map->last_word_ptr&= ~map->last_word_mask; /*Clear bits*/
269
if (prefix_bits && *m++ != (1 << prefix_bits)-1)
281
bool bitmap_is_set_all(const MY_BITMAP *map)
283
my_bitmap_map *data_ptr= map->bitmap;
284
my_bitmap_map *end= map->last_word_ptr;
285
*map->last_word_ptr |= map->last_word_mask;
286
for (; data_ptr <= end; data_ptr++)
287
if (*data_ptr != 0xFFFFFFFF)
293
bool bitmap_is_clear_all(const MY_BITMAP *map)
295
my_bitmap_map *data_ptr= map->bitmap;
297
if (*map->last_word_ptr & ~map->last_word_mask)
299
end= map->last_word_ptr;
300
for (; data_ptr < end; data_ptr++)
306
/* Return true if map1 is a subset of map2 */
308
bool bitmap_is_subset(const MY_BITMAP *map1, const MY_BITMAP *map2)
310
my_bitmap_map *m1= map1->bitmap, *m2= map2->bitmap, *end;
312
assert(map1->bitmap && map2->bitmap &&
313
map1->n_bits==map2->n_bits);
315
end= map1->last_word_ptr;
316
*map1->last_word_ptr &= ~map1->last_word_mask;
317
*map2->last_word_ptr &= ~map2->last_word_mask;
320
if ((*m1++) & ~(*m2++))
326
/* True if bitmaps has any common bits */
328
bool bitmap_is_overlapping(const MY_BITMAP *map1, const MY_BITMAP *map2)
330
my_bitmap_map *m1= map1->bitmap, *m2= map2->bitmap, *end;
332
assert(map1->bitmap && map2->bitmap &&
333
map1->n_bits==map2->n_bits);
335
end= map1->last_word_ptr;
336
*map1->last_word_ptr &= ~map1->last_word_mask;
337
*map2->last_word_ptr &= ~map2->last_word_mask;
340
if ((*m1++) & (*m2++))
347
void bitmap_intersect(MY_BITMAP *map, const MY_BITMAP *map2)
349
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end;
350
uint len= no_words_in_map(map), len2 = no_words_in_map(map2);
352
assert(map->bitmap && map2->bitmap);
354
end= to+min(len,len2);
355
*map2->last_word_ptr&= ~map2->last_word_mask; /*Clear last bits in map2*/
369
Set/clear all bits above a bit.
373
map RETURN The bitmap to change.
374
from_byte The bitmap buffer byte offset to start with.
375
use_bit The bit value (1/0) to use for all upper bits.
378
You can only set/clear full bytes.
379
The function is meant for the situation that you copy a smaller bitmap
380
to a bigger bitmap. Bitmap lengths are always multiple of eigth (the
381
size of a byte). Using 'from_byte' saves multiplication and division
382
by eight during parameter passing.
388
void bitmap_set_above(MY_BITMAP *map, uint from_byte, uint use_bit)
390
uchar use_byte= use_bit ? 0xff : 0;
391
uchar *to= (uchar *)map->bitmap + from_byte;
392
uchar *end= (uchar *)map->bitmap + (map->n_bits+7)/8;
399
void bitmap_subtract(MY_BITMAP *map, const MY_BITMAP *map2)
401
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end;
402
assert(map->bitmap && map2->bitmap &&
403
map->n_bits==map2->n_bits);
405
end= map->last_word_ptr;
412
void bitmap_union(MY_BITMAP *map, const MY_BITMAP *map2)
414
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end;
416
assert(map->bitmap && map2->bitmap &&
417
map->n_bits==map2->n_bits);
418
end= map->last_word_ptr;
425
void bitmap_xor(MY_BITMAP *map, const MY_BITMAP *map2)
427
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end= map->last_word_ptr;
428
assert(map->bitmap && map2->bitmap &&
429
map->n_bits==map2->n_bits);
435
void bitmap_invert(MY_BITMAP *map)
437
my_bitmap_map *to= map->bitmap, *end;
440
end= map->last_word_ptr;
447
uint bitmap_bits_set(const MY_BITMAP *map)
449
uchar *m= (uchar*)map->bitmap;
450
uchar *end= m + no_bytes_in_map(map);
454
*map->last_word_ptr&= ~map->last_word_mask; /*Reset last bits to zero*/
456
res+= my_count_bits_ushort(*m++);
461
void bitmap_copy(MY_BITMAP *map, const MY_BITMAP *map2)
463
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end;
465
assert(map->bitmap && map2->bitmap &&
466
map->n_bits==map2->n_bits);
467
end= map->last_word_ptr;
473
uint bitmap_get_first_set(const MY_BITMAP *map)
477
my_bitmap_map *data_ptr, *end= map->last_word_ptr;
480
data_ptr= map->bitmap;
481
*map->last_word_ptr &= ~map->last_word_mask;
483
for (i=0; data_ptr <= end; data_ptr++, i++)
487
byte_ptr= (uchar*)data_ptr;
488
for (j=0; ; j++, byte_ptr++)
494
if (*byte_ptr & (1 << k))
495
return (i*32) + (j*8) + k;
507
uint bitmap_get_first(const MY_BITMAP *map)
511
my_bitmap_map *data_ptr, *end= map->last_word_ptr;
514
data_ptr= map->bitmap;
515
*map->last_word_ptr|= map->last_word_mask;
517
for (i=0; data_ptr <= end; data_ptr++, i++)
519
if (*data_ptr != 0xFFFFFFFF)
521
byte_ptr= (uchar*)data_ptr;
522
for (j=0; ; j++, byte_ptr++)
524
if (*byte_ptr != 0xFF)
528
if (!(*byte_ptr & (1 << k)))
529
return (i*32) + (j*8) + k;
541
uint bitmap_lock_set_next(MY_BITMAP *map)
545
bit_found= bitmap_set_next(map);
551
void bitmap_lock_clear_bit(MY_BITMAP *map, uint bitmap_bit)
554
assert(map->bitmap && bitmap_bit < map->n_bits);
555
bitmap_clear_bit(map, bitmap_bit);
561
bool bitmap_lock_is_prefix(const MY_BITMAP *map, uint prefix_size)
564
bitmap_lock((MY_BITMAP *)map);
565
res= bitmap_is_prefix(map, prefix_size);
566
bitmap_unlock((MY_BITMAP *)map);
571
void bitmap_lock_set_all(MY_BITMAP *map)
579
void bitmap_lock_clear_all(MY_BITMAP *map)
582
bitmap_clear_all(map);
587
void bitmap_lock_set_prefix(MY_BITMAP *map, uint prefix_size)
590
bitmap_set_prefix(map, prefix_size);
595
bool bitmap_lock_is_clear_all(const MY_BITMAP *map)
598
bitmap_lock((MY_BITMAP *)map);
599
res= bitmap_is_clear_all(map);
600
bitmap_unlock((MY_BITMAP *)map);
605
bool bitmap_lock_is_set_all(const MY_BITMAP *map)
608
bitmap_lock((MY_BITMAP *)map);
609
res= bitmap_is_set_all(map);
610
bitmap_unlock((MY_BITMAP *)map);
615
bool bitmap_lock_is_set(const MY_BITMAP *map, uint bitmap_bit)
618
assert(map->bitmap && bitmap_bit < map->n_bits);
619
bitmap_lock((MY_BITMAP *)map);
620
res= bitmap_is_set(map, bitmap_bit);
621
bitmap_unlock((MY_BITMAP *)map);
626
bool bitmap_lock_is_subset(const MY_BITMAP *map1, const MY_BITMAP *map2)
629
bitmap_lock((MY_BITMAP *)map1);
630
bitmap_lock((MY_BITMAP *)map2);
631
res= bitmap_is_subset(map1, map2);
632
bitmap_unlock((MY_BITMAP *)map2);
633
bitmap_unlock((MY_BITMAP *)map1);
638
bool bitmap_lock_cmp(const MY_BITMAP *map1, const MY_BITMAP *map2)
642
assert(map1->bitmap && map2->bitmap &&
643
map1->n_bits==map2->n_bits);
644
bitmap_lock((MY_BITMAP *)map1);
645
bitmap_lock((MY_BITMAP *)map2);
646
res= bitmap_cmp(map1, map2);
647
bitmap_unlock((MY_BITMAP *)map2);
648
bitmap_unlock((MY_BITMAP *)map1);
653
void bitmap_lock_intersect(MY_BITMAP *map, const MY_BITMAP *map2)
656
bitmap_lock((MY_BITMAP *)map2);
657
bitmap_intersect(map, map2);
658
bitmap_unlock((MY_BITMAP *)map2);
663
void bitmap_lock_subtract(MY_BITMAP *map, const MY_BITMAP *map2)
666
bitmap_lock((MY_BITMAP *)map2);
667
bitmap_subtract(map, map2);
668
bitmap_unlock((MY_BITMAP *)map2);
673
void bitmap_lock_union(MY_BITMAP *map, const MY_BITMAP *map2)
676
bitmap_lock((MY_BITMAP *)map2);
677
bitmap_union(map, map2);
678
bitmap_unlock((MY_BITMAP *)map2);
688
Number of set bits in the bitmap.
690
uint bitmap_lock_bits_set(const MY_BITMAP *map)
693
bitmap_lock((MY_BITMAP *)map);
695
res= bitmap_bits_set(map);
696
bitmap_unlock((MY_BITMAP *)map);
706
Number of first unset bit in the bitmap or MY_BIT_NONE if all bits are set.
708
uint bitmap_lock_get_first(const MY_BITMAP *map)
711
bitmap_lock((MY_BITMAP*)map);
712
res= bitmap_get_first(map);
713
bitmap_unlock((MY_BITMAP*)map);
718
uint bitmap_lock_get_first_set(const MY_BITMAP *map)
721
bitmap_lock((MY_BITMAP*)map);
722
res= bitmap_get_first_set(map);
723
bitmap_unlock((MY_BITMAP*)map);
728
void bitmap_lock_set_bit(MY_BITMAP *map, uint bitmap_bit)
730
assert(map->bitmap && bitmap_bit < map->n_bits);
732
bitmap_set_bit(map, bitmap_bit);
737
void bitmap_lock_flip_bit(MY_BITMAP *map, uint bitmap_bit)
739
assert(map->bitmap && bitmap_bit < map->n_bits);
741
bitmap_flip_bit(map, bitmap_bit);
747
uint get_rand_bit(uint bitsize)
749
return (rand() % bitsize);
752
bool test_set_get_clear_bit(MY_BITMAP *map, uint bitsize)
755
uint no_loops= bitsize > 128 ? 128 : bitsize;
756
for (i=0; i < no_loops; i++)
758
test_bit= get_rand_bit(bitsize);
759
bitmap_set_bit(map, test_bit);
760
if (!bitmap_is_set(map, test_bit))
762
bitmap_clear_bit(map, test_bit);
763
if (bitmap_is_set(map, test_bit))
768
printf("Error in set bit, bit %u, bitsize = %u", test_bit, bitsize);
771
printf("Error in clear bit, bit %u, bitsize = %u", test_bit, bitsize);
775
bool test_flip_bit(MY_BITMAP *map, uint bitsize)
778
uint no_loops= bitsize > 128 ? 128 : bitsize;
779
for (i=0; i < no_loops; i++)
781
test_bit= get_rand_bit(bitsize);
782
bitmap_flip_bit(map, test_bit);
783
if (!bitmap_is_set(map, test_bit))
785
bitmap_flip_bit(map, test_bit);
786
if (bitmap_is_set(map, test_bit))
791
printf("Error in flip bit 1, bit %u, bitsize = %u", test_bit, bitsize);
794
printf("Error in flip bit 2, bit %u, bitsize = %u", test_bit, bitsize);
798
bool test_operators(MY_BITMAP *map __attribute__((unused)),
799
uint bitsize __attribute__((unused)))
804
bool test_get_all_bits(MY_BITMAP *map, uint bitsize)
808
if (!bitmap_is_set_all(map))
810
if (!bitmap_is_prefix(map, bitsize))
812
bitmap_clear_all(map);
813
if (!bitmap_is_clear_all(map))
815
if (!bitmap_is_prefix(map, 0))
817
for (i=0; i<bitsize;i++)
818
bitmap_set_bit(map, i);
819
if (!bitmap_is_set_all(map))
821
for (i=0; i<bitsize;i++)
822
bitmap_clear_bit(map, i);
823
if (!bitmap_is_clear_all(map))
827
printf("Error in set_all, bitsize = %u", bitsize);
830
printf("Error in clear_all, bitsize = %u", bitsize);
833
printf("Error in bitmap_is_set_all, bitsize = %u", bitsize);
836
printf("Error in bitmap_is_clear_all, bitsize = %u", bitsize);
839
printf("Error in set_all through set_prefix, bitsize = %u", bitsize);
842
printf("Error in clear_all through set_prefix, bitsize = %u", bitsize);
846
bool test_compare_operators(MY_BITMAP *map, uint bitsize)
848
uint i, j, test_bit1, test_bit2, test_bit3,test_bit4;
849
uint no_loops= bitsize > 128 ? 128 : bitsize;
850
MY_BITMAP map2_obj, map3_obj;
851
MY_BITMAP *map2= &map2_obj, *map3= &map3_obj;
852
my_bitmap_map map2buf[1024];
853
my_bitmap_map map3buf[1024];
854
bitmap_init(&map2_obj, map2buf, bitsize, false);
855
bitmap_init(&map3_obj, map3buf, bitsize, false);
856
bitmap_clear_all(map2);
857
bitmap_clear_all(map3);
858
for (i=0; i < no_loops; i++)
860
test_bit1=get_rand_bit(bitsize);
861
bitmap_set_prefix(map, test_bit1);
862
test_bit2=get_rand_bit(bitsize);
863
bitmap_set_prefix(map2, test_bit2);
864
bitmap_intersect(map, map2);
865
test_bit3= test_bit2 < test_bit1 ? test_bit2 : test_bit1;
866
bitmap_set_prefix(map3, test_bit3);
867
if (!bitmap_cmp(map, map3))
869
bitmap_clear_all(map);
870
bitmap_clear_all(map2);
871
bitmap_clear_all(map3);
872
test_bit1=get_rand_bit(bitsize);
873
test_bit2=get_rand_bit(bitsize);
874
test_bit3=get_rand_bit(bitsize);
875
bitmap_set_prefix(map, test_bit1);
876
bitmap_set_prefix(map2, test_bit2);
877
test_bit3= test_bit2 > test_bit1 ? test_bit2 : test_bit1;
878
bitmap_set_prefix(map3, test_bit3);
879
bitmap_union(map, map2);
880
if (!bitmap_cmp(map, map3))
882
bitmap_clear_all(map);
883
bitmap_clear_all(map2);
884
bitmap_clear_all(map3);
885
test_bit1=get_rand_bit(bitsize);
886
test_bit2=get_rand_bit(bitsize);
887
test_bit3=get_rand_bit(bitsize);
888
bitmap_set_prefix(map, test_bit1);
889
bitmap_set_prefix(map2, test_bit2);
890
bitmap_xor(map, map2);
891
test_bit3= test_bit2 > test_bit1 ? test_bit2 : test_bit1;
892
test_bit4= test_bit2 < test_bit1 ? test_bit2 : test_bit1;
893
bitmap_set_prefix(map3, test_bit3);
894
for (j=0; j < test_bit4; j++)
895
bitmap_clear_bit(map3, j);
896
if (!bitmap_cmp(map, map3))
898
bitmap_clear_all(map);
899
bitmap_clear_all(map2);
900
bitmap_clear_all(map3);
901
test_bit1=get_rand_bit(bitsize);
902
test_bit2=get_rand_bit(bitsize);
903
test_bit3=get_rand_bit(bitsize);
904
bitmap_set_prefix(map, test_bit1);
905
bitmap_set_prefix(map2, test_bit2);
906
bitmap_subtract(map, map2);
907
if (test_bit2 < test_bit1)
909
bitmap_set_prefix(map3, test_bit1);
910
for (j=0; j < test_bit2; j++)
911
bitmap_clear_bit(map3, j);
913
if (!bitmap_cmp(map, map3))
915
bitmap_clear_all(map);
916
bitmap_clear_all(map2);
917
bitmap_clear_all(map3);
918
test_bit1=get_rand_bit(bitsize);
919
bitmap_set_prefix(map, test_bit1);
921
bitmap_set_all(map3);
922
for (j=0; j < test_bit1; j++)
923
bitmap_clear_bit(map3, j);
924
if (!bitmap_cmp(map, map3))
926
bitmap_clear_all(map);
927
bitmap_clear_all(map3);
931
printf("intersect error bitsize=%u,size1=%u,size2=%u", bitsize,
932
test_bit1,test_bit2);
935
printf("union error bitsize=%u,size1=%u,size2=%u", bitsize,
936
test_bit1,test_bit2);
939
printf("xor error bitsize=%u,size1=%u,size2=%u", bitsize,
940
test_bit1,test_bit2);
943
printf("subtract error bitsize=%u,size1=%u,size2=%u", bitsize,
944
test_bit1,test_bit2);
947
printf("invert error bitsize=%u,size=%u", bitsize,
952
bool test_count_bits_set(MY_BITMAP *map, uint bitsize)
954
uint i, bit_count=0, test_bit;
955
uint no_loops= bitsize > 128 ? 128 : bitsize;
956
for (i=0; i < no_loops; i++)
958
test_bit=get_rand_bit(bitsize);
959
if (!bitmap_is_set(map, test_bit))
961
bitmap_set_bit(map, test_bit);
965
if (bit_count==0 && bitsize > 0)
967
if (bitmap_bits_set(map) != bit_count)
971
printf("No bits set bitsize = %u", bitsize);
974
printf("Wrong count of bits set, bitsize = %u", bitsize);
978
bool test_get_first_bit(MY_BITMAP *map, uint bitsize)
981
uint no_loops= bitsize > 128 ? 128 : bitsize;
982
for (i=0; i < no_loops; i++)
984
test_bit=get_rand_bit(bitsize);
985
bitmap_set_bit(map, test_bit);
986
if (bitmap_get_first_set(map) != test_bit)
989
bitmap_clear_bit(map, test_bit);
990
if (bitmap_get_first(map) != test_bit)
992
bitmap_clear_all(map);
996
printf("get_first_set error bitsize=%u,prefix_size=%u",bitsize,test_bit);
999
printf("get_first error bitsize= %u, prefix_size= %u",bitsize,test_bit);
1003
bool test_get_next_bit(MY_BITMAP *map, uint bitsize)
1005
uint i, j, test_bit;
1006
uint no_loops= bitsize > 128 ? 128 : bitsize;
1007
for (i=0; i < no_loops; i++)
1009
test_bit=get_rand_bit(bitsize);
1010
for (j=0; j < test_bit; j++)
1011
bitmap_set_next(map);
1012
if (!bitmap_is_prefix(map, test_bit))
1014
bitmap_clear_all(map);
1018
printf("get_next error bitsize= %u, prefix_size= %u", bitsize,test_bit);
1022
bool test_prefix(MY_BITMAP *map, uint bitsize)
1024
uint i, j, test_bit;
1025
uint no_loops= bitsize > 128 ? 128 : bitsize;
1026
for (i=0; i < no_loops; i++)
1028
test_bit=get_rand_bit(bitsize);
1029
bitmap_set_prefix(map, test_bit);
1030
if (!bitmap_is_prefix(map, test_bit))
1032
bitmap_clear_all(map);
1033
for (j=0; j < test_bit; j++)
1034
bitmap_set_bit(map, j);
1035
if (!bitmap_is_prefix(map, test_bit))
1037
bitmap_set_all(map);
1038
for (j=bitsize - 1; ~(j-test_bit); j--)
1039
bitmap_clear_bit(map, j);
1040
if (!bitmap_is_prefix(map, test_bit))
1042
bitmap_clear_all(map);
1046
printf("prefix1 error bitsize = %u, prefix_size = %u", bitsize,test_bit);
1049
printf("prefix2 error bitsize = %u, prefix_size = %u", bitsize,test_bit);
1052
printf("prefix3 error bitsize = %u, prefix_size = %u", bitsize,test_bit);
1057
bool do_test(uint bitsize)
1060
my_bitmap_map buf[1024];
1061
if (bitmap_init(&map, buf, bitsize, false))
1063
printf("init error for bitsize %d", bitsize);
1066
if (test_set_get_clear_bit(&map,bitsize))
1068
bitmap_clear_all(&map);
1069
if (test_flip_bit(&map,bitsize))
1071
bitmap_clear_all(&map);
1072
if (test_operators(&map,bitsize))
1074
bitmap_clear_all(&map);
1075
if (test_get_all_bits(&map, bitsize))
1077
bitmap_clear_all(&map);
1078
if (test_compare_operators(&map,bitsize))
1080
bitmap_clear_all(&map);
1081
if (test_count_bits_set(&map,bitsize))
1083
bitmap_clear_all(&map);
1084
if (test_get_first_bit(&map,bitsize))
1086
bitmap_clear_all(&map);
1087
if (test_get_next_bit(&map,bitsize))
1089
if (test_prefix(&map,bitsize))
1100
for (i= 1; i < 4096; i++)
1102
printf("Start test for bitsize=%u\n",i);
1113
will build the bitmap tests and ./test_bitmap will execute it