Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Clones
  5. {
  6. public class StackItem<T>
  7. {
  8. public T Value;
  9. public StackItem<T> Previous;
  10. }
  11. public class StackClone<T>
  12. {
  13. public StackItem<T> Tail;
  14. public int Count;
  15. public void Push(T item)
  16. {
  17. var stackItem = new StackItem<T> { Value = item };
  18. if (Count == 0)
  19. {
  20. stackItem.Previous = null;
  21. Tail = stackItem;
  22. Count++;
  23. return;
  24. }
  25.  
  26. stackItem.Previous = Tail;
  27. Tail = stackItem;
  28. Count++;
  29. }
  30.  
  31. public T Pop()
  32. {
  33. var item = Tail;
  34. if (Count == 0)
  35. {
  36. Tail = null;
  37. return item.Value;
  38. }
  39. Tail = Tail.Previous;
  40. Count--;
  41. return item.Value;
  42. }
  43. }
  44. public class Clon
  45. {
  46. public StackClone<string> izucheniecomand = new StackClone<string>();
  47. public StackClone<string> otcati = new StackClone<string>();
  48. }
  49. public class CloneVersionSystem : ICloneVersionSystem
  50. {
  51. public Dictionary<string, Clon> dictionary = new Dictionary<string, Clon>() { { "1", new Clon() } };
  52.  
  53. public string Execute(string query)
  54. {
  55. var array = query.Split(' ');
  56. if (!(dictionary.ContainsKey(array[1])))
  57. return null;
  58. switch (array[0])
  59. {
  60. case "learn":
  61. dictionary[array[1]].izucheniecomand.Push(array[2]);
  62. dictionary[array[1]].otcati = new StackClone<string>();
  63. break;
  64.  
  65. case "rollback":
  66. var programmClone = dictionary[array[1]].izucheniecomand.Pop();
  67. dictionary[array[1]].otcati.Push(programmClone);
  68. break;
  69.  
  70. case "clone":
  71. dictionary.Add((int.Parse(array[1]) + 1).ToString(), new Clon());
  72. dictionary[(int.Parse(array[1]) + 1).ToString()].izucheniecomand.Tail = dictionary[array[1]].izucheniecomand.Tail;
  73. dictionary[(int.Parse(array[1]) + 1).ToString()].otcati.Tail = dictionary[array[1]].otcati.Tail;
  74. break;
  75.  
  76. case "check":
  77. if (dictionary[array[1]].izucheniecomand.Tail == null) return "basic";
  78. else return dictionary[array[1]].izucheniecomand.Tail.Value;
  79.  
  80. case "relearn":
  81. var programmClone1 = dictionary[array[1]].otcati.Pop();
  82. dictionary[array[1]].izucheniecomand.Push(programmClone1);
  83. break;
  84. }
  85.  
  86. return null;
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement