require(inline)
require(Rcpp)
generateIndex17 <- local( {

    fun <- cxxfunction(
        signature( n_ = "integer", width_ = "integer", format_ = "character"  ), '
 
        int n = as<int>(n_) ;
        int width = as<int>( width_ ) ;
        const char* format = as<const char*>( format_ ) ;
        
        std::string buffer( width, \'0\' ) ;
        std::vector< std::string > elements( n ) ;
        for( int i=0; i<n; i++){
                sprintf( const_cast<char*>(buffer.data() ), 
                    format, 
                    i+1
                    ) ;
            elements[i] = buffer.c_str() ;
        }
        
        char buf[100] ;
        buf[0] = \'i\' ;
        buf[width+1] = \'.\' ;
        
        CharacterVector res( n*(n-1)/2) ;
        for( int i=0, k=0; i<n-1; i++){
            strncpy( buf+1, elements[i].data(), width ) ;
            for(int j=i+1; j<n; j++, k++){
                strncpy( buf + 2 + width, elements[j].c_str(), width ) ;
                res[k] = buf ;
            }
        }
        return res ;
        ', plugin = "Rcpp" )

    function( n ){
        width <- ifelse( n<1000, 3, ceiling( log10(n+1) ) )
        fun( n, width, sprintf("%%0%dd", width)  )
    }
} )