Exemple #1
0
        /// <summary>
        /// 节省空间插入主要是在分裂节点的时候,先看兄弟节点是否有空余位置
        /// 例如假设有2个兄弟节点,一个是2一个是3,现在我往3那边插一个节点
        /// 我先查找到兄弟节点,发现有剩余位置,就把3的最左边的数往兄弟节点挪
        /// 然后再插入
        /// </summary>
        /// <param name="value"></param>
        /// <param name="tree"></param>
        /// <returns></returns>
        private IBNode ComplexInsert(int value, IBNode tree)
        {
            //调整内部节点


            return(tree);
        }
Exemple #2
0
        public void Xml2IBFTest1Level2(string xml, string country,
                                       string city, string county1, string county2)
        {
            IBNode ibNode = IBFormat.ConvertXml2IBF(xml);

            Assert.IsTrue(ibNode.IsRoot());
            Assert.AreEqual(1, ibNode.GetChildNodes().Count);
            Assert.AreEqual("Country", ibNode.GetName());
            Assert.AreEqual(country, ibNode.GetValue());

            IBNode childNode = ibNode.GetChildNodes()[0];

            Assert.AreEqual(2, childNode.GetChildNodes().Count);
            Assert.AreEqual("City", childNode.GetName());
            Assert.AreEqual(city, childNode.GetValue());

            IBNode grandChild1 = childNode.GetChildNodes()[0];

            Assert.AreEqual(0, grandChild1.GetChildNodes().Count);
            Assert.AreEqual("County", grandChild1.GetName());
            Assert.AreEqual(county1, grandChild1.GetValue());

            IBNode grandChild2 = childNode.GetChildNodes()[1];

            Assert.AreEqual(0, grandChild2.GetChildNodes().Count);
            Assert.AreEqual("County", grandChild2.GetName());
            Assert.AreEqual(county2, grandChild2.GetValue());
        }
Exemple #3
0
        public void BacthVonverse()
        {
            long tick = Environment.TickCount;

            IAnalyser btAnalyser = new CommonAnalyser();

            foreach (FileInfo fInfo in _sourFilenameList)
            {
                byte[] buffer = null;
                using (FileStream stream = new FileStream(fInfo.FullName, FileMode.Open))
                {
                    buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, (int)stream.Length);
                }
                // 分析
                IBNode rootNode = btAnalyser.Analysis(buffer);
                // 转换
                Iterate(btAnalyser.BNodeList);
                // 生成新的文件名
                string newFilename = String.Format("{0}\\{1}", _destFolder, fInfo.Name);
                // 保存
                buffer = rootNode.ToBytes();
                using (FileStream stream = new FileStream(newFilename, FileMode.Create))
                {
                    stream.Write(buffer, 0, buffer.Length);
                }
            }

            if (_callBackFunc != null)
            {
                _callBackFunc(String.Format("转换完毕, 总用时{0} 秒", (Environment.TickCount - tick) / 1000.0));
            }
        }
Exemple #4
0
 private IBNode Delete(int value, IBNode tree)
 {
     if (tree == null)
     {
     }
     else if (tree.IsLeaf())
     {
         tree = DeleteLeaf(value, tree);
     }
     else//递归去找叶子节点
     {
         var  node   = (BNode)tree;
         bool finded = false;
         for (int i = 0; i < node.Keys.Count; i++)
         {
             if (value < node.Keys[i])
             {
                 tree = Delete(value, node.Children[i]);
             }
         }
         if (!finded)
         {
             tree = Delete(value, node.Children[node.Keys.Count]);
         }
     }
     return(tree);
 }
Exemple #5
0
        public void AddNode(string childName, object childValue, string parentName, object parentValue)
        {
            IBNode parentNode = new IBNode(parentName, parentValue, null);
            IBNode childNode  = new IBNode(childName, childValue, parentNode);

            Assert.AreEqual(parentNode, childNode.GetParentNode());
        }
Exemple #6
0
        public void CreateNodeTest(string name, object value)
        {
            IBNode node = new IBNode(name, value, null);

            Assert.AreEqual(name, node.GetName());
            Assert.AreEqual(value, node.GetValue());
        }
Exemple #7
0
 /// <summary>
 /// 这是最简单的插入,没有节省空间,
 /// 如果一个节点的孩子数是2,除非关键字在这个节点的范围内,否则基本不会在往这里插入节点
 /// </summary>
 /// <param name="value"></param>
 /// <param name="tree"></param>
 /// <returns></returns>
 private IBNode Insert(int value, IBNode tree)
 {
     //说明空
     if (tree == null)
     {
         BLeaf leaf = new BLeaf(M);
         leaf.AddValue(value);
         tree = leaf;
     }
     else if (tree.IsLeaf())
     {
         tree = InsertLeaf(value, tree);
     }
     else//递归去找叶子节点
     {
         var  node   = (BNode)tree;
         bool finded = false;
         for (int i = 0; i < node.Keys.Count; i++)
         {
             if (value < node.Keys[i])
             {
                 tree = Insert(value, node.Children[i]);
             }
         }
         if (!finded)
         {
             tree = Insert(value, node.Children[node.Keys.Count]);
         }
     }
     return(tree);
 }
Exemple #8
0
        public List <IBNode> GetSiblings(IBNode node)
        {
            List <IBNode> siblings = new List <IBNode>();

            for (int i = 0; i < GetChildrenNum(); i++)
            {
                if (Children[i].Equals(node))
                {
                    if (i == 0)//开头
                    {
                        siblings.Add(Children[i + 1]);
                    }
                    else if (i == (GetChildrenNum() - 1))//结尾
                    {
                        siblings.Add(Children[i - 1]);
                    }
                    else
                    {
                        siblings.Add(Children[i - 1]);
                        siblings.Add(Children[i + 1]);
                    }
                    break;
                }
            }
            return(siblings);
        }
Exemple #9
0
        public void Xml2IBFTest1Level(string xml, object value)
        {
            IBNode ibNode = IBFormat.ConvertXml2IBF(xml);

            Assert.IsTrue(ibNode.IsRoot());
            Assert.AreEqual(value, ibNode.Value);
            Assert.AreEqual("sample", ibNode.GetName());
            Assert.AreEqual(0, ibNode.GetChildNodes().Count);
        }
Exemple #10
0
 private static IBNode <TValue> DeleteMin(IBNode <TValue> bNode)
 {
     if (bNode.LeftNode == null)
     {
         return(bNode.RightNode);
     }
     bNode.LeftNode = DeleteMin(bNode.LeftNode);
     return(bNode);
 }
Exemple #11
0
        public void AddChild(string childName, object childValue, string parentName, object parentValue)
        {
            IBNode parentNode = new IBNode(parentName, parentValue, null);
            IBNode childNode  = new IBNode(childName, childValue, parentNode);

            List <IBNode> nodes = parentNode.GetChildNodes();

            Assert.AreEqual(1, nodes.Count);
            Assert.AreEqual(childNode, nodes[0]);
        }
Exemple #12
0
        public bool RemoveChildren(IBNode node)
        {
            bool res = Children.Remove(node);

            if (res)
            {
                BuildKeys();
            }
            return(res);
        }
Exemple #13
0
        private IBNode DeleteLeaf(int value, IBNode tree)
        {
            BLeaf leaf = (BLeaf)tree;
            bool  res  = leaf.RemoveValue(value);

            if (res)
            {
                if (leaf.GetChildrenNum() < Math.Ceiling(M / 2m))
                {
                    BNode parent = leaf.Parent;
                    if (parent == null)
                    {
                    }
                    else
                    {
                        //先找到孩子节点
                        var siblings = parent.GetSiblings(leaf);
                        if (siblings.Count == 0)
                        {//非孩子节点
                         //do nothing
                        }
                        else
                        {
                            IBNode sibling = null;
                            foreach (var node in siblings)
                            {
                                if (node.GetChildrenNum() > Math.Ceiling(M / 2m))
                                {
                                    sibling = node;
                                    break;
                                }
                            }
                            if (sibling == null)
                            {//兄弟节点都没有多的元素
                                MergeLeaf((BLeaf)siblings[0], leaf);
                                //开始检索上级
                                parent = (BNode)DeleteNode(parent);
                            }
                            else
                            {
                                ShareLeafValue((BLeaf)sibling, leaf);
                            }
                        }
                    }
                }
                else
                {//do nothing
                }
            }
            else
            {//节点不存在
            }
            return(tree);
        }
Exemple #14
0
 private void ConstructTree(TreeNode tParent, IBNode bParent)
 {
     tParent.Text = bParent.ToString();
     bParent.Child.ForEach(bNode =>
     {
         TreeNode tNode = new TreeNode();
         _treeNodeList.Add(tNode);
         tNode.Text = bNode.ToString();
         tParent.Nodes.Add(tNode);
         ConstructTree(tNode, bNode);
     });
 }
Exemple #15
0
        public void AddLevel3Child(string name1, object value1, string name2, object value2, string name3, object value3)
        {
            IBNode node1 = new IBNode(name1, value1, null);
            IBNode node2 = new IBNode(name2, value2, node1);
            IBNode node3 = new IBNode(name3, value3, node2);


            List <IBNode> nodes1 = node1.GetChildNodes();
            List <IBNode> nodes2 = node2.GetChildNodes();

            Assert.AreEqual(1, nodes1.Count);
            Assert.AreEqual(1, nodes2.Count);

            Assert.AreEqual(node2, nodes1[0]);
            Assert.AreEqual(node3, nodes2[0]);
        }
Exemple #16
0
        private void AnalysisBt(string filename)
        {
            SetLogger(BTToolLogger.Start('f', filename));

            // 清理上一次的工作
            treeView.Nodes.Clear();
            _btAnalyser = new CommonAnalyser();
            rootNode    = null;

            // 读入BT文件
            byte[] buffer = null;
            using (FileStream stream = new FileStream(filename, FileMode.Open))
            {
                buffer = new byte[stream.Length];
                stream.Read(buffer, 0, (int)stream.Length);
            }

            // 解析
            IBNode rootBNode = null;

            try
            {
                rootBNode = this._btAnalyser.Analysis(buffer);
                rootNode  = rootBNode;
            }
            catch
            {
                SetLogger(BTToolLogger.Start('e', filename));
                tabControl.SelectedIndex = 1;
                filename = null;
                return;
            }
            SetLogger(BTToolLogger.End('f'));

            SetLogger(BTToolLogger.Start('s'));
            // 构建树
            TreeNode rootTNode = new TreeNode();

            ConstructTree(rootTNode, rootBNode);
            rootTNode.Expand();
            treeView.Nodes.Add(rootTNode);
            SetLogger(BTToolLogger.End('s'));
        }
Exemple #17
0
        private void AnalyseDictionary(IBNode parent)
        {
            // 字典一定是d开始的
            if (GetCurrentCharMove() != 'd')
            {
                return;
            }

            // 循环分析键值对
            do
            {
                KeyValueNode keyValueNode = _bNodeFactory.GetBNode('k') as KeyValueNode;
                // 键值对,键一定是string
                keyValueNode.SetKey(AnalyseString());
                // 值
                switch (GetCurrentChar())
                {
                case 'i':     // 数字
                    keyValueNode.SetValue(AnalyseInteger());
                    keyValueNode.ValueType = 'i';
                    break;

                case 'd':     // 字典
                    AnalyseDictionary(keyValueNode);
                    keyValueNode.ValueType = 'd';
                    break;

                case 'l':     // 列表
                    AnalyseList(keyValueNode);
                    keyValueNode.ValueType = 'l';
                    break;

                default:
                    keyValueNode.SetValue(AnalyseString());
                    keyValueNode.ValueType = 's';
                    break;
                }
                parent.Child.Add(keyValueNode);
            } while (GetCurrentChar() != 'e');
            GetCurrentCharMove();
        }
