-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitmap.h
82 lines (72 loc) · 1.71 KB
/
bitmap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// Created by 26220 on 2021/2/21.
//
#ifndef MYMALLOC_BITMAP_H
#define MYMALLOC_BITMAP_H
#include <cstring>
#include "cstdio"
class BitMap {
public:
BitMap() {
bitmap = NULL;
size = 0;
}
BitMap(int size) { // contractor, init the bitmap
bitmap = NULL;
bitmap = new char[size];
if (bitmap == NULL) {
printf("ErroR In BitMap Constractor!\n");
} else {
memset(bitmap, 0x0, size * sizeof(char));
this->size = size;
}
}
/*
* set the index bit to 1;
*/
int Set(int index) {
int addr = index / 8;
int addroffset = index % 8;
unsigned char temp = 0x1 << addroffset;
if (addr > (size + 1)) {
return 0;
} else {
bitmap[addr] |= temp;
return 1;
}
}
/*
* return if the index in bitmap is 1;
*/
bool Get(int index) {
int addr = index / 8;
int addroffset = index % 8;
unsigned char temp = 0x1 << addroffset;
if (addr > (size + 1)) {
return 0;
} else {
return (bitmap[addr] & temp) > 0 ? 1 : 0;
}
}
/*
* del the index from 1 to 0
*/
int Del(int index) {
if (Get(index) == 0) {
return 0;
}
int addr = index / 8;
int addroffset = index % 8;
unsigned char temp = 0x1 << addroffset;
if (addr > (size + 1)) {
return 0;
} else {
bitmap[addr] ^= temp;
return 1;
}
}
private:
char *bitmap;
int size;
};
#endif //MYMALLOC_BITMAP_H