22 #include <grass/linkm.h>
23 #include <grass/bitmap.h>
25 #define BM_col_to_byte(x) ((x) >> 3)
26 #define BM_col_to_bit(x) ((x) & 7)
46 if (
NULL == (map = (
struct BM *)malloc(
sizeof(
struct BM))))
48 map->bytes = (
x + 7) / 8;
51 (map->data = (
unsigned char *)malloc(
sizeof(
struct BMlink *) * y))) {
61 map->token =
link_init(
sizeof(
struct BMlink));
63 for (i = 0; i < map->rows; i++) {
64 ((
struct BMlink **)(map->data))[i] =
65 (
struct BMlink *)
link_new(map->token);
66 ((
struct BMlink **)(map->data))[i]->
count =
x;
67 ((
struct BMlink **)(map->data))[i]->val = 0;
68 ((
struct BMlink **)(map->data))[i]->next =
NULL;
89 struct BMlink *p, *tmp;
91 for (i = 0; i < map->rows; i++) {
92 p = ((
struct BMlink **)(map->data))[i];
124 struct BMlink *p, *p2, *prev;
131 p = ((
struct BMlink **)(map->data))[y];
134 if (
cur_x + p->count >
x) {
148 dist_b = (
cur_x + p->count - 1) -
x;
151 if (dist_b == 0 && p->next && p->next->val == val) {
152 if (dist_a == 0 &&
x > 0) {
153 if (prev !=
NULL && prev->val == val) {
154 prev->count += p->next->count + 1;
155 prev->next = p->next->next;
164 if (dist_b == 0 && p->next && p->next->val == val) {
169 prev->next = p->next;
172 ((
struct BMlink **)(map->data))[y] = p->next;
180 if (dist_a == 0 &&
x > 0) {
182 if (prev !=
NULL && prev->val == val) {
186 prev->next = p->next;
197 p2 = (
struct BMlink *)
link_new(map->token);
206 p2 = (
struct BMlink *)
link_new(map->token);
241 p = ((
struct BMlink **)(map->data))[y];
266 size = (size_t)map->rows *
sizeof(
struct BMlink *);
267 for (i = 0; i < map->rows; i++) {
268 p = ((
struct BMlink **)(map->data))[i];
270 size +=
sizeof(
struct BMlink);
293 for (i = 0; i < map->rows; i++) {
294 p = ((
struct BMlink **)(map->data))[i];
296 fprintf(stdout,
"(%2d %2d) ", p->count, p->val);
299 fprintf(stdout,
"\n");
323 p = ((
struct BMlink **)(map->data))[i];
325 fprintf(stdout,
"(%2d %2d) ", p->count, p->val);
328 fprintf(stdout,
"\n");
353 fwrite(&c,
sizeof(
char),
sizeof(
char), fp);
355 fwrite(BM_TEXT, BM_TEXT_LEN,
sizeof(
char), fp);
358 fwrite(&c,
sizeof(
char),
sizeof(
char), fp);
360 fwrite(&(map->rows),
sizeof(map->rows),
sizeof(
char), fp);
362 fwrite(&(map->cols),
sizeof(map->cols),
sizeof(
char), fp);
364 for (y = 0; y < map->rows; y++) {
366 p = ((
struct BMlink **)(map->data))[y];
375 fwrite(&i,
sizeof(i),
sizeof(
char), fp);
378 p = ((
struct BMlink **)(map->data))[y];
381 fwrite(&i,
sizeof(i),
sizeof(
char), fp);
384 fwrite(&i,
sizeof(i),
sizeof(
char), fp);
void link_dispose(struct link_head *Head, VOID_T *ptr)
void link_cleanup(struct link_head *Head)
struct link_head * link_init(int size)
void link_set_chunk_size(int size)
VOID_T * link_new(struct link_head *Head)
struct BM * BM_create_sparse(int x, int y)
Create a sparse bitmap of dimension 'x'/'y'.
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 ...
int BM_get_sparse(struct BM *map, int x, int y)
Returns sparse bitmap value at location 'x'/'y'.
size_t BM_get_map_size_sparse(struct BM *map)
Returns size of sparse bitmap in bytes.
int BM_destroy_sparse(struct BM *map)
Destroy sparse bitmap and free all associated memory.
int BM_set_sparse(struct BM *map, int x, int y, int val)
Set sparse bitmap value to 'val' at location 'x'/'y'.
int BM_dump_map_sparse(struct BM *map)
Debugging code to dump out structure of links.