Example #1
0
        private static void PrintHelper(IRBNode n, int indent)
        {
            if (n == null)
            {
                Trace.WriteLine("<empty tree>");
                return;
            }

            if (n.Left != null)
            {
                PrintHelper(n.Left, indent + INDENT_STEP);
            }

            for (int i = 0; i < indent; i++)
            {
                Trace.Write(" ");
            }
            if (n.Color == Color.BLACK)
            {
                Trace.WriteLine(" " + n.ToString() + " ");
            }
            else
            {
                Trace.WriteLine("<" + n.ToString() + ">");
            }

            if (n.Right != null)
            {
                PrintHelper(n.Right, indent + INDENT_STEP);
            }
        }
Example #2
0
        private IRBNode LookupNode(IRBNode template)
        {
            IRBNode n = Root;

            while (n != null)
            {
                int compResult = template.CompareTo(n);

                if (compResult == 0)
                {
                    return(n);
                }
                else if (compResult < 0)
                {
                    n = n.Left;
                }
                else
                {
                    //assert compResult > 0;
                    n = n.Right;
                }
            }

            return(n);
        }
Example #3
0
 internal void FireNodeOperation(IRBNode node, NodeOperation operation)
 {
     if (NodeOperation != null)
     {
         NodeOperation(node, operation);
     }
 }
Example #4
0
        /// <summary>
        /// Get a named storage contained in the current one if existing.
        /// </summary>
        /// <param name="storageName">Name of the storage to look for</param>
        /// <param name="cfStorage">A storage reference if found else null</param>
        /// <returns><see cref="T:System.Boolean"> true if storage found, else false</returns>
        /// <example>
        /// <code>
        ///
        /// String FILENAME = "MultipleStorage2.cfs";
        /// CompoundFile cf = new CompoundFile(FILENAME, UpdateMode.ReadOnly, false, false);
        ///
        /// bool b = cf.RootStorage.TryGetStorage("MyStorage",out CFStorage st);
        ///
        /// Assert.IsNotNull(st);
        /// Assert.IsTrue(b);
        ///
        /// cf.Close();
        /// </code>
        /// </example>
        public bool TryGetStorage(String storageName, out ReadonlyCompoundFileStorage cfStorage)
        {
            bool result = false;

            cfStorage = null;

            try
            {
                CheckDisposed();

                IDirectoryEntry template = DirectoryEntry.Mock(storageName, StgType.StgInvalid);
                IRBNode         outDe    = null;

                if (Children.TryLookup(template, out outDe) && ((IDirectoryEntry)outDe).StgType == StgType.StgStorage)
                {
                    cfStorage = new ReadonlyCompoundFileStorage(this.CompoundFile, outDe as IDirectoryEntry);
                    result    = true;
                }
            }
            catch (CFDisposedException)
            {
                result = false;
            }

            return(result);
        }
Example #5
0
        /// <summary>
        /// Get a named <see cref="T:OpenMcdf.CFStream">stream</see> contained in the current storage if existing.
        /// </summary>
        /// <param name="streamName">Name of the stream to look for</param>
        /// <param name="cfStream">Found <see cref="T:OpenMcdf.CFStream"> if any</param>
        /// <returns><see cref="T:System.Boolean"> true if stream found, else false</returns>
        /// <example>
        /// <code>
        /// String filename = "report.xls";
        ///
        /// CompoundFile cf = new CompoundFile(filename);
        /// bool b = cf.RootStorage.TryGetStream("Workbook",out CFStream foundStream);
        ///
        /// byte[] temp = foundStream.GetData();
        ///
        /// Assert.IsNotNull(temp);
        /// Assert.IsTrue(b);
        ///
        /// cf.Close();
        /// </code>
        /// </example>
        public bool TryGetStream(String streamName, out CFStream cfStream)
        {
            bool result = false;

            cfStream = null;

            try
            {
                CheckDisposed();

                IDirectoryEntry tmp = DirectoryEntry.Mock(streamName, StgType.StgStream);

                IRBNode outDe = null;

                if (Children.TryLookup(tmp, out outDe) && (((IDirectoryEntry)outDe).StgType == StgType.StgStream))
                {
                    cfStream = new CFStream(this.CompoundFile, (IDirectoryEntry)outDe);
                    result   = true;
                }
            }
            catch (CFDisposedException)
            {
                result = false;
            }

            return(result);
        }
Example #6
0
        public void Delete(IRBNode template, out IRBNode deletedAlt)
        {
            deletedAlt = null;
            var n = LookupNode(template);

            template = n;
            if (n == null)
            {
                return; // Key not found, do nothing
            }
            if (n.Left != null && n.Right != null)
            {
                // Copy key/value from predecessor and then delete it instead
                var pred = MaximumNode(n.Left);
                pred.AssignValueTo(n);
                n          = pred;
                deletedAlt = pred;
            }

            //assert n.left == null || n.right == null;
            var child = n.Right == null ? n.Left : n.Right;

            if (NodeColor(n) == Color.BLACK)
            {
                n.Color = NodeColor(child);
                DeleteCase1(n);
            }

            ReplaceNode(n, child);

            if (NodeColor(Root) == Color.RED)
            {
                Root.Color = Color.BLACK;
            }
        }
Example #7
0
 private void DeleteCase1(IRBNode n)
 {
     if (n.Parent == null)
     {
         return;
     }
     DeleteCase2(n);
 }
Example #8
0
 //-----------------------------------
 private void InsertCase2(IRBNode n)
 {
     if (NodeColor(n.Parent) == Color.BLACK)
     {
         return; // Tree is still valid
     }
     InsertCase3(n);
 }
Example #9
0
        public void Insert(IRBNode newNode)
        {
            newNode.Color = Color.RED;
            var insertedNode = newNode;

            if (Root == null)
            {
                Root = insertedNode;
            }
            else
            {
                var n = Root;
                while (true)
                {
                    var compResult = newNode.CompareTo(n);
                    if (compResult == 0)
                    {
                        throw new RBTreeDuplicatedItemException("RBNode " + newNode + " already present in tree");
                    }

                    if (compResult < 0)
                    {
                        if (n.Left == null)
                        {
                            n.Left = insertedNode;

                            break;
                        }

                        n = n.Left;
                    }
                    else
                    {
                        //assert compResult > 0;
                        if (n.Right == null)
                        {
                            n.Right = insertedNode;

                            break;
                        }

                        n = n.Right;
                    }
                }

                insertedNode.Parent = n;
            }

            InsertCase1(insertedNode);

            if (NodeInserted != null)
            {
                NodeInserted(insertedNode);
            }

            //Trace.WriteLine(" ");
            //Print();
        }
Example #10
0
        public void VisitTree(Action <IRBNode> action)
        {
            //IN Order visit
            IRBNode walker = Root;

            if (walker != null)
            {
                DoVisitTree(action, walker);
            }
        }
Example #11
0
        internal void VisitTreeNodes(Action <IRBNode> action)
        {
            //IN Order visit
            IRBNode walker = Root;

            if (walker != null)
            {
                DoVisitTreeNodes(action, walker);
            }
        }
Example #12
0
        private static IRBNode MaximumNode(IRBNode n)
        {
            //assert n != null;
            while (n.Right != null)
            {
                n = n.Right;
            }

            return(n);
        }
Example #13
0
 //------------------------------------
 private void InsertCase1(IRBNode n)
 {
     if (n.Parent == null)
     {
         n.Color = Color.BLACK;
     }
     else
     {
         InsertCase2(n);
     }
 }
Example #14
0
        private static void VerifyProperty1(IRBNode n)
        {
            Assert.IsTrue(NodeColor(n) == Color.RED || NodeColor(n) == Color.BLACK);

            if (n == null)
            {
                return;
            }
            VerifyProperty1(n.Left);
            VerifyProperty1(n.Right);
        }
Example #15
0
        /// <summary>
        ///     Internally Get a named storage contained in the current one if existing.
        /// </summary>
        /// <param name="storageName">Name of the storage to look for</param>
        /// <returns>A storage reference or null.</returns>
        public CFStorage GetStorageInternal(string storageName)
        {
            var     template = DirectoryEntry.Mock(storageName, StgType.StgInvalid);
            IRBNode outDe    = null;

            if (!Children.TryLookup(template, out outDe) || ((IDirectoryEntry)outDe).StgType != StgType.StgStorage)
            {
                return(null);
            }

            return(new CFStorage(CompoundFile, (IDirectoryEntry)outDe));
        }
Example #16
0
        /// <summary>
        ///     Internally get a named <see cref="T:OpenMcdf.CFStream">streamName</see> contained in the current storage if existing.
        /// </summary>
        /// <param name="streamName">Name of the streamName to look for</param>
        /// <returns>A streamName reference or null</returns>
        /// <exception cref="T:OpenMcdf.CFDisposedException">Raised if trying to delete item from a closed compound file</exception>
        private CFStream GetStreamInternal(string streamName)
        {
            var     tmp   = DirectoryEntry.Mock(streamName, StgType.StgStream);
            IRBNode outDe = null;

            if (!Children.TryLookup(tmp, out outDe) || ((IDirectoryEntry)outDe).StgType != StgType.StgStream)
            {
                return(null);
            }

            return(new CFStream(CompoundFile, (IDirectoryEntry)outDe));
        }
Example #17
0
        private void RotateLeft(IRBNode n)
        {
            IRBNode r = n.Right;

            ReplaceNode(n, r);
            n.Right = r.Left;
            if (r.Left != null)
            {
                r.Left.Parent = n;
            }
            r.Left   = n;
            n.Parent = r;
        }
Example #18
0
        public bool TryLookup(IRBNode template, out IRBNode val)
        {
            var n = LookupNode(template);

            if (n == null)
            {
                val = null;
                return(false);
            }

            val = n;
            return(true);
        }
Example #19
0
 //----------------------------
 private void InsertCase5(IRBNode n)
 {
     n.Parent.Color = Color.BLACK;
     n.Grandparent().Color = Color.RED;
     if (n == n.Parent.Left && n.Parent == n.Grandparent().Left)
     {
         RotateRight(n.Grandparent());
     }
     else
     {
         RotateLeft(n.Grandparent());
     }
 }
Example #20
0
 //----------------------------
 private void InsertCase3(IRBNode n)
 {
     if (NodeColor(n.Uncle()) == Color.RED)
     {
         n.Parent.Color = Color.BLACK;
         n.Uncle().Color       = Color.BLACK;
         n.Grandparent().Color = Color.RED;
         InsertCase1(n.Grandparent());
     }
     else
     {
         InsertCase4(n);
     }
 }
Example #21
0
        private void RotateRight(IRBNode n)
        {
            IRBNode l = n.Left;

            ReplaceNode(n, l);
            n.Left = l.Right;

            if (l.Right != null)
            {
                l.Right.Parent = n;
            }

            l.Right  = n;
            n.Parent = l;
        }
Example #22
0
        //----------------------------
        private void InsertCase4(IRBNode n)
        {
            if (n == n.Parent.Right && n.Parent == n.Grandparent().Left)
            {
                RotateLeft(n.Parent);
                n = n.Left;
            }
            else if (n == n.Parent.Left && n.Parent == n.Grandparent().Right)
            {
                RotateRight(n.Parent);
                n = n.Right;
            }

            InsertCase5(n);
        }
Example #23
0
 private void DeleteCase4(IRBNode n)
 {
     if (NodeColor(n.Parent) == Color.RED &&
         NodeColor(n.Sibling()) == Color.BLACK &&
         NodeColor(n.Sibling().Left) == Color.BLACK &&
         NodeColor(n.Sibling().Right) == Color.BLACK)
     {
         n.Sibling().Color = Color.RED;
         n.Parent.Color = Color.BLACK;
     }
     else
     {
         DeleteCase5(n);
     }
 }
Example #24
0
        private static void VerifyProperty4(IRBNode n)
        {
            if (NodeColor(n) == Color.RED)
            {
                Assert.IsTrue((NodeColor(n.Left) == Color.BLACK));
                Assert.IsTrue((NodeColor(n.Right) == Color.BLACK));
                Assert.IsTrue((NodeColor(n.Parent) == Color.BLACK));
            }

            if (n == null)
            {
                return;
            }
            VerifyProperty4(n.Left);
            VerifyProperty4(n.Right);
        }
Example #25
0
        /// <summary>
        /// Get a named storage contained in the current one if existing.
        /// </summary>
        /// <param name="storageName">Name of the storage to look for</param>
        /// <returns>A storage reference if found else null</returns>
        /// <exception cref="T:OpenMcdf.CFDisposedException">Raised if trying to delete item from a closed compound file</exception>
        /// <example>
        /// <code>
        ///
        /// String FILENAME = "MultipleStorage2.cfs";
        /// CompoundFile cf = new CompoundFile(FILENAME, UpdateMode.ReadOnly, false, false);
        ///
        /// CFStorage st = cf.RootStorage.TryGetStorage("MyStorage");
        ///
        /// Assert.IsNotNull(st);
        /// cf.Close();
        /// </code>
        /// </example>
        public CFStorage TryGetStorage(String storageName)
        {
            CheckDisposed();

            IDirectoryEntry template = DirectoryEntry.Mock(storageName, StgType.StgInvalid);
            IRBNode         outDe    = null;

            if (Children.TryLookup(template, out outDe) && ((IDirectoryEntry)outDe).StgType == StgType.StgStorage)
            {
                return(new CFStorage(this.CompoundFile, outDe as IDirectoryEntry));
            }
            else
            {
                return(null);
            }
        }
Example #26
0
        /// <summary>
        /// Get a named storage contained in the current one if existing.
        /// </summary>
        /// <param name="storageName">Name of the storage to look for</param>
        /// <returns>A storage reference if existing.</returns>
        /// <exception cref="T:OpenMcdf.CFDisposedException">Raised if trying to delete item from a closed compound file</exception>
        /// <exception cref="T:OpenMcdf.CFItemNotFound">Raised if item to delete is not found</exception>
        /// <example>
        /// <code>
        ///
        /// String FILENAME = "MultipleStorage2.cfs";
        /// CompoundFile cf = new CompoundFile(FILENAME, UpdateMode.ReadOnly, false, false);
        ///
        /// CFStorage st = cf.RootStorage.GetStorage("MyStorage");
        ///
        /// Assert.IsNotNull(st);
        /// cf.Close();
        /// </code>
        /// </example>
        public CFStorage GetStorage(String storageName)
        {
            CheckDisposed();

            IDirectoryEntry template = DirectoryEntry.Mock(storageName, StgType.StgInvalid);
            IRBNode         outDe    = null;

            if (Children.TryLookup(template, out outDe) && ((IDirectoryEntry)outDe).StgType == StgType.StgStorage)
            {
                return(new CFStorage(this.CompoundFile, outDe as IDirectoryEntry));
            }
            else
            {
                throw new CFItemNotFound("Cannot find item [" + storageName + "] within the current storage");
            }
        }
Example #27
0
        private void DoVisitTreeNodes(Action <IRBNode> action, IRBNode walker)
        {
            if (walker.Left != null)
            {
                DoVisitTreeNodes(action, walker.Left);
            }

            if (action != null)
            {
                action(walker);
            }

            if (walker.Right != null)
            {
                DoVisitTreeNodes(action, walker.Right);
            }
        }
Example #28
0
 private void DeleteCase6(IRBNode n)
 {
     n.Sibling().Color = NodeColor(n.Parent);
     n.Parent.Color = Color.BLACK;
     if (n == n.Parent.Left)
     {
         //assert nodeColor(n.sibling().right) == Color.RED;
         n.Sibling().Right.Color = Color.BLACK;
         RotateLeft(n.Parent);
     }
     else
     {
         //assert nodeColor(n.sibling().left) == Color.RED;
         n.Sibling().Left.Color = Color.BLACK;
         RotateRight(n.Parent);
     }
 }
Example #29
0
        private void DeleteCase2(IRBNode n)
        {
            if (NodeColor(n.Sibling()) == Color.RED)
            {
                n.Parent.Color = Color.RED;
                n.Sibling().Color = Color.BLACK;
                if (n == n.Parent.Left)
                {
                    RotateLeft(n.Parent);
                }
                else
                {
                    RotateRight(n.Parent);
                }
            }

            DeleteCase3(n);
        }
Example #30
0
        /// <summary>
        /// Get a named <see cref="T:OpenMcdf.CFStream">stream</see> contained in the current storage if existing.
        /// </summary>
        /// <param name="streamName">Name of the stream to look for</param>
        /// <returns>A stream reference if found, else null</returns>
        /// <exception cref="T:OpenMcdf.CFDisposedException">Raised if trying to delete item from a closed compound file</exception>
        /// <example>
        /// <code>
        /// String filename = "report.xls";
        ///
        /// CompoundFile cf = new CompoundFile(filename);
        /// CFStream foundStream = cf.RootStorage.TryGetStream("Workbook");
        ///
        /// byte[] temp = foundStream.GetData();
        ///
        /// Assert.IsNotNull(temp);
        ///
        /// cf.Close();
        /// </code>
        /// </example>
        public CFStream TryGetStream(String streamName)
        {
            CheckDisposed();

            IDirectoryEntry tmp = DirectoryEntry.Mock(streamName, StgType.StgStream);

            //if (children == null)
            //{
            //    children = compoundFile.GetChildrenTree(SID);
            //}

            IRBNode outDe = null;

            if (Children.TryLookup(tmp, out outDe) && (((IDirectoryEntry)outDe).StgType == StgType.StgStream))
            {
                return(new CFStream(this.CompoundFile, (IDirectoryEntry)outDe));
            }
            else
            {
                return(null);
            }
        }