private static async Task <HashSet <string> > GetWebsiteLocalAssembliesAsync(EnvDTE.Project envDTEProject) { await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); var assemblies = new HashSet <string>(PathComparer.Default); var references = GetAssemblyReferences(envDTEProject); foreach (AssemblyReference reference in references) { // For websites only include bin assemblies if (reference.ReferencedProject == null && reference.ReferenceKind == AssemblyReferenceType.AssemblyReferenceBin && File.Exists(reference.FullPath)) { assemblies.Add(reference.FullPath); } } // For website projects, we always add .refresh files that point to the corresponding binaries in packages. In the event of bin deployed assemblies that are also GACed, // the ReferenceKind is not AssemblyReferenceBin. Consequently, we work around this by looking for any additional assembly declarations specified via .refresh files. var envDTEProjectPath = await envDTEProject.GetFullPathAsync(); CollectionsUtility.AddRange(assemblies, RefreshFileUtility.ResolveRefreshPaths(envDTEProjectPath)); return(assemblies); }
internal static HashSet <string> GetAssemblyClosure(EnvDTE.Project envDTEProject, IDictionary <string, HashSet <string> > visitedProjects) { ThreadHelper.ThrowIfNotOnUIThread(); HashSet <string> assemblies; if (visitedProjects.TryGetValue(envDTEProject.UniqueName, out assemblies)) { return(assemblies); } assemblies = new HashSet <string>(PathComparer.Default); visitedProjects.Add(envDTEProject.UniqueName, assemblies); var localProjectAssemblies = GetLocalProjectAssemblies(envDTEProject); CollectionsUtility.AddRange(assemblies, localProjectAssemblies); var referencedProjects = GetReferencedProjects(envDTEProject); foreach (var project in referencedProjects) { var assemblyClosure = GetAssemblyClosure(project, visitedProjects); CollectionsUtility.AddRange(assemblies, assemblyClosure); } return(assemblies); }
internal static async Task <HashSet <string> > GetAssemblyClosureAsync(EnvDTE.Project envDTEProject, IDictionary <string, HashSet <string> > visitedProjects) { await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); HashSet <string> assemblies; if (visitedProjects.TryGetValue(envDTEProject.UniqueName, out assemblies)) { return(assemblies); } assemblies = new HashSet <string>(PathComparer.Default); visitedProjects.Add(envDTEProject.UniqueName, assemblies); var localProjectAssemblies = await GetLocalProjectAssembliesAsync(envDTEProject); CollectionsUtility.AddRange(assemblies, localProjectAssemblies); var referencedProjects = GetReferencedProjects(envDTEProject); foreach (var project in referencedProjects) { var assemblyClosure = await GetAssemblyClosureAsync(project, visitedProjects); CollectionsUtility.AddRange(assemblies, assemblyClosure); } return(assemblies); }
public void SequenceEqualColl() { Assert.IsTrue(CollectionsUtility.SequenceEqual( (ICollection <String>)null, (ICollection <String>)null)); Assert.IsFalse(CollectionsUtility.SequenceEqual( (ICollection <String>) new List <String>(), (ICollection <String>)null)); Assert.IsFalse(CollectionsUtility.SequenceEqual( (ICollection <String>)null, (ICollection <String>) new List <String>())); Assert.IsTrue(CollectionsUtility.SequenceEqual( (ICollection <String>) new List <String>(), (ICollection <String>) new List <String>())); Assert.IsTrue(CollectionsUtility.SequenceEqual( (ICollection <String>) new List <String>() { "One", "Two" }, (ICollection <String>) new List <String>() { "One", "Two" })); Assert.IsFalse(CollectionsUtility.SequenceEqual( (ICollection <String>) new List <String>() { "One", "Two" }, (ICollection <String>) new List <String>() { "Two", "One" })); }
public void SequenceEqualUntypedColl() { Assert.IsTrue(CollectionsUtility.SequenceEqualUntyped( (ICollection)null, (ICollection)null)); Assert.IsFalse(CollectionsUtility.SequenceEqualUntyped( (ICollection) new ArrayList(), (ICollection)null)); Assert.IsFalse(CollectionsUtility.SequenceEqualUntyped( (ICollection)null, (ICollection) new ArrayList())); Assert.IsTrue(CollectionsUtility.SequenceEqualUntyped( (ICollection) new ArrayList(), (ICollection) new ArrayList())); Assert.IsTrue(CollectionsUtility.SequenceEqualUntyped( (ICollection) new ArrayList() { "One", "Two" }, (ICollection) new ArrayList() { "One", "Two" })); Assert.IsFalse(CollectionsUtility.SequenceEqualUntyped( (ICollection) new ArrayList() { "One", "Two" }, (ICollection) new ArrayList() { "Two", "One" })); }
/// <summary> /// Handles the <see cref="ButtonBase.Click"/> event for the "Move Entity Up" <see /// cref="Button"/> on any tab page.</summary> /// <param name="sender"> /// The <see cref="Object"/> where the event handler is attached.</param> /// <param name="args"> /// A <see cref="RoutedEventArgs"/> object containing event data.</param> /// <remarks> /// <b>OnEntityDown</b> swaps the first selected item in the "Entity Stack" list view with /// its upper neighbor and sets the <see cref="DataChanged"/> flag.</remarks> private void OnEntityUp(object sender, RoutedEventArgs args) { args.Handled = true; ListView stackList; Action onDataChanged; if (args.Source == MoveTerrainUpButton) { stackList = TerrainList; onDataChanged = OnTerrainTabChanged; } else { stackList = EntityList; onDataChanged = OnOtherTabChanged; } // retrieve first selected item, if any if (stackList.SelectedItems.Count == 0 || stackList.Items.Count < 2) { return; } // move item up and re-select it object item = stackList.SelectedItem; int index = CollectionsUtility.MoveItemUntyped(stackList.Items, item, -1); stackList.SelectAndShow(Math.Max(0, index)); onDataChanged(); }
public void IndexArray() { for (int count = 0; count < 10; count++) { int[] list = CollectionsUtility.IndexArray(count); for (int i = 0; i < list.Length; i++) { Assert.AreEqual(i, list[i]); } } }
internal static HashSet <string> GetAssemblyClosure(EnvDTEProject envDTEProject, IDictionary <string, HashSet <string> > visitedProjects) { HashSet <string> assemblies; if (visitedProjects.TryGetValue(envDTEProject.UniqueName, out assemblies)) { return(assemblies); } assemblies = new HashSet <string>(PathComparer.Default); CollectionsUtility.AddRange(assemblies, GetLocalProjectAssemblies(envDTEProject)); CollectionsUtility.AddRange(assemblies, GetReferencedProjects(envDTEProject).SelectMany(p => GetAssemblyClosure(p, visitedProjects))); visitedProjects.Add(envDTEProject.UniqueName, assemblies); return(assemblies); }
/// <summary> /// Handles the <see cref="ButtonBase.Click"/> event for the "Move Frame Right" <see /// cref="Button"/>.</summary> /// <param name="sender"> /// The <see cref="Object"/> where the event handler is attached.</param> /// <param name="args"> /// A <see cref="RoutedEventArgs"/> object containing event data.</param> /// <remarks> /// <b>OnFrameRight</b> swaps the selected item in the "Image Frames" list box with its /// right neighbor and sets the <see cref="DataChanged"/> flag.</remarks> private void OnFrameRight(object sender, RoutedEventArgs args) { args.Handled = true; // retrieve selected item object item = FrameList.SelectedItem; if (FrameList.Items.Count < 2 || item == null) { return; } // move item right and re-select it int index = CollectionsUtility.MoveItemUntyped(FrameList.Items, item, +1); FrameList.SelectAndShow(index); // frame list has changed DataChanged = true; }
public void Randomize() { for (int count = 0; count < 10; count++) { int[] list = CollectionsUtility.IndexArray(count); CollectionsUtility.Randomize(list); // check that all elements still occur exactly once bool[] found = new bool[count]; for (int i = 0; i < list.Length; i++) { Assert.IsFalse(found[list[i]]); found[list[i]] = true; } for (int i = 0; i < list.Length; i++) { Assert.IsTrue(found[list[i]]); } } }
/// <summary> /// Creates a random <see cref="Subdivision"/> with the specified number of full edges and /// comparison epsilon.</summary> /// <param name="size"> /// The number of full edges, i.e. half the number of <see cref="Subdivision.Edges"/>, in /// the returned <see cref="Subdivision"/>.</param> /// <param name="epsilon"> /// The maximum absolute difference at which two coordinates should be considered equal. /// </param> /// <returns> /// A new random <see cref="Subdivision"/> with the specified <paramref name="size"/> and /// <paramref name="epsilon"/>.</returns> private static Subdivision CreateSubdivision(int size, double epsilon) { LineD[] lines = new LineD[size]; for (int i = 0; i < size; i++) { lines[i] = GeoAlgorithms.RandomLine(0, 0, 1000, 1000); } // split random set into non-intersecting line segments var crossings = MultiLineIntersection.FindSimple(lines, epsilon); var splitLines = MultiLineIntersection.Split(lines, crossings); Array.Copy(splitLines, lines, size); // re-randomize lines to eliminate split ordering CollectionsUtility.Randomize(lines); Subdivision division = Subdivision.FromLines(lines); division.Validate(); return(division); }
private void DictionaryTest(bool random) { Stopwatch timer = new Stopwatch(); var testCases = _dictionaryTestCases; Output(String.Format("{0,8}", " ")); foreach (TestCase test in testCases) { Output(String.Format("{0,14}", test.Name)); } Output("\n"); // count units of size x operation in milliseconds, // rather than individual operations in microseconds const int outerLoop = 100, size = 60000; const int iterations = outerLoop * 1000; long[] addTicks = new long[testCases.Length], iterateTicks = new long[testCases.Length], searchTicks = new long[testCases.Length], removeTicks = new long[testCases.Length]; // generate keys in ascending order var array = new KeyValuePair <Int32, String> [size]; for (int i = 0; i < array.Length; i++) { array[i] = new KeyValuePair <Int32, String>(i, null); } // create random permutation of keys if (random) { CollectionsUtility.Randomize(array); } // trigger JIT compilation foreach (TestCase test in testCases) { test.Dictionary.Clear(); foreach (var pair in array) { test.Dictionary.Add(pair.Key, pair.Value); } } for (int i = 0; i < outerLoop; i++) { for (int j = 0; j < testCases.Length; j++) { TestCase test = testCases[j]; test.Dictionary.Clear(); timer.Restart(); foreach (var pair in array) { test.Dictionary.Add(pair.Key, pair.Value); } timer.Stop(); addTicks[j] += timer.ElapsedTicks; int sum = 0; timer.Restart(); foreach (var pair in test.Dictionary) { unchecked { sum += pair.Key; } } timer.Stop(); iterateTicks[j] += timer.ElapsedTicks; int key = MersenneTwister.Default.Next(size - 1); timer.Restart(); for (int k = 0; k < size; k++) { test.Dictionary.ContainsKey((key + k) % size); } timer.Stop(); searchTicks[j] += timer.ElapsedTicks; timer.Restart(); foreach (var pair in array) { test.Dictionary.Remove(pair.Key); } timer.Stop(); removeTicks[j] += timer.ElapsedTicks; } } Output(String.Format("{0,8}", "Add")); for (int i = 0; i < testCases.Length; i++) { Output(String.Format("{0,14:N2}", AverageMicrosecs(addTicks[i], iterations))); } Output(String.Format("\n{0,8}", "Iterate")); for (int i = 0; i < testCases.Length; i++) { Output(String.Format("{0,14:N2}", AverageMicrosecs(iterateTicks[i], iterations))); } Output(String.Format("\n{0,8}", "Search")); for (int i = 0; i < testCases.Length; i++) { Output(String.Format("{0,14:N2}", AverageMicrosecs(searchTicks[i], iterations))); } Output(String.Format("\n{0,8}", "Remove")); for (int i = 0; i < testCases.Length; i++) { Output(String.Format("{0,14:N2}", AverageMicrosecs(removeTicks[i], iterations))); } Output(String.Format("\n\nTimes are msec averages for {0:N0} integer keys in {1} order.\n", size, (random ? "random" : "ascending"))); }
private void CollectionTest() { Stopwatch timer = new Stopwatch(); var testCases = _collectionTestCases; Output(String.Format("{0,8}", " ")); foreach (TestCase test in testCases) { Output(String.Format("{0,14}", test.Name)); } Output("\n"); // count units of size x operation in milliseconds, // rather than individual operations in microseconds const int outerLoop = 100, size = 200000; const int iterations = outerLoop * 1000; long[] addTicks = new long[testCases.Length], iterateTicks = new long[testCases.Length], searchTicks = new long[testCases.Length], removeTicks = new long[testCases.Length]; // generate random permutation of items int[] array = CollectionsUtility.IndexArray(size); CollectionsUtility.Randomize(array); // trigger JIT compilation foreach (TestCase test in testCases) { test.Collection.Clear(); foreach (int item in array) { test.Collection.Add(item); } } for (int i = 0; i < outerLoop; i++) { for (int j = 0; j < testCases.Length; j++) { TestCase test = testCases[j]; test.Collection.Clear(); timer.Restart(); foreach (int item in array) { test.Collection.Add(item); } timer.Stop(); addTicks[j] += timer.ElapsedTicks; int sum = 0; timer.Restart(); foreach (int item in test.Collection) { unchecked { sum += item; } } timer.Stop(); iterateTicks[j] += timer.ElapsedTicks; int key = MersenneTwister.Default.Next(size - 1); timer.Restart(); for (int k = 0; k < size; k++) { test.Collection.Contains((key + k) % size); } timer.Stop(); searchTicks[j] += timer.ElapsedTicks; timer.Restart(); foreach (int item in array) { test.Collection.Remove(item); } timer.Stop(); removeTicks[j] += timer.ElapsedTicks; } } Output(String.Format("{0,8}", "Add")); for (int i = 0; i < testCases.Length; i++) { Output(String.Format("{0,14:N2}", AverageMicrosecs(addTicks[i], iterations))); } Output(String.Format("\n{0,8}", "Iterate")); for (int i = 0; i < testCases.Length; i++) { Output(String.Format("{0,14:N2}", AverageMicrosecs(iterateTicks[i], iterations))); } Output(String.Format("\n{0,8}", "Search")); for (int i = 0; i < testCases.Length; i++) { Output(String.Format("{0,14:N2}", AverageMicrosecs(searchTicks[i], iterations))); } Output(String.Format("\n{0,8}", "Remove")); for (int i = 0; i < testCases.Length; i++) { Output(String.Format("{0,14:N2}", AverageMicrosecs(removeTicks[i], iterations))); } Output(String.Format( "\n\nTimes are msec averages for {0:N0} integer items in random order.\n", size)); }
protected override void OnMouseDown(MouseButtonEventArgs args) { base.OnMouseDown(args); if (args.Handled || _division == null) { return; } Point cursor = Mouse.GetPosition(OutputBox); if (cursor.X < 0 || cursor.X >= OutputBox.Width || cursor.Y < 0 || cursor.Y >= OutputBox.Height) { return; } if (AddEdgeToggle.IsChecked == true) { if (_selectedVertex >= 0) { PointD oldVertex = _division.Vertices.GetKey(_selectedVertex); _division.AddEdge(oldVertex, cursor.ToPointD()); } } else if (RemoveEdgeToggle.IsChecked == true) { if (_selectedEdge >= 0) { _division.RemoveEdge(_selectedEdge); } } else if (SplitEdgeToggle.IsChecked == true) { if (_selectedEdge >= 0) { _division.SplitEdge(_selectedEdge); } } else if (ConnectVertexToggle.IsChecked == true) { if (_selectedVertex >= 0) { PointD oldVertex = _division.Vertices.GetKey(_selectedVertex); CollectionsUtility.AnyRandom(_division.Vertices.Keys, p => (_division.AddEdge(oldVertex, p) != null)); } } else if (MoveVertexToggle.IsChecked == true) { if (_selectedVertex >= 0) { PointD newVertex = cursor.ToPointD(); _division.MoveVertex(_selectedVertex, newVertex); } } else if (RemoveVertexToggle.IsChecked == true) { if (_selectedVertex >= 0) { _division.RemoveVertex(_selectedVertex); } } else { return; } DrawSubdivision(_division); SetNearestEdge(null, 0); SetSelectedEdge(-1); SetSelectedVertex(-1); }
/// <summary> /// Determines whether this instance and a specified <see cref="MultiLinePoint"/> have the /// same value.</summary> /// <param name="other"> /// A <see cref="MultiLinePoint"/> to compare to this instance.</param> /// <returns> /// <c>true</c> if the value of <paramref name="other"/> is the same as this instance; /// otherwise, <c>false</c>.</returns> /// <remarks> /// <b>Equals</b> compares the values of the <see cref="Shared"/>, <see cref="Lines"/>, and /// <see cref="Locations"/> properties of the two <see cref="MultiLinePoint"/> instances to /// test for value equality.</remarks> public bool Equals(MultiLinePoint other) { return(Shared == other.Shared && CollectionsUtility.SequenceEqual(Lines, other.Lines) && CollectionsUtility.SequenceEqual(Locations, other.Locations)); }
/// <summary> /// Changes or deletes all occurrences of the specified identifier in the specified /// collection or in the entire scenario.</summary> /// <typeparam name="TValue"> /// The type of all values in the specified <paramref name="collection"/>. The type of all /// keys is assumed to be <see cref="String"/>.</typeparam> /// <param name="collection"> /// The <see cref="ICollection{T}"/> whose elements to process. This must be either an <see /// cref="IDictionary{TKey, TValue}"/> or an <see cref="IList{T}"/> holding <see /// cref="KeyValuePair{TKey, TValue}"/> elements.</param> /// <param name="oldId"> /// The identifier to remove from <paramref name="collection"/> or from the current /// scenario.</param> /// <param name="newId"><para> /// The identifier to store with all values of <paramref name="oldId"/> in <paramref /// name="collection"/> or in the current scenario. /// </para><para>-or-</para><para> /// A null reference to delete all elements with <paramref name="oldId"/> from <paramref /// name="collection"/> or from the current scenario.</para></param> /// <returns> /// <c>true</c> if the user confirmed the change; otherwise, <c>false</c>.</returns> /// <exception cref="ArgumentException"> /// <paramref name="collection"/> implements neither <see cref="IDictionary{TKey, TValue}"/> /// nor <see cref="IList{T}"/>.</exception> /// <exception cref="ArgumentNullException"> /// <paramref name="collection"/> is a null reference.</exception> /// <exception cref="ArgumentNullOrEmptyException"> /// <paramref name="oldId"/> is a null reference or an empty string.</exception> /// <remarks><para> /// <b>ProcessAllIdentifiers</b> invokes <see /// cref="MasterSection.ProcessIdentifierBySection"/> to count all occurrences of the /// specified <paramref name="oldId"/> in the current scenario. /// </para><para> /// If any occurrences are found, <b>ProcessAllIdentifiers</b> asks the user if all of them /// should be deleted or changed to the specified <paramref name="newId"/>, or only those in /// the specified <paramref name="collection"/>, or if the entire operation should be /// cancelled. /// </para><para> /// If the user cancels, <b>ProcessAllIdentifiers</b> returns <c>false</c> without changing /// any data. Otherwise, the requested changes are performed using either /// <b>ProcessIdentifierBySection</b> or <see cref="CollectionsUtility.ChangeKey"/>. In the /// first case, the Hexkit Editor tab pages managing the changed scenario sections, if any, /// are also flagged as containing unsaved changes.</para></remarks> public static bool ProcessAllIdentifiers <TValue>( ICollection <KeyValuePair <String, TValue> > collection, string oldId, string newId) { if (collection == null) { ThrowHelper.ThrowArgumentNullException("collection"); } if (String.IsNullOrEmpty(oldId)) { ThrowHelper.ThrowArgumentNullOrEmptyException("oldId"); } // check if required interface is available IDictionary <String, TValue> dictionary = collection as IDictionary <String, TValue>; IList <KeyValuePair <String, TValue> > list = collection as IList <KeyValuePair <String, TValue> >; if (dictionary == null && list == null) { ThrowHelper.ThrowArgumentException("collection", Tektosyne.Strings.ArgumentNotInTypes + "IDictionary, IList"); } // count all occurrences of old key MasterSection scenario = MasterSection.Instance; int[] found = scenario.ProcessIdentifierBySection(oldId, oldId); int totalFound = found[(int)ScenarioSection.Master]; // ask to change those occurrences, if any MessageBoxResult result = MessageBoxResult.No; if (totalFound > 1) { string dialogText = (newId == null ? Global.Strings.DialogIdentifierDelete : Global.Strings.DialogIdentifierChange); result = MessageBox.Show(MainWindow.Instance, String.Format(ApplicationInfo.Culture, dialogText, totalFound - 1), Global.Strings.TitleIdentifierReferenced, MessageBoxButton.YesNoCancel, MessageBoxImage.Question); // process identifiers throughout scenario if (result == MessageBoxResult.Yes) { scenario.ProcessIdentifierBySection(oldId, newId); for (int i = 0; i < found.Length; i++) { if (found[i] == 0) { continue; } ScenarioSection section = (ScenarioSection)i; MainWindow.Instance.GetTabPage(section).DataChanged = true; } } } // process identifiers in specified collection only if (result == MessageBoxResult.No) { if (dictionary != null) { CollectionsUtility.ProcessKey(dictionary, oldId, newId); } else { CollectionsUtility.ProcessKey(list, oldId, newId); } } // allow user to abort if a dialog came up return(result != MessageBoxResult.Cancel); }