summaryrefslogtreecommitdiff
path: root/src/bitmap.c
blob: e004bf2f7f950c00feef4d0d1155af6f5e655586 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (c) 2007, Novell Inc.
 *
 * This program is licensed under the BSD license, read LICENSE.BSD
 * for further information
 */

/*
 * bitmap.c
 *
 */

#include <stdlib.h>
#include <string.h>

#include "bitmap.h"
#include "util.h"

/* constructor */
void
map_init(Map *m, int n)
{
  m->size = (n + 7) >> 3;
  m->map = m->size ? solv_calloc(m->size, 1) : 0;
}

/* destructor */
void
map_free(Map *m)
{
  m->map = solv_free(m->map);
  m->size = 0;
}

/* copy constructor t <- s */
void
map_init_clone(Map *t, Map *s)
{
  t->size = s->size;
  if (s->size)
    {
      t->map = solv_malloc(s->size);
      memcpy(t->map, s->map, s->size);
    }
  else
    t->map = 0;
}

/* grow a map */
void
map_grow(Map *m, int n)
{
  n = (n + 7) >> 3;
  if (m->size < n)
    {
      m->map = solv_realloc(m->map, n);
      memset(m->map + m->size, 0, n - m->size);
      m->size = n;
    }
}

/* bitwise-ands maps t and s, stores the result in t. */
void
map_and(Map *t, Map *s)
{
  unsigned char *ti, *si, *end;
  ti = t->map;
  si = s->map;
  end = ti + (t->size < s->size ? t->size : s->size);
  while (ti < end)
    *ti++ &= *si++;
}

/* bitwise-ors maps t and s, stores the result in t. */
void
map_or(Map *t, Map *s)
{
  unsigned char *ti, *si, *end;
  if (t->size < s->size)
    map_grow(t, s->size << 3);
  ti = t->map;
  si = s->map;
  end = ti + (t->size < s->size ? t->size : s->size);
  while (ti < end)
    *ti++ |= *si++;
}

/* remove all set bits in s from t. */
void
map_subtract(Map *t, Map *s)
{
  unsigned char *ti, *si, *end;
  ti = t->map;
  si = s->map;
  end = ti + (t->size < s->size ? t->size : s->size);
  while (ti < end)
    *ti++ &= ~*si++;
}

void
map_invertall(Map *m)
{
  unsigned char *ti, *end;
  ti = m->map;
  end = ti + m->size;
  while (ti < end)
    *ti++ ^= 0xff;
}

/* EOF */