public void TestFindModificationByPrimaryIndex() { TextFile primary = new TextFile(); primary.AddLines(new[] { "a", "b", "c", "d", "e", "f" }); TextFile secondary = new TextFile(); secondary.AddLines(new[] { "1", "2", "3", "4", "5", "6" }); ModificationCollection modifications = new ModificationCollection(primary, secondary); modifications.AddNoChanged(0, 2); modifications.AddReplaced(2, 0, 2); modifications.AddNoChanged(4, 2); Modification wanted = modifications.FindModificationByPrimaryIndex(1); Assert.AreEqual(wanted.Index, 0); wanted = modifications.FindModificationByPrimaryIndex(3); Assert.AreEqual(wanted.Index, 1); wanted = modifications.FindModificationByPrimaryIndex(4); Assert.AreEqual(wanted.Index, 2); wanted = modifications.FindModificationByPrimaryIndex(5); Assert.AreEqual(wanted.Index, 2); }
public void TestStartIndex() { Modification modification = Modification.CreateNoChanged(new List<string> {"aaa", "bbb", "ccc"}); Assert.AreEqual(modification.Primary.StartIndex, 0); Assert.AreEqual(modification.Secondary.StartIndex, 0); ModificationCollection modifications = new ModificationCollection(); modifications.Add(modification); modification = Modification.CreateAdded(new List<string> { "ddd", "eee", "fff" }); modifications.Add(modification); Assert.AreEqual(modification.Primary.StartIndex, 3); Assert.AreEqual(modification.Primary.Length, 0); Assert.AreEqual(modification.Secondary.StartIndex, 3); Assert.AreEqual(modification.Secondary.Length, 3); modification = Modification.CreateAdded(new List<string> { "ggg", "hhh", "iii" }); modifications.Add(modification); Assert.AreEqual(modification.Primary.StartIndex, 3); Assert.AreEqual(modification.Primary.Length, 0); Assert.AreEqual(modification.Secondary.StartIndex, 6); Assert.AreEqual(modification.Secondary.Length, 3); }
public void ModificationCollectionScrambledEquals() { ModificationCollection a = new ModificationCollection(new OldSchoolModification(1, "Mod1"), new OldSchoolModification(2, "Mod2")); ModificationCollection b = new ModificationCollection(new OldSchoolModification(1, "Mod1"), new OldSchoolModification(3, "Mod3")); Assert.IsFalse(a.Equals(b)); }
public void TestSplit() { TextFile primary = new TextFile(); primary.AddLines(new[] { "a", "b", "c", "d", "e", "f" }); TextFile secondary = new TextFile(); secondary.AddLines(new[] { "1", "2", "3", "4", "5", "6" }); ModificationCollection modifications = new ModificationCollection(primary, secondary); modifications.AddNoChanged(0, 2); modifications.AddReplaced(2, 0, 2); modifications.AddAdded(4, 2); Modification[] splited = modifications[1].Split(1); // неверный индекс Assert.AreEqual(splited.Length, 1); Assert.AreSame(splited[0], modifications[1]); splited = modifications[1].Split(2); // граничный индекс Assert.AreEqual(splited.Length, 1); Assert.AreSame(splited[0], modifications[1]); splited = modifications[1].Split(3); // индекс в середине Assert.AreEqual(splited.Length, 2); }
public void TestFindModificationByPrimaryIndex() { TextFile primary = new TextFile(); primary.AddLines(new[] {"a","b","c","d","e","f"}); TextFile secondary = new TextFile(); secondary.AddLines(new[] { "1", "2", "3", "4", "5", "6" }); ModificationCollection modifications = new ModificationCollection(primary, secondary); modifications.AddNoChanged(0, 2); modifications.AddReplaced(2, 0, 2); modifications.AddNoChanged(4, 2); Modification wanted = modifications.FindModificationByPrimaryIndex(1); Assert.AreEqual(wanted.Index, 0); wanted = modifications.FindModificationByPrimaryIndex(3); Assert.AreEqual(wanted.Index, 1); wanted = modifications.FindModificationByPrimaryIndex(4); Assert.AreEqual(wanted.Index, 2); wanted = modifications.FindModificationByPrimaryIndex(5); Assert.AreEqual(wanted.Index, 2); }
public void ModificationCollectionTest() { ModificationCollection a = new ModificationCollection(new OldSchoolModification(1, "Mod1"), new OldSchoolModification(2, "Mod2")); double lala = 0; IEnumerable aasdf = a; foreach (var jadfk in aasdf) { lala += (jadfk as IHasMass).MonoisotopicMass; } Assert.AreEqual(3, lala); Assert.AreEqual("Mod1 | Mod2", a.ToString()); a.Add(new OldSchoolModification(3, "Mod3")); Assert.AreEqual("Mod1 | Mod2 | Mod3", a.ToString()); Assert.IsTrue(a.Contains(new OldSchoolModification(2, "Mod2"))); IHasMass[] myArray = new IHasMass[4]; a.CopyTo(myArray, 1); Assert.AreEqual(3, myArray.Sum(b => b == null ? 0 : 1)); Assert.AreEqual(3, a.Count()); Assert.IsFalse(a.IsReadOnly); a.Remove(new OldSchoolModification(2, "Mod2")); Assert.AreEqual("Mod1 | Mod3", a.ToString()); double ok = 0; foreach (var b in a) { ok += b.MonoisotopicMass; } Assert.AreEqual(4, ok); a.Clear(); Assert.AreEqual("", a.ToString()); }
private static string GetLocalizedString(LocalizedHit hit) { StringBuilder sb = new StringBuilder(); IMass[] mods = hit.LocalizedIsoform.GetModifications(); bool first = false; for (int resNumber = 1; resNumber < mods.Length - 1; resNumber++) { IMass mod = mods[resNumber]; if (mod == null) { continue; } ModificationCollection modCollection = mod as ModificationCollection; if (modCollection != null) { IMass trueMod = mod; bool passes = false; foreach (IMass mod2 in modCollection) { if (Lotor.QuantifiedModifications.Contains(mod2)) { trueMod = mod2; passes = true; break; } } if (!passes) { continue; } mod = trueMod; } else { if (Lotor.FixedModifications.Contains(mod) || !Lotor.QuantifiedModifications.Contains(mod)) { continue; } } int fullResidueNumber = hit.PSM.StartResidue + resNumber - 1; char res = hit.LocalizedIsoform.Sequence[resNumber - 1]; if (first) { sb.Append(" & "); } sb.Append(res); sb.Append(fullResidueNumber); sb.Append("[" + mod + "]"); first = true; } return(sb.ToString()); }
public Merger() { ServerFile = new TextFile(); User1File = new TextFile(); User2File = new TextFile(); ServerUser1Modifications = new ModificationCollection(ServerFile, User1File); ServerUser2Modifications = new ModificationCollection(ServerFile, User2File); MergedModifications = new ModificationCollection(); }
public void ExecuteComapare() { FileComparer fileComparer = new FileComparer(); ServerUser1Modifications = fileComparer.Compare(ServerFile, User1File); ServerUser2Modifications = fileComparer.Compare(ServerFile, User2File); MergedModifications = new ModificationCollection(); RaiseStateChanged(); }
public void ModificationCollectionTestTest() { OldSchoolModification mod1 = new OldSchoolModification(10, "mass 10 modification"); OldSchoolModification mod2 = new OldSchoolModification(100, "mass 100 modification"); OldSchoolModification mod3 = new OldSchoolModification(1000, "mass 1000 modification"); ModificationCollection a = new ModificationCollection(mod1, mod2, mod3, mod1); ModificationCollection b = new ModificationCollection(mod1, mod3, mod1, mod2); Assert.IsTrue(a.Equals(b)); ModificationCollection c = new ModificationCollection(mod1); Assert.IsFalse(c.Equals(b)); }
private void miDelete_Click(object sender, EventArgs e) { #if CHECKERS if (IsSelectionEmpty) { throw new InvalidOperationException(); } #endif var b = new StringBuilder(); bool isFirst = true; foreach (var c in _selectionList) { if (!isFirst) { b.Append(", "); } else { isFirst = false; } b.Append(c.Owner.Title); } if (Extenders.ConfirmDelete(b.ToString())) { var list = _selectionList.ToArray(); UnSelectAll(); var mg = new ModificationCollection <HtmlControlModification>(); foreach (var c in list) { FreeResources(c.Owner); HtmlDesignMovableControl owner = c.Owner; owner.NotifyDelete(); mg.Add(HtmlControlModification.GetRemoved(owner)); owner.Parent = null; } if (mg.Count > 0) { HtmlPage.AddUndoOperation(mg.Count == 1 ? (IModification)mg[0] : mg); } else { throw new InvalidOperationException(); } UpdateUndoRedoState(); } }
public void TestSplit() { TextFile primary = new TextFile(); primary.AddLines(new[] { "a", "b", "c", "d", "e", "f" }); TextFile secondary = new TextFile(); secondary.AddLines(new[] { "1", "2", "3", "4", "5", "6" }); ModificationCollection modifications = new ModificationCollection(primary, secondary); modifications.AddNoChanged(0, 2); modifications.AddReplaced(2, 0, 2); modifications.AddAdded(4, 2); modifications.Split(3); // индекс в середине Assert.AreEqual(modifications.Count, 4); }
public void TestReplaced2() { TextFile file1 = new TextFile(); file1.AddLine("1"); file1.AddLine("4"); file1.AddLine("3"); TextFile file2 = new TextFile(); file2.AddLine("2"); FileComparer fileComparer = new FileComparer(); ModificationCollection report = fileComparer.Compare(file1, file2); ModificationAreEqual(report[0], "Replaced", 0, 0, 1); ModificationAreEqual(report[1], "Removed", 1, -1, 2); }
private bool IsFirstMoreSecond(ModificationCollection first, ModificationCollection second) { if (first.Count == 0) { return(second.Count == 0); } if (second.Count == 0) { return(true); } Modification firstLast = first.Last(); Modification secondLast = second.Last(); return(firstLast.Secondary.StartIndex + firstLast.Secondary.Length > secondLast.Secondary.StartIndex + secondLast.Secondary.Length); }
public void ExecuteMerge() { MergedModifications = new ModificationCollection(new TextFile(ServerFile.Name), new TextFile("Merged")); MergedModifications.Initialize(ServerUser1Modifications); MergedModifications.Split(ServerUser2Modifications); ModificationCollection temporary = new ModificationCollection(ServerUser2Modifications); temporary.Split(MergedModifications); ICollection <Modification> processed = new List <Modification>(); for (int mergedIndex = 0; mergedIndex < MergedModifications.Count; mergedIndex++) { Modification currentMerged = MergedModifications[mergedIndex]; Modification currentUser2 = temporary.FindModificationByPrimaryIndex(processed, currentMerged.Primary.StartIndex); if (currentUser2 == null) { continue; } processed.Add(currentUser2); Modification[] merged = currentMerged.Merge(currentUser2); currentMerged.Remove(); foreach (Modification mergedModification in merged) { MergedModifications.Insert(mergedIndex, mergedModification); mergedIndex++; } mergedIndex--; } MergedModifications.GenerateFiles(); RaiseStateChanged(); }
public void TestAdded() { TextFile file1 = new TextFile(); file1.AddLine("1"); file1.AddLine("3"); TextFile file2 = new TextFile(); file2.AddLine("1"); file2.AddLine("2"); file2.AddLine("3"); FileComparer fileComparer = new FileComparer(); ModificationCollection report = fileComparer.Compare(file1, file2); ModificationAreEqual(report[0], "NoChanged", 0, 0, 1); ModificationAreEqual(report[1], "Added", -1, 1, 1); ModificationAreEqual(report[2], "NoChanged", 1, 2, 1); }
public void Initialize(ModificationCollection modifications) { primaryListView.Items.Clear(); secondaryListView.Items.Clear(); primaryTextBox.Text = modifications.Primary.Name; secondaryTextBox.Text = modifications.Secondary.Name; int rowNumber = 1; foreach (var modification in modifications) { for (int index = 0; index < modification.Length; index++) { var primaryItem = new ListViewItem(rowNumber.ToString("00000")); primaryItem.BackColor = modification.Primary.Color; string primaryLine = string.Empty; if (index < modification.Primary.Length) { primaryLine = modification.Primary.Lines[index]; } primaryItem.SubItems.Add(primaryLine); primaryListView.Items.Add(primaryItem); var secondaryItem = new ListViewItem(rowNumber.ToString("00000")); secondaryItem.BackColor = modification.Secondary.Color; string secondaryLine = string.Empty; if (index < modification.Secondary.Length) { secondaryLine = modification.Secondary.Lines[index]; } secondaryItem.SubItems.Add(secondaryLine); secondaryListView.Items.Add(secondaryItem); rowNumber++; } } }
public void ExecuteMerge() { MergedModifications = new ModificationCollection(new TextFile(ServerFile.Name), new TextFile("Merged")); MergedModifications.Initialize(ServerUser1Modifications); MergedModifications.Split(ServerUser2Modifications); ModificationCollection temporary = new ModificationCollection(ServerUser2Modifications); temporary.Split(MergedModifications); ICollection<Modification> processed = new List<Modification>(); for (int mergedIndex = 0; mergedIndex < MergedModifications.Count; mergedIndex++) { Modification currentMerged = MergedModifications[mergedIndex]; Modification currentUser2 = temporary.FindModificationByPrimaryIndex(processed, currentMerged.Primary.StartIndex); if (currentUser2 == null) { continue; } processed.Add(currentUser2); Modification[] merged = currentMerged.Merge(currentUser2); currentMerged.Remove(); foreach (Modification mergedModification in merged) { MergedModifications.Insert(mergedIndex, mergedModification); mergedIndex++; } mergedIndex--; } MergedModifications.GenerateFiles(); RaiseStateChanged(); }
public void TestEmptyFiles() { TextFile emptyFile = new TextFile(); TextFile file2 = new TextFile(); file2.AddLine("public class MyClass"); file2.AddLine("{"); file2.AddLine("}"); FileComparer fileComparer = new FileComparer(); ModificationCollection report = fileComparer.Compare(emptyFile, file2); ModificationAreEqual(report[0], "Added", -1, 0, 3); report = fileComparer.Compare(file2, emptyFile); ModificationAreEqual(report[0], "Removed", 0, -1, 3); report = fileComparer.Compare(emptyFile, emptyFile); Assert.AreEqual(report.Count, 0); }
public void TestMethod1() { TextFile file1 = new TextFile(); file1.AddLine("public class MyClass"); file1.AddLine("{"); file1.AddLine(" public MyClass()"); file1.AddLine(" { }"); file1.AddLine(); file1.AddLine(" public string StringProperty"); file1.AddLine(" {"); file1.AddLine(" get;"); file1.AddLine(" set;"); file1.AddLine(" }"); file1.AddLine("}"); TextFile file2 = new TextFile(); file2.AddLine("public class MyClass"); file2.AddLine("{"); file2.AddLine(" public MyClass()"); file2.AddLine(" { }"); file2.AddLine(); file2.AddLine(" public int MyMethod(int value)"); file2.AddLine(" {"); file2.AddLine(" Console.WriteLine(value);"); file2.AddLine(" }"); file2.AddLine("}"); FileComparer fileComparer = new FileComparer(); ModificationCollection report = fileComparer.Compare(file1, file2); ModificationAreEqual(report[0], "NoChanged", 0, 0, 5); ModificationAreEqual(report[1], "Replaced", 5, 5, 1); ModificationAreEqual(report[2], "NoChanged", 6, 6, 1); ModificationAreEqual(report[3], "Replaced", 7, 7, 1); ModificationAreEqual(report[4], "Removed", 8, -1, 1); ModificationAreEqual(report[5], "NoChanged", 9, 8, 2); }
private void Control_EndMove(HtmlDesignMovableControl c) { IModification m; if (_selectionList.Count == 1) { HtmlDesignMovableControl o = _selectionList[0].Owner; Point previousLocation = _PreviousLocations[o]; m = previousLocation != o.Control.Location ? HtmlControlModification.GetMoved(o, previousLocation) : (IModification)null; } else { var list = new ModificationCollection <HtmlControlModification>(); foreach (var bc in _selectionList) { HtmlDesignMovableControl o = bc.Owner; Point previousLocation = _PreviousLocations[o]; if (previousLocation != o.Control.Location) { list.Add(HtmlControlModification.GetMoved(o, previousLocation)); } } if (list.Count == 1) { m = list[0]; } else { m = list.Count > 0 ? list : null; } } if (m != null) { HtmlPage.AddUndoOperation(m); UpdateUndoRedoState(); } c.Control.LocationChanged -= Control_LocationChanged; }
private void miDelete_Click(object sender, EventArgs e) { #if CHECKERS if (IsSelectionEmpty) { throw new InvalidOperationException(); } #endif var b = new StringBuilder(); bool isFirst = true; foreach (var c in _selectionList) { if (!isFirst) { b.Append(", "); } else { isFirst = false; } b.Append(c.Owner.Title); } if (Extenders.ConfirmDelete(b.ToString())) { var list = _selectionList.ToArray(); UnSelectAll(); var mg = new ModificationCollection<HtmlControlModification>(); foreach (var c in list) { FreeResources(c.Owner); HtmlDesignMovableControl owner = c.Owner; owner.NotifyDelete(); mg.Add(HtmlControlModification.GetRemoved(owner)); owner.Parent = null; } if (mg.Count > 0) { HtmlPage.AddUndoOperation(mg.Count == 1 ? (IModification)mg[0] : mg); } else { throw new InvalidOperationException(); } UpdateUndoRedoState(); } }
private bool IsFirstMoreSecond(ModificationCollection first, ModificationCollection second) { if (first.Count == 0) { return second.Count == 0; } if (second.Count == 0) { return true; } Modification firstLast = first.Last(); Modification secondLast = second.Last(); return firstLast.Secondary.StartIndex + firstLast.Secondary.Length > secondLast.Secondary.StartIndex + secondLast.Secondary.Length; }
public static IEnumerable <Peptide> GetUniquePeptides(Peptide peptide) { HashSet <QuantitationChannelSet> sets = new HashSet <QuantitationChannelSet>(); Dictionary <IQuantitationChannel, HashSet <int> > locations = new Dictionary <IQuantitationChannel, HashSet <int> >(); IMass[] mods = peptide.Modifications; int modLength = mods.Length; for (int i = 0; i < modLength; i++) { if (mods[i] != null) { IMass mod = mods[i]; List <QuantitationChannelSet> channelsets = new List <QuantitationChannelSet>(); QuantitationChannelSet quantSetMod; ModificationCollection modCol; if ((modCol = mod as ModificationCollection) != null) { foreach (IMass mod2 in modCol) { if ((quantSetMod = mod2 as QuantitationChannelSet) != null) { channelsets.Add(quantSetMod); } } } else if ((quantSetMod = mod as QuantitationChannelSet) != null) { channelsets.Add(quantSetMod); } foreach (QuantitationChannelSet channelset in channelsets) { sets.Add(channelset); foreach (IQuantitationChannel channel in channelset.GetChannels()) { HashSet <int> residues; if (locations.TryGetValue(channel, out residues)) { residues.Add(i); } else { residues = new HashSet <int> { i }; locations.Add(channel, residues); } } } } } if (sets.Count == 0) { yield return(new Peptide(peptide, true)); } else if (sets.Count == 1) { foreach (QuantitationChannelSet set in sets) { foreach (IQuantitationChannel channel in set.GetChannels()) { Peptide toReturn = new Peptide(peptide, true); foreach (int residue in locations[channel]) { toReturn.SetModification(channel, residue); } yield return(toReturn); } } } else { List <HashSet <IQuantitationChannel> > quantChannels = new List <HashSet <IQuantitationChannel> >(); GetUniquePeptides_helper(sets.ToList(), 0, new HashSet <IQuantitationChannel>(), quantChannels); foreach (HashSet <IQuantitationChannel> channelset in quantChannels) { Peptide toReturn = new Peptide(peptide, true); Dictionary <int, IMass> modsToAdd = new Dictionary <int, IMass>(); foreach (IQuantitationChannel channel in channelset) { foreach (int residue in locations[channel]) { IMass modToAdd; if (modsToAdd.TryGetValue(residue, out modToAdd)) { ModificationCollection col = new ModificationCollection(channel, modToAdd); modsToAdd[residue] = col; } else { modsToAdd.Add(residue, channel); } } } foreach (KeyValuePair <int, IMass> kvp in modsToAdd) { toReturn.SetModification(kvp.Value, kvp.Key); } yield return(toReturn); } } }
public static IEnumerable<Peptide> GetUniquePeptides(Peptide peptide) { QuantitationChannelSet quantSetMod; IMass mod; ModificationCollection modCol; HashSet<QuantitationChannelSet> sets = new HashSet<QuantitationChannelSet>(); Dictionary<IQuantitationChannel, HashSet<int>> locations = new Dictionary<IQuantitationChannel, HashSet<int>>(); HashSet<int> residues; IMass[] mods = peptide.Modifications; int modLength = mods.Length; for (int i = 0; i < modLength; i++) { if (mods[i] != null) { mod = mods[i]; List<QuantitationChannelSet> channelsets = new List<QuantitationChannelSet>(); if ((modCol = mod as ModificationCollection) != null) { foreach (IMass mod2 in modCol) { if ((quantSetMod = mod2 as QuantitationChannelSet) != null) { channelsets.Add(quantSetMod); } } } else if ((quantSetMod = mod as QuantitationChannelSet) != null) { channelsets.Add(quantSetMod); } foreach (QuantitationChannelSet channelset in channelsets) { sets.Add(channelset); foreach (IQuantitationChannel channel in channelset.GetChannels()) { if (locations.TryGetValue(channel, out residues)) { residues.Add(i); } else { residues = new HashSet<int>() { i }; locations.Add(channel, residues); } } } } } if (sets.Count == 0) { yield return new Peptide(peptide, true); } else if (sets.Count == 1) { foreach (QuantitationChannelSet set in sets) { foreach (IQuantitationChannel channel in set.GetChannels()) { Peptide toReturn = new Peptide(peptide, true); foreach (int residue in locations[channel]) { toReturn.SetModification(channel, residue); } yield return toReturn; } } } else { List<HashSet<IQuantitationChannel>> quantChannels = new List<HashSet<IQuantitationChannel>>(); GetUniquePeptides_helper(sets.ToList(), 0, new HashSet<IQuantitationChannel>(), quantChannels); foreach (HashSet<IQuantitationChannel> channelset in quantChannels) { Peptide toReturn = new Peptide(peptide, true); Dictionary<int, IMass> modsToAdd = new Dictionary<int, IMass>(); IMass modToAdd; foreach (IQuantitationChannel channel in channelset) { foreach (int residue in locations[channel]) { if (modsToAdd.TryGetValue(residue, out modToAdd)) { ModificationCollection col = new ModificationCollection(channel, modToAdd); col.Add(channel); col.Add(modToAdd); modsToAdd[residue] = col; } else { modsToAdd.Add(residue, channel); } } } foreach (KeyValuePair<int, IMass> kvp in modsToAdd) { toReturn.SetModification(kvp.Value, kvp.Key); } yield return toReturn; } } yield break; }
private bool DetermineModification(int primaryStart, int primaryEnd, int secondaryStart, int secondaryEnd, ModificationCollection modifications) { bool result = false; int primaryLength = primaryEnd - primaryStart; int secondaryLength = secondaryEnd - secondaryStart; if (secondaryLength > 0) { if (primaryLength > 0) { var minLength = Math.Min(secondaryLength, primaryLength); modifications.AddReplaced(primaryStart, secondaryStart, minLength); if (secondaryLength > primaryLength) { secondaryStart += minLength; modifications.AddAdded(secondaryStart, secondaryLength - primaryLength); } else { if (primaryLength > secondaryLength) { primaryStart += minLength; modifications.AddRemoved(primaryStart, primaryLength - secondaryLength); } } } else { modifications.AddAdded(secondaryStart, secondaryLength); } result = true; } else { if (primaryLength > 0) { modifications.AddRemoved(primaryStart, primaryLength); result = true; } } return(result); }
public ModificationCollection Compare(TextFile primary, TextFile secondary) { _primary = primary; _secondary = secondary; ModificationCollection result = new ModificationCollection(primary, secondary); if (primary.LineCount == 0 && secondary.LineCount == 0) { // два пустых файла _primary = null; _secondary = null; return(result); } if (primary.LineCount == 0) { if (secondary.LineCount > 0) { // первый файл пустой, а второй имеет строки result.AddAdded(0, secondary.LineCount); } _primary = null; _secondary = null; return(result); } if (secondary.LineCount == 0) { if (primary.LineCount > 0) { // первый файл имеет строки, а второй пустой result.AddRemoved(0, primary.LineCount); } _primary = null; _secondary = null; return(result); } List <Area> areas = new List <Area>(); DivideIntoAreas(0, primary.LineCount - 1, 0, secondary.LineCount - 1, areas); areas.Sort(Area.SecondaryIndexComparer); int primaryIndex = 0; int secondaryIndex = 0; Area last = null; foreach (Area area in areas) { if (DetermineModification(primaryIndex, area.PrimaryIndex, secondaryIndex, area.SecondaryIndex, result) || last == null) { result.AddNoChanged(area.PrimaryIndex, area.Length); } primaryIndex = area.PrimaryIndex + area.Length; secondaryIndex = area.SecondaryIndex + area.Length; last = area; } // этот вызов решает проблему, когда файлы совершенно разные // это граничная ситуация, областей пересечения нет DetermineModification(primaryIndex, primary.LineCount, secondaryIndex, secondary.LineCount, result); return(result); }
public void ModificationCollectionTest2() { ModificationCollection a = new ModificationCollection(new OldSchoolModification(1, "Mod1"), new OldSchoolModification(2, "Mod2")); Assert.IsFalse(a.Remove(new OldSchoolModification(3, "Mod3"))); }
public ModificationCollection Compare(TextFile primary, TextFile secondary) { _primary = primary; _secondary = secondary; ModificationCollection result = new ModificationCollection(primary, secondary); if (primary.LineCount == 0 && secondary.LineCount == 0) { // два пустых файла _primary = null; _secondary = null; return result; } if (primary.LineCount == 0) { if (secondary.LineCount > 0) { // первый файл пустой, а второй имеет строки result.AddAdded(0, secondary.LineCount); } _primary = null; _secondary = null; return result; } if (secondary.LineCount == 0) { if (primary.LineCount > 0) { // первый файл имеет строки, а второй пустой result.AddRemoved(0, primary.LineCount); } _primary = null; _secondary = null; return result; } List<Area> areas = new List<Area>(); DivideIntoAreas(0, primary.LineCount - 1, 0, secondary.LineCount - 1, areas); areas.Sort(Area.SecondaryIndexComparer); int primaryIndex = 0; int secondaryIndex = 0; Area last = null; foreach (Area area in areas) { if (DetermineModification(primaryIndex, area.PrimaryIndex, secondaryIndex, area.SecondaryIndex, result) || last == null) { result.AddNoChanged(area.PrimaryIndex, area.Length); } primaryIndex = area.PrimaryIndex + area.Length; secondaryIndex = area.SecondaryIndex + area.Length; last = area; } // этот вызов решает проблему, когда файлы совершенно разные // это граничная ситуация, областей пересечения нет DetermineModification(primaryIndex, primary.LineCount, secondaryIndex, secondary.LineCount, result); return result; }
private bool DetermineModification(int primaryStart, int primaryEnd, int secondaryStart, int secondaryEnd, ModificationCollection modifications) { bool result = false; int primaryLength = primaryEnd - primaryStart; int secondaryLength = secondaryEnd - secondaryStart; if (secondaryLength > 0) { if (primaryLength > 0) { var minLength = Math.Min(secondaryLength, primaryLength); modifications.AddReplaced(primaryStart, secondaryStart, minLength); if (secondaryLength > primaryLength) { secondaryStart += minLength; modifications.AddAdded(secondaryStart, secondaryLength - primaryLength); } else { if (primaryLength > secondaryLength) { primaryStart += minLength; modifications.AddRemoved(primaryStart, primaryLength - secondaryLength); } } } else { modifications.AddAdded(secondaryStart, secondaryLength); } result = true; } else { if (primaryLength > 0) { modifications.AddRemoved(primaryStart, primaryLength); result = true; } } return result; }
private void Control_EndMove(HtmlDesignMovableControl c) { IModification m; if (_selectionList.Count == 1) { HtmlDesignMovableControl o = _selectionList[0].Owner; Point previousLocation = _PreviousLocations[o]; m = previousLocation != o.Control.Location ? HtmlControlModification.GetMoved(o, previousLocation) : (IModification)null; } else { var list = new ModificationCollection<HtmlControlModification>(); foreach (var bc in _selectionList) { HtmlDesignMovableControl o = bc.Owner; Point previousLocation = _PreviousLocations[o]; if (previousLocation != o.Control.Location) { list.Add(HtmlControlModification.GetMoved(o, previousLocation)); } } if (list.Count == 1) { m = list[0]; } else { m = list.Count > 0 ? list : null; } } if(m != null) { HtmlPage.AddUndoOperation(m); UpdateUndoRedoState(); } c.Control.LocationChanged -= Control_LocationChanged; }