--- /dev/null
+/*-\r
+ * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.\r
+ * Redistribution and modifications are permitted subject to BSD license.\r
+ */\r
+#include <asn_internal.h>\r
+#include <asn_SET_OF.h>\r
+#include <errno.h>\r
+\r
+/*\r
+ * Add another element into the set.\r
+ */\r
+int\r
+asn_set_add(void *asn_set_of_x, void *ptr) {\r
+ asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x);\r
+\r
+ if(as == 0 || ptr == 0) {\r
+ errno = EINVAL; /* Invalid arguments */\r
+ return -1;\r
+ }\r
+\r
+ /*\r
+ * Make sure there's enough space to insert an element.\r
+ */\r
+ if(as->count == as->size) {\r
+ int _newsize = as->size ? (as->size << 1) : 4;\r
+ void *_new_arr;\r
+ _new_arr = REALLOC(as->array, _newsize * sizeof(as->array[0]));\r
+ if(_new_arr) {\r
+ as->array = (void **)_new_arr;\r
+ as->size = _newsize;\r
+ } else {\r
+ /* ENOMEM */\r
+ return -1;\r
+ }\r
+ }\r
+\r
+ as->array[as->count++] = ptr;\r
+\r
+ return 0;\r
+}\r
+\r
+void\r
+asn_set_del(void *asn_set_of_x, int number, int _do_free) {\r
+ asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x);\r
+\r
+ if(as) {\r
+ void *ptr;\r
+ if(number < 0 || number >= as->count)\r
+ return;\r
+\r
+ if(_do_free && as->free) {\r
+ ptr = as->array[number];\r
+ } else {\r
+ ptr = 0;\r
+ }\r
+\r
+ as->array[number] = as->array[--as->count];\r
+\r
+ /*\r
+ * Invoke the third-party function only when the state\r
+ * of the parent structure is consistent.\r
+ */\r
+ if(ptr) as->free(ptr);\r
+ }\r
+}\r
+\r
+/*\r
+ * Free the contents of the set, do not free the set itself.\r
+ */\r
+void\r
+asn_set_empty(void *asn_set_of_x) {\r
+ asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x);\r
+\r
+ if(as) {\r
+ if(as->array) {\r
+ if(as->free) {\r
+ while(as->count--)\r
+ as->free(as->array[as->count]);\r
+ }\r
+ FREEMEM(as->array);\r
+ as->array = 0;\r
+ }\r
+ as->count = 0;\r
+ as->size = 0;\r
+ }\r
+\r
+}\r
+\r