Imported Upstream version 0.6.35
[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 target <- source */
36 void
37 map_init_clone(Map *target, const Map *source)
38 {
39   target->size = source->size;
40   if (source->size)
41     {
42       target->map = solv_malloc(source->size);
43       memcpy(target->map, source->map, source->size);
44     }
45   else
46     target->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 maps t and s, stores the result in t. */
63 void
64 map_and(Map *t, const Map *s)
65 {
66   unsigned char *ti, *si, *end;
67   ti = t->map;
68   si = s->map;
69   end = ti + (t->size < s->size ? t->size : s->size);
70   while (ti < end)
71     *ti++ &= *si++;
72 }
73
74 /* bitwise-ors maps t and s, stores the result in t. */
75 void
76 map_or(Map *t, const Map *s)
77 {
78   unsigned char *ti, *si, *end;
79   if (t->size < s->size)
80     map_grow(t, s->size << 3);
81   ti = t->map;
82   si = s->map;
83   end = ti + (t->size < s->size ? t->size : s->size);
84   while (ti < end)
85     *ti++ |= *si++;
86 }
87
88 /* remove all set bits in s from t. */
89 void
90 map_subtract(Map *t, const Map *s)
91 {
92   unsigned char *ti, *si, *end;
93   ti = t->map;
94   si = s->map;
95   end = ti + (t->size < s->size ? t->size : s->size);
96   while (ti < end)
97     *ti++ &= ~*si++;
98 }
99
100 void
101 map_invertall(Map *m)
102 {
103   unsigned char *ti, *end;
104   ti = m->map;
105   end = ti + m->size;
106   while (ti < end)
107     *ti++ ^= 0xff;
108 }
109
110 /* EOF */