Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="VanEmdeBoasTree"/> class
        /// with the provided <paramref name="universe"/> size.
        /// </summary>
        /// <param name="universe">
        /// The size of the universe for this tree.
        /// </param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="universe"/> is smaller than two.
        /// -or-
        /// <paramref name="universe"/> is not a power of two.
        /// </exception>
        /// <remarks>
        /// Calling this constructor will create an empty van Emde Boas subtree recursively.
        /// </remarks>
        public VanEmdeBoasTree(int universe)
        {
            VanEmdeBoasTreeNode root;

            try
            {
                root = new VanEmdeBoasTreeNode(universe);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                throw new ArgumentOutOfRangeException(nameof(universe), universe, ex.Message);
            }

            Root = root;
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="VanEmdeBoasTreeNode"/> class
        /// with the provided <paramref name="universe"/> size.
        /// </summary>
        /// <param name="universe">
        /// The size of the universe for this node.
        /// </param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="universe"/> is smaller than two.
        /// -or-
        /// <paramref name="universe"/> is not a power of two.
        /// </exception>
        /// <remarks>
        /// Calling this constructor will create an empty van Emde Boas subtree recursively.
        /// </remarks>
        public VanEmdeBoasTreeNode(int universe)
        {
            if (universe < 2)
            {
                throw new ArgumentOutOfRangeException(nameof(universe), universe, "The universe cannot be smaller than two.");
            }

            if (!Utility.IsPowerOfTwo(universe))
            {
                throw new ArgumentOutOfRangeException(nameof(universe), universe, "The universe must be a power of two.");
            }

            Universe = universe;
            Maximum  = null;
            Minimum  = null;

            if (universe == 2)
            {
                // Base case: the node has no cluster or summary.
                Cluster = null;
                Summary = null;
            }
            else
            {
                int universeUpperSquareRoot = (int)Utility.UpperSquareRoot(universe);
                int universeLowerSquareRoot = (int)Utility.LowerSquareRoot(universe);

                Summary = new VanEmdeBoasTreeNode(universeUpperSquareRoot);

                var clusterList = new List <VanEmdeBoasTreeNode>(universeUpperSquareRoot);
                for (int i = 0; i < universeUpperSquareRoot; i++)
                {
                    clusterList.Add(new VanEmdeBoasTreeNode(universeLowerSquareRoot));
                }
                Cluster = clusterList.AsReadOnly();
            }
        }