Thread: Opinion needed about two different implementations

  1. #1
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444

    Opinion needed about two different implementations

    I'm still working on implementing several sorting algorithms and wanted to implement a command-line menu where a user could chose the sorting algorithm he wants to use to sort the data. Every sorting algorithm has a "wrapper function" that takes the same arguments, the array containing the data and the size of that array. From there, every function does something specific to its algorithm.

    My initial try to implement the menu was to first create an enumeration of the algorithms implemented and then create a switch() every time I want to use the specific algorithm:

    Code:
    typdef enum sort_e{
        BUBBLE,
        SELECTION,
        INSERTION,
        QUICK,
        RADIX,
        //...
    } sortOption;
    
    void startAlgorithm(int *array, size_t s, sortOption opt)
    {
        switch(opt)
        {
            case BUBBLE:
                bubbleSort(array, s);
                break;
            case SELECTION:
                selectionSort(array, s);
                break;
            // ...
        }
    }
    This felt a little heavy and takes a lot of editing just to add a new algorithm.
    I then was inspired by the way OO programming does it, so I did the following:

    1. create a structure that keeps a function pointer:
    Code:
    typedef struct sort_t{
        char name[BUFSIZ];
        void (*pt2Func)(int*, int);
    } sort;
    2. Initialize an array of those structures, with the respective functions:
    Code:
        sort sortMethods[] = {
            {"Bubble Sort", &bubbleSort},
            {"Selection Sort", &selectionSort},
            {"Insertion Sort", &insertionSort},
            {"Merge Sort", &mergeSort},
            {"Merge Sort (2 threads in parallel)", &mergeSortMulti},
            {"Quick Sort (with fixed pivot)", &quickSort},
            {"Quick Sort (with variable pivot)", &quickSortPivot},
            {"Quick Sort (2 threads in parallel)", &quickSortMulti},
            // ...
        };
    3. Finally, all I have to do is call the function pointer inside the structure:
    Code:
    (sortMethods[i]).pt2Func(array, arraySize);
    Since I can't loop back on several years of C experience, I was wondering if that implementation style was well-known or even used at all? Maybe it has a specific name people refer it to (just guessing), since it seems so practical that it could be very common.

    Is there maybe an even better of doing it or what solution do you prefer personally?

    Thanks for the input.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    This is typically called a callback. While keeping the "name" of the callback in the callback structure itself isn't bad per se, it does make the implementation more specialized. If I were doing this, I'd use a typedef to declare the type of the sorting callback:

    Code:
    typedef void (*sort_func)(some_type *array, int len);
    Then, another function would perform lookup by name and return the sorting function appropriate to a given name. This decouples the data processing somewhat, keeping the identification of the names separate from the use of the function itself:

    Code:
    sort_func lookup_sort_func(const char *name);
    The lookup function could maintain its own table which describes the relationship, similar to your sort_t structure, or it could simply be a big chain of strcmp() calls. Or it could do more complicated processing if necessary (like looking in a database of dynamically loadable sorting algorithms ;-)

    Also, the standard specifies that all type name which end in "_t" are reserved for the implementation. Which means it isn't really allowable to have a type called "sort_t."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  2. Declaring instances of different implementations
    By dwks in forum C++ Programming
    Replies: 8
    Last Post: 07-16-2008, 11:43 AM
  3. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  4. Freedom of opinion
    By Shiro in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-10-2002, 07:06 AM
  5. opinion about books
    By clement in forum C Programming
    Replies: 7
    Last Post: 09-24-2001, 04:18 PM