コード例 #1
0
        /// <summary>Returns an index at which the specified item can be found.</summary>
        /// <param name="item">Item to find.</param>
        /// <returns>The index of the item in the list being indexed by this
        /// object, or -1 if the item does not exist in the list.</returns>
        /// <remarks>
        /// The search takes O(M log^2 N) time, where N is the size of the list
        /// and M is the maximum size of nodes in the list. Due to the "M" factor,
        /// A-lists with large nodes are searched more slowly than A-lists with
        /// small nodes; however, the "log N" part is a base-M logarithm, so you
        /// don't actually gain performance by using very small nodes. This is
        /// because very small nodes require deeply nested trees, and deep trees
        /// are slow. The <see cref="AListBase{K,T}"/> documentation discusses
        /// the effect of node size further.
        /// </remarks>
        public int IndexOfAny(T item)
        {
            AListLeaf <K, T> leaf;
            bool             found;

            _items.FindLowerBoundExact(ref item, out leaf, out found);
            if (!found)
            {
                return(-1);
            }
            return(ReconstructIndex(item, leaf));
        }
コード例 #2
0
        /// <summary>Given an item and a leaf that is known to contain a copy of
        /// the item, this method returns the index of the item in the tree as
        /// a whole. Requires O(M )</summary>
        protected int ReconstructIndex(T item, AListLeaf <K, T> leaf)
        {
            AListInnerBase <K, T> inner;
            AListNode <K, T>      node;
            bool found;

            int index = leaf.IndexOf(item, 0), localIndex;

            if (index <= -1)
            {
                BadState();
            }

            node = leaf;
            while (node != _root)
            {
                Debug.Assert(node != null);
                _nodes.FindLowerBoundExact(ref node, out inner, out found);
                if (!found)
                {
                    BadState();
                }
                if ((localIndex = (int)inner.BaseIndexOf(node)) <= -1)
                {
                    BadState();
                }
                node   = inner;
                index += localIndex;
            }

            return(index);
        }