Exemple #18
0
        private IBNode InsertLeaf(int value, IBNode tree)
        {
            var leaf = (BLeaf)tree;

            if (leaf.Values.Count < M)
            {
                //这里改完也得找到父节点返回
                leaf.AddValue(value);
            }
            else
            {
                //不管怎样,都要先加节点
                BLeaf newLeaf;
                SplitLeaf(ref leaf, out newLeaf, value);

                var parent = leaf.Parent;
                if (parent == null)
                {
                    //如果为空,新建一个
                    parent = new BNode(M);
                    parent.InsertChild(leaf);
                    parent.InsertChild(newLeaf);
                    tree = parent;
                }
                else
                {
                    //把叶子插入了,然后开始递归Node节点
                    parent.InsertChild(newLeaf);

                    //如果改编了node,返回的就是parent了,最后返回了最后一层改变的parent
                    tree = InsertNode(parent);
                }
            }
            //因为InsertLeaf和InsertNode都没有递归,所以在这里递归开始往上返回,在这里开始找父节点
            //找到根节点返回
            while (tree.Parent != null)
            {
                tree = tree.Parent;
            }
            return(tree);
        }
Exemple #19
0
        private void AnalyseList(IBNode parent)
        {
            // 列表一定是l开始的
            if (GetCurrentCharMove() != 'l')
            {
                return;
            }

            int count = 0;

            // 循环读入列表项
            do
            {
                ListItemNode listItemNode = _bNodeFactory.GetBNode('l') as ListItemNode;
                switch (GetCurrentChar())
                {
                case 'i':     // 数字
                    listItemNode.SetValue(AnalyseInteger());
                    listItemNode.ValueType = 'i';
                    break;

                case 'd':     // 字典
                    AnalyseDictionary(listItemNode);
                    listItemNode.ValueType = 'd';
                    break;

                case 'l':     // 列表
                    AnalyseList(listItemNode);
                    listItemNode.ValueType = 'l';
                    break;

                default:
                    listItemNode.SetValue(AnalyseString());
                    listItemNode.ValueType = 's';
                    break;
                }
                listItemNode.ListIndex = count++;
                parent.Child.Add(listItemNode);
            } while (GetCurrentChar() != 'e');
            GetCurrentCharMove();
        }
Exemple #20
0
        public IBNode GetBNode(char type)
        {
            IBNode node = null;

            switch (type)
            {
            case 'l':     // listitem node
                node = new ListItemNode();
                break;

            case 'd':     // dict node
                node = new DictNode();
                break;

            case 'k':     // key value node
                node = new KeyValueNode();
                break;
            }
            _bNodeList.Add(node);
            return(node);
        }
Exemple #21
0
        /// <summary>
        /// 读入BT文件,并解析,但不生成树
        /// </summary>
        /// <param name="filename">bt文件名</param>
        public void OpenFile(string filename)
        {
            _btAnalyser = new CommonAnalyser();
            _tRootNode  = null;

            // 读入BT文件
            byte[] buffer = null;
            using (FileStream stream = new FileStream(filename, FileMode.Open))
            {
                buffer = new byte[stream.Length];
                stream.Read(buffer, 0, (int)stream.Length);
            }

            // 解析
            try
            {
                _bRootNode = this._btAnalyser.Analyse(buffer);
            }
            catch { throw; }

            _tRootNode = null; // 重置标志
        }
Exemple #22
0
        public void InsertChild(IBNode child)
        {
            var  key      = child.GetMinKey();
            bool inserted = false;

            for (int i = 0; i < Children.Count; i++)
            {
                if (key < Children[i].GetMinKey())
                {
                    inserted = true;
                    Children.Insert(i, child);
                    break;
                }
            }
            if (!inserted)
            {
                Children.Add(child);
            }
            //设置父节点
            child.Parent = this;
            //重新整理key
            BuildKeys();
        }
