コード例 #1
0
ファイル: InternedString.cs プロジェクト: rawlinson/Since
        /// <summary>
        ///     Initializes a new <see cref="InternedString" /> from the specified <see cref="InternedStringNode" />.
        /// </summary>
        /// <param name="node">The <see cref="InternedStringNode" /> of the new <see cref="InternedString" />.</param>
        internal InternedString(InternedStringNode node)
        {
            Contract.Requires(node != null);
            Contract.Requires(node.Root == this.Root);

            this.Node = node;
        }
コード例 #2
0
ファイル: InternedString.cs プロジェクト: rawlinson/Since
        /// <summary>
        ///     Initializes a new <see cref="InternedString" /> from the specified <see cref="System.String" />
        ///     using the supplied <see cref="InternedStringNode" /> as the root.
        /// </summary>
        /// <param name="s">The value of the new <see cref="InternedString" />.</param>
        /// <param name="root">The root of the new <see cref="InternedString" />.</param>
        public InternedString(string s, InternedStringNode root)
        {
            Contract.Requires(root != null);

            this.Node = root[s];
            this.Root = root;
        }
コード例 #3
0
 public static string Substring(InternedStringNode exclusiveFrom, InternedStringNode inclusiveUntil)
 {
     if (inclusiveUntil != exclusiveFrom)
     {
         return(Substring(exclusiveFrom, inclusiveUntil._parent) + inclusiveUntil._value);
     }
     return(inclusiveUntil._value);
 }
コード例 #4
0
        public bool HasParent(InternedStringNode node)
        {
            for (var n = this; n != null; n = n._parent)
            {
                if (n == node)
                {
                    return(true);
                }
            }

            return(node == null);
        }
コード例 #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public InternedStringNode Get(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(this);
            }

            var node = this;

            for (var i = 0; i < value.Length;)
            {
                InternedStringNode next = null;
                if (node._children != null)
                {
                    foreach (var wn in node._children)
                    {
                        InternedStringNode n;
                        if (!wn.TryGetTarget(out n))
                        {
                            continue;
                        }

                        var k = 0;
                        for (; k < n._value.Length && i < value.Length; k++, i++)
                        {
                            if (value[i] != n._value[k])
                            {
                                break;
                            }
                        }
                        if (k == n._value.Length)
                        {
                            next = n;
                            break;
                        }

                        if (k <= 0)
                        {
                            continue;
                        }

                        var newNode = new InternedStringNode(null,
                                                             new List <NodeReference>(2),
                                                             _value.Substring(0, k));
                        n._value = n._value.Substring(k);

                        node._children.Remove(wn);
                        newNode._children.Add(wn);
                        n._parent = newNode;

                        node._children.Add(new NodeReference(node));
                        newNode._parent = node;

                        next = newNode;
                        break;
                    }
                }

                if (next == null && i < value.Length)
                {
                    var newNode = new InternedStringNode(node, null, value.Substring(i));
                    if (node._children == null)
                    {
                        node._children = new List <NodeReference>(2);
                    }
                    node._children.Add(new NodeReference(newNode));
                    node = newNode;
                    break;
                }

                node = next;
                if (node == null)
                {
                    break;
                }
            }

            return(node);
        }
コード例 #6
0
 private InternedStringNode(InternedStringNode parent, List <NodeReference> children, string value)
 {
     _parent   = parent;
     _children = children;
     _value    = value;
 }