Esempio n. 1
0
        bool _innerGetNodeByPath(IList <TKey> path, int endIdx, out DTree <TKey, TValue> node)
        {
            TKey key;
            DTree <TKey, TValue> stree = this;

            for (int i = 0; i < endIdx; i++)
            {
                key = path[i];
                if (!stree.TryGetChild(key, out stree))
                {
                    node = null;
                    return(false);
                }
            }

            key = path[endIdx];
            if (!stree.TryGetChild(key, out stree))
            {
                node = null;
                return(false);
            }
            else
            {
                node = stree;
                return(true);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Adds a value together with the path it is to associate to this tree.
        /// </summary>
        /// <param name="path">A path consists of several keys.</param>
        /// <param name="value">The value associated with the specified path.</param>
        /// <param name="nodeCreated">A function called everytime a new node is created.</param>
        /// <returns>A node that stores the value associated with the specified path, if the path and value are both successfully added;
        /// <c>null</c> if a value is already associated with the path.
        /// This node is also the last node along the path.</returns>
        public DTree <TKey, TValue> TryAdd(IList <TKey> path, TValue value, Action <DTree <TKey, TValue> > nodeCreated = null)
        {
            try
            {
                TKey key;
                DTree <TKey, TValue> stree = this;
                DTree <TKey, TValue> ttree;
                var len = path.Count - 1;
                for (int i = 0; i < len; i++)
                {
                    key = path[i];
                    if (!stree.TryGetChild(key, out ttree))
                    {
                        stree = stree.AddChild(key, default(TValue));
                        if (nodeCreated != null)
                        {
                            nodeCreated(stree);
                        }
                    }
                    else
                    {
                        stree = ttree;
                    }
                }

                key = path[len];
                if (!stree.TryGetChild(key, out ttree))
                {
                    return(stree.AddChild(key, value));
                }
                else
                {
                    return(null);
                }
            }
            catch { return(null); }
        }
Esempio n. 3
0
        /// <summary>
        /// Determines whether the specified path exists in this tree.
        /// </summary>
        /// <param name="path">A path consists of several keys.</param>
        /// <returns><c>true</c> if the specified path exists in the current tree; otherwise, <c>false</c>.</returns>
        public bool ContainsPath(IList <TKey> path)
        {
            TKey key;
            DTree <TKey, TValue> stree = this;

            for (int i = 0; i < path.Count; i++)
            {
                key = path[i];
                if (!stree.TryGetChild(key, out stree))
                {
                    return(false);
                }
            }

            return(stree != null);
        }