Strange char[] behaviour

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Tue Mar 14 08:07:51 PST 2006


Jari-Matti Mäkelä wrote:
> Somehow I though that the CoW prevents the misuse of strings. The
> dchar-version works perfectly without .dup-cloning on any arbitrary long
> input. The char-version doesn't work, if too many iterations/nodes are
> created, but works perfectly when run once with short input strings. The
> maximum input string length is then a constant. I guess it might be
> valuable to end up with a short test case here. I'll try my best to
> create one.

Actually the dchar-version doesn't work perfectly either. I tried to use
the code coverage (dmd -cov) and it "failed": (as you can see, it
shouldn't be even possible to run those commented lines.)

       |import std.stdio, suffixnode, abstractsearcher;
       |
       |class SuffixTreeSearcher(T) : AbstractSearcher!(T) {
       |    T[] catenatedStrings;
       |    T[] sentinels;
       |    alias SuffixNode!(T) GenericNode;
       |    GenericNode root;
    130|    this(T[][] strings, T[] sentinels ...)
       |    {
      1|        this.sentinels = sentinels;
      1|        catenatedStrings = strings[0]~sentinels[0]~
strings[1]~sentinels[1];
       |    }
       |    public T[] searchLCS() {
      1|        root = new GenericNode();
    206|        for (int i = 0; i < catenatedStrings.length; ++i) {
    185|            root.addSuffix(catenatedStrings[i..$]);
       |        }
     84|        return null;
    388|        //return root.mark(sentinels);
       |    }
     83|
       |    char[] name() {
      1|        return "Suffix tree";
       |    }
       |}
suffixtreesearcher.d is 100% covered

I works as it should only when I comment the addSuffix-line:

       |import std.stdio, suffixnode, abstractsearcher;
       |
       |class SuffixTreeSearcher(T) : AbstractSearcher!(T) {
       |    T[] catenatedStrings;
       |    T[] sentinels;
       |    alias SuffixNode!(T) GenericNode;
       |    GenericNode root;
       |    this(T[][] strings, T[] sentinels ...)
       |    {
      1|        this.sentinels = sentinels;
      1|        catenatedStrings = strings[0]~sentinels[0]~
strings[1]~sentinels[1];
       |    }
       |    public T[] searchLCS() {
      1|        root = new GenericNode();
    206|        for (int i = 0; i < catenatedStrings.length; ++i) {
       |//            root.addSuffix(catenatedStrings[i..$]);
       |        }
      1|        return null;
       |        //return root.mark(sentinels);
       |    }
       |
       |    char[] name() {
      1|        return "Suffix tree";
       |    }
       |}
suffixtreesearcher.d is 100% covered

Well, the only think I can think of is that the algorithm truly corrupts
the runtime code by using array slicing. I also noticed that although it
works fine without -cov, running it >=2 times segfaults with -cov.

-- 
Jari-Matti



More information about the Digitalmars-d-bugs mailing list