Esempio n. 1
0
        public static void CreateDatabase(IEnumerable <Address> ips)
        {
            BinaryRoot = new BinaryNode()
            {
                NextHop = "Root"
            };
            CompressedRoot = new CompressedNode()
            {
                NextHop = "Root"
            };
            MultibitRoot = new MultibitNode()
            {
                Stride = MultibitStride
            };

            foreach (var ip in ips)
            {
                BinaryRoot.AddChild(ip.MaskedIPv4, ip.BinaryString);
                CompressedRoot.AddChild(ip.MaskedIPv4, ip.BinaryString);
                MultibitRoot.AddChild(ip.MaskedIPv4, ip.BinaryString);
            }

            CompressedRoot.Compress();

            Main.UpdateTreeViews();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            // Create the tries
            var binaryRoot     = new BinaryNode("Root");
            var compressedRoot = new CompressedNode("Root");
            var multibitRoot   = new MultibitNode()
            {
                Stride = 3
            };

            // Generate IPs
            var gen = new AddressGenerator().GenerateAddress();
            var db  = gen.Take(10).ToArray();

            // Add children to tries
            foreach (var ip in db)
            {
                binaryRoot.AddChild(ip.MaskedIPv4, ip.BinaryString);
                compressedRoot.AddChild(ip.MaskedIPv4, ip.BinaryString);
                multibitRoot.AddChild(ip.MaskedIPv4, ip.BinaryString);
            }
            compressedRoot.Compress();

            // Perform a lookup
            var tosearch = new Address("132.15.162.33/17");

            Console.WriteLine($"Binary: {binaryRoot.Lookup(tosearch.BinaryString)}");
            Console.WriteLine($"Compressed: {compressedRoot.Lookup(tosearch.BinaryString)}");
            Console.WriteLine($"Multibit: {multibitRoot.Lookup(tosearch.BinaryString, "Root")}");

            Console.ReadLine();
        }
        /// <inheritdoc/>
        public void AddChild(string prefix, string path)
        {
            switch (path.Length)
            {
            case 0:
                break;

            case 1:
                if (path == "0")
                {
                    Left = new CompressedNode(prefix);
                }
                else
                {
                    Right = new CompressedNode(prefix);
                }
                break;

            default:
                if (path.StartsWith("0"))
                {
                    if (Left == null)
                    {
                        Left = new CompressedNode();
                    }

                    Left.AddChild(prefix, path.Substring(1));
                }
                else
                {
                    if (Right == null)
                    {
                        Right = new CompressedNode();
                    }

                    Right.AddChild(prefix, path.Substring(1));
                }
                break;
            }
        }
Esempio n. 4
0
        public void UpdateCompressedTreeView(CompressedNode root)
        {
            compressedTreeView.Items.Clear();

            TreeViewItem child = new TreeViewItem();

            child.Header     = "Root";
            child.Foreground = new SolidColorBrush(Colors.OrangeRed);
            child.Tag        = root;

            compressedTreeView.Items.Add(child);

            if (root.Left != null)
            {
                AddCompressedChild(root.Left, child, root.Left.NextHop != "" ? $"0 ({root.Left.NextHop})" : "0");
            }

            if (root.Right != null)
            {
                AddCompressedChild(root.Right, child, root.Right.NextHop != "" ? $"1 ({root.Right.NextHop})" : "1");
            }
        }
Esempio n. 5
0
        public void AddCompressedChild(CompressedNode node, TreeViewItem parentItem, string name)
        {
            var child = new TreeViewItem();

            child.Header = name;
            child.Tag    = node;
            if (node.NextHop != "")
            {
                child.Foreground = new SolidColorBrush(Colors.DodgerBlue);
            }

            if (node.Left != null)
            {
                AddCompressedChild(node.Left, child, node.Left.NextHop != "" ? $"0 ({node.Left.NextHop})" : "0");
            }

            if (node.Right != null)
            {
                AddCompressedChild(node.Right, child, node.Right.NextHop != "" ? $"1 ({node.Right.NextHop})" : "1");
            }

            parentItem.Items.Add(child);
        }
        private CompressedGraph CreateCompressedGraph(IGraph graph, int[] directGraphOfSimilaNode)
        {
            var compressedGraph = new CompressedGraph();

            for (int i = 0; i < directGraphOfSimilaNode.Length; i++)
            {
                var nodeId        = i;
                var similarNodeId = directGraphOfSimilaNode[i];

                var node = graph.GetNode(nodeId);

                var compressedNode = new CompressedNode
                {
                    Id = nodeId,
                };

                if (similarNodeId == -1)
                {
                    compressedNode.ExtraNodes = new SortedSet <int>(node.Neighbors);
                }
                else
                {
                    var similarNode = graph.GetNode(similarNodeId);

                    var referenceList = GetReferenceListAndExtraNodes(node, similarNode);
                    var extraNodes    = GetExtraNodes(node, similarNode);

                    compressedNode.ReferenceList = referenceList;
                    compressedNode.ExtraNodes    = extraNodes;
                    compressedNode.ReferenceId   = similarNodeId;
                }

                compressedGraph.AddNode(compressedNode);
            }

            return(compressedGraph);
        }
        /// <summary>
        /// Prints the compressed tree.
        /// </summary>
        /// <param name="root">The root node of the tree</param>
        /// <param name="topMargin">The amount of lines to leave at the top of the tree</param>
        /// <param name="leftMargin">The amount of spaces to leave at the left of the tree</param>
        public static void Print(this CompressedNode root, int topMargin = 2, int leftMargin = 2)
        {
            if (root == null)
            {
                return;
            }

            int rootTop = Console.CursorTop + topMargin;
            var last    = new List <NodeInfo>();
            var next    = root;

            for (int level = 0; next != null; level++)
            {
                var item = new NodeInfo {
                    Node = next, Text = next.NextHop == "" ? $" -- ({next.Segment}) " : $" {next.NextHop} ({next.Segment}) "
                };

                if (level < last.Count)
                {
                    item.StartPos = last[level].EndPos + 1;
                    last[level]   = item;
                }
                else
                {
                    item.StartPos = leftMargin;
                    last.Add(item);
                }

                if (level > 0)
                {
                    item.Parent = last[level - 1];

                    if (next == item.Parent.Node.Left)
                    {
                        item.Parent.Left = item;
                        item.EndPos      = Math.Max(item.EndPos, item.Parent.StartPos);
                    }
                    else
                    {
                        item.Parent.Right = item;
                        item.StartPos     = Math.Max(item.StartPos, item.Parent.EndPos);
                    }
                }

                next = next.Left ?? next.Right;

                for (; next == null; item = item.Parent)
                {
                    Print(item, rootTop + 2 * level);

                    if (--level < 0)
                    {
                        break;
                    }

                    if (item == item.Parent.Left)
                    {
                        item.Parent.StartPos = item.EndPos;
                        next = item.Parent.Node.Right;
                    }
                    else
                    {
                        if (item.Parent.Left == null)
                        {
                            item.Parent.EndPos = item.StartPos;
                        }
                        else
                        {
                            item.Parent.StartPos += (item.StartPos - item.Parent.EndPos) / 2;
                        }
                    }
                }
            }
            Console.SetCursorPosition(0, rootTop + 2 * last.Count - 1);
        }