0

I am trying to do a total template specialisation which should execute the first block of code (suffixTry) if i > -1 else do nothing (termination condition) . Am not very sure how the last suffixTry template should be written. When compiling, the compiler points that -1 is unknown in the scope.

template< typename S ,typename I >
void suffixTry(S pattern, I suff[], I size, I f, I g, I i) {
    suff[size - 1] = size;

    if (i > g && suff[i + size - 1 - f]  < i - g){
        suff[i] = suff[i + size - 1 - f];
    } else {
        if (i < g)
            g = i;

        f = i;
        reduceToZero(pattern, g, size, f);
        suff[i] = f - g;
    }

    suffixTry(pattern, suff, size, f, g, --i);
}

template< typename S ,typename I>
void  suffixTry(S pattern, I suff[], I size, I f, I g, -1) {

}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
Walker
  • 323
  • 1
  • 12

2 Answers2

3

From your code it seems that you just want plain recursion:

template< typename S ,typename I >
void  suffixTry(S pattern, I suff[], I size, I f, I g, I i   ) {
    // base case:
    if (i == -1) {
        return;
    }

    suff[size-1]=size;

    if( i > g && suff[i + size - 1 - f]  < i -g ){
        suff[i] = suff[i+size-1-f];
    }
    else{
        if ( i < g)
            g = i;

        f=i;
        reduceToZero(pattern , g,size,f);
        suff[i] = f - g ;

     }
     suffixes(pattern,suff, size, f , g ,  --i);

}
Maksim Solovjov
  • 3,147
  • 18
  • 28
  • you right, am actually working backwards, so I initially wrote that same function. I then thought perhaps template specialisations would make the code better for the copiler to unroll since I would like the end result( `suff` ) to be completely know by compiletime – Walker Aug 06 '15 at 20:01
  • 1
    You can do it as per Jarod42 answer, with structs, but I'm not sure if it is a worthy optimisation: recursive templates are a bit less readable, and your intent will be less clear :) – Maksim Solovjov Aug 06 '15 at 20:04
  • Please one last thing hey. Looking at Jarod42 's solution, the recursive call is referencing `suffixes` which is not the template `suffixTry` . The recursive part ` suffixes(pattern, suff, size, f , g, --i);` should recurse recurse over the struct with `i` decremented. not the function suffixes . Have you an idea how that could be accomplished? – Walker Aug 07 '15 at 06:20
  • I think there is an error in copying code, the call should have been `suffixTry()(pattern, suff, size, f , g);` – Maksim Solovjov Aug 07 '15 at 06:38
1

You can't do specialization on normal parameters, only on template parameters.

So it would requires:

template<typename S, typename I, I i>
void suffixTry(S pattern, I suff[], I size, I f, I g);

but then you can't partially specialization on function, so you have to introduce a struct. In addition i cannot depend of I for partial specialization, so I use int instead.

template<typename S, typename I, int i>
struct suffixTry
{
    void operator () (S pattern, I suff[], I size, I f, I g) const
    {
        suff[size - 1]=size;

        if (i > g && suff[i + size - 1 - f]  < i - g) {
            suff[i] = suff[i + size - 1 - f];
        } else {
            if (i < g)
                g = i;
            f = i;
            reduceToZero(pattern, g, size, f);
            suff[i] = f - g ;
        }
        suffixTry<S, I, i - 1>()(pattern, suff, size, f , g);

    }
};

template<typename S, typename I>
struct suffixTry<S, I, -1>
{
    void operator () (S pattern, I suff[], I size, I f, I g) const {}
};

but it seems simpler/clearer to do the check at runtime:

template <typename S, typename I>
void suffixTry(S pattern, I suff[], I size, I f, I g, I i) {
    if (i == -1) {
        return;
    }
    suff[size - 1] = size;

    if (i > g && suff[i + size - 1 - f]  < i - g){
        suff[i] = suff[i + size - 1 - f];
    } else {
        if (i < g)
            g = i;

        f = i;
        reduceToZero(pattern, g, size, f);
        suff[i] = f - g;
    }

    suffixTry(pattern, suff, size, f, g, --i);
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Thanks for the insight hey @Jerod42 . Thats more like what I need to do. Will test and come back to you. – Walker Aug 06 '15 at 20:04
  • I am thinking around you first solution since the final idea was to generete the `suff` table at compiletime. – Walker Aug 06 '15 at 20:09
  • 1
    If you have access to c++14, as alternative may use `constexpr` function (in c++11, it should be to restrictive to write understandable code for your case) – Jarod42 Aug 06 '15 at 20:14
  • Thanks a lot @Jarod42, I understant . Unfortunately am not very rooted to the templating world. – Walker Aug 06 '15 at 20:22
  • Have you tried compiling the code though? The last specialisation fails with the error :- `suffixes.cpp:57:8: error: type ‘I’ of template argument ‘-1’ depends on a template parameter struct suffixTry` . I hadnt instantiated the struct yet, I dont think this could be the issue though. – Walker Aug 06 '15 at 20:25
  • Not compiled. I forget that rule :-(... Should `I` be really a type template or can we get ride of `I` and use directly `int` instead ? – Jarod42 Aug 06 '15 at 20:30
  • `i` will always an integer, just picked a letter for the typename to make it sound generic . Its logical to think of it( `i` ) as size_t or int. I dont know if its reserved for imaginary numbers ;) . – Walker Aug 06 '15 at 20:37
  • 1
    So you may change `I i` by `int i` and it should compile. – Jarod42 Aug 06 '15 at 20:40
  • Great, thanks man. I learnt today. I have also realised the recursive call `suffixes(pattern, suff, size, f , g, --i);` has to be `suffixes(pattern, suff, size, f , g, i-1);` since the variable `i` cant be mutated . – Walker Aug 06 '15 at 20:55
  • Perhaps you could modify that so I mark the question answered and give you some points ;) . – Walker Aug 06 '15 at 20:56
  • Please one last thing hey. We seem to have overlooked something. The recursive part ` suffixes(pattern, suff, size, f , g, --i);` should recurse recurse over the struct with `i` decremented. not the function `suffixes` . How can that call be made – Walker Aug 07 '15 at 06:15