35 map->free_fnc = free_fnc;
43 if (map->max_val == 0) {
48 if (map->free_fnc !=
NULL) {
51 for (i = 0; i < map->n_val; i++) map->free_fnc(map->vals[i].val);
57 if (map->mark !=
NULL) {
71 static int map_resize(
struct hecmw_map_int *map,
size_t new_max_val) {
83 if (new_max_val == 0) {
102 map->pairs = new_pairs;
105 map->max_val = new_max_val;
114 if (map->max_val == 0)
119 return map_resize(map, new_max_val);
125 if (map->n_val == map->max_val)
128 map->vals[map->n_val].key =
key;
129 map->vals[map->n_val].val = value;
131 map->pairs[map->n_val].key =
key;
132 map->pairs[map->n_val].local = map->n_val;
134 if (map->n_val > 0 && map->sorted) {
135 int key_prev = map->vals[map->n_val - 1].key;
137 if (key_prev >
key) map->sorted = map->checked = 0;
139 if (map->checked && key_prev ==
key) map->checked = 0;
147 static int pair_cmp(
const void *v1,
const void *v2) {
153 if (p1->
key < p2->
key)
return -1;
154 if (p1->
key > p2->
key)
return 1;
159 size_t i, n_dup = 0, n = 1;
163 if (map->checked)
return 0;
173 for (i = 1; i < map->n_val; i++) {
174 if (map->pairs[i - n].key == map->pairs[i].key) {
176 if (map->pairs[i - n].local < map->pairs[i].local) {
192 map_resize(map, map->n_val);
197 static int map_search(
const struct hecmw_map_int *map,
int key,
size_t *index) {
198 size_t left, right, center;
206 right = map->n_val - 1;
208 while (left <= right) {
209 center = (left + right) / 2;
210 ckey = map->pairs[center].key;
217 *index = map->pairs[center].local;
248 return map->vals[
local].val;
265 if (map->iter == map->n_val) {
271 *
key = map->vals[map->iter].key;
272 if (value !=
NULL) *value = map->vals[map->iter].val;
282 if (map->mark !=
NULL) {
288 if (map->mark ==
NULL) {
315 HECMW_assert(0 <= map->iter && map->iter <= map->n_val);
330 for (i = 0; i < map->n_val; i++) {
331 map->pairs[i].key = map->vals[i].key;
332 map->pairs[i].local = i;
333 if (i > 0 && map->vals[i].key < map->vals[i - 1].key) sorted = 0;
347 for (i = 0; i < map->n_val; i++) {
349 if (n_del > 0) map->vals[i - n_del] = map->vals[i];
351 if (map->free_fnc !=
NULL) map->free_fnc(map->vals[i].val);
int HECMW_bit_array_init(struct hecmw_bit_array *ba, size_t len)
int HECMW_bit_array_get(struct hecmw_bit_array *ba, size_t index)
void HECMW_bit_array_set(struct hecmw_bit_array *ba, size_t index)
void HECMW_bit_array_unset(struct hecmw_bit_array *ba, size_t index)
void HECMW_bit_array_set_all(struct hecmw_bit_array *ba)
void HECMW_bit_array_finalize(struct hecmw_bit_array *ba)
#define HECMW_realloc(ptr, size)
#define HECMW_malloc(size)
size_t HECMW_map_int_nval(const struct hecmw_map_int *map)
int HECMW_map_int_add(struct hecmw_map_int *map, int key, void *value)
int HECMW_map_int_iter_next(struct hecmw_map_int *map, int *key, void **value)
int HECMW_map_int_mark_init(struct hecmw_map_int *map)
int HECMW_map_int_mark(struct hecmw_map_int *map, int key)
int HECMW_map_int_iter_next_unmarked(struct hecmw_map_int *map, int *key, void **value)
int HECMW_map_int_del_unmarked(struct hecmw_map_int *map)
void * HECMW_map_int_get(const struct hecmw_map_int *map, int key)
size_t HECMW_map_int_check_dup(struct hecmw_map_int *map)
void HECMW_map_int_iter_init(struct hecmw_map_int *map)
void HECMW_map_int_finalize(struct hecmw_map_int *map)
int HECMW_map_int_key2local(const struct hecmw_map_int *map, int key, size_t *local)
int HECMW_map_int_init(struct hecmw_map_int *map, void(*free_fnc)(void *))
#define HECMW_assert(cond)