Beispiel #1
0
        public void AddStartingBranch(string to, int cost)
        {
            int        difference;
            RouterData neighbor = null;
            bool       found    = false;

            foreach (RouterData data in table)
            {
                if (data.to == to)
                {
                    found         = true;
                    difference    = cost - data.cost;
                    data.cost     = cost;
                    neighbor      = FindNeighbor(to);
                    neighbor.cost = cost;
                    UpdateCosts(data.from, difference);
                    break;
                }
            }
            if (!found)
            {
                neighbors.Add(new RouterData(name, to, to, cost));
                table.Add(new RouterData(name, to, to, cost));
            }
        }
Beispiel #2
0
        public void AddBranch(RouterData newData)
        {
            RouterData data, oldData;

            if (newData.from != name)
            {
                if (newData.to != name)
                {
                    oldData = FindBranch(newData.to);
                    if (oldData != null)
                    {
                        if (oldData.cost > newData.cost)
                        {
                            data = FindBranch(newData.owner);
                            if (oldData.cost > newData.cost + data.cost)
                            {
                                oldData.cost = newData.cost + data.cost;
                                oldData.from = data.from;
                            }
                        }
                        else if (oldData.to == newData.to && oldData.from == newData.owner)
                        {
                            data         = FindBranch(newData.owner);
                            oldData.cost = newData.cost + data.cost;
                            foreach (RouterData neighbor in neighbors)
                            {
                                if (oldData.to == neighbor.to)
                                {
                                    if (oldData.cost >= neighbor.cost)
                                    {
                                        oldData.cost = neighbor.cost;
                                        oldData.from = neighbor.from;
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        data = FindBranch(newData.owner);
                        table.Add(new RouterData(name, data.from, newData.to, newData.cost + data.cost));
                    }
                }
                else
                {
                    if (newData.to == newData.from)
                    {
                        int difference;
                        oldData = FindBranch(newData.owner);
                        if (oldData != null)
                        {
                            difference   = newData.cost - oldData.cost;
                            oldData.cost = newData.cost;
                            UpdateCosts(oldData.from, difference);
                        }
                        else
                        {
                            table.Add(new RouterData(name, newData.owner, newData.owner, newData.cost));
                        }
                    }
                }
            }
        }