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 <my_bitmap.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= ~0U;
69
map->last_word_mask= ~0U;
74
map->last_word_mask= 0U;
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)))
102
DBUG_ENTER("bitmap_init");
105
uint size_in_bytes= bitmap_buffer_size(n_bits);
109
size_in_bytes= ALIGN_SIZE(size_in_bytes);
110
extra= sizeof(pthread_mutex_t);
113
if (!(buf= (my_bitmap_map*) my_malloc(size_in_bytes+extra, MYF(MY_WME))))
117
map->mutex= (pthread_mutex_t *) ((char*) buf + size_in_bytes);
118
pthread_mutex_init(map->mutex, MY_MUTEX_INIT_FAST);
123
DBUG_ASSERT(thread_safe == 0);
128
create_last_word_mask(map);
129
bitmap_clear_all(map);
134
void bitmap_free(MY_BITMAP *map)
136
DBUG_ENTER("bitmap_free");
140
pthread_mutex_destroy(map->mutex);
141
my_free((char*) map->bitmap, MYF(0));
149
test if bit already set and set it if it was not (thread unsafe method)
152
bitmap_fast_test_and_set()
161
bool bitmap_fast_test_and_set(MY_BITMAP *map, uint bitmap_bit)
163
uchar *value= ((uchar*) map->bitmap) + (bitmap_bit / 8);
164
uchar bit= 1 << ((bitmap_bit) & 7);
165
uchar res= (*value) & bit;
172
test if bit already set and set it if it was not (thread safe method)
175
bitmap_fast_test_and_set()
177
bitmap_bit bit number
184
bool bitmap_test_and_set(MY_BITMAP *map, uint bitmap_bit)
187
DBUG_ASSERT(map->bitmap && bitmap_bit < map->n_bits);
189
res= bitmap_fast_test_and_set(map, bitmap_bit);
195
test if bit already set and clear it if it was set(thread unsafe method)
198
bitmap_fast_test_and_set()
207
bool bitmap_fast_test_and_clear(MY_BITMAP *map, uint bitmap_bit)
209
uchar *byte= (uchar*) map->bitmap + (bitmap_bit / 8);
210
uchar bit= 1 << ((bitmap_bit) & 7);
211
uchar res= (*byte) & bit;
217
bool bitmap_test_and_clear(MY_BITMAP *map, uint bitmap_bit)
220
DBUG_ASSERT(map->bitmap && bitmap_bit < map->n_bits);
222
res= bitmap_fast_test_and_clear(map, bitmap_bit);
228
uint bitmap_set_next(MY_BITMAP *map)
231
DBUG_ASSERT(map->bitmap);
232
if ((bit_found= bitmap_get_first(map)) != MY_BIT_NONE)
233
bitmap_set_bit(map, bit_found);
238
void bitmap_set_prefix(MY_BITMAP *map, uint prefix_size)
240
uint prefix_bytes, prefix_bits, d;
241
uchar *m= (uchar *)map->bitmap;
243
DBUG_ASSERT(map->bitmap &&
244
(prefix_size <= map->n_bits || prefix_size == (uint) ~0));
245
set_if_smaller(prefix_size, map->n_bits);
246
if ((prefix_bytes= prefix_size / 8))
247
memset(m, 0xff, prefix_bytes);
249
if ((prefix_bits= prefix_size & 7))
250
*m++= (1 << prefix_bits)-1;
251
if ((d= no_bytes_in_map(map)-prefix_bytes))
256
bool bitmap_is_prefix(const MY_BITMAP *map, uint prefix_size)
258
uint prefix_bits= prefix_size & 0x7, res;
259
uchar *m= (uchar*)map->bitmap;
260
uchar *end_prefix= m+prefix_size/8;
262
DBUG_ASSERT(m && prefix_size <= map->n_bits);
263
end= m+no_bytes_in_map(map);
265
while (m < end_prefix)
269
*map->last_word_ptr&= ~map->last_word_mask; /*Clear bits*/
271
if (prefix_bits && *m++ != (1 << prefix_bits)-1)
283
bool bitmap_is_set_all(const MY_BITMAP *map)
285
my_bitmap_map *data_ptr= map->bitmap;
286
my_bitmap_map *end= map->last_word_ptr;
287
*map->last_word_ptr |= map->last_word_mask;
288
for (; data_ptr <= end; data_ptr++)
289
if (*data_ptr != 0xFFFFFFFF)
295
bool bitmap_is_clear_all(const MY_BITMAP *map)
297
my_bitmap_map *data_ptr= map->bitmap;
299
if (*map->last_word_ptr & ~map->last_word_mask)
301
end= map->last_word_ptr;
302
for (; data_ptr < end; data_ptr++)
308
/* Return true if map1 is a subset of map2 */
310
bool bitmap_is_subset(const MY_BITMAP *map1, const MY_BITMAP *map2)
312
my_bitmap_map *m1= map1->bitmap, *m2= map2->bitmap, *end;
314
DBUG_ASSERT(map1->bitmap && map2->bitmap &&
315
map1->n_bits==map2->n_bits);
317
end= map1->last_word_ptr;
318
*map1->last_word_ptr &= ~map1->last_word_mask;
319
*map2->last_word_ptr &= ~map2->last_word_mask;
322
if ((*m1++) & ~(*m2++))
328
/* True if bitmaps has any common bits */
330
bool bitmap_is_overlapping(const MY_BITMAP *map1, const MY_BITMAP *map2)
332
my_bitmap_map *m1= map1->bitmap, *m2= map2->bitmap, *end;
334
DBUG_ASSERT(map1->bitmap && map2->bitmap &&
335
map1->n_bits==map2->n_bits);
337
end= map1->last_word_ptr;
338
*map1->last_word_ptr &= ~map1->last_word_mask;
339
*map2->last_word_ptr &= ~map2->last_word_mask;
342
if ((*m1++) & (*m2++))
349
void bitmap_intersect(MY_BITMAP *map, const MY_BITMAP *map2)
351
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end;
352
uint len= no_words_in_map(map), len2 = no_words_in_map(map2);
354
DBUG_ASSERT(map->bitmap && map2->bitmap);
356
end= to+min(len,len2);
357
*map2->last_word_ptr&= ~map2->last_word_mask; /*Clear last bits in map2*/
371
Set/clear all bits above a bit.
375
map RETURN The bitmap to change.
376
from_byte The bitmap buffer byte offset to start with.
377
use_bit The bit value (1/0) to use for all upper bits.
380
You can only set/clear full bytes.
381
The function is meant for the situation that you copy a smaller bitmap
382
to a bigger bitmap. Bitmap lengths are always multiple of eigth (the
383
size of a byte). Using 'from_byte' saves multiplication and division
384
by eight during parameter passing.
390
void bitmap_set_above(MY_BITMAP *map, uint from_byte, uint use_bit)
392
uchar use_byte= use_bit ? 0xff : 0;
393
uchar *to= (uchar *)map->bitmap + from_byte;
394
uchar *end= (uchar *)map->bitmap + (map->n_bits+7)/8;
401
void bitmap_subtract(MY_BITMAP *map, const MY_BITMAP *map2)
403
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end;
404
DBUG_ASSERT(map->bitmap && map2->bitmap &&
405
map->n_bits==map2->n_bits);
407
end= map->last_word_ptr;
414
void bitmap_union(MY_BITMAP *map, const MY_BITMAP *map2)
416
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end;
418
DBUG_ASSERT(map->bitmap && map2->bitmap &&
419
map->n_bits==map2->n_bits);
420
end= map->last_word_ptr;
427
void bitmap_xor(MY_BITMAP *map, const MY_BITMAP *map2)
429
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end= map->last_word_ptr;
430
DBUG_ASSERT(map->bitmap && map2->bitmap &&
431
map->n_bits==map2->n_bits);
437
void bitmap_invert(MY_BITMAP *map)
439
my_bitmap_map *to= map->bitmap, *end;
441
DBUG_ASSERT(map->bitmap);
442
end= map->last_word_ptr;
449
uint bitmap_bits_set(const MY_BITMAP *map)
451
uchar *m= (uchar*)map->bitmap;
452
uchar *end= m + no_bytes_in_map(map);
455
DBUG_ASSERT(map->bitmap);
456
*map->last_word_ptr&= ~map->last_word_mask; /*Reset last bits to zero*/
458
res+= my_count_bits_ushort(*m++);
463
void bitmap_copy(MY_BITMAP *map, const MY_BITMAP *map2)
465
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end;
467
DBUG_ASSERT(map->bitmap && map2->bitmap &&
468
map->n_bits==map2->n_bits);
469
end= map->last_word_ptr;
475
uint bitmap_get_first_set(const MY_BITMAP *map)
479
my_bitmap_map *data_ptr, *end= map->last_word_ptr;
481
DBUG_ASSERT(map->bitmap);
482
data_ptr= map->bitmap;
483
*map->last_word_ptr &= ~map->last_word_mask;
485
for (i=0; data_ptr <= end; data_ptr++, i++)
489
byte_ptr= (uchar*)data_ptr;
490
for (j=0; ; j++, byte_ptr++)
496
if (*byte_ptr & (1 << k))
497
return (i*32) + (j*8) + k;
509
uint bitmap_get_first(const MY_BITMAP *map)
513
my_bitmap_map *data_ptr, *end= map->last_word_ptr;
515
DBUG_ASSERT(map->bitmap);
516
data_ptr= map->bitmap;
517
*map->last_word_ptr|= map->last_word_mask;
519
for (i=0; data_ptr <= end; data_ptr++, i++)
521
if (*data_ptr != 0xFFFFFFFF)
523
byte_ptr= (uchar*)data_ptr;
524
for (j=0; ; j++, byte_ptr++)
526
if (*byte_ptr != 0xFF)
530
if (!(*byte_ptr & (1 << k)))
531
return (i*32) + (j*8) + k;
543
uint bitmap_lock_set_next(MY_BITMAP *map)
547
bit_found= bitmap_set_next(map);
553
void bitmap_lock_clear_bit(MY_BITMAP *map, uint bitmap_bit)
556
DBUG_ASSERT(map->bitmap && bitmap_bit < map->n_bits);
557
bitmap_clear_bit(map, bitmap_bit);
563
bool bitmap_lock_is_prefix(const MY_BITMAP *map, uint prefix_size)
566
bitmap_lock((MY_BITMAP *)map);
567
res= bitmap_is_prefix(map, prefix_size);
568
bitmap_unlock((MY_BITMAP *)map);
573
void bitmap_lock_set_all(MY_BITMAP *map)
581
void bitmap_lock_clear_all(MY_BITMAP *map)
584
bitmap_clear_all(map);
589
void bitmap_lock_set_prefix(MY_BITMAP *map, uint prefix_size)
592
bitmap_set_prefix(map, prefix_size);
597
bool bitmap_lock_is_clear_all(const MY_BITMAP *map)
600
bitmap_lock((MY_BITMAP *)map);
601
res= bitmap_is_clear_all(map);
602
bitmap_unlock((MY_BITMAP *)map);
607
bool bitmap_lock_is_set_all(const MY_BITMAP *map)
610
bitmap_lock((MY_BITMAP *)map);
611
res= bitmap_is_set_all(map);
612
bitmap_unlock((MY_BITMAP *)map);
617
bool bitmap_lock_is_set(const MY_BITMAP *map, uint bitmap_bit)
620
DBUG_ASSERT(map->bitmap && bitmap_bit < map->n_bits);
621
bitmap_lock((MY_BITMAP *)map);
622
res= bitmap_is_set(map, bitmap_bit);
623
bitmap_unlock((MY_BITMAP *)map);
628
bool bitmap_lock_is_subset(const MY_BITMAP *map1, const MY_BITMAP *map2)
631
bitmap_lock((MY_BITMAP *)map1);
632
bitmap_lock((MY_BITMAP *)map2);
633
res= bitmap_is_subset(map1, map2);
634
bitmap_unlock((MY_BITMAP *)map2);
635
bitmap_unlock((MY_BITMAP *)map1);
640
bool bitmap_lock_cmp(const MY_BITMAP *map1, const MY_BITMAP *map2)
644
DBUG_ASSERT(map1->bitmap && map2->bitmap &&
645
map1->n_bits==map2->n_bits);
646
bitmap_lock((MY_BITMAP *)map1);
647
bitmap_lock((MY_BITMAP *)map2);
648
res= bitmap_cmp(map1, map2);
649
bitmap_unlock((MY_BITMAP *)map2);
650
bitmap_unlock((MY_BITMAP *)map1);
655
void bitmap_lock_intersect(MY_BITMAP *map, const MY_BITMAP *map2)
658
bitmap_lock((MY_BITMAP *)map2);
659
bitmap_intersect(map, map2);
660
bitmap_unlock((MY_BITMAP *)map2);
665
void bitmap_lock_subtract(MY_BITMAP *map, const MY_BITMAP *map2)
668
bitmap_lock((MY_BITMAP *)map2);
669
bitmap_subtract(map, map2);
670
bitmap_unlock((MY_BITMAP *)map2);
675
void bitmap_lock_union(MY_BITMAP *map, const MY_BITMAP *map2)
678
bitmap_lock((MY_BITMAP *)map2);
679
bitmap_union(map, map2);
680
bitmap_unlock((MY_BITMAP *)map2);
690
Number of set bits in the bitmap.
692
uint bitmap_lock_bits_set(const MY_BITMAP *map)
695
bitmap_lock((MY_BITMAP *)map);
696
DBUG_ASSERT(map->bitmap);
697
res= bitmap_bits_set(map);
698
bitmap_unlock((MY_BITMAP *)map);
708
Number of first unset bit in the bitmap or MY_BIT_NONE if all bits are set.
710
uint bitmap_lock_get_first(const MY_BITMAP *map)
713
bitmap_lock((MY_BITMAP*)map);
714
res= bitmap_get_first(map);
715
bitmap_unlock((MY_BITMAP*)map);
720
uint bitmap_lock_get_first_set(const MY_BITMAP *map)
723
bitmap_lock((MY_BITMAP*)map);
724
res= bitmap_get_first_set(map);
725
bitmap_unlock((MY_BITMAP*)map);
730
void bitmap_lock_set_bit(MY_BITMAP *map, uint bitmap_bit)
732
DBUG_ASSERT(map->bitmap && bitmap_bit < map->n_bits);
734
bitmap_set_bit(map, bitmap_bit);
739
void bitmap_lock_flip_bit(MY_BITMAP *map, uint bitmap_bit)
741
DBUG_ASSERT(map->bitmap && bitmap_bit < map->n_bits);
743
bitmap_flip_bit(map, bitmap_bit);
749
uint get_rand_bit(uint bitsize)
751
return (rand() % bitsize);
754
bool test_set_get_clear_bit(MY_BITMAP *map, uint bitsize)
757
uint no_loops= bitsize > 128 ? 128 : bitsize;
758
for (i=0; i < no_loops; i++)
760
test_bit= get_rand_bit(bitsize);
761
bitmap_set_bit(map, test_bit);
762
if (!bitmap_is_set(map, test_bit))
764
bitmap_clear_bit(map, test_bit);
765
if (bitmap_is_set(map, test_bit))
770
printf("Error in set bit, bit %u, bitsize = %u", test_bit, bitsize);
773
printf("Error in clear bit, bit %u, bitsize = %u", test_bit, bitsize);
777
bool test_flip_bit(MY_BITMAP *map, uint bitsize)
780
uint no_loops= bitsize > 128 ? 128 : bitsize;
781
for (i=0; i < no_loops; i++)
783
test_bit= get_rand_bit(bitsize);
784
bitmap_flip_bit(map, test_bit);
785
if (!bitmap_is_set(map, test_bit))
787
bitmap_flip_bit(map, test_bit);
788
if (bitmap_is_set(map, test_bit))
793
printf("Error in flip bit 1, bit %u, bitsize = %u", test_bit, bitsize);
796
printf("Error in flip bit 2, bit %u, bitsize = %u", test_bit, bitsize);
800
bool test_operators(MY_BITMAP *map __attribute__((unused)),
801
uint bitsize __attribute__((unused)))
806
bool test_get_all_bits(MY_BITMAP *map, uint bitsize)
810
if (!bitmap_is_set_all(map))
812
if (!bitmap_is_prefix(map, bitsize))
814
bitmap_clear_all(map);
815
if (!bitmap_is_clear_all(map))
817
if (!bitmap_is_prefix(map, 0))
819
for (i=0; i<bitsize;i++)
820
bitmap_set_bit(map, i);
821
if (!bitmap_is_set_all(map))
823
for (i=0; i<bitsize;i++)
824
bitmap_clear_bit(map, i);
825
if (!bitmap_is_clear_all(map))
829
printf("Error in set_all, bitsize = %u", bitsize);
832
printf("Error in clear_all, bitsize = %u", bitsize);
835
printf("Error in bitmap_is_set_all, bitsize = %u", bitsize);
838
printf("Error in bitmap_is_clear_all, bitsize = %u", bitsize);
841
printf("Error in set_all through set_prefix, bitsize = %u", bitsize);
844
printf("Error in clear_all through set_prefix, bitsize = %u", bitsize);
848
bool test_compare_operators(MY_BITMAP *map, uint bitsize)
850
uint i, j, test_bit1, test_bit2, test_bit3,test_bit4;
851
uint no_loops= bitsize > 128 ? 128 : bitsize;
852
MY_BITMAP map2_obj, map3_obj;
853
MY_BITMAP *map2= &map2_obj, *map3= &map3_obj;
854
my_bitmap_map map2buf[1024];
855
my_bitmap_map map3buf[1024];
856
bitmap_init(&map2_obj, map2buf, bitsize, false);
857
bitmap_init(&map3_obj, map3buf, bitsize, false);
858
bitmap_clear_all(map2);
859
bitmap_clear_all(map3);
860
for (i=0; i < no_loops; i++)
862
test_bit1=get_rand_bit(bitsize);
863
bitmap_set_prefix(map, test_bit1);
864
test_bit2=get_rand_bit(bitsize);
865
bitmap_set_prefix(map2, test_bit2);
866
bitmap_intersect(map, map2);
867
test_bit3= test_bit2 < test_bit1 ? test_bit2 : test_bit1;
868
bitmap_set_prefix(map3, test_bit3);
869
if (!bitmap_cmp(map, map3))
871
bitmap_clear_all(map);
872
bitmap_clear_all(map2);
873
bitmap_clear_all(map3);
874
test_bit1=get_rand_bit(bitsize);
875
test_bit2=get_rand_bit(bitsize);
876
test_bit3=get_rand_bit(bitsize);
877
bitmap_set_prefix(map, test_bit1);
878
bitmap_set_prefix(map2, test_bit2);
879
test_bit3= test_bit2 > test_bit1 ? test_bit2 : test_bit1;
880
bitmap_set_prefix(map3, test_bit3);
881
bitmap_union(map, map2);
882
if (!bitmap_cmp(map, map3))
884
bitmap_clear_all(map);
885
bitmap_clear_all(map2);
886
bitmap_clear_all(map3);
887
test_bit1=get_rand_bit(bitsize);
888
test_bit2=get_rand_bit(bitsize);
889
test_bit3=get_rand_bit(bitsize);
890
bitmap_set_prefix(map, test_bit1);
891
bitmap_set_prefix(map2, test_bit2);
892
bitmap_xor(map, map2);
893
test_bit3= test_bit2 > test_bit1 ? test_bit2 : test_bit1;
894
test_bit4= test_bit2 < test_bit1 ? test_bit2 : test_bit1;
895
bitmap_set_prefix(map3, test_bit3);
896
for (j=0; j < test_bit4; j++)
897
bitmap_clear_bit(map3, j);
898
if (!bitmap_cmp(map, map3))
900
bitmap_clear_all(map);
901
bitmap_clear_all(map2);
902
bitmap_clear_all(map3);
903
test_bit1=get_rand_bit(bitsize);
904
test_bit2=get_rand_bit(bitsize);
905
test_bit3=get_rand_bit(bitsize);
906
bitmap_set_prefix(map, test_bit1);
907
bitmap_set_prefix(map2, test_bit2);
908
bitmap_subtract(map, map2);
909
if (test_bit2 < test_bit1)
911
bitmap_set_prefix(map3, test_bit1);
912
for (j=0; j < test_bit2; j++)
913
bitmap_clear_bit(map3, j);
915
if (!bitmap_cmp(map, map3))
917
bitmap_clear_all(map);
918
bitmap_clear_all(map2);
919
bitmap_clear_all(map3);
920
test_bit1=get_rand_bit(bitsize);
921
bitmap_set_prefix(map, test_bit1);
923
bitmap_set_all(map3);
924
for (j=0; j < test_bit1; j++)
925
bitmap_clear_bit(map3, j);
926
if (!bitmap_cmp(map, map3))
928
bitmap_clear_all(map);
929
bitmap_clear_all(map3);
933
printf("intersect error bitsize=%u,size1=%u,size2=%u", bitsize,
934
test_bit1,test_bit2);
937
printf("union error bitsize=%u,size1=%u,size2=%u", bitsize,
938
test_bit1,test_bit2);
941
printf("xor error bitsize=%u,size1=%u,size2=%u", bitsize,
942
test_bit1,test_bit2);
945
printf("subtract error bitsize=%u,size1=%u,size2=%u", bitsize,
946
test_bit1,test_bit2);
949
printf("invert error bitsize=%u,size=%u", bitsize,
954
bool test_count_bits_set(MY_BITMAP *map, uint bitsize)
956
uint i, bit_count=0, test_bit;
957
uint no_loops= bitsize > 128 ? 128 : bitsize;
958
for (i=0; i < no_loops; i++)
960
test_bit=get_rand_bit(bitsize);
961
if (!bitmap_is_set(map, test_bit))
963
bitmap_set_bit(map, test_bit);
967
if (bit_count==0 && bitsize > 0)
969
if (bitmap_bits_set(map) != bit_count)
973
printf("No bits set bitsize = %u", bitsize);
976
printf("Wrong count of bits set, bitsize = %u", bitsize);
980
bool test_get_first_bit(MY_BITMAP *map, uint bitsize)
983
uint no_loops= bitsize > 128 ? 128 : bitsize;
984
for (i=0; i < no_loops; i++)
986
test_bit=get_rand_bit(bitsize);
987
bitmap_set_bit(map, test_bit);
988
if (bitmap_get_first_set(map) != test_bit)
991
bitmap_clear_bit(map, test_bit);
992
if (bitmap_get_first(map) != test_bit)
994
bitmap_clear_all(map);
998
printf("get_first_set error bitsize=%u,prefix_size=%u",bitsize,test_bit);
1001
printf("get_first error bitsize= %u, prefix_size= %u",bitsize,test_bit);
1005
bool test_get_next_bit(MY_BITMAP *map, uint bitsize)
1007
uint i, j, test_bit;
1008
uint no_loops= bitsize > 128 ? 128 : bitsize;
1009
for (i=0; i < no_loops; i++)
1011
test_bit=get_rand_bit(bitsize);
1012
for (j=0; j < test_bit; j++)
1013
bitmap_set_next(map);
1014
if (!bitmap_is_prefix(map, test_bit))
1016
bitmap_clear_all(map);
1020
printf("get_next error bitsize= %u, prefix_size= %u", bitsize,test_bit);
1024
bool test_prefix(MY_BITMAP *map, uint bitsize)
1026
uint i, j, test_bit;
1027
uint no_loops= bitsize > 128 ? 128 : bitsize;
1028
for (i=0; i < no_loops; i++)
1030
test_bit=get_rand_bit(bitsize);
1031
bitmap_set_prefix(map, test_bit);
1032
if (!bitmap_is_prefix(map, test_bit))
1034
bitmap_clear_all(map);
1035
for (j=0; j < test_bit; j++)
1036
bitmap_set_bit(map, j);
1037
if (!bitmap_is_prefix(map, test_bit))
1039
bitmap_set_all(map);
1040
for (j=bitsize - 1; ~(j-test_bit); j--)
1041
bitmap_clear_bit(map, j);
1042
if (!bitmap_is_prefix(map, test_bit))
1044
bitmap_clear_all(map);
1048
printf("prefix1 error bitsize = %u, prefix_size = %u", bitsize,test_bit);
1051
printf("prefix2 error bitsize = %u, prefix_size = %u", bitsize,test_bit);
1054
printf("prefix3 error bitsize = %u, prefix_size = %u", bitsize,test_bit);
1059
bool do_test(uint bitsize)
1062
my_bitmap_map buf[1024];
1063
if (bitmap_init(&map, buf, bitsize, false))
1065
printf("init error for bitsize %d", bitsize);
1068
if (test_set_get_clear_bit(&map,bitsize))
1070
bitmap_clear_all(&map);
1071
if (test_flip_bit(&map,bitsize))
1073
bitmap_clear_all(&map);
1074
if (test_operators(&map,bitsize))
1076
bitmap_clear_all(&map);
1077
if (test_get_all_bits(&map, bitsize))
1079
bitmap_clear_all(&map);
1080
if (test_compare_operators(&map,bitsize))
1082
bitmap_clear_all(&map);
1083
if (test_count_bits_set(&map,bitsize))
1085
bitmap_clear_all(&map);
1086
if (test_get_first_bit(&map,bitsize))
1088
bitmap_clear_all(&map);
1089
if (test_get_next_bit(&map,bitsize))
1091
if (test_prefix(&map,bitsize))
1102
for (i= 1; i < 4096; i++)
1104
printf("Start test for bitsize=%u\n",i);
1115
will build the bitmap tests and ./test_bitmap will execute it