void stSortedSet_insert(stSortedSet *sortedSet, void *object) { checkModifiable(sortedSet); // FIXME: two passes, modify avl code. if(stSortedSet_search(sortedSet, object) != NULL) { avl_replace(sortedSet->sortedSet, object); } else { avl_insert(sortedSet->sortedSet, object); } }
/* * Add a connection to the connection pool. * Possible error codes: * ICE_ERROR_NOT_INITIALIZED * ICE_ERROR_NULL - Argument was NULL * Assert Class: 3 */ int pool_add (connection_t *con) { if (!con) return ICE_ERROR_NULL; if ((pool_mutex.thread_id == MUTEX_STATE_UNINIT) || pool == NULL) { xa_debug (1, "WARNING: Tried to use an unitialized pool"); return ICE_ERROR_NOT_INITIALIZED; } /* Acquire mutex lock */ pool_lock_write (); /* Throw connection into the pool */ if (avl_replace (pool, con) != NULL) xa_debug (1, "WARNING: Duplicate connections in the pool (id = %d)", con->id); /* Release mutex lock */ pool_unlock_write (); return OK; }
int main (int argc, char ** argv) { avl_t avl; size_t size, i; void ** tmp; /* Default Constructor */ avl = avl_init_free (compare, free_int); if (avl == NULL) return 99; /* Test Empty Size */ if (avl_size (avl) != 0) { printf ("Invalid Initial Size\n"); return EXIT_FAILURE; } /* Test get, remove, and replace on empty */ if (avl_replace (avl, (void *)1, (void *)1) != AVL_KEY_INVALID) { printf ("Empty AVL Replace Invalid Key\n"); return EXIT_FAILURE; } if (avl_get (avl, (void *)1) != NULL) { printf ("Empty AVL Get Invalid Key\n"); return EXIT_FAILURE; } if (avl_remove (avl, (void *)1) != AVL_KEY_INVALID) { printf ("Empty AVL Remove Invalid Key\n"); return EXIT_FAILURE; } /* Test 3 inserts */ if (avl_insert (avl, (void *)0, (void *)0) == AVL_MALLOC_FAILED || avl_insert (avl, (void *)1, (void *)3) == AVL_MALLOC_FAILED || avl_insert (avl, (void *)2, (void *)2) == AVL_MALLOC_FAILED) return 99; if (avl_size (avl) != 3) { printf ("Three Inserts Size Failed\n"); return EXIT_FAILURE; } if (avl_get (avl, (void *)0) != (void *)0 || avl_get (avl, (void *)2) != (void *)2 || avl_get (avl, (void *)1) != (void *)3) { printf ("Three Inserts Failed\n"); return EXIT_FAILURE; } /* Test replace and insert_replace */ if (avl_insert_rep (avl, (void *)3, (void *)10) != AVL_OK || avl_replace (avl, (void *)3, (void *)3) != AVL_OK || avl_insert_rep (avl, (void *)1, (void *)1) != AVL_OK) { printf ("Replaces and inserts failed\n"); return EXIT_FAILURE; } if (avl_size (avl) != 4) { printf ("Replace and Inserts Size Failed\n"); return EXIT_FAILURE; } if (avl_get (avl, (void *)0) != (void *)0 || avl_get (avl, (void *)1) != (void *)1 || avl_get (avl, (void *)2) != (void *)2 || avl_get (avl, (void *)3) != (void *)3) { printf ("Replace and Inserts Failed\n"); return EXIT_FAILURE; } /* Test Failed Insert and Failed Replace */ if (avl_insert (avl, (void *)1, (void *)4) != AVL_KEY_EXISTS || avl_replace (avl, (void *)10, (void *)1) != AVL_KEY_INVALID) { printf ("Passed when insert / replace it should fail\n"); return EXIT_FAILURE; } if (avl_size (avl) != 4) { printf ("Invalid insert / replace Size Failed\n"); return EXIT_FAILURE; } if (avl_get (avl, (void *)0) != (void *)0 || avl_get (avl, (void *)1) != (void *)1 || avl_get (avl, (void *)2) != (void *)2 || avl_get (avl, (void *)3) != (void *)3) { printf ("Invalid insert / replace Failed\n"); return EXIT_FAILURE; } /* Test Clear */ avl_clear (avl); if (avl_size (avl) != 0 || avl_get (avl, (void *)1) != NULL) { printf ("Clearing the AVL Failed\n"); return EXIT_FAILURE; } /* Test Many Inserts */ size = 1000; for (i = 0; i < size; i++) avl_insert (avl, (void *)i, (void *)i); if (avl_size (avl) != size) { printf ("Many Inserts Size Failed\n"); return EXIT_FAILURE;; } for (i = 0; i < size; i++) if (avl_get (avl, (void *)i) != (void *)i) { printf ("Many Inserts Failed\n"); return EXIT_FAILURE; } /* Default Destructor */ avl_destroy (avl); /* Test more complex free routine */ avl = avl_init_free (compare, free_complex); tmp = (void **) malloc (sizeof (void *)); *tmp = malloc (16); avl_insert (avl, tmp, NULL); avl_destroy (avl); return EXIT_SUCCESS;; }