3 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
7 #include <asn_SET_OF.h>
11 * Add another element into the set.
14 asn_set_add(void *asn_set_of_x, void *ptr) {
15 asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x);
17 if(as == 0 || ptr == 0) {
18 errno = EINVAL; /* Invalid arguments */
23 * Make sure there's enough space to insert an element.
25 if(as->count == as->size) {
26 int _newsize = as->size ? (as->size << 1) : 4;
28 _new_arr = REALLOC(as->array, _newsize * sizeof(as->array[0]));
30 as->array = (void **)_new_arr;
38 as->array[as->count++] = ptr;
44 asn_set_del(void *asn_set_of_x, int number, int _do_free) {
45 asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x);
49 if(number < 0 || number >= as->count)
52 if(_do_free && as->free) {
53 ptr = as->array[number];
58 as->array[number] = as->array[--as->count];
61 * Invoke the third-party function only when the state
62 * of the parent structure is consistent.
64 if(ptr) as->free(ptr);
69 * Free the contents of the set, do not free the set itself.
72 asn_set_empty(void *asn_set_of_x) {
73 asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x);
79 as->free(as->array[as->count]);