2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
5 #include <asn_internal.h>
6 #include <asn_SET_OF.h>
10 * Add another element into the set.
13 asn_set_add(void *asn_set_of_x, void *ptr) {
14 asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x);
16 if(as == 0 || ptr == 0) {
17 errno = EINVAL; /* Invalid arguments */
22 * Make sure there's enough space to insert an element.
24 if(as->count == as->size) {
25 int _newsize = as->size ? (as->size << 1) : 4;
27 _new_arr = REALLOC(as->array, _newsize * sizeof(as->array[0]));
29 as->array = (void **)_new_arr;
37 as->array[as->count++] = ptr;
43 asn_set_del(void *asn_set_of_x, int number, int _do_free) {
44 asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x);
48 if(number < 0 || number >= as->count)
51 if(_do_free && as->free) {
52 ptr = as->array[number];
57 as->array[number] = as->array[--as->count];
60 * Invoke the third-party function only when the state
61 * of the parent structure is consistent.
63 if(ptr) as->free(ptr);
68 * Free the contents of the set, do not free the set itself.
71 asn_set_empty(void *asn_set_of_x) {
72 asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x);
78 as->free(as->array[as->count]);