22 #include <grass/linkm.h>
23 #include <grass/bitmap.h>
26 #define BM_col_to_byte(x) ((x)/8)
27 #define BM_col_to_bit(x) ((x)%8)
49 if (
NULL == (map = (
struct BM *)
malloc(
sizeof(
struct BM))))
51 map->bytes = (x + 7) / 8;
53 if (
NULL == (map->data = (
unsigned char *)
54 malloc(
sizeof(
struct BMlink *) * y)))
62 map->token =
link_init(
sizeof(
struct BMlink));
64 for (i = 0; i < map->rows; i++) {
65 ((
struct BMlink **)(map->data))[i] =
66 (
struct BMlink *)
link_new(map->token);
67 ((
struct BMlink **)(map->data))[i]->
count = x;
68 ((
struct BMlink **)(map->data))[i]->val = 0;
69 ((
struct BMlink **)(map->data))[i]->next =
NULL;
93 struct BMlink *p, *tmp;
95 for (i = 0; i < map->rows; i++) {
96 p = ((
struct BMlink **)(map->data))[i];
130 struct BMlink *p, *p2, *prev;
137 p = ((
struct BMlink **)(map->data))[y];
140 if (cur_x + p->count > x) {
155 dist_b = (cur_x + p->count - 1) - x;
158 if (dist_b == 0 && p->next && p->next->val == val) {
159 if (dist_a == 0 && x > 0) {
160 if (prev !=
NULL && prev->val == val) {
161 prev->count += p->next->count + 1;
162 prev->next = p->next->next;
171 if (dist_b == 0 && p->next && p->next->val == val) {
176 prev->next = p->next;
179 ((
struct BMlink **)(map->data))[y] = p->next;
187 if (dist_a == 0 && x > 0) {
189 if (prev !=
NULL && prev->val == val) {
193 prev->next = p->next;
204 p2 = (
struct BMlink *)
link_new(map->token);
213 p2 = (
struct BMlink *)
link_new(map->token);
251 p = ((
struct BMlink **)(map->data))[y];
253 if (cur_x + p->count > x)
278 size = map->rows *
sizeof(
struct BMlink *);
279 for (i = 0; i < map->rows; i++) {
280 p = ((
struct BMlink **)(map->data))[i];
282 size +=
sizeof(
struct BMlink);
307 for (i = 0; i < map->rows; i++) {
308 p = ((
struct BMlink **)(map->data))[i];
310 fprintf(stdout,
"(%2d %2d) ", p->count, p->val);
313 fprintf(stdout,
"\n");
339 p = ((
struct BMlink **)(map->data))[i];
341 fprintf(stdout,
"(%2d %2d) ", p->count, p->val);
344 fprintf(stdout,
"\n");
372 fwrite(&c,
sizeof(
char),
sizeof(
char), fp);
374 fwrite(BM_TEXT, BM_TEXT_LEN,
sizeof(
char), fp);
377 fwrite(&c,
sizeof(
char),
sizeof(
char), fp);
379 fwrite(&(map->rows),
sizeof(map->rows),
sizeof(
char), fp);
381 fwrite(&(map->cols),
sizeof(map->cols),
sizeof(
char), fp);
383 for (y = 0; y < map->rows; y++) {
385 p = ((
struct BMlink **)(map->data))[y];
393 fwrite(&i,
sizeof(i),
sizeof(
char), fp);
397 p = ((
struct BMlink **)(map->data))[y];
400 fwrite(&i,
sizeof(i),
sizeof(
char), fp);
403 fwrite(&i,
sizeof(i),
sizeof(
char), fp);
struct link_head * link_init(int size)
struct link_head * link_new(struct link_head *Head)
void link_cleanup(struct link_head *Head)
struct BM * BM_create_sparse(int x, int y)
Create a sparse bitmap of dimension 'x'/'y'.
void link_set_chunk_size(int size)
int BM_dump_map_row_sparse(struct BM *map, int y)
Debugging code to dump out structure of links for single row.
int BM_file_write_sparse(FILE *fp, struct BM *map)
Write sparse bitmap matrix out to disk file 'fp'. NOTE: 'fp' must already be opened and later closed ...
void link_dispose(struct link_head *Head, VOID_T *ptr)
int BM_get_map_size_sparse(struct BM *map)
Returns size of sparse bitmap in bytes.
int BM_get_sparse(struct BM *map, int x, int y)
Returns sparse bitmap value at location 'x'/'y'.
int BM_destroy_sparse(struct BM *map)
Destroy sparse bitmap and free all associated memory.
int BM_dump_map_sparse(struct BM *map)
Debugging code to dump out structure of links.
int BM_set_sparse(struct BM *map, int x, int y, int val)
Set sparse bitmap value to 'val' at location 'x'/'y'.