Guest User

Untitled

a guest
Feb 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. void Main()
  2. {
  3. var lines = File.ReadAllLines(@"d:\temp\input.txt");
  4.  
  5. var re = new Regex(@":(?<type>Allocation|Deallocation).*Item\s*:(?<id>\d+)\s*$", RegexOptions.IgnoreCase);
  6. var matches = new Dictionary<int, ReferenceMatch>();
  7. foreach (var line in lines)
  8. {
  9. Match ma = re.Match(line);
  10. if (!ma.Success)
  11. continue;
  12.  
  13. int id = int.Parse(ma.Groups["id"].Value);
  14. matches.TryGetValue(id, out ReferenceMatch refMatch);
  15. if (StringComparer.CurrentCultureIgnoreCase.Equals(ma.Groups["type"].Value, "Allocation"))
  16. refMatch.Allocated = true;
  17. else
  18. refMatch.Deallocated = true;
  19. matches[id] = refMatch;
  20. }
  21.  
  22. using (var writer = new StreamWriter(@"d:\temp\output.txt"))
  23. {
  24. foreach (var line in lines)
  25. {
  26. Match ma = re.Match(line);
  27. if (!ma.Success)
  28. {
  29. writer.WriteLine(line);
  30. continue;
  31. }
  32.  
  33. int id = int.Parse(ma.Groups["id"].Value);
  34. matches.TryGetValue(id, out ReferenceMatch refMatch);
  35.  
  36. if (refMatch.Allocated && refMatch.Deallocated)
  37. continue;
  38. writer.WriteLine(line);
  39. }
  40. }
  41.  
  42. matches.Dump();
  43. }
  44.  
  45. public struct ReferenceMatch
  46. {
  47. public bool Allocated;
  48. public bool Deallocated;
  49. }
Add Comment
Please, Sign In to add comment