SO rotate question

simendsjo simen.endsjo at pandavre.com
Thu Sep 2 23:30:31 PDT 2010


On 02.09.2010 22:24, bearophile wrote:
> simendsjo:
>> Suggestions for D-ifying the code is welcome.
>
> Your unit tests are not good enough, they miss some important corner cases.
> This my first version in D2:
>
> import std.string: indexOf;
>
> /// return True if s1 is a rotated version of s2
> bool isRotated(T)(T[] s1, T[] s2) {
>      return (s1.length + s2.length == 0) ||
>             (s1.length == s2.length&&  indexOf(s1 ~ s1, s2) != -1);
> }
>
> unittest { // of isRotated
>      assert(isRotated("", ""));
>      assert(!isRotated("", "x"));
>      assert(!isRotated("x", ""));
>      assert(isRotated("x", "x"));
>

(...)
I agree that's much simpler, but s1 ~ s1 doesn't perform too well I 
think. Always creates a new heap allocation and copies the array..


More information about the Digitalmars-d-learn mailing list