public void TestRotateRightRecolor() { RBTree <int> tree = new RBTree <int>(); tree.Insert(10); tree.Insert(5); tree.Insert(15); tree.Insert(3); tree.Insert(2); tree.Insert(1); VerifyTree(tree); }
public void TestRemoveBlackSiblingRedBlackNephewMirror() { //case 3-2, 4-2 RBTree <int> tree = new RBTree <int>(); tree.Insert(10); tree.Insert(5); tree.Insert(15); tree.Insert(3); tree.Insert(8); tree.Insert(12); tree.Insert(18); tree.Insert(9); tree.Insert(13); tree.Remove(18); int result = 0; if (tree.Find(18, out result)) { Assert.Fail(); } VerifyTree(tree); }
public void TestRemoveBlackSiblingNephewRed() { //case 1 RBTree <int> tree = new RBTree <int>(); tree.Insert(11); tree.Insert(2); tree.Insert(14); tree.Insert(1); tree.Insert(7); tree.Insert(15); tree.Insert(5); tree.Insert(8); tree.Insert(4); tree.Remove(2); int result = 0; if (tree.Find(2, out result)) { Assert.Fail(); } VerifyTree(tree); }
public void Insert100Keys_ContainsKeys_ReturnTrue() { RBTree <int> tree = new RBTree <int>(intComparison); int size = 100; int[] keys = new int[size]; for (int i = 0; i < size; i++) { keys[i] = i; tree.Insert(keys[i]); } bool containsAll = true; for (int i = 0; i < size; i++) { containsAll = containsAll && tree.Contains(keys[i]); } }
public void Test_RBTREE_DELETE() { RBTree rbTree = new RBTree(); System.Collections.Generic.IList <IDirectoryEntry> repo = GetDirectoryRepository(25); foreach (var item in repo) { rbTree.Insert(item); } try { IRBNode n; rbTree.Delete(DirectoryEntry.Mock("5", StgType.StgInvalid), out n); rbTree.Delete(DirectoryEntry.Mock("24", StgType.StgInvalid), out n); rbTree.Delete(DirectoryEntry.Mock("7", StgType.StgInvalid), out n); } catch (Exception ex) { Assert.Fail("Item removal failed: " + ex.Message); } // CFItem c; // bool s = rbTree.TryLookup(new CFMock("7", StgType.StgStream), out c); // Assert.IsFalse(s); // c = null; // Assert.IsTrue(rbTree.TryLookup(new CFMock("6", StgType.StgStream), out c)); // Assert.IsTrue(c.IsStream); // Assert.IsTrue(rbTree.TryLookup(new CFMock("12", StgType.StgStream), out c)); // Assert.IsTrue(c.Name == "12"); //} }
private IEnumerator Start() { RBTree <int> rbMap = new RBTree <int>(); int[] nums = new int[100000]; for (int i = 0; i < nums.Length; ++i) { nums[i] = i; } int counter = 0; while (true) { ++counter; //追加 var sw = System.Diagnostics.Stopwatch.StartNew(); for (int i = 0; i < nums.Length; ++i) { rbMap.Insert(nums[i]); } sw.Stop(); Debug.Log("Insert(" + nums.Length + ") is " + sw.ElapsedMilliseconds + " : Avg(" + counter + ") is " + (insertSum += sw.ElapsedMilliseconds) / counter); sw.Reset(); //確認 //Debug.Log(rbMap); //削除 sw.Start(); for (int i = 0; i < nums.Length; ++i) { rbMap.Delete(nums[i]); } sw.Stop(); Debug.Log("Delete(" + nums.Length + ") is " + sw.ElapsedMilliseconds + " : Avg(" + counter + ") is " + (deleteSum += sw.ElapsedMilliseconds) / counter); Debug.Log(rbMap); yield return(new WaitForSeconds(0.4f)); } }
public void Test_RBTREE_INSERT() { RBTree rbTree = new RBTree(); System.Collections.Generic.IList <IDirectoryEntry> repo = GetDirectoryRepository(25); foreach (var item in repo) { rbTree.Insert(item); } for (int i = 0; i < repo.Count; i++) { IRBNode c; rbTree.TryLookup(DirectoryEntry.Mock(i.ToString(), StgType.StgInvalid), out c); Assert.IsTrue(c is IDirectoryEntry); Assert.IsTrue(((IDirectoryEntry)c).Name == i.ToString()); //Assert.IsTrue(c.IsStream); } }
private string InsertTimeTest() { sw = Stopwatch.StartNew(); for (int i = 1; i <= count; i++) { tree.Insert(i); //DisplayTree(); } /*tree.Insert(13); * DisplayTree(); * tree.Insert(8); * DisplayTree(); * tree.Insert(1); * DisplayTree(); * tree.Insert(6); * tree.Insert(11); * tree.Insert(17); * tree.Insert(15); * tree.Insert(25); * tree.Insert(22); * tree.Insert(27);*/ long elapsed = sw.ElapsedMilliseconds; var isPassed = true; var treeList = tree.GetTree(); for (int i = 1; i <= count; i++) { if (treeList.Contains(i)) { continue; } isPassed = false; break; } var tmp = isPassed ? "passed in " : "failer in "; return(tmp + elapsed + "ms"); }
public void Add(TKey key, TValue value) { try { var tmp = array.Find(new Node() { Key = key, Value = value }); if (tmp == null) { array.Insert(new Node { Key = key, Value = value }); } else { throw new ArgumentException(); } } catch (ArgumentException ex) { Console.WriteLine("Exception: Key " + key + " is already exist: Add(" + key + "," + value + ")"); } }
public void TestRemoveBlackSiblingRedMirror() { //case 1-2,2-2 RBTree <int> tree = new RBTree <int>(); tree.Insert(5); tree.Insert(3); tree.Insert(8); tree.Insert(2); tree.Insert(4); tree.Insert(1); tree.Remove(8); int result = 0; if (tree.Find(8, out result)) { Assert.Fail(); } VerifyTree(tree); }
public void TestRemoveBlackSiblingRed() { //case 1,2 RBTree <int> tree = new RBTree <int>(); tree.Insert(5); tree.Insert(3); tree.Insert(10); tree.Insert(7); tree.Insert(13); tree.Insert(14); tree.Remove(3); int result = 0; if (tree.Find(3, out result)) { Assert.Fail(); } VerifyTree(tree); }
/// <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; }