grapes / som / ChunkIDSet / chunkids_ops.c @ 747990ee
History | View | Annotate | Download (2.98 KB)
1 | 8ab58ec7 | Luca Abeni | /*
|
---|---|---|---|
2 | * Copyright (c) 2010 Luca Abeni
|
||
3 | *
|
||
4 | * This is free software; see lgpl-2.1.txt
|
||
5 | */
|
||
6 | |||
7 | 286ee331 | Luca Abeni | #include <stdlib.h> |
8 | 412fbdcc | Luca Abeni | #include <stdint.h> |
9 | f621745b | Csaba Kiraly | #include <limits.h> |
10 | 747990ee | Luca Abeni | #include <string.h> |
11 | adadc388 | Csaba Kiraly | #include <assert.h> |
12 | 286ee331 | Luca Abeni | |
13 | 412fbdcc | Luca Abeni | #include "chunkids_private.h" |
14 | 286ee331 | Luca Abeni | #include "chunkidset.h" |
15 | ea821d90 | Csaba Kiraly | #include "config.h" |
16 | 286ee331 | Luca Abeni | |
17 | #define DEFAULT_SIZE_INCREMENT 32 |
||
18 | |||
19 | ea821d90 | Csaba Kiraly | struct chunkID_set *chunkID_set_init(const char *config) |
20 | 286ee331 | Luca Abeni | { |
21 | struct chunkID_set *p;
|
||
22 | ea821d90 | Csaba Kiraly | struct tag *cfg_tags;
|
23 | 747990ee | Luca Abeni | int res;
|
24 | const char *type; |
||
25 | 286ee331 | Luca Abeni | |
26 | p = malloc(sizeof(struct chunkID_set)); |
||
27 | if (p == NULL) { |
||
28 | return NULL; |
||
29 | } |
||
30 | a2971dec | CsabaKiraly | p->n_elements = 0;
|
31 | ea821d90 | Csaba Kiraly | cfg_tags = config_parse(config); |
32 | res = config_value_int(cfg_tags, "size", &p->size);
|
||
33 | adadc388 | Csaba Kiraly | if (!res) {
|
34 | ea821d90 | Csaba Kiraly | p->size = 0;
|
35 | } |
||
36 | 286ee331 | Luca Abeni | if (p->size) {
|
37 | a2971dec | CsabaKiraly | p->elements = malloc(p->size * sizeof(int)); |
38 | 286ee331 | Luca Abeni | } else {
|
39 | a2971dec | CsabaKiraly | p->elements = NULL;
|
40 | 286ee331 | Luca Abeni | } |
41 | 92006fb2 | Luca Abeni | p->type = CIST_PRIORITY; |
42 | 747990ee | Luca Abeni | 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 | 92006fb2 | Luca Abeni | } |
54 | ea821d90 | Csaba Kiraly | } |
55 | 747990ee | Luca Abeni | free(cfg_tags); |
56 | 030c2402 | Csaba Kiraly | assert(p->type == CIST_PRIORITY || p->type == CIST_BITMAP); |
57 | 747990ee | Luca Abeni | |
58 | 286ee331 | Luca Abeni | return p;
|
59 | } |
||
60 | |||
61 | int chunkID_set_add_chunk(struct chunkID_set *h, int chunk_id) |
||
62 | { |
||
63 | b263e43d | Luca Abeni | if (chunkID_set_check(h, chunk_id) >= 0) { |
64 | 286ee331 | Luca Abeni | return 0; |
65 | } |
||
66 | |||
67 | a2971dec | CsabaKiraly | if (h->n_elements == h->size) {
|
68 | 286ee331 | Luca Abeni | int *res;
|
69 | |||
70 | a911d3e3 | CsabaKiraly | res = realloc(h->elements, (h->size + DEFAULT_SIZE_INCREMENT) * sizeof(int)); |
71 | 286ee331 | Luca Abeni | if (res == NULL) { |
72 | return -1; |
||
73 | } |
||
74 | h->size += DEFAULT_SIZE_INCREMENT; |
||
75 | a2971dec | CsabaKiraly | h->elements = res; |
76 | 286ee331 | Luca Abeni | } |
77 | a2971dec | CsabaKiraly | h->elements[h->n_elements++] = chunk_id; |
78 | 286ee331 | Luca Abeni | |
79 | a2971dec | CsabaKiraly | return h->n_elements;
|
80 | 286ee331 | Luca Abeni | } |
81 | |||
82 | f3480090 | CsabaKiraly | int chunkID_set_size(const struct chunkID_set *h) |
83 | 286ee331 | Luca Abeni | { |
84 | a2971dec | CsabaKiraly | return h->n_elements;
|
85 | 286ee331 | Luca Abeni | } |
86 | |||
87 | int chunkID_set_get_chunk(const struct chunkID_set *h, int i) |
||
88 | { |
||
89 | a2971dec | CsabaKiraly | if (i < h->n_elements) {
|
90 | return h->elements[i];
|
||
91 | 286ee331 | Luca Abeni | } |
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 | a2971dec | CsabaKiraly | for (i = 0; i < h->n_elements; i++) { |
101 | if (h->elements[i] == chunk_id) {
|
||
102 | 286ee331 | Luca Abeni | return i;
|
103 | } |
||
104 | } |
||
105 | |||
106 | return -1; |
||
107 | } |
||
108 | |||
109 | f621745b | Csaba Kiraly | 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 | a2971dec | CsabaKiraly | 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 | f3480090 | CsabaKiraly | void chunkID_set_clear(struct chunkID_set *h, int size) |
145 | 286ee331 | Luca Abeni | { |
146 | a2971dec | CsabaKiraly | h->n_elements = 0;
|
147 | 286ee331 | Luca Abeni | h->size = size; |
148 | a911d3e3 | CsabaKiraly | h->elements = realloc(h->elements, size * sizeof(int)); |
149 | a2971dec | CsabaKiraly | if (h->elements == NULL) { |
150 | 286ee331 | Luca Abeni | h->size = 0;
|
151 | } |
||
152 | } |
||
153 | 8484348e | Csaba Kiraly | |
154 | void chunkID_set_free(struct chunkID_set *h) |
||
155 | { |
||
156 | chunkID_set_clear(h,0);
|
||
157 | free(h->elements); |
||
158 | free(h); |
||
159 | } |