Statistics
| Branch: | Revision:

grapes / som / ChunkIDSet / chunkids_ops.c @ 747990ee

History | View | Annotate | Download (2.98 KB)

1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 */
6

    
7
#include <stdlib.h>
8
#include <stdint.h>
9
#include <limits.h>
10
#include <string.h>
11
#include <assert.h>
12

    
13
#include "chunkids_private.h"
14
#include "chunkidset.h"
15
#include "config.h"
16

    
17
#define DEFAULT_SIZE_INCREMENT 32
18

    
19
struct chunkID_set *chunkID_set_init(const char *config)
20
{
21
  struct chunkID_set *p;
22
  struct tag *cfg_tags;
23
  int res;
24
  const char *type;
25

    
26
  p = malloc(sizeof(struct chunkID_set));
27
  if (p == NULL) {
28
    return NULL;
29
  }
30
  p->n_elements = 0;
31
  cfg_tags = config_parse(config);
32
  res = config_value_int(cfg_tags, "size", &p->size);
33
  if (!res) {
34
    p->size = 0;
35
  }
36
  if (p->size) {
37
    p->elements = malloc(p->size * sizeof(int));
38
  } else {
39
    p->elements = NULL;
40
  }
41
  p->type = CIST_PRIORITY;
42
  type = config_value_str(cfg_tags, "type");
43
  if (type) {
44
    if (!memcmp(type, "priority", strlen(type) - 1)) {
45
      p->type = CIST_PRIORITY;
46
    } else if (!memcmp(type, "bitmap", strlen(type) - 1)) {
47
      p->type = CIST_BITMAP;
48
    } else {
49
      chunkID_set_free(p);
50
      free(cfg_tags);
51

    
52
      return NULL; 
53
    }
54
  }
55
  free(cfg_tags);
56
  assert(p->type == CIST_PRIORITY || p->type == CIST_BITMAP);
57

    
58
  return p;
59
}
60

    
61
int chunkID_set_add_chunk(struct chunkID_set *h, int chunk_id)
62
{
63
  if (chunkID_set_check(h, chunk_id) >= 0) {
64
    return 0;
65
  }
66

    
67
  if (h->n_elements == h->size) {
68
    int *res;
69

    
70
    res = realloc(h->elements, (h->size + DEFAULT_SIZE_INCREMENT) * sizeof(int));
71
    if (res == NULL) {
72
      return -1;
73
    }
74
    h->size += DEFAULT_SIZE_INCREMENT;
75
    h->elements = res;
76
  }
77
  h->elements[h->n_elements++] = chunk_id;
78

    
79
  return h->n_elements;
80
}
81

    
82
int chunkID_set_size(const struct chunkID_set *h)
83
{
84
  return h->n_elements;
85
}
86

    
87
int chunkID_set_get_chunk(const struct chunkID_set *h, int i)
88
{
89
  if (i < h->n_elements) {
90
    return h->elements[i];
91
  }
92

    
93
  return -1;
94
}
95

    
96
int chunkID_set_check(const struct chunkID_set *h, int chunk_id)
97
{
98
  int i;
99

    
100
  for (i = 0; i < h->n_elements; i++) {
101
    if (h->elements[i] == chunk_id) {
102
      return i;
103
    }
104
  }
105

    
106
  return -1;
107
}
108

    
109
int chunkID_set_get_earliest(const struct chunkID_set *h)
110
{
111
  int i, min;
112

    
113
  min = INT_MAX;
114
  for (i = 0; i < h->n_elements; i++) {
115
    min = (h->elements[i] < min) ? h->elements[i] : min;
116
  }
117

    
118
  return min;
119
}
120

    
121
int chunkID_set_get_latest(const struct chunkID_set *h)
122
{
123
  int i, max;
124

    
125
  max = INT_MIN;
126
  for (i = 0; i < h->n_elements; i++) {
127
    max = (h->elements[i] > max) ? h->elements[i] : max;
128
  }
129

    
130
  return max;
131
}
132

    
133
int chunkID_set_union(struct chunkID_set *h, struct chunkID_set *a)
134
{
135
  int i;
136

    
137
  for (i = 0; i < a->n_elements; i++) {
138
    int ret = chunkID_set_add_chunk(h,a->elements[i]);
139
    if (ret < 0) return ret;
140
  }
141
  return h->n_elements;
142
}
143

    
144
void chunkID_set_clear(struct chunkID_set *h, int size)
145
{
146
  h->n_elements = 0;
147
  h->size = size;
148
  h->elements = realloc(h->elements, size * sizeof(int));
149
  if (h->elements == NULL) {
150
    h->size = 0;
151
  }
152
}
153

    
154
void chunkID_set_free(struct chunkID_set *h)
155
{
156
  chunkID_set_clear(h,0);
157
  free(h->elements);
158
  free(h);
159
}