leonardo

View: Recent Entries. View: Archive. View: Friends. View: Profile. View: Website (My Website). June 2nd, 2011 Tags: c++, d language, programming Security: Subject: Knight's Tour in D Time: 11:47 am

http://www.fantascienza.net/leonardo/js/index.html#knight_tour



The original page:

http://blog.tiaan.com/link/2010/06/01/knights-tour-warnsdorff-csharp-cplusplus



The original code:

http://blog.tiaan.com/sites/blog.tiaan.com/files/2010/warnsdorff-knight-tour-20x20_csharp.zip

http://blog.tiaan.com/sites/blog.tiaan.com/files/2010/warnsdorff-knight-tour-20x20_cplusplus.zip



The D code is as fast as the C++ code (or a bit faster thanks to avoiding memory allocation inside the function, and thanks to a bit better optimization done by the LDC compiler). The D version uses post-condition adapted from the C# code and asserts, avoids all explicit usages of pointers, and thanks to TypeTuples that induce static foreach loops, is much shorter than the C++ versions despite having equal or better efficiency. The variabiles are const, where possible. testScore is not const because it's incremented in a static loop. The function argument is by ref to avoid copies or unnecessary allocations, but this also makes the function signature far from nice.



The original C++ code looks like (about 130 lines):

testPosition = currentPosition + DirectionNNW; if (isAvailable[testPosition] != 0) { testScore = isAvailable[testPosition + DirectionNNW] + isAvailable[testPosition + DirectionNNE] + isAvailable[testPosition + DirectionWNW] + isAvailable[testPosition + DirectionENE] + isAvailable[testPosition + DirectionWSW] + isAvailable[testPosition + DirectionESE] + isAvailable[testPosition + DirectionSSW]; // Ignore DirectionSSE if (testScore < pickScore) { pickScore = testScore; pickPosition = testPosition; } } testPosition = currentPosition + DirectionNNE; if (isAvailable[testPosition] != 0) { testScore = isAvailable[testPosition + DirectionNNW] + isAvailable[testPosition + DirectionNNE] + isAvailable[testPosition + DirectionWNW] + isAvailable[testPosition + DirectionENE] + isAvailable[testPosition + DirectionWSW] + isAvailable[testPosition + DirectionESE] + // Ignore DirectionSSW isAvailable[testPosition + DirectionSSE]; if (testScore < pickScore) { pickScore = testScore; pickPosition = testPosition; } } ... [six more of those]

Using the static foreach in D (about 17 lines):

alias TypeTuple!(directionNNW, directionNNE, directionWNW, directionENE, directionWSW, directionESE, directionSSW, directionSSE) toUse; alias TypeTuple!(directionSSE, directionSSW, directionESE, directionWSW, directionENE, directionWNW, directionNNE, directionNNW) toIgnore; ... /*static*/ foreach (const int i, use; toUse) { const int testPosition = currentPosition + use; if (isAvailable[testPosition]) { int testScore = 0; /*static*/ foreach (pos; toUse) static if (pos != toIgnore[i]) testScore += isAvailable[testPosition + pos]; if (testScore < pickScore) { pickScore = testScore; pickPosition = testPosition; } } } A D (V2) port of C++/C# code written by Tiaan Geldenhuys that implements a fast solver for the Knight's Tour using the Warnsdorff's Algorithm:The original page:The original code:The D code is as fast as the C++ code (or a bit faster thanks to avoiding memory allocation inside the function, and thanks to a bit better optimization done by the LDC compiler). The D version uses post-condition adapted from the C# code and asserts, avoids all explicit usages of pointers, and thanks to TypeTuples that induce static foreach loops, is much shorter than the C++ versions despite having equal or better efficiency. The variabiles are const, where possible. testScore is not const because it's incremented in a static loop. The function argument is by ref to avoid copies or unnecessary allocations, but this also makes the function signature far from nice.The original C++ code looks like (about 130 lines):Using the static foreach in D (about 17 lines): comments: Leave a comment

leonardo

View: Recent Entries. View: Archive. View: Friends. View: Profile. View: Website (My Website).