Exemple #23
0
        private void AnalysisDictionary(IBNode parent)
        {
            // 字典一定是d开始的
            if (GetCurrentCharMove() != 'd')
                return;

            // 循环分析键值对
            do
            {
                KeyValueNode keyValueNode = _bNodeFactory.GetBNode('k') as KeyValueNode;
                // 键值对,键一定是string
                keyValueNode.SetKey(AnalysisString());
                // 值
                switch (GetCurrentChar())
                {
                    case 'i': // 数字
                        keyValueNode.SetValue(AnalysisInteger());
                        keyValueNode.ValueType = 'i';
                        break;
                    case 'd': // 字典
                        AnalysisDictionary(keyValueNode);
                        keyValueNode.ValueType = 'd';
                        break;
                    case 'l': // 列表
                        AnalysisList(keyValueNode);
                        keyValueNode.ValueType = 'l';
                        break;
                    default:
                        keyValueNode.SetValue(AnalysisString());
                        keyValueNode.ValueType = 's';
                        break;
                }
                parent.Child.Add(keyValueNode);
            } while (GetCurrentChar() != 'e');
            GetCurrentCharMove();
        }
Exemple #24
0
 public BNodeIterator(IBNode rootNode)
 {
 }
Exemple #25
0
 public void InsertNodeChild(IBNode value)
 {
     InsertChild(value);
 }
Exemple #26
0
        private void AnalysisList(IBNode parent)
        {
            // 列表一定是l开始的
            if (GetCurrentCharMove() != 'l')
                return;

            int count = 0;
            // 循环读入列表项
            do
            {
                ListItemNode listItemNode = _bNodeFactory.GetBNode('l') as ListItemNode;
                switch (GetCurrentChar())
                {
                    case 'i': // 数字
                        listItemNode.SetValue(AnalysisInteger());
                        listItemNode.ValueType = 'i';
                        break;
                    case 'd': // 字典
                        AnalysisDictionary(listItemNode);
                        listItemNode.ValueType = 'd';
                        break;
                    case 'l': // 列表
                        AnalysisList(listItemNode);
                        listItemNode.ValueType = 'l';
                        break;
                    default:
                        listItemNode.SetValue(AnalysisString());
                        listItemNode.ValueType = 's';
                        break;
                }
                listItemNode.ListIndex = count++;
                parent.Child.Add(listItemNode);
            } while (GetCurrentChar() != 'e');
            GetCurrentCharMove();
        }
Exemple #27
0
 public void InsertNodeChild(IBNode value)
 {
 }
Exemple #28
0
 public bool RemoveNodeChild(IBNode value)
 {
     return(RemoveChildren(value));
 }
Exemple #29
0
 private void ConstructTree(TreeNode tParent, IBNode bParent)
 {
     tParent.Text = bParent.ToString();
     bParent.Child.ForEach(bNode =>
     {
         TreeNode tNode = new TreeNode();
         _treeNodeList.Add(tNode);
         tNode.Text = bNode.ToString();
         tParent.Nodes.Add(tNode);
         ConstructTree(tNode, bNode);
     });
 }
Exemple #30
0
 public bool RemoveNodeChild(IBNode value)
 {
     return(false);
 }
Exemple #31
0
 public void Insert(int value)
 {
     root = Insert(value, root);
 }
Exemple #32
0
 private void ShareNode(IBNode leftNode, IBNode rightNode)
 {
 }
Exemple #33
0
 private void MergeNode(IBNode leftNode, IBNode rightNode)
 {
 }
Exemple #34
0
 private IBNode DeleteNode(IBNode tree)
 {
     return(tree);
 }
Exemple #35
0
        /// <summary>
        /// 读入BT文件,并解析,但不生成树
        /// </summary>
        /// <param name="filename">bt文件名</param>
        public void OpenFile(string filename)
        {
            _btAnalyser = new CommonAnalyser();
            _tRootNode = null;

            // 读入BT文件
            byte[] buffer = null;
            using (FileStream stream = new FileStream(filename, FileMode.Open))
            {
                buffer = new byte[stream.Length];
                stream.Read(buffer, 0, (int)stream.Length);
            }

            // 解析
            try
            {
                _bRootNode = this._btAnalyser.Analyse(buffer);
            }
            catch { throw; }

            _tRootNode = null; // 重置标志
        }