Beispiel #1
0
/* remove the node associated with the data_ptr
 * if there is not record found print the error err
 * requires a OC_find_item_arg_fp_t fafp that returns true for the data_ptr given
 * return 1 on success NULL if otherwise
 */
static int find_remove( struct Ordered_container* c_ptr, OC_find_item_arg_fp_t fafp, void* data_ptr )
{
    void* node = OC_find_item_arg( c_ptr, data_ptr, fafp );
    
    if ( node == NULL )
    {
        return 0 ;
    }
    
    OC_delete_item( c_ptr, node );
    return 1;
}
Beispiel #2
0
static void delete_collection( struct Ordered_container* catalog )
{
    void* node = find_collection_by_name( catalog );
    
    if ( node != NULL  )
    {
        struct Collection* coll = OC_get_data_ptr( node );
        
        printf("Collection %s deleted\n", get_Collection_name( coll ) );
        
        OC_delete_item( catalog, node );
        
        destroy_Collection( coll );
    }
}
void find_and_remove(struct Ordered_container * container, struct Point probe)
{
	void * found_item;
	printf("\nsearch for (%d, %d):\n", probe.x, probe.y);
	found_item = OC_find_item(container, &probe);
	if(found_item) {
		struct Point * p = (struct Point *)OC_get_data_ptr(found_item);
		printf("found item points to (%d, %d)\n", p->x, p->y);
		OC_delete_item(container, found_item);
		printf("item removed\n");
		/* found_item now points to an undefined value - it is invalid */
		}
	else {
		printf("probed item not found\n");
		}
}
void find_and_remove(struct Ordered_container * container, char* probe)
{
    void* found_item;
    printf("\nsearch for %s:\n", probe);
    found_item = OC_find_item(container, probe);
    if(found_item) {
        printf("found item points to %s\n", (char*)OC_get_data_ptr(found_item));
        OC_delete_item(container, found_item);
        printf("item removed\n");
        /* found_item now points to an undefined value - it is invalid */
    }
    else
    {
        printf("probed item not found\n");
    }
}
Beispiel #5
0
void deleteIndividual(struct Ordered_container* people, struct Ordered_container* rooms) {
	char lastnameBuffer[MAX_STRING_LENGTH];
	struct Person* person;
	SAFESCANF(lastnameBuffer);
	if (!(person = findPersonByLastname(people, lastnameBuffer))) {
		printErrNoPerson();
		return;
	}
	if (OC_apply_if_arg(rooms,
			(OC_apply_if_arg_fp_t)findPersonRoomMeeting, person)) {
		readRestOfLine();
		printf("This person is a participant in a meeting!\n");
	} else {
		OC_delete_item(people, findPersonByLastname(people, lastnameBuffer));
		destroy_Person(person);
		printf("Person %s deleted\n", lastnameBuffer);
	}
}
int main( void )
{
    int i; 
    int cur_size_container = 0;
    
    char* test_string[] = { "t5", "t6", "t3", "t9", "t7", "t1", "t2" };
    int size_test = 7;
    
    char* s1 = "s1";
    /*char* s2 = "s2";
    char* s3 = "s3";
    char* s4 = "s4";
    char* s5 = "s5";
    char* s6 = "s6";
    
    char* probe1 = "s3";
    char* probe2 = "s99";
    
    char* probe3 = "t2";
    char* probe4 = "t11";*/
    
    void* node;
    
    struct Ordered_container * container;

    printf("Starting test\nCreating\n" );

    /* create the container */
    container = OC_create_container((int (*)(const void*, const void*))compare_string);

    for (  i = 0 ; i < size_test ; ++i )
    {

        printf("Inserting %s\n", test_string[ i ] );
        OC_insert( container, test_string[ i ] );
        cur_size_container = OC_get_size( container );
        printf("\tsize is %d\n", cur_size_container );
        /* make sure it has the correct size */
        assert( ( i + 1 ) == cur_size_container );
        
        if ( /*( i % 3 ) == 1 */ 1 )
        {
            OC_apply( container, (void (*)(void*))print_as_string) ;
        }
    }
    
    assert( OC_apply_if( container, (int (*)(void*))right_form ) == 0  );
    
    printf( "Inserting s1\n " );
    OC_insert( container, s1 );
    OC_apply( container, (void (*)(void*))print_as_string) ;
    
    assert( OC_apply_if( container, (int (*)(void*))right_form ) == ('s' - 't' ) );
    
    node = OC_find_item( container, s1 );
    
    OC_apply( container, (void (*)(void*))print_as_string) ;
    
    /* check the correct pointer is returned */
    printf( "\nAfter adding s1\n" );
    assert( OC_get_data_ptr( node ) == s1 );
    
    /* check to make sure delete is working */
    OC_delete_item( container, node );
    assert( OC_get_size( container ) == size_test );


    /* check clear function */
    OC_clear( container );
    assert( OC_empty( container ) != 0 );
    

    
    /* rebuild array to run more tests */
    for (  i = 0 ; i < size_test ; ++i )
    {
        OC_insert( container, test_string[ i ] );
        cur_size_container = OC_get_size( container );
        /* make sure it has the correct size */
        assert( ( i + 1 ) == cur_size_container );
    }

    /* removing one element at a time */
    for( i = 0 ; i < size_test ; ++i )
    {
        printf( "%d: removing %s\n", i , test_string[i] );
        OC_apply( container, (void (*)(void*))print_as_string) ;
        find_and_remove( container, test_string[i] );
        assert( ( size_test - i - 1 ) == OC_get_size( container ) );
    }
    
    /* clean up memory */
    /* run with valgrind to ensure no mem leaks */
    OC_destroy_container( container );

    return 0;
}
int main(int argc, const char * argv[]) {
    
    int a = 1, b = 3, c = 5, d = 7,
        e = 2, f = 4, g = 6, h = 8;
    void *ptr1 = &a, *ptr2 = &b, *ptr3 = &c, *ptr4 = &d,
         *ptr5 = &e, *ptr6 = &f, *ptr7 = &g, *ptr8 = &h;
    
    struct Ordered_container* container1 = OC_create_container( (OC_comp_fp_t)compare_int_ptr);
    
    
    assert(container1);
    OC_insert(container1, ptr3);
    print_containter(container1); // 5
    OC_insert(container1, ptr2);
    print_containter(container1); // 3 5
    OC_insert(container1, ptr6);   // 4
    print_containter(container1);  // 3 4 5
    OC_insert(container1, ptr4);
    print_containter(container1); // 3 4 5 7
    OC_insert(container1, ptr1);
    print_containter(container1); // 1 3 4 5 7
    OC_insert(container1, ptr8);
    print_containter(container1); // 1 3 4 5 7 8
    
    printf("%d\n",OC_get_size(container1)); // 6
    printf("%d\n",OC_empty(container1));    // 0
    OC_clear(container1);
    printf("%d\n",OC_get_size(container1));
    
    
    OC_insert(container1, ptr3);
    print_containter(container1); // 5
    OC_insert(container1, ptr2);
    print_containter(container1); // 3 5
    OC_insert(container1, ptr4);
    print_containter(container1); // 3 5 7
    OC_insert(container1, ptr1);
    print_containter(container1); // 1 3 5 7
    OC_insert(container1, ptr8);
    print_containter(container1); // 1 3 5 7 8
    
    
    //OC_destroy_container(container1);
    
    void* found_item_ptr1 = OC_find_item(container1, ptr4);
    
    if (!found_item_ptr1) {
        printf("found_item_ptr not found");
    } else {
        printf("found_item_ptr");
        printf(" and it's %d\n", *(int*)OC_get_data_ptr(found_item_ptr1));
    }
    
    OC_delete_item(container1, found_item_ptr1);
    print_containter(container1); // 1 3 5 8

    void* found_item_ptr2 = OC_find_item(container1, ptr1);
    OC_delete_item(container1, found_item_ptr2);
    void* found_item_ptr3 = OC_find_item(container1, ptr8);
    OC_delete_item(container1, found_item_ptr3);
    
    print_containter(container1); // 3 5
    
    OC_insert(container1, ptr6);   // 4
    print_containter(container1);  // 3 4 5
    OC_insert(container1, ptr5);   // 2
    print_containter(container1);  // 2 3 4 5
    printf("%d\n",OC_get_size(container1)); // 4

    OC_apply(container1, (OC_apply_fp_t)print_int); // 2 3 4 5
    printf("\n");
    

    

    
    
    return 0;
    
    
}