1*f491972cSAugustin Cavalier /*
2*f491972cSAugustin Cavalier * Copyright (c) 2007, Novell Inc.
3*f491972cSAugustin Cavalier *
4*f491972cSAugustin Cavalier * This program is licensed under the BSD license, read LICENSE.BSD
5*f491972cSAugustin Cavalier * for further information
6*f491972cSAugustin Cavalier */
7*f491972cSAugustin Cavalier
8*f491972cSAugustin Cavalier /*
9*f491972cSAugustin Cavalier * bitmap.c
10*f491972cSAugustin Cavalier *
11*f491972cSAugustin Cavalier */
12*f491972cSAugustin Cavalier
13*f491972cSAugustin Cavalier #include <stdlib.h>
14*f491972cSAugustin Cavalier #include <string.h>
15*f491972cSAugustin Cavalier
16*f491972cSAugustin Cavalier #include "bitmap.h"
17*f491972cSAugustin Cavalier #include "util.h"
18*f491972cSAugustin Cavalier
19*f491972cSAugustin Cavalier /* constructor */
20*f491972cSAugustin Cavalier void
map_init(Map * m,int n)21*f491972cSAugustin Cavalier map_init(Map *m, int n)
22*f491972cSAugustin Cavalier {
23*f491972cSAugustin Cavalier m->size = (n + 7) >> 3;
24*f491972cSAugustin Cavalier m->map = m->size ? solv_calloc(m->size, 1) : 0;
25*f491972cSAugustin Cavalier }
26*f491972cSAugustin Cavalier
27*f491972cSAugustin Cavalier /* destructor */
28*f491972cSAugustin Cavalier void
map_free(Map * m)29*f491972cSAugustin Cavalier map_free(Map *m)
30*f491972cSAugustin Cavalier {
31*f491972cSAugustin Cavalier m->map = solv_free(m->map);
32*f491972cSAugustin Cavalier m->size = 0;
33*f491972cSAugustin Cavalier }
34*f491972cSAugustin Cavalier
35*f491972cSAugustin Cavalier /* copy constructor t <- s */
36*f491972cSAugustin Cavalier void
map_init_clone(Map * t,Map * s)37*f491972cSAugustin Cavalier map_init_clone(Map *t, Map *s)
38*f491972cSAugustin Cavalier {
39*f491972cSAugustin Cavalier t->size = s->size;
40*f491972cSAugustin Cavalier if (s->size)
41*f491972cSAugustin Cavalier {
42*f491972cSAugustin Cavalier t->map = solv_malloc(s->size);
43*f491972cSAugustin Cavalier memcpy(t->map, s->map, s->size);
44*f491972cSAugustin Cavalier }
45*f491972cSAugustin Cavalier else
46*f491972cSAugustin Cavalier t->map = 0;
47*f491972cSAugustin Cavalier }
48*f491972cSAugustin Cavalier
49*f491972cSAugustin Cavalier /* grow a map */
50*f491972cSAugustin Cavalier void
map_grow(Map * m,int n)51*f491972cSAugustin Cavalier map_grow(Map *m, int n)
52*f491972cSAugustin Cavalier {
53*f491972cSAugustin Cavalier n = (n + 7) >> 3;
54*f491972cSAugustin Cavalier if (m->size < n)
55*f491972cSAugustin Cavalier {
56*f491972cSAugustin Cavalier m->map = solv_realloc(m->map, n);
57*f491972cSAugustin Cavalier memset(m->map + m->size, 0, n - m->size);
58*f491972cSAugustin Cavalier m->size = n;
59*f491972cSAugustin Cavalier }
60*f491972cSAugustin Cavalier }
61*f491972cSAugustin Cavalier
62*f491972cSAugustin Cavalier /* bitwise-ands maps t and s, stores the result in t. */
63*f491972cSAugustin Cavalier void
map_and(Map * t,Map * s)64*f491972cSAugustin Cavalier map_and(Map *t, Map *s)
65*f491972cSAugustin Cavalier {
66*f491972cSAugustin Cavalier unsigned char *ti, *si, *end;
67*f491972cSAugustin Cavalier ti = t->map;
68*f491972cSAugustin Cavalier si = s->map;
69*f491972cSAugustin Cavalier end = ti + (t->size < s->size ? t->size : s->size);
70*f491972cSAugustin Cavalier while (ti < end)
71*f491972cSAugustin Cavalier *ti++ &= *si++;
72*f491972cSAugustin Cavalier }
73*f491972cSAugustin Cavalier
74*f491972cSAugustin Cavalier /* bitwise-ors maps t and s, stores the result in t. */
75*f491972cSAugustin Cavalier void
map_or(Map * t,Map * s)76*f491972cSAugustin Cavalier map_or(Map *t, Map *s)
77*f491972cSAugustin Cavalier {
78*f491972cSAugustin Cavalier unsigned char *ti, *si, *end;
79*f491972cSAugustin Cavalier if (t->size < s->size)
80*f491972cSAugustin Cavalier map_grow(t, s->size << 3);
81*f491972cSAugustin Cavalier ti = t->map;
82*f491972cSAugustin Cavalier si = s->map;
83*f491972cSAugustin Cavalier end = ti + (t->size < s->size ? t->size : s->size);
84*f491972cSAugustin Cavalier while (ti < end)
85*f491972cSAugustin Cavalier *ti++ |= *si++;
86*f491972cSAugustin Cavalier }
87*f491972cSAugustin Cavalier
88*f491972cSAugustin Cavalier /* remove all set bits in s from t. */
89*f491972cSAugustin Cavalier void
map_subtract(Map * t,Map * s)90*f491972cSAugustin Cavalier map_subtract(Map *t, Map *s)
91*f491972cSAugustin Cavalier {
92*f491972cSAugustin Cavalier unsigned char *ti, *si, *end;
93*f491972cSAugustin Cavalier ti = t->map;
94*f491972cSAugustin Cavalier si = s->map;
95*f491972cSAugustin Cavalier end = ti + (t->size < s->size ? t->size : s->size);
96*f491972cSAugustin Cavalier while (ti < end)
97*f491972cSAugustin Cavalier *ti++ &= ~*si++;
98*f491972cSAugustin Cavalier }
99*f491972cSAugustin Cavalier
100*f491972cSAugustin Cavalier /* EOF */
101