Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5.  
  6. namespace GitTask
  7. {
  8. public class Git
  9. {
  10. private List<int>[] commitsKeys;
  11. private List<int>[] commitsValues;
  12. private int timesCommited;
  13.  
  14. public Git(int filesCount)
  15. {
  16. commitsKeys = new List<int>[filesCount];
  17. commitsValues = new List<int>[filesCount];
  18.  
  19. for (var i = 0; i < filesCount; i++)
  20. {
  21. commitsKeys[i] = new List<int>();
  22. commitsValues[i] = new List<int>();
  23. commitsKeys[i].Add(-1);
  24. commitsValues[i].Add(0);
  25. }
  26. }
  27.  
  28. public void Update(int fileNumber, int value)
  29. {
  30. var file = commitsValues[fileNumber];
  31. file[0] = value;
  32. }
  33.  
  34. public int Commit()
  35. {
  36. for (var i = 0; i < commitsKeys.Length; i++)
  37. {
  38. var file = commitsValues[i];
  39. var currentValue = file[0];
  40. var latestCommitValue = file.Last();
  41. if (currentValue != latestCommitValue || file.Count == 1)
  42. {
  43. commitsKeys[i].Add(timesCommited);
  44. file.Add(currentValue);
  45. }
  46. }
  47.  
  48. // var tasks = new List<Task>();
  49. // for (var i = 0; i < commitsKeys.Length; i++)
  50. // {
  51. // var tmp = i;
  52. // tasks.Add(Task.Factory.StartNew(() =>
  53. // {
  54. // var file = commitsValues[tmp];
  55. // var currentValue = file[0];
  56. // var latestCommitValue = file.Last();
  57. // if (currentValue != latestCommitValue || file.Count == 1)
  58. // {
  59. // commitsKeys[tmp].Add(timesCommited);
  60. // file.Add(currentValue);
  61. // }
  62. // }));
  63. // }
  64. // Task.WaitAll(tasks.ToArray());
  65. // Console.WriteLine("All threads complete");
  66.  
  67. timesCommited++;
  68. return timesCommited - 1;
  69. }
  70.  
  71. private static int BinarySearch<T>(IList<T> list, T value)
  72. {
  73. var comp = Comparer<T>.Default;
  74. var low = 0;
  75. var high = list.Count - 1;
  76. while (low < high) {
  77. var m = (high + low) / 2;
  78. if (comp.Compare(list[m], value) < 0)
  79. low = m + 1;
  80. else
  81. high = m;
  82. }
  83. return low;
  84. }
  85.  
  86. public int Checkout(int commitNumber, int fileNumber)
  87. {
  88. if (timesCommited - 1 < commitNumber)
  89. {
  90. throw new System.ArgumentException();
  91. }
  92.  
  93. var lessOrEqKey = BinarySearch(commitsKeys[fileNumber], commitNumber);
  94. return commitsValues[fileNumber][lessOrEqKey];
  95. }
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement