Beispiel #1
0
int main(void)
{
    //OC_comp_fp_t f_ptr;
    struct Ordered_container* container_ptr = OC_create_container(&thing_comp_f);

    OC_insert(container_ptr, make_thing(5));
    OC_insert(container_ptr, make_thing(3));
    OC_insert(container_ptr, make_thing(4));
    OC_insert(container_ptr, make_thing(1));
    OC_insert(container_ptr, make_thing(2));
    OC_insert(container_ptr, make_thing(99));
    OC_insert(container_ptr, make_thing(-2));

    for (int i = 0; i < 7; ++i)
    {
        printf("Element %d: %d\n", i, *(int*)(*(container_ptr->array + i)));
    }

    printf("size = %d\n", OC_get_size(container_ptr));
    printf("Empty = %d\n", OC_empty(container_ptr));

    int search_num = 5;
    int* data_ptr = OC_find_item(container_ptr, &search_num);
    printf("Found data: %d\n", *(int*)OC_get_data_ptr(data_ptr));
    search_num = 99;
    printf("Apply_if_arg returned: %d\n", 
           OC_apply_if_arg(container_ptr, (OC_apply_if_arg_fp_t)apply_if_arg_tester, &search_num));
    data_ptr = OC_find_item_arg(container_ptr, &search_num, alt_thing_comp_f);
    printf("Found data: %d\n", *(int*)OC_get_data_ptr(data_ptr));

    printf("Things: %d\n", g_num_things);
    OC_apply(container_ptr, &delete_thing);
    OC_clear(container_ptr);
    printf("Things: %d\n", g_num_things);
    printf("size = %d\n", OC_get_size(container_ptr));
    printf("Empty = %d\n", OC_empty(container_ptr));

    OC_destroy_container(container_ptr);

    void* test = malloc(8);

    printf("Old way %p   New Way %p\n", *(void**)test, test);

    return 0;
}
Beispiel #2
0
/* Print the data in a struct Meeting. */
void print_Meeting(const struct Meeting* meeting_ptr) {
	printf("Meeting time: %d, Topic: %s\nParticipants:", meeting_ptr->time, meeting_ptr->topic);
	if (OC_empty(meeting_ptr->participants)) {
		printf(" None\n");
	} else {
		printf("\n");
	}
	OC_apply(meeting_ptr->participants, (OC_apply_fp_t) print_Person);
}
Beispiel #3
0
/*  prints the container with the supplied function pointer
 *  type  is the name of the container
 *  holds is what the container's data_ptrs are
 *  all ptr must be valid
 */
static void print_containter( struct Ordered_container* c_ptr, char* type, char* holds, OC_apply_fp_t fp )
{
    if ( OC_empty( c_ptr ) )
    {
        printf( "%s is empty\n", type );
    }
    else
    {
        printf( "%s contains %d %s:\n", type, OC_get_size( c_ptr ), holds );
        OC_apply( c_ptr, fp );
    }
}
Beispiel #4
0
int main(void)
{
    //OC_comp_fp_t f_ptr;
    struct Ordered_container* container_ptr = OC_create_container(&thing_comp_f);

    OC_insert(container_ptr, make_thing(5));
    OC_insert(container_ptr, make_thing(3));
    OC_insert(container_ptr, make_thing(4));
    OC_insert(container_ptr, make_thing(1));
    OC_insert(container_ptr, make_thing(2));
    OC_insert(container_ptr, make_thing(99));
    OC_insert(container_ptr, make_thing(-2));

    printf("size = %d\n", OC_get_size(container_ptr));
    printf("Empty = %d\n", OC_empty(container_ptr));
    Print_container(container_ptr, sizeof(int));

    int search_num = 5;
    int* data_ptr = OC_find_item(container_ptr, &search_num);
    printf("Found data: %d\n", *(int*)OC_get_data_ptr(data_ptr));
    search_num = 99;
    printf("Apply_if_arg returned: %d\n", 
           OC_apply_if_arg(container_ptr, (OC_apply_if_arg_fp_t)apply_if_arg_tester, &search_num));
    data_ptr = OC_find_item_arg(container_ptr, &search_num, alt_thing_comp_f);
    printf("Found data: %d\n", *(int*)OC_get_data_ptr(data_ptr));

    printf("Things: %d\n", g_num_things);
    OC_apply(container_ptr, &delete_thing);
    OC_clear(container_ptr);
    printf("Things: %d\n", g_num_things);
    printf("size = %d\n", OC_get_size(container_ptr));
    printf("Empty = %d\n", OC_empty(container_ptr));
    Print_container(container_ptr, sizeof(int));

    OC_destroy_container(container_ptr);

    return 0;
}
void OC_insert(struct Ordered_container* c_ptr, void* data_ptr)
{
    struct LL_Node *const node_ptr = malloc(sizeof(struct LL_Node));
    if (!node_ptr)
    {
        perror("OC_insert malloc error in container\n");
        exit(EXIT_FAILURE);
    }

    node_ptr->data_ptr = data_ptr;

    if (OC_empty(c_ptr))
    {
        c_ptr->first = node_ptr;
        c_ptr->last = node_ptr;
        node_ptr->prev = NULL;
        node_ptr->next = NULL;
    }
    else 
    {
        Comp_pair_t comp_pair = Find_first_node_greater_equal(data_ptr, c_ptr, c_ptr->comp_func);
        struct LL_Node* insert_before_node_ptr = comp_pair.node_ptr;

        if (insert_before_node_ptr)
        {
            Insert_before(node_ptr, insert_before_node_ptr, c_ptr);
        }
        else 
        {
            c_ptr->last->next = node_ptr;
            node_ptr->prev = c_ptr->last;
            node_ptr->next = NULL;
            c_ptr->last = node_ptr;
        }
    }

    ++c_ptr->size;
    ++g_Container_items_in_use;
    ++g_Container_items_allocated;
}
static void* OC_bsearch( const struct Ordered_container* c_ptr, OC_comp_fp_t f_ptr, const void* arg_ptr )
{
    int left = 0 ;
    int right;
    int mid = 0;
    
    if ( OC_empty( c_ptr ) )
        return NULL;
    
    right = c_ptr->size - 1;
    
    while( left <= right )
    {
        int com_value;
        
        mid = ( right + left ) / 2 ;
        
        com_value = f_ptr( arg_ptr, c_ptr->array[ mid ] );
        
        if ( com_value == 0 )
        {
            return c_ptr->array + mid ;
        }
        if ( com_value < 0 )
        {
            right = mid - 1;
        }
        else
        {
            left = mid + 1;
        }
        
    }
    
    return NULL;
}
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;
    
    
}
Beispiel #9
0
int roomHasMeeting(struct Room* room) {
	return (!OC_empty(get_Room_Meetings(room)));
}