0 Replies - 75 Views - Last Post: 07 June 2009 - 10:00 PM

#1 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Preprocessor Initialize Arrays II

Posted 07 June 2009 - 10:00 PM

Description: This snippet uses an X-macro so you must define the macro X taking 1 argument before you can use the LIST_TO_SUM(num, ...) macro. In the LIST_TO_SUM(num, ...) macro the num is the number of arguments that follow. This can create a sum of any power of 2 up to 128, using 8 numbers. See the main() function for usage.Demonstrates another way to use the preprocessor to initialize arrays at compile time.
#include <iostream>

#ifndef INIT_ARRAY_WITH_MACRO
#   define DO_0(m, n) 
#   define DO_1(m, n) m((n))
#   define DO_2(m, n) DO_1(m, n) DO_1(m, (n+1))
#   define DO_4(m, n) DO_2(m, n) DO_2(m, (n+2))
#   define DO_8(m, n) DO_4(m, n) DO_4(m, (n+4)) 
#   define DO_16(m, n) DO_8(m, n) DO_8(m, (n+8)) 
#   define DO_32(m, n) DO_16(m, n) DO_16(m, (n+16))
#   define DO_64(m, n) DO_32(m, n) DO_32(m, (n+32))
#   define DO_128(m, n) DO_64(m, n) DO_64(m, (n+64))


#   define LIST_TO_SUM1(a1) LIST_TO_SUM8(a1, 0, 0, 0, 0, 0, 0, 0)
#   define LIST_TO_SUM2(a1, a2) LIST_TO_SUM8(a1, a2, 0, 0, 0, 0, 0, 0)
#   define LIST_TO_SUM3(a1, a2, a3) LIST_TO_SUM8(a1, a2, a3, 0, 0, 0, 0, 0)
#   define LIST_TO_SUM4(a1, a2, a3, a4) LIST_TO_SUM8(a1, a2, a3, a4, 0, 0, 0, 0)
#   define LIST_TO_SUM5(a1, a2, a3, a4, a5) LIST_TO_SUM8(a1, a2, a3, a4, a5, 0, 0, 0)
#   define LIST_TO_SUM6(a1, a2, a3, a4, a5, a6) LIST_TO_SUM8(a1, a2, a3, a4, a5, a6, 0, 0)
#   define LIST_TO_SUM7(a1, a2, a3, a4, a5, a6, a7) LIST_TO_SUM8(a1, a2, a3, a4, a5, a6, a7, 0)
#   define LIST_TO_SUM8(a1, a2, a3, a4, a5, a6, a7, a8)                        
        DO_ ## a1(X, 1)                                                         
        DO_ ## a2(X, (1+ ## a1 ))                                               
        DO_ ## a3(X, (1+ ## a1 + ## a2))                                        
        DO_ ## a4(X, (1+ ## a1 + ## a2 + ## a3))                                
        DO_ ## a5(X, (1+ ## a1 + ## a2 + ## a3 + ## a4))                        
        DO_ ## a6(X, (1+ ## a1 + ## a2 + ## a3 + ## a4 + ## a5))                
        DO_ ## a7(X, (1+ ## a1 + ## a2 + ## a3 + ## a4 + ## a5 + ## a6))        
        DO_ ## a8(X, (1+ ## a1 + ## a2 + ## a3 + ## a4 + ## a5 + ## a6 + ## a7))  
    
#   define LIST_TO_SUM(num, ...) LIST_TO_SUM ## num ( __VA_ARGS__ )
#   define INIT_ARRAY_WITH_MACRO(mac) { mac }
#endif

// Define a template meta program to demostrate its usage to initialize an array
template <int n>
struct Fib {
    enum { value = (Fib<n-1>::value + Fib<n-2>::value) };
};

template <>
struct Fib<1> {
    enum { value = 1 };
};

template <>
struct Fib<0> {
    enum { value = 0 };
};


//Utility macro to display the array and its size
#define DISPLAY_ARRAY(name, cols) 
    std::cout << "Array: " #name "[" << (sizeof(name)/sizeof(int)) << "] =n"; 
    for (int i = 0; i < sizeof(name)/sizeof(int); i++) { 
        std::cout << name[i] << ((i%cols)==(cols-1) ? "n" : ",  t"); 
    } 
    std::cout << std::endl; 

int main() {
    //Create an array with 100 elements such that each element equals the square of its index
    #define X(n)  (n) * (n),
    int aSqrs[100] = INIT_ARRAY_WITH_MACRO( LIST_TO_SUM(3, 64, 32, 4) ); 
    #undef X
    
    //Create an array of the first 14 fibonacci numbers, use the sum of seven 2's just for the heck of it. 
    //This uses a template meta-program to generate the fibonacci numbers
    #define X(n)  Fib<n>::value,
    int aFibs[] = INIT_ARRAY_WITH_MACRO( LIST_TO_SUM(7, 2,2,2,2,2,2,2) ); 
    #undef X
   
    //Display our two arrays to check the content.
    DISPLAY_ARRAY(aSqrs, 10);
    DISPLAY_ARRAY(aFibs, 7);
    
    //Create an integer that is the sum of the first 14 elements of aFibs
    #define X(n)  + aFibs[(n) - 1]
    int sum = 0 + LIST_TO_SUM(3, 8, 4, 2);
    #undef X
    
    std::cout << "Sum of first 14 Fibonacci Numbers is: " << sum << std::endl;
    return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Preprocessor Initialize Arrays II

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Preprocessor Initialize Arrays II

Posted 07 June 2009 - 10:00 PM

Description: This snippet uses an X-macro so you must define the macro X taking 1 argument before you can use the LIST_TO_SUM(num, ...) macro. In the LIST_TO_SUM(num, ...) macro the num is the number of arguments that follow. This can create a sum of any power of 2 up to 128, using 8 numbers. See the main() function for usage.Demonstrates another way to use the preprocessor to initialize arrays at compile time.
#include <iostream>

#ifndef INIT_ARRAY_WITH_MACRO
//The DO_# Macros will do a particular macro m a given number of times. 
// the argument n keeps track of now many times.. 
// note n starts at 1 (as apposed to 0 which might seem natural). 
#   define DO_0(m, n) 
#   define DO_1(m, n) m((n))
#   define DO_2(m, n) DO_1(m, n) DO_1(m, (n+1))
#   define DO_4(m, n) DO_2(m, n) DO_2(m, (n+2))
#   define DO_8(m, n) DO_4(m, n) DO_4(m, (n+4)) 
#   define DO_16(m, n) DO_8(m, n) DO_8(m, (n+8)) 
#   define DO_32(m, n) DO_16(m, n) DO_16(m, (n+16))
#   define DO_64(m, n) DO_32(m, n) DO_32(m, (n+32))
#   define DO_128(m, n) DO_64(m, n) DO_64(m, (n+64))

//These macros will preform the X macro N times where N = Sum(a1...a8)
//  note that a1-a8 must be powers of 2
#   define LIST_TO_SUM1(a1) LIST_TO_SUM8(a1, 0, 0, 0, 0, 0, 0, 0)
#   define LIST_TO_SUM2(a1, a2) LIST_TO_SUM8(a1, a2, 0, 0, 0, 0, 0, 0)
#   define LIST_TO_SUM3(a1, a2, a3) LIST_TO_SUM8(a1, a2, a3, 0, 0, 0, 0, 0)
#   define LIST_TO_SUM4(a1, a2, a3, a4) LIST_TO_SUM8(a1, a2, a3, a4, 0, 0, 0, 0)
#   define LIST_TO_SUM5(a1, a2, a3, a4, a5) LIST_TO_SUM8(a1, a2, a3, a4, a5, 0, 0, 0)
#   define LIST_TO_SUM6(a1, a2, a3, a4, a5, a6) LIST_TO_SUM8(a1, a2, a3, a4, a5, a6, 0, 0)
#   define LIST_TO_SUM7(a1, a2, a3, a4, a5, a6, a7) LIST_TO_SUM8(a1, a2, a3, a4, a5, a6, a7, 0)
#   define LIST_TO_SUM8(a1, a2, a3, a4, a5, a6, a7, a8)                        
        DO_ ## a1(X, 1)                                                         
        DO_ ## a2(X, (1+ ## a1 ))                                               
        DO_ ## a3(X, (1+ ## a1 + ## a2))                                        
        DO_ ## a4(X, (1+ ## a1 + ## a2 + ## a3))                                
        DO_ ## a5(X, (1+ ## a1 + ## a2 + ## a3 + ## a4))                        
        DO_ ## a6(X, (1+ ## a1 + ## a2 + ## a3 + ## a4 + ## a5))                
        DO_ ## a7(X, (1+ ## a1 + ## a2 + ## a3 + ## a4 + ## a5 + ## a6))        
        DO_ ## a8(X, (1+ ## a1 + ## a2 + ## a3 + ## a4 + ## a5 + ## a6 + ## a7))  

//This will simplify the process by using an arbitrary number of arguments given by num
//  Note that this still must be between 1 and 8 arguments    
#   define LIST_TO_SUM(num, ...) LIST_TO_SUM ## num ( __VA_ARGS__ )
#   define INIT_ARRAY_WITH_MACRO(mac) { mac }
#endif

// Define a template meta program to demostrate its usage to initialize an array
template <int n>
struct Fib {
    enum { value = (Fib<n-1>::value + Fib<n-2>::value) };
};

template <>
struct Fib<1> {
    enum { value = 1 };
};

template <>
struct Fib<0> {
    enum { value = 0 };
};

//Utility macro to display the array and its size
#define DISPLAY_ARRAY(name, cols) 
    std::cout << "Array: " #name "[" << (sizeof(name)/sizeof(int)) << "] =n"; 
    for (int i = 0; i < sizeof(name)/sizeof(int); i++) { 
        std::cout << name[i] << ((i%cols)==(cols-1) ? "n" : ",  t"); 
    } 
    std::cout << std::endl; 

int main() {
    //Create an array with 100 elements such that each element equals the square of its index
    #define X(n)  (n) * (n),
    int aSqrs[100] = INIT_ARRAY_WITH_MACRO( LIST_TO_SUM(3, 64, 32, 4) ); 
    #undef X
    
    //Create an array of the first 14 fibonacci numbers, use the sum of seven 2's just for the heck of it. 
    //This uses a template meta-program to generate the fibonacci numbers
    #define X(n)  Fib<n>::value,
    int aFibs[] = INIT_ARRAY_WITH_MACRO( LIST_TO_SUM(7, 2,2,2,2,2,2,2) ); 
    #undef X
   
    //Display our two arrays to check the content.
    DISPLAY_ARRAY(aSqrs, 10);
    DISPLAY_ARRAY(aFibs, 7);
    
    //Create an integer that is the sum of the first 14 elements of aFibs
    #define X(n)  + aFibs[(n) - 1]
    int sum = 0 + LIST_TO_SUM(3, 8, 4, 2);
    #undef X
    
    std::cout << "Sum of first 14 Fibonacci Numbers is: " << sum << std::endl;
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Preprocessor Initialize Arrays II

Posted 07 June 2009 - 10:00 PM

Description: This snippet uses an X-macro so you must define the macro X taking 1 argument before you can use the LIST_TO_SUM(num, ...) macro. In the LIST_TO_SUM(num, ...) macro the num is the number of arguments that follow. This can create a sum of any power of 2 up to 128, using 8 numbers. See the main() function for usage.Demonstrates another way to use the preprocessor to initialize arrays at compile time.
#include <iostream>

#ifndef INIT_ARRAY_WITH_MACRO
//The DO_# Macros will do a particular macro m a given number of times. 
// the argument n keeps track of now many times.. 
// note n starts at 1 (as apposed to 0 which might seem natural). 
#   define DO_0(m, n) 
#   define DO_1(m, n) m((n))
#   define DO_2(m, n) DO_1(m, n) DO_1(m, (n+1))
#   define DO_4(m, n) DO_2(m, n) DO_2(m, (n+2))
#   define DO_8(m, n) DO_4(m, n) DO_4(m, (n+4)) 
#   define DO_16(m, n) DO_8(m, n) DO_8(m, (n+8)) 
#   define DO_32(m, n) DO_16(m, n) DO_16(m, (n+16))
#   define DO_64(m, n) DO_32(m, n) DO_32(m, (n+32))
#   define DO_128(m, n) DO_64(m, n) DO_64(m, (n+64))

//These macros will preform the X macro N times where N = Sum(a1...a8)
//  note that a1-a8 must be powers of 2
#   define LIST_TO_SUM1(a1) LIST_TO_SUM8(a1, 0, 0, 0, 0, 0, 0, 0)
#   define LIST_TO_SUM2(a1, a2) LIST_TO_SUM8(a1, a2, 0, 0, 0, 0, 0, 0)
#   define LIST_TO_SUM3(a1, a2, a3) LIST_TO_SUM8(a1, a2, a3, 0, 0, 0, 0, 0)
#   define LIST_TO_SUM4(a1, a2, a3, a4) LIST_TO_SUM8(a1, a2, a3, a4, 0, 0, 0, 0)
#   define LIST_TO_SUM5(a1, a2, a3, a4, a5) LIST_TO_SUM8(a1, a2, a3, a4, a5, 0, 0, 0)
#   define LIST_TO_SUM6(a1, a2, a3, a4, a5, a6) LIST_TO_SUM8(a1, a2, a3, a4, a5, a6, 0, 0)
#   define LIST_TO_SUM7(a1, a2, a3, a4, a5, a6, a7) LIST_TO_SUM8(a1, a2, a3, a4, a5, a6, a7, 0)
#   define LIST_TO_SUM8(a1, a2, a3, a4, a5, a6, a7, a8)                        
        DO_ ## a1(X, 1)                                                        
        DO_ ## a2(X, (1 +  a1 ))                                               
        DO_ ## a3(X, (1 +  a1 +  a2))                                          
        DO_ ## a4(X, (1 +  a1 +  a2 +  a3))                                    
        DO_ ## a5(X, (1 +  a1 +  a2 +  a3 +  a4))                              
        DO_ ## a6(X, (1 +  a1 +  a2 +  a3 +  a4 +  a5))                        
        DO_ ## a7(X, (1 +  a1 +  a2 +  a3 +  a4 +  a5 +  a6))                  
        DO_ ## a8(X, (1 +  a1 +  a2 +  a3 +  a4 +  a5 +  a6 +  a7))  

//This will simplify the process by using an arbitrary number of arguments given by num
//  Note that this still must be between 1 and 8 arguments    
#   define LIST_TO_SUM(num, ...) LIST_TO_SUM ## num ( __VA_ARGS__ )
#   define INIT_ARRAY_WITH_MACRO(mac) { mac }
#endif

// Define a template meta program to demostrate its usage to initialize an array
template <int n>
struct Fib {
    enum { value = (Fib<n-1>::value + Fib<n-2>::value) };
};

template <>
struct Fib<1> {
    enum { value = 1 };
};

template <>
struct Fib<0> {
    enum { value = 0 };
};

//Utility macro to display the array and its size
#define DISPLAY_ARRAY(name, cols) 
    std::cout << "Array: " #name "[" << (sizeof(name)/sizeof(int)) << "] =n"; 
    for (int i = 0; i < sizeof(name)/sizeof(int); i++) { 
        std::cout << name[i] << ((i%cols)==(cols-1) ? "n" : ",  t"); 
    } 
    std::cout << std::endl; 

int main() {
    //Create an array with 100 elements such that each element equals the square of its index
    #define X(n)  (n) * (n),
    int aSqrs[100] = INIT_ARRAY_WITH_MACRO( LIST_TO_SUM(3, 64, 32, 4) ); 
    #undef X
    
    //Create an array of the first 14 fibonacci numbers, use the sum of seven 2's just for the heck of it. 
    //This uses a template meta-program to generate the fibonacci numbers
    #define X(n)  Fib<n>::value,
    int aFibs[] = INIT_ARRAY_WITH_MACRO( LIST_TO_SUM(7, 2,2,2,2,2,2,2) ); 
    #undef X
   
    //Display our two arrays to check the content.
    DISPLAY_ARRAY(aSqrs, 10);
    DISPLAY_ARRAY(aFibs, 7);
    
    //Create an integer that is the sum of the first 14 elements of aFibs
    #define X(n)  + aFibs[(n) - 1]
    int sum = 0 + LIST_TO_SUM(3, 8, 4, 2);
    #undef X
    
    std::cout << "Sum of first 14 Fibonacci Numbers is: " << sum << std::endl;
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1