Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- namespace GitTask
- {
- public class Git
- {
- private List<int>[] commitsKeys;
- private List<int>[] commitsValues;
- private int timesCommited;
- public Git(int filesCount)
- {
- commitsKeys = new List<int>[filesCount];
- commitsValues = new List<int>[filesCount];
- for (var i = 0; i < filesCount; i++)
- {
- commitsKeys[i] = new List<int>();
- commitsValues[i] = new List<int>();
- commitsKeys[i].Add(-1);
- commitsValues[i].Add(0);
- }
- }
- public void Update(int fileNumber, int value)
- {
- var file = commitsValues[fileNumber];
- file[0] = value;
- }
- public int Commit()
- {
- for (var i = 0; i < commitsKeys.Length; i++)
- {
- var file = commitsValues[i];
- var currentValue = file[0];
- var latestCommitValue = file.Last();
- if (currentValue != latestCommitValue || file.Count == 1)
- {
- commitsKeys[i].Add(timesCommited);
- file.Add(currentValue);
- }
- }
- // var tasks = new List<Task>();
- // for (var i = 0; i < commitsKeys.Length; i++)
- // {
- // var tmp = i;
- // tasks.Add(Task.Factory.StartNew(() =>
- // {
- // var file = commitsValues[tmp];
- // var currentValue = file[0];
- // var latestCommitValue = file.Last();
- // if (currentValue != latestCommitValue || file.Count == 1)
- // {
- // commitsKeys[tmp].Add(timesCommited);
- // file.Add(currentValue);
- // }
- // }));
- // }
- // Task.WaitAll(tasks.ToArray());
- // Console.WriteLine("All threads complete");
- timesCommited++;
- return timesCommited - 1;
- }
- private static int BinarySearch<T>(IList<T> list, T value)
- {
- var comp = Comparer<T>.Default;
- var low = 0;
- var high = list.Count - 1;
- while (low < high) {
- var m = (high + low) / 2;
- if (comp.Compare(list[m], value) < 0)
- low = m + 1;
- else
- high = m;
- }
- return low;
- }
- public int Checkout(int commitNumber, int fileNumber)
- {
- if (timesCommited - 1 < commitNumber)
- {
- throw new System.ArgumentException();
- }
- var lessOrEqKey = BinarySearch(commitsKeys[fileNumber], commitNumber);
- return commitsValues[fileNumber][lessOrEqKey];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement