Can this implementation of Damm algorithm be optimized?

Nestor via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 11 12:19:51 PST 2017


On Saturday, 11 February 2017 at 11:45:02 UTC, Era Scarecrow 
wrote:
> On Friday, 10 February 2017 at 11:27:02 UTC, Nestor wrote:
>> Thank you for the detailed reply. I wasn't able to follow you 
>> regarding the multilevel stuff though :(
>
>  The idea behind it is like this (which you can scale up):
>
> static immutable int[] QG10Matrix2 = buildMatrix2();
>
> int[] buildMatrix2() {
>     string digits = "0123456789";
>     int[] l = new int[16*16*10];
>     char[3] s;
>     foreach(a; digits)
>     foreach(b; digits)
>     foreach(c; digits) {
>         s[] = [a,b,c];
>         l[(a-'0')<< 
> 8|(b-'0')<<4|(c-'0')]=checkDigit(cast(string) s) - '0';
>     }
>
>     return l;
> }
>
>
> Using that it SHOULD allow you to get the result of 2 inputs 
> simply by using 2 characters (plus the old result)
>
> char checkDigit2(string str) {
>     int tmpdigit = 0;
>     for(;str.length >= 2;str=str[2 .. $]) {
>         tmpdigit = QG10Matrix2[tmpdigit<<8|(str[0]-'0')<< 
> 4|(str[1]-'0')];
>     }
>    // handle remainder single character and return value
>
>
>  While it should be easy, I'm having issues trying to get the 
> proper results via unittests and I'm not sure why. Probably 
> something incredibly simple on my part.

Notice this is no ordinary matrix, but an Anti-Simmetric 
QuasiGroup of order 10, and tmpdigit (called interim in the 
algorithm) is used in each round (although the function isn't 
recursive) together with each digit to calculate final check 
digit.


More information about the Digitalmars-d-learn mailing list