bitmap: implement map_and and MAPSETALL
[platform/upstream/libsolv.git] / src / bitmap.c
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * bitmap.c
10  * 
11  */
12
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "bitmap.h"
17 #include "util.h"
18
19 /* constructor */
20 void
21 map_init(Map *m, int n)
22 {
23   m->size = (n + 7) >> 3;
24   m->map = m->size ? solv_calloc(m->size, 1) : 0;
25 }
26
27 /* destructor */
28 void
29 map_free(Map *m)
30 {
31   m->map = solv_free(m->map);
32   m->size = 0;
33 }
34
35 /* copy constructor t <- s */
36 void
37 map_init_clone(Map *t, Map *s)
38 {
39   t->size = s->size;
40   if (s->size)
41     {
42       t->map = solv_malloc(s->size);
43       memcpy(t->map, s->map, s->size);
44     }
45   else
46     t->map = 0;
47 }
48
49 /* grow a map */
50 void
51 map_grow(Map *m, int n)
52 {
53   n = (n + 7) >> 3;
54   if (m->size < n)
55     {
56       m->map = solv_realloc(m->map, n);
57       memset(m->map + m->size, 0, n - m->size);
58       m->size = n;
59     }
60 }
61
62 /* bitwise-ands same-sized maps t and s, stores the result in t. */
63 void
64 map_and(Map *t, Map *s)
65 {
66     unsigned char *ti, *si, *end;
67     ti = t->map;
68     si = s->map;
69     end = ti + t->size;
70     while (ti < end)
71         *ti++ &= *si++;
72 }
73
74 /* EOF */