Can this implementation of Damm algorithm be optimized?
Nestor via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Feb 11 16:43:55 PST 2017
On Saturday, 11 February 2017 at 21:41:11 UTC, Era Scarecrow
wrote:
> On Saturday, 11 February 2017 at 21:02:40 UTC, Era Scarecrow
> wrote:
>> Yes i know, which is why i had 3 to calculate 2 inputs
>> because the third is the temp/previous calculation.
>
> Alright I've found the bug and fixed it, and it passes with
> flying colors (brute force tests up to 6 digits); However it
> doesn't use the original function to build the table. So I'm
> satisfied it will handle any length now.
>
> But it seriously is a lot of overhead for such a simple
> function.
>
> int[] buildMatrix2() {
> string digits = "0123456789";
> int[] l = new int[16*16*10];
> l[] = -1; //printing the array it's obvious to see what is
> padding
> foreach(a; digits)
> foreach(b; digits)
> foreach(c; digits) {
> int t = (a-'0')*10,
> t2 = (QG10Matrix[(b - '0') + t]-'0') * 10,
> off = (a - '0') << 8 | (b - '0') << 4 | (c - '0');
> l[off] = (QG10Matrix[(c - '0') + t2]-'0')<<8;
> }
>
> return l;
> }
>
> char checkDigit2(string str) {
> int tmpdigit = 0;
> for(;str.length >= 2;str = str[2 .. $])
> tmpdigit =
> QG10Matrix2[tmpdigit|(str[0]-'0')<<4|(str[1]-'0')];
>
> tmpdigit>>=8;
> if (str.length==1)
> return QG10Matrix[(str[0]-'0')+tmpdigit*10];
>
> return (tmpdigit+'0') & 0xff;
> }
I fail to see where you are declaring QG10Matrix2, because
apparently it's an array of chars, but buildMatrix2 returns an
array of int (2560 elements??) with lots of -1 values.
More information about the Digitalmars-d-learn
mailing list