grapes / som / ChunkIDSet / chunkids_ops.c @ adadc388
History | View | Annotate | Download (2.68 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 <assert.h> |
11 |
|
12 |
#include "chunkids_private.h" |
13 |
#include "chunkidset.h" |
14 |
#include "config.h" |
15 |
|
16 |
#define DEFAULT_SIZE_INCREMENT 32 |
17 |
|
18 |
struct chunkID_set *chunkID_set_init(const char *config) |
19 |
{ |
20 |
struct chunkID_set *p;
|
21 |
struct tag *cfg_tags;
|
22 |
int res;
|
23 |
|
24 |
p = malloc(sizeof(struct chunkID_set)); |
25 |
if (p == NULL) { |
26 |
return NULL; |
27 |
} |
28 |
p->n_elements = 0;
|
29 |
cfg_tags = config_parse(config); |
30 |
res = config_value_int(cfg_tags, "size", &p->size);
|
31 |
if (!res) {
|
32 |
p->size = 0;
|
33 |
} |
34 |
if (p->size) {
|
35 |
p->elements = malloc(p->size * sizeof(int)); |
36 |
} else {
|
37 |
p->elements = NULL;
|
38 |
} |
39 |
res = config_value_int(cfg_tags, "type", &p->type);
|
40 |
if (!res) {
|
41 |
p->type = CIST_PRIORITY; |
42 |
} |
43 |
if(!p->type)
|
44 |
p->type = CIST_PRIORITY; |
45 |
free(cfg_tags); |
46 |
return p;
|
47 |
} |
48 |
|
49 |
int chunkID_set_add_chunk(struct chunkID_set *h, int chunk_id) |
50 |
{ |
51 |
if (chunkID_set_check(h, chunk_id) >= 0) { |
52 |
return 0; |
53 |
} |
54 |
|
55 |
if (h->n_elements == h->size) {
|
56 |
int *res;
|
57 |
|
58 |
res = realloc(h->elements, (h->size + DEFAULT_SIZE_INCREMENT) * sizeof(int)); |
59 |
if (res == NULL) { |
60 |
return -1; |
61 |
} |
62 |
h->size += DEFAULT_SIZE_INCREMENT; |
63 |
h->elements = res; |
64 |
} |
65 |
h->elements[h->n_elements++] = chunk_id; |
66 |
|
67 |
return h->n_elements;
|
68 |
} |
69 |
|
70 |
int chunkID_set_size(const struct chunkID_set *h) |
71 |
{ |
72 |
return h->n_elements;
|
73 |
} |
74 |
|
75 |
int chunkID_set_get_chunk(const struct chunkID_set *h, int i) |
76 |
{ |
77 |
if (i < h->n_elements) {
|
78 |
return h->elements[i];
|
79 |
} |
80 |
|
81 |
return -1; |
82 |
} |
83 |
|
84 |
int chunkID_set_check(const struct chunkID_set *h, int chunk_id) |
85 |
{ |
86 |
int i;
|
87 |
|
88 |
for (i = 0; i < h->n_elements; i++) { |
89 |
if (h->elements[i] == chunk_id) {
|
90 |
return i;
|
91 |
} |
92 |
} |
93 |
|
94 |
return -1; |
95 |
} |
96 |
|
97 |
int chunkID_set_get_earliest(const struct chunkID_set *h) |
98 |
{ |
99 |
int i, min;
|
100 |
|
101 |
min = INT_MAX; |
102 |
for (i = 0; i < h->n_elements; i++) { |
103 |
min = (h->elements[i] < min) ? h->elements[i] : min; |
104 |
} |
105 |
|
106 |
return min;
|
107 |
} |
108 |
|
109 |
int chunkID_set_get_latest(const struct chunkID_set *h) |
110 |
{ |
111 |
int i, max;
|
112 |
|
113 |
max = INT_MIN; |
114 |
for (i = 0; i < h->n_elements; i++) { |
115 |
max = (h->elements[i] > max) ? h->elements[i] : max; |
116 |
} |
117 |
|
118 |
return max;
|
119 |
} |
120 |
|
121 |
int chunkID_set_union(struct chunkID_set *h, struct chunkID_set *a) |
122 |
{ |
123 |
int i;
|
124 |
|
125 |
for (i = 0; i < a->n_elements; i++) { |
126 |
int ret = chunkID_set_add_chunk(h,a->elements[i]);
|
127 |
if (ret < 0) return ret; |
128 |
} |
129 |
return h->n_elements;
|
130 |
} |
131 |
|
132 |
void chunkID_set_clear(struct chunkID_set *h, int size) |
133 |
{ |
134 |
h->n_elements = 0;
|
135 |
h->size = size; |
136 |
h->elements = realloc(h->elements, size * sizeof(int)); |
137 |
if (h->elements == NULL) { |
138 |
h->size = 0;
|
139 |
} |
140 |
} |
141 |
|
142 |
void chunkID_set_free(struct chunkID_set *h) |
143 |
{ |
144 |
chunkID_set_clear(h,0);
|
145 |
free(h->elements); |
146 |
free(h); |
147 |
} |