public void RootIsBlackTest() { var tree = new RBTree(); tree.Insert(1); Assert.IsFalse(tree.Root.Red, "Root was not black"); }
private static void SimpleTest() { RBTree tree = CreateTree(); PrintTree(tree.Root); string input; int value; do { Console1.Write("Input Value:"); input = Console1.ReadLine(); if (input.Equals("exit", StringComparison.OrdinalIgnoreCase)) { break; } if (int.TryParse(input, out value)) { tree.Remove(value); PrintTree(tree.Root); } }while (true); }
public LineGroup(LineMesh lmesh = null, PointMesh pmesh = null) { mLineMesh = lmesh; mPointMesh = pmesh; mEventQ = new RBTree <LineEvent>(mCompareEvents); mActiveLines = new RBTree <LineSegment>(mCompareLines); }
static void Main(string[] args) { RBTree<int> Tree = new RBTree<int>(); Random gen = new Random(); List<int> lst = new List<int>(); for (int i = 0; i < 1000000; ++i) { int tmp = gen.Next(10000000); Tree.InsertNode(tmp); lst.Add(tmp); } lst = (from int i in lst orderby Guid.NewGuid() select i).ToList(); foreach (int i in lst) { Tree.DeleteNode(i); } //foreach (int i in new int[] { 881, 58, 270, 366, 879, 429, 845, 582, 832, 235 }) //{ // Tree.InsertNode(i); //} //foreach (int i in new int[] { 235, 582, 881, 845, 832, 58, 270, 879, 429, 366 }) //{ // Tree.DeleteNode(i); //} Console.WriteLine("Готово"); Console.ReadLine(); }
public void TestSimpleInsertDuplicate() { RBTree <int> tree = new RBTree <int>(); tree.Insert(5); Assert.Throws <InvalidOperationException>(delegate { tree.Insert(5); }); }
private int CheckNode(RBTree <int> tree, RBTreeNode <int> node) { if (node == RBTree <int> .NIL) { return(1); } int left = CheckNode(tree, node.LeftChild); int right = CheckNode(tree, node.RightChild); if (left != right) { throw new Exception("All paths to leaves must contain the same number of black nodes"); } if (node.Color == Color.Black) { return(left + 1); } else { if ((node.LeftChild != RBTree <int> .NIL && node.LeftChild.Color == Color.Red) || (node.RightChild != RBTree <int> .NIL && node.RightChild.Color == Color.Red)) { throw new Exception("A red node can only have black children"); } } return(left); }
private static RBTree <T> .RBTreeNode <T> ToRBTree <T>(T[] array, int left, int right) where T : IComparable <T> { if (right < left) { return(null); } //求中点不要用 int mid = (l + r) / 2,有溢出风险,稳妥的方法是 int mid = l + (r - l) / 2。 var mid = left + (right - left) / 2; var tmpNode = new RBTree <T> .RBTreeNode <T>(); tmpNode.value = array[mid]; tmpNode.left = ToRBTree(array, left, mid - 1); tmpNode.right = ToRBTree(array, mid + 1, right); tmpNode.color = (tmpNode.left == null && tmpNode.right == null) ? NodeColor.Red : NodeColor.Black; if (tmpNode.left != null) { tmpNode.left.parent = tmpNode; } if (tmpNode.right != null) { tmpNode.right.parent = tmpNode; } return(tmpNode); }
public void FindMinMax_EmptyTree_CheckNull() { ITree tree = new RBTree(); Assert.AreEqual(null, tree.MaxNode()); Assert.AreEqual(null, tree.MinNode()); }
public void TestRemove() { var tree = new RBTree <int>(); for (var i = 0; i < 500; i++) { tree.InsertSuccessor(RBTree <int> .GetLast(tree.Root), i); for (var j = 0; j <= i; j++) { var traverse = RBTree <int> .GetFirst(tree.Root); for (var k = 0; k < j; k++) { traverse = traverse.Next; } //remove jth element tree.RemoveNode(traverse); var check = RBTree <int> .GetFirst(tree.Root); for (var k = 0; k < j; k++) { Assert.AreEqual(k, check.Data); check = check.Next; } for (var k = j; k < i; k++) { Assert.AreEqual(k + 1, check.Data); check = check.Next; } //readd tree.InsertSuccessor(traverse.Previous, traverse.Data); } } }
public void TestInsertAtBack() { var tree = new RBTree <char>(); var last = tree.InsertSuccessor(null, 'L'); last = tree.InsertSuccessor(last, 'o'); last = tree.InsertSuccessor(last, 'g'); last = tree.InsertSuccessor(last, 'a'); last = tree.InsertSuccessor(last, 'n'); last = tree.InsertSuccessor(last, '!'); Assert.AreEqual('o', tree.Root.Data); Assert.AreEqual('L', tree.Root.Left.Data); Assert.AreEqual('a', tree.Root.Right.Data); Assert.AreEqual('g', tree.Root.Right.Left.Data); Assert.AreEqual('n', tree.Root.Right.Right.Data); Assert.AreEqual('!', tree.Root.Right.Right.Right.Data); var traverse = RBTree <char> .GetFirst(tree.Root); Assert.AreEqual('L', traverse.Data); traverse = traverse.Next; Assert.AreEqual('o', traverse.Data); traverse = traverse.Next; Assert.AreEqual('g', traverse.Data); traverse = traverse.Next; Assert.AreEqual('a', traverse.Data); traverse = traverse.Next; Assert.AreEqual('n', traverse.Data); traverse = traverse.Next; Assert.AreEqual('!', traverse.Data); traverse = traverse.Next; Assert.AreEqual(null, traverse); }
internal Enumerator(RBTree <T> t, RBTreeNode <T> cur, bool forceStarted) { tree = t; started = forceStarted; isValid = true; current = cur; }
public void Remove_TrueTest() { IRBTree <int, string> target = new RBTree <int, string>(); target.Add(1, "1"); target.Add(5, "5"); target.Add(2, "2"); target.Add(12, "12"); target.Add(4, "4"); Assert.IsTrue(target.Remove(1)); Assert.IsFalse(target.ContainsKey(1)); Assert.AreEqual(4, target.Count); Assert.IsTrue(target.Remove(12)); Assert.IsFalse(target.ContainsKey(12)); Assert.AreEqual(3, target.Count); Assert.IsTrue(target.Remove(4)); Assert.IsFalse(target.ContainsKey(4)); Assert.AreEqual(2, target.Count); Assert.IsTrue(target.Remove(2)); Assert.IsFalse(target.ContainsKey(2)); Assert.AreEqual(1, target.Count); Assert.IsTrue(target.Remove(5)); Assert.IsFalse(target.ContainsKey(5)); Assert.AreEqual(0, target.Count); }
/// <summary> /// 获取最近的Entity /// </summary> /// <param name="queryEntityId"></param> /// <param name="queryEntityPos"></param> /// <param name="range"></param> /// <param name="selfLayer"></param> /// <returns></returns> public GameObject GetNearestEnemy(int queryEntityId, Vector3 queryEntityPos, float range, int selfLayer) { int nearestEnemyId = queryEntityId; List <RBTree <int, Vector3> > nearestEntities = BattleFieldAOI.Instance.GetNearEntities(queryEntityId, queryEntityPos, range); GameObject go; float sqrMagnitude = Mathf.Infinity; float tmpSqrMagnitude = 0; foreach (RBTree <int, Vector3> entities in nearestEntities) { for (RBTree <int, Vector3> .RBNode node = entities.First; entities.HasNext(node); node = entities.MoveNext(node)) { go = GetItem(node.key); if (go != null && go.layer != selfLayer) { tmpSqrMagnitude = Vector3.SqrMagnitude(go.transform.position - queryEntityPos); if (tmpSqrMagnitude < sqrMagnitude) { sqrMagnitude = tmpSqrMagnitude; nearestEnemyId = node.key; } } } } return((nearestEnemyId == queryEntityId || range * range < sqrMagnitude) ? null : GetItem(nearestEnemyId)); }
/// <summary> /// 获取范围内的单位数 /// </summary> /// <param name="queryEntityId">查询者的id</param> /// <param name="queryEntityPos">查询的地点</param> /// <param name="range">查询范围</param> /// <param name="selfLayer">自己的layer</param> /// <param name="isFriend">是否为友方</param> /// <returns></returns> public int GetPartEntityCountInRange(int queryEntityId, Vector3 queryEntityPos, float range, int selfLayer, bool isFriend) { int entityCount = 0; List <RBTree <int, Vector3> > nearestEntities = BattleFieldAOI.Instance.GetNearEntities(queryEntityId, queryEntityPos, range); GameObject go; foreach (RBTree <int, Vector3> entities in nearestEntities) { for (RBTree <int, Vector3> .RBNode node = entities.First; entities.HasNext(node); node = entities.MoveNext(node)) { go = GetItem(node.key); if (go != null) { if (isFriend && (go.layer == selfLayer)) { entityCount++; } else if ((!isFriend) && (go.layer != selfLayer)) { entityCount++; } } } } return(entityCount); }
/// <summary> /// 获取最近的敌方英雄数量 /// </summary> /// <param name="queryEntityId"></param> /// <param name="queryEntityPos"></param> /// <param name="range"></param> /// <param name="selfLayer"></param> /// <returns></returns> public int GetPartHeroCountInRange(int queryEntityId, Vector3 queryEntityPos, float range, int selfLayer, bool isFriend) { int heroCount = 0; List <RBTree <int, Vector3> > nearestEntities = BattleFieldAOI.Instance.GetNearEntities(queryEntityId, queryEntityPos, range); GameObject go; FightDriver fightDriver; foreach (RBTree <int, Vector3> entities in nearestEntities) { for (RBTree <int, Vector3> .RBNode node = entities.First; entities.HasNext(node); node = entities.MoveNext(node)) { go = GetItem(node.key); if (go != null && ((fightDriver = go.GetComponent <FightDriver>()) != null) && fightDriver.GetModelType() == ModelType.Hero) { if (isFriend && go.layer == selfLayer) { heroCount++; } else if ((!isFriend && go.layer != selfLayer)) { heroCount++; } } } } return(heroCount); }
public static bool VolateBlackCountRule <T>(RBTree <T> tree) where T : IComparable <T> { var tmpArray = new List <RBTree <T> .RBTreeNode <T> >(100); bool sameCount = true; List <int> tmpList = new List <int>(); var sb = new StringBuilder(); sb.Append("Start::"); LeafToRoot(tree.root, tmpArray, 0, (tmpArra) => { var tmpBCount = 0; sb.Append("Begin:"); for (int i = 0; i < tmpArra.Count; i++) { sb.Append(tmpArra[i] + ","); if (tmpArra[i].color == NodeColor.Black) { tmpBCount = tmpBCount + 1; } } sb.Append("END:\n"); if (tmpList.Count > 0 && tmpList[tmpList.Count - 1] != tmpBCount) { sameCount = false; } tmpList.Add(tmpBCount); }); sb.Append("Content:\n"); sb.AppendLine(string.Join(",", tmpList.ToArray())); sb.AppendLine("END::"); Console.WriteLine(sb.ToString()); return(!sameCount); }
internal Enumerator(RBTree <T> t, RBTreeNode <T> cur) { tree = t; started = (cur == tree.root ? false : true); isValid = true; current = cur; }
private int[] GetLinearFilteredRecords(Range range) { if (_linearExpression == null) { int[] resultRecords = new int[range.Count]; RBTree <int> .RBTreeEnumerator iterator = _index.GetEnumerator(range.Min); for (int i = 0; i < range.Count && iterator.MoveNext(); i++) { resultRecords[i] = iterator.Current; } return(resultRecords); } else { List <int> matchingRecords = new List <int>(); RBTree <int> .RBTreeEnumerator iterator = _index.GetEnumerator(range.Min); for (int i = 0; i < range.Count && iterator.MoveNext(); i++) { if (AcceptRecord(iterator.Current)) { matchingRecords.Add(iterator.Current); } } return(matchingRecords.ToArray()); } }
public void AddNode_InEmptyTree_ShouldBe_Root() { RBTree <int> tree = new RBTree <int>(); tree.Add(10); Assert.True(tree.Root.Key == 10); }
private void Initialize(double delta, int K) { n = 0; this.delta = delta; this.K = K; C = new RBTree<double, Centroid>(); }
public void TestSimpleInsert() { RBTree <int> tree = new RBTree <int>(); tree.Insert(5); Assert.AreEqual(5, tree.Root.Data); VerifyTree(tree); }
public void InsertKey_InsertSameKey_ThrowsException() { RBTree <int> tree = new RBTree <int>(intComparison); int key = 0; tree.Insert(key); tree.Insert(key); }
private void VerifyTree(RBTree <int> tree) { if (tree.Root.Color == Color.Red) { throw new Exception("Root must be black"); } CheckNode(tree, tree.Root); }
public void SetUp() { twoElementTree = new RBTree<int> (); firstElement = 1; secondElement = 2; absentElement = 3; twoElementTree.Add (firstElement); twoElementTree.Add (secondElement); }
public void EmptyTree_ContainsKey_ReturnsFalse() { RBTree <int> tree = new RBTree <int>(intComparison); int notInserted = 5; bool containsResult = tree.Contains(notInserted); Assert.IsFalse(containsResult); }
private static int InOrderAdd(RBTree.Node node, RBTree.Node[] array, int index) { if (node == null) return index; index = InOrderAdd(node.left, array, index); array[index++] = node; return InOrderAdd(node.right, array, index); }
private static void VerifyProperties(RBTree t) { VerifyProperty1(t.Root); VerifyProperty2(t.Root); // Property 3 is implicit VerifyProperty4(t.Root); VerifyProperty5(t.Root); }
void AssertRBTreeProperties <T>(RBTree <T> tree) where T : IComparable { AssertTreeProperties <T> (tree); AssertRootIsBlack(tree); AssertAllLeavesBlack(tree); AssertRedNodesHaveBlackChildren(tree); EveryDescendantPathHasTheSameNumberOfBlackNodes(tree); }
public void AddNode_WithKey_SmallerThan_RootKey_ShouldBe_OnLeft() { RBTree <int> tree = new RBTree <int>(); tree.Add(10); tree.Add(9); Assert.True(tree.Root.Key == 10); Assert.True(tree.Root.Left.Key == 9); }
public void Indexer_GetValueTest() { IRBTree <int, string> target = new RBTree <int, string>(); target.Add(23, "Hello"); target.Add(33, "Good morning"); Assert.AreEqual("Hello", target[23]); }
public void CreateRBTree(int n = 10) { var tree = new RBTree <int>(1); for (int i = 1; i < n; i++) { tree.AddNewKey(i, i); } System.Console.ReadLine(); }
public void RemoveNode_CheckFullRemove() { ITree tree = new RBTree(); for (int i = 0; i < Nodes.Length; i++) { tree.RemoveNode(Nodes[i]); } Assert.AreEqual(null, tree.Root); }
public void RedBlackTree_Should_BeBalanced() { RBTree <int> tree = new RBTree <int>(); for (int i = 0; i < 10; i++) { tree.Add(i); } Assert.True(tree.IsBalanced()); }
public static void CopySorted(this RBTree tree, RBTree.Node[] array, int offset) { InOrderAdd(tree.root, array, offset); }
public void Reset() { if (!this.beachLine) { this.beachLine = new RBTree<BeachSection>(); } // Move leftover beachsections to the beachsection junkyard. if (this.beachLine.Root) { BeachSection beachSection = this.beachLine.GetFirst(this.beachLine.Root); while (beachSection) { this.beachSectionJunkyard.Add(beachSection); beachSection = beachSection.Next; } } this.beachLine.Root = null; if (!this.circleEvents) { this.circleEvents = new RBTree<CircleEvent>(); } this.circleEvents.Root = this.firstCircleEvent = null; this.edges = new List<Edge>(); this.cells = new List<Cell>(); }
/// <summary> /// Dijkstra in graph gr, from source to destination, using vehicle v. /// </summary> /// <param name="source"> the startpoint </param> /// <param name="destination"> the destination </param> /// <param name="v"> vehicle that is used </param> /// <returns></returns> public Route Dijkstra(long from, long to, Vehicle[] vehicles, RouteMode mode, bool useBus) { Route result = null; if (from == 0 || to == 0 || graph == null) return result; Node source = graph.GetNode(from); Node destination = graph.GetNode(to); //all nodes that are completely solved SortedList<long, Node> solved = new SortedList<long, Node>(); //nodes that are encountered but not completely solved SortedList<double, Node> unsolved = new SortedList<double, Node>(); RBTree<Node> prevs = new RBTree<Node>(); RBTree<double> times = new RBTree<double>(); RBTree<double> distances = new RBTree<double>(); RBTree<Vehicle> vehicleUse = new RBTree<Vehicle>(); ListTree<Vehicle> forbiddenVehicles = new ListTree<Vehicle>(); List<Edge> abstractBusses = graph.GetAbstractBusEdges(); Node current = source; bool found = false; //if there's no new current node it means the algorithm should stop while (current != null) { //if we encounter the destination it means we found the shortest route so we break if (current == destination) { found = true; break; } List<Edge> edges = new List<Edge>(graph.GetEdgesFromNode(current.ID)); foreach (Edge busEdge in abstractBusses) { if (busEdge.End == current.ID || busEdge.Start == current.ID) edges.Add(busEdge); } Node prev = prevs.Get(current.ID); if (prev != null) { foreach (Vehicle vehicle in forbiddenVehicles.Get(prev.ID)) { forbiddenVehicles.Insert(current.ID, vehicle); } } foreach (Edge e in edges) { if (IsAllowed(e, vehicles, useBus)) { Node start = graph.GetNode(e.Start); Node end = graph.GetNode(e.End); double distance = double.PositiveInfinity; double time = double.PositiveInfinity; Vehicle v = vehicles[0]; if (e.Type != CurveType.AbstractBusRoute) { if (e.Type != CurveType.Bus) { double speed = 0; foreach (Vehicle vehicle in vehicles) { if (!forbiddenVehicles.Get(current.ID).Contains(vehicle)) { double vSpeed = GetSpeed(vehicle, e); if (vSpeed > speed && IsAllowed(e, vehicle, useBus)) { speed = vSpeed; v = vehicle; } } } distance = NodeCalcExtensions.Distance(start, end); time = distance / speed; } else if (e.Route != null) { // Take busroute if better if (mode == RouteMode.Fastest && time > e.Route.Time || mode == RouteMode.Shortest && distance > e.Route.Length) { distance = e.Route.Length; time = e.Route.Time; v = Vehicle.Foot; foreach (Vehicle vehicle in vehicles) if (vehicle != Vehicle.Foot) forbiddenVehicles.Insert(current.ID, vehicle); } } } else { Node n1 = null, n2 = null; if (start.Longitude != 0 || start.Latitude != 0) n1 = graph.GetNodeByPos(start.Longitude, start.Latitude, Vehicle.Bus); if (end.Longitude != 0 || end.Latitude != 0) n2 = graph.GetNodeByPos(end.Longitude, end.Latitude, Vehicle.Bus); if (n1 != default(Node) && n2 != default(Node)) { Curve curve = new Curve(new long[] { start.ID, end.ID }, e.Name); Route r = this.Dijkstra(n1.ID, n2.ID, new Vehicle[] { Vehicle.Bus }, RouteMode.Fastest, false); r = new Route(new Node[] { start }, Vehicle.Bus) + r + new Route(new Node[] { end }, Vehicle.Bus); curve.Type = CurveType.Bus; curve.Route = r; // We calculate with 60 seconds of switch over to the bus. r.Time += 60; graph.AddWay(start.ID, curve); graph.AddWay(end.ID, curve); e.Route = r; e.Type = CurveType.Bus; // Take busroute if better if (mode == RouteMode.Fastest && time > e.Route.Time || mode == RouteMode.Shortest && distance > e.Route.Length) { distance = e.Route.Length; time = e.Route.Time; v = Vehicle.Foot; foreach (Vehicle vehicle in vehicles) if (vehicle != Vehicle.Foot) forbiddenVehicles.Insert(current.ID, vehicle); } } graph.RemoveAbstractBus(e); abstractBusses.Remove(e); } time += times.Get(current.ID); double trueDist = distances.Get(current.ID) + distance; if (!solved.ContainsValue(end) && current != end) { if (end.Latitude != 0 && end.Longitude != 0) { if (times.Get(end.ID) == 0 || distances.Get(end.ID) == 0) { times.Insert(end.ID, double.PositiveInfinity); distances.Insert(end.ID, double.PositiveInfinity); vehicleUse.Insert(end.ID, v); } if ((mode == RouteMode.Fastest && times.Get(end.ID) > time) || (mode == RouteMode.Shortest && distances.Get(end.ID) > trueDist)) { times.GetNode(end.ID).Content = time; distances.GetNode(end.ID).Content = trueDist; vehicleUse.GetNode(end.ID).Content = v; if (prevs.GetNode(end.ID).Content == null) prevs.Insert(end.ID, current); else prevs.GetNode(end.ID).Content = current; if (!unsolved.ContainsValue(end)) { if (mode == RouteMode.Fastest) { // Very bad solution but I couldn't think of a simple better one. while (unsolved.ContainsKey(times.Get(end.ID))) { times.GetNode(end.ID).Content += 0.0000000001; } unsolved.Add(times.Get(end.ID), end); } else if (mode == RouteMode.Shortest) { // Very bad solution but I couldn't think of a simple better one. while (unsolved.ContainsKey(distances.Get(end.ID))) { distances.GetNode(end.ID).Content += 0.0000000001; } unsolved.Add(distances.Get(end.ID), end); } if (prevs.GetNode(current.ID).Content != null && vehicleUse.Get(prevs.GetNode(current.ID).Content.ID) == Vehicle.Car) { if (v == Vehicle.Foot) { forbiddenVehicles.Insert(end.ID, Vehicle.Car); } } } } } } else if (!solved.ContainsValue(start) && current != start) { if (start.Latitude != 0 && start.Longitude != 0) { if (times.Get(start.ID) == 0 || distances.Get(start.ID) == 0) { times.Insert(start.ID, double.PositiveInfinity); distances.Insert(start.ID, double.PositiveInfinity); vehicleUse.Insert(start.ID, v); } if ((mode == RouteMode.Fastest && times.Get(start.ID) > time) || (mode == RouteMode.Shortest && distances.Get(start.ID) > trueDist)) { times.GetNode(start.ID).Content = time; distances.GetNode(start.ID).Content = trueDist; vehicleUse.GetNode(start.ID).Content = v; if (prevs.GetNode(start.ID).Content == null) prevs.Insert(start.ID, current); else prevs.GetNode(start.ID).Content = current; if (!unsolved.ContainsValue(start)) { if (mode == RouteMode.Fastest) { // Very bad solution but I couldn't think of a simple better one. while (unsolved.ContainsKey(times.Get(start.ID))) { times.GetNode(start.ID).Content += 0.0000000001; } unsolved.Add(times.Get(start.ID), start); } else if (mode == RouteMode.Shortest) { // Very bad solution but I couldn't think of a simple better one. while (unsolved.ContainsKey(distances.Get(start.ID))) { distances.GetNode(start.ID).Content += 0.0000000001; } unsolved.Add(distances.Get(start.ID), start); } } if (prevs.GetNode(current.ID).Content != null && vehicleUse.Get(prevs.GetNode(current.ID).Content.ID) == Vehicle.Car) { if (v == Vehicle.Foot) { forbiddenVehicles.Insert(start.ID, Vehicle.Car); } } } } } } } //dit zou niet voor moeten komen maar toch gebeurt het... if (!solved.ContainsKey(current.ID)) solved.Add(current.ID, current); if (unsolved.Count > 0) { current = unsolved.Values[0]; unsolved.RemoveAt(0); } else { current = null; } } if (found) { List<Node> nodes = new List<Node>(); List<long> extras = graph.GetExtras(); Node n = destination; List<long> busStartStop = new List<long>(); do { bool foundRoute = false; if (extras.Contains(n.ID)) { // Change straigt buslines in for the actual route. foreach (Edge e in graph.GetEdgesFromNode(n.ID)) { if (prevs.Get(n.ID) != null && e.Route != null) { if (n.ID == e.Start && prevs.Get(n.ID).ID == e.End) { Node[] busNodes = e.Route.Points; if (busNodes[0].ID == e.Start) Array.Reverse(busNodes); busStartStop.Add(busNodes[busNodes.Length - 1].ID); busStartStop.Add(busNodes[0].ID); nodes.InsertRange(0, busNodes); n = prevs.Get(n.ID); foundRoute = true; break; } else if (n.ID == e.End && prevs.Get(n.ID).ID == e.Start) { Node[] busNodes = e.Route.Points; if (busNodes[0].ID == e.End) Array.Reverse(busNodes); busStartStop.Add(busNodes[busNodes.Length - 1].ID); busStartStop.Add(busNodes[0].ID); nodes.InsertRange(0, busNodes); n = prevs.Get(n.ID); foundRoute = true; break; } } } } if (!foundRoute) { nodes.Insert(0, n); n = prevs.Get(n.ID); } } while (n != null); result = new Route(nodes.ToArray(), vehicles[0]); result.Time = times.Get(destination.ID); result.Length = distances.Get(destination.ID); // Set bus as vehicle if (busStartStop.Count > 0) { int i = busStartStop.Count - 1; Node[] routePoints = result.Points; for (int j = 0; j < routePoints.Length; j++) { if (routePoints[j].ID == busStartStop[i]) { if (i % 2 == 1) { result.SetVehicle(Vehicle.Bus, j); i--; } else { result.SetVehicle(Vehicle.Foot, j); i--; } } if (i < 0) break; } } Vehicle cur = result.GetVehicle(0), prev; for (int i = 2; i < result.NumOfNodes; i++) { if (result.GetVehicle(i) != Vehicle.Bus) { prev = vehicleUse.Get(result[i].ID); if (prev != cur && i > 1) result.SetVehicle(prev, i - 1); cur = prev; } } } else { result = new Route(new Node[] { source }, vehicles[0]); result.Time = double.PositiveInfinity; result.Length = double.PositiveInfinity; } return result; }