private void addChildrenWithStride(FibTreeNode to, FibTreeNode from, Dictionary <FibTreeNode, CompressData> compressData) { if (from.Label != null) { FibTreeLabel label = Labels.GetLabelByNextHop(from.Label.NextHop); if (label == null) { label = Labels.AddLabelForNextHop(from.Label.NextHop); } to.Label = label; } if (from.Children.Count == 0) { return; } List <FibTreeNode> childrenToAdd = from.GetChildrenAtLevel(compressData[from].OptimalStride); foreach (FibTreeNode childToAdd in childrenToAdd) { FibTreeNode newNode = to.AddChild(getConcatenatedEdgeLabelBetweenNodes(from, childToAdd)); addChildrenWithStride(newNode, childToAdd, compressData); } }
public void CreateFromFibTable(FibTable table) { Root = new FibTreeNode(null); Labels.Clear(); foreach (FibEntry entry in table) { FibTreeNode node = Root; for (int i = 0; i < entry.BinaryForm.Length; i++) { int edgeLabel = entry.BinaryForm[i] - (int)'0'; if (node.GetChild(edgeLabel) == null) { node.AddChild(edgeLabel); } node = node.GetChild(edgeLabel); } FibTreeLabel label = Labels.GetLabelByNextHop(entry.NextHop); if (label == null) { label = Labels.AddLabelForNextHop(entry.NextHop); } node.Label = label; } TreeChanged?.Invoke(); }
public void CreateFromFibTreeAndNormalize(FibTree tree) { Root = new FibTreeNode(null); Labels.Clear(); copyNodeAndChildrens(Root, tree.Root); normalize(); TreeChanged?.Invoke(); }
private string getConcatenatedEdgeLabelBetweenNodes(FibTreeNode parent, FibTreeNode child) { if (parent == child) { return(""); } return(getConcatenatedEdgeLabelBetweenNodes(parent, child.Parent) + child.Parent.GetEdgeLabelForChild(child)); }
public void CreateFromNormalizedFibTreeAndCompress(FibTree tree) { Root = new FibTreeNode(null); Labels.Clear(); Dictionary <FibTreeNode, CompressData> compressData = new Dictionary <FibTreeNode, CompressData>(); calculateCompressData(tree.Root, compressData); addChildrenWithStride(Root, tree.Root, compressData); TreeChanged?.Invoke(); }
public string GetEdgeLabelForChild(FibTreeNode childNode) { foreach (KeyValuePair <string, FibTreeNode> childEntry in Children) { if (childEntry.Value == childNode) { return(childEntry.Key); } } return(null); }
private void normalize_RemoveInteriorLabels(FibTreeNode node) { if (node.Children.Count == 0) { return; } foreach (FibTreeNode child in node.Children.Values) { normalize_RemoveInteriorLabels(child); } node.Label = null; }
private void addTreeNodeAndChildren(FibTreeNode node, string binaryPath = "") { if (node.Label != null) { FibEntry newEntry = new FibEntry(binaryPath, node.Label.NextHop); entries.Add(newEntry); } foreach (KeyValuePair <string, FibTreeNode> child in node.Children) { addTreeNodeAndChildren(child.Value, binaryPath + child.Key); } }
private void copyNodeAndChildrens(FibTreeNode to, FibTreeNode from) { foreach (KeyValuePair <string, FibTreeNode> child in from.Children) { FibTreeNode newNode = to.AddChild(child.Key); copyNodeAndChildrens(newNode, child.Value); } if (from.Label != null) { FibTreeLabel label = Labels.GetLabelByNextHop(from.Label.NextHop); if (label == null) { label = Labels.AddLabelForNextHop(from.Label.NextHop); } to.Label = label; } }
private void _lookup(FibTreeNode node, string ipBits) { Nodes.Add(node); if (node.Label != null) { NextHop = node.Label; SolutionNode = node; } string examinedBits = ipBits.Substring(0, node.EdgeLabelLength ?? 0); string remainderBits = ipBits.Substring(node.EdgeLabelLength ?? 0); foreach (KeyValuePair <string, FibTreeNode> childEntry in node.Children) { if (childEntry.Key == examinedBits) { _lookup(childEntry.Value, remainderBits); } } }
private void normalize_Reduce(FibTreeNode node) { if (node.Children.Count == 0) { return; } foreach (FibTreeNode child in node.Children.Values) { normalize_Reduce(child); } List <FibTreeLabel> usedLabels = node.AllLabelsInSubtreeUnique(); if (usedLabels.Count == 1) { node.ClearChildren(); node.Label = usedLabels[0]; } }
public FibTreeNode AddChild(string edgeLabel, FibTreeLabel nodeLabel = null) { if (Children.ContainsKey(edgeLabel)) { throw new Exception("This node already contains a child with this edge label!"); } if (EdgeLabelLength == null) { EdgeLabelLength = edgeLabel.Length; } if (edgeLabel.Length != EdgeLabelLength) { throw new Exception(string.Format("Edge labels in this node must be {0} character(s) long.", EdgeLabelLength)); } FibTreeNode newNode = new FibTreeNode(this); newNode.Label = nodeLabel; Children.Add(edgeLabel, newNode); return(newNode); }
private void calculateCompressData(FibTreeNode node, Dictionary <FibTreeNode, CompressData> compressData) { if (node.Children.Count == 0) { compressData.Add(node, new CompressData() { Node = node, Depth = 0, OptimalStride = 0, PointerCount = 0 }); } else { foreach (FibTreeNode child in node.Children.Values) { calculateCompressData(child, compressData); } CompressData nodeCompressData = new CompressData() { Node = node, Depth = node.GetDepth(), OptimalStride = -1, PointerCount = -1 }; for (int k = 1; k <= nodeCompressData.Depth; k++) { int pointerCount = (int)Math.Pow(2, k); foreach (FibTreeNode child in node.GetChildrenAtLevel(k)) { pointerCount += compressData[child].PointerCount; } if ((nodeCompressData.PointerCount == -1) || (pointerCount < nodeCompressData.PointerCount)) { nodeCompressData.OptimalStride = k; nodeCompressData.PointerCount = pointerCount; } } compressData.Add(node, nodeCompressData); } }
private void normalize_CreateLeaves(FibTreeNode node, FibTreeLabel nearestLabel) { if (node.Children.Count == 0) { return; } if (node.Label != null) { nearestLabel = node.Label; } if (node.Children.Count == 1) { string notExistingEdgeLabel = string.Format("{0}", (int)'1' - (int)node.Children.ToList()[0].Key[0]); node.AddChild(notExistingEdgeLabel, nearestLabel); } normalize_CreateLeaves(node.GetChild(0), nearestLabel); normalize_CreateLeaves(node.GetChild(1), nearestLabel); }
public FibTreeNode(FibTreeNode parent, FibTreeNode child0, FibTreeNode child1) { Parent = parent; Children.Add("0", child0); Children.Add("1", child1); }
public FibTreeNode(FibTreeNode parent) { Parent = parent; }