Note that there are some explanatory texts on larger screens.

plurals
  1. POSerialize prefix tree
    primarykey
    data
    text
    <p>I get a <code>ProtoException</code> ("Possible recursion detected (offset: 4 level(s)): o EOW") when serializing a tree structure like so:</p> <pre><code>var tree = new PrefixTree(); tree.Add("racket".ToCharArray()); tree.Add("rambo".ToCharArray()); using (var stream = File.Open("test.prefix", FileMode.Create)) { Serializer.Serialize(stream, tree); } </code></pre> <p>The tree implementation:</p> <pre><code>[ProtoContract] public class PrefixTree { public PrefixTree() { _nodes = new Dictionary&lt;char, PrefixTree&gt;(); } public PrefixTree(char[] chars, PrefixTree parent) { if (chars == null) throw new ArgumentNullException("chars"); if (parent == null) throw new ArgumentNullException("parent"); if (chars.Length == 0) throw new ArgumentException(); _parent = parent; _nodes = new Dictionary&lt;char, PrefixTree&gt;(); _value = chars[0]; var overflow = chars.SubSet(1); if (!overflow.Any()) _endOfWord = true; else Add(overflow.ToArray()); } [ProtoMember(1)] private readonly char _value; [ProtoMember(2)] private readonly bool _endOfWord; [ProtoMember(3)] private readonly IDictionary&lt;char, PrefixTree&gt; _nodes; [ProtoMember(4, AsReference = true)] private readonly PrefixTree _parent; public void Add(char[] word) { if (word == null) throw new ArgumentNullException("word"); if (word.Length == 0) return; var character = word[0]; PrefixTree node; if (_nodes.TryGetValue(character, out node)) { node.Add(word.SubSet(1)); } else { node = new PrefixTree(word, this); _nodes.Add(character, node); } } public override string ToString() { return _endOfWord ? _value + " EOW" : _value.ToString(); } } public static class ListHelper { public static char[] SubSet(this char[] source, int start) { return source.SubSet(start, source.Length - start); } public static char[] SubSet(this char[] source, int start, int length) { if (start &lt; 0) throw new ArgumentOutOfRangeException(); if (start &gt; source.Length) throw new ArgumentOutOfRangeException(); if (length &lt; 0) throw new ArgumentOutOfRangeException(); var result = new char[length]; Array.Copy(source, start, result, 0, length); return result; } } </code></pre> <p>Am I decorating with the wrong attributes or have I simply designed a non-serializable tree?</p> <p>Edit: tried this to no avail:</p> <pre><code>var typeModel = RuntimeTypeModel.Default; var type = typeModel.Add(typeof(PrefixTree), false); type.AsReferenceDefault = true; type.Add("_value", "_endOfWord", "_nodes", "_parent"); var tree = new PrefixTree(); tree.Add("racket".ToCharArray()); tree.Add("rambo".ToCharArray()); using (var stream = File.Open("test.prefix", FileMode.Create)) { typeModel.Serialize(stream, tree); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload