-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbitmap.c
More file actions
106 lines (91 loc) · 2.69 KB
/
Copy pathbitmap.c
File metadata and controls
106 lines (91 loc) · 2.69 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "bitmap.h"
#define bitmap_shift 3
#define bitmap_mask 0x07
struct struct_bitmap {
char *array;
size_t capacity_bytes;
size_t last_bit_pos;
};
bitmap_t* bitmap_create(size_t size) {
bitmap_t *ret = malloc(sizeof(bitmap_t));
size_t size_bytes = ceil((double)size / 8);
ret->array = calloc(size_bytes, 1);
ret->last_bit_pos = 0;
ret->capacity_bytes = size_bytes;
return ret;
}
void bitmap_destroy(bitmap_t *bitmap) {
free(bitmap->array);
free(bitmap);
}
char bitmap_get(bitmap_t *bitmap, size_t index) {
if (index > bitmap->last_bit_pos) {
return 0;
}
size_t byte_offset = index >> bitmap_shift;
unsigned bit_offset = index & bitmap_mask;
char byte = (bitmap->array)[byte_offset];
return ( (byte >> bit_offset) & 1U );
}
void bitmap_mark(bitmap_t *bitmap, size_t index) {
size_t byte_offset = index >> bitmap_shift;
if (byte_offset >= bitmap->capacity_bytes) {
bitmap_resize(bitmap, index+1);
}
unsigned bit_offset = index & bitmap_mask;
(bitmap->array)[byte_offset] |= (1U << bit_offset);
if (index > bitmap->last_bit_pos) {
bitmap->last_bit_pos = index;
}
}
void bitmap_unmark(bitmap_t *bitmap, size_t index) {
size_t byte_offset = index >> bitmap_shift;
if (byte_offset >= bitmap->capacity_bytes) {
bitmap_resize(bitmap, index+1);
}
unsigned bit_offset = index & bitmap_mask;
(bitmap->array)[byte_offset] &= ~(1U << bit_offset);
if (index > bitmap->last_bit_pos) {
bitmap->last_bit_pos = index;
}
}
void bitmap_toggle(bitmap_t *bitmap, size_t index) {
size_t byte_offset = index >> bitmap_shift;
if (index >= bitmap->last_bit_pos) {
bitmap->last_bit_pos = index;
bitmap_resize(bitmap, index+1);
}
unsigned bit_offset = index & bitmap_mask;
(bitmap->array)[byte_offset] ^= (1U << bit_offset);
}
char bitmap_resize(bitmap_t *bitmap, size_t newsize) {
size_t old_capacity = bitmap->capacity_bytes;
size_t target_capacity = ceil((double)newsize / 8);
size_t new_capacity = 1;
while (new_capacity < target_capacity) {
new_capacity *= 2;
}
bitmap->array = realloc(bitmap->array, new_capacity);
if (!bitmap->array) {
return 1; // realloc failed
}
bitmap->capacity_bytes = new_capacity;
if (newsize > old_capacity) {
memset(bitmap->array+old_capacity, 0, new_capacity-old_capacity);
}
else {
bitmap->last_bit_pos = newsize-1;
unsigned char mask = 0xff;
size_t byte_offset = newsize >> bitmap_shift;
mask = mask >> (8*sizeof(char) - (newsize & bitmap_mask));
// (bitmap->array)[target_capacity-1] &= mask;
(bitmap->array)[byte_offset] &= mask;
}
return 0;
}
void bitmap_clear(bitmap_t *bitmap) {
memset(bitmap->array, 0, bitmap->capacity_bytes);
}