Thread: Expand code n times?

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Expand code n times?

    Hi!

    I'm using a template class mp_image which has a template argument that specifies the number of channels in the image. "Normal" image has 3 channels, either RGB, Lab, HSL, HSV or any other. A gray scale image has one channel.

    Now, for a simple example, I have a member function called SetColor which takes a color (a pointer to an array containing one value for each channel), and puts that color to the whole picture. Currently, I have an iterator i going through each layer one at a time. But if the number of channels would have been known when I wrote the code, I would simply have implemented the code that many times, one time for each channel, without making a loop. Is there some way to perform this (maybe using some template trick) when the number of channels isn't known at the time of coding but at the time of compiling?
    Come on, you can do it! b( ~_')

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    I don't know any trick to do this, but you should not do it anyway. Your compiler should be smart enough to know when it's better to unroll a loop and your template parameter is a compile time constant so it should be pretty easy.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    So if I, for example, write
    Code:
    int i;
    for (i = 0; i < 100; i++) printf("&#37;d\n", i);
    Will the compiler do some form of unroll (not a complete unroll), so that there's not only one printf statement in each loop, but maybe two or more (cause it would run a little bit faster, wouldn't it)? Of course I'm not using 100 channels in my images. ^^
    Come on, you can do it! b( ~_')

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If the compiler thinks it's worth unrolling the loop, it will do so itself.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by TriKri View Post
    So if I, for example, write
    Code:
    int i;
    for (i = 0; i < 100; i++) printf("%d\n", i);
    Will the compiler do some form of unroll (not a complete unroll), so that there's not only one printf statement in each loop, but maybe two or more (cause it would run a little bit faster, wouldn't it)? Of course I'm not using 100 channels in my images. ^^

    Yes, if the compiler think it's right, it will do something like:
    Code:
    for(i = 0; i < 100; i+=2)
    {
       printf("%d\n", i);
       printf("%d\n", i+1);
    }
    Thus halfing the overhead of the loop in comparison to the processing within the loop. Probably not worth it for printf, as the overhead of 100 loops would dwarf in comparison with the code inside printf - but the compiler may not know that... ;-)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> I'm using a template class mp_image which has a template argument that specifies the number of channels in the image

    You could use member template specialization based on the "number-of-channels" template argument. Member template specialization (MTS) inside a template class is a bit tricky because: 1) MTS's must be implemented at "namespace scope" 2) The containing template must also be specialized when implementing MTS's.
    Code:
    template<size_t NC>
    struct Image
    {
        char image[NC];
    
        void SetColor(char *c)
        {
            return SetColorT<NC>(c);
        }
    
        template<size_t NC2>
        void SetColorT(char *c)
        // comment out this code to get a compile/link error if a value
        // of NC is used that hasn't been specialized for SetColorT<>
        {
            for (size_t n = 0; n < NC2; ++n)
                image[n] = c[n];
        }
    };
    
    template<>
    template<>
    void Image<2>::SetColorT<2>(char *c)
    {
        image[0] = c[0];
        image[1] = c[1];
    }
    
    template<>
    template<>
    void Image<4>::SetColorT<4>(char *c)
    {
        image[0] = c[0];
        image[1] = c[1];
        image[2] = c[2];
        image[3] = c[3];
    }
    Now the compiler knows exactly what to do when NC is 2 or 4.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check number of times a process is running
    By linuxwolf in forum Windows Programming
    Replies: 6
    Last Post: 10-17-2008, 11:08 AM
  2. Something about probablility
    By mike_g in forum A Brief History of Cprogramming.com
    Replies: 116
    Last Post: 03-13-2008, 05:33 PM
  3. arrays with elements
    By bradleyd in forum C Programming
    Replies: 5
    Last Post: 04-10-2007, 12:00 PM
  4. how can I re-sort a map
    By indigo0086 in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2006, 06:21 AM
  5. Fastest STL container?
    By Shakti in forum C++ Programming
    Replies: 18
    Last Post: 02-17-2006, 02:07 AM