Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Clones
  5. {
  6. //interface ICommand
  7. //{
  8. //}
  9.  
  10. public class CloneVersionSystem : ICloneVersionSystem
  11. {
  12. // enum command { learn , rollback, relearn, clone, check };
  13.  
  14. List<CloneItem> clones = new List<CloneItem>() { new CloneItem() };
  15.  
  16. public string Execute(string query)
  17. {
  18. var commands = query.Split();
  19. var cloneIndex = int.Parse(commands[1]) - 1;
  20.  
  21.  
  22.  
  23. switch (commands[0])
  24. {
  25. case "learn":
  26. clones[cloneIndex].learn(int.Parse(commands[2]));
  27. break;
  28. case "rollback":
  29. clones[cloneIndex].rollback();
  30. break;
  31. case "relearn":
  32. clones[cloneIndex].relearn();
  33. break;
  34. case "clone":
  35. clones.Add((CloneItem)clones[cloneIndex].Clone());
  36. break;
  37. case "check":
  38. return clones[cloneIndex].check().ToString();
  39. }
  40.  
  41. return null;
  42. }
  43. }
  44.  
  45. public class CloneItem : ICloneable
  46. {
  47. public Stack<int> learned = new Stack<int>();
  48. public Stack<int> delearned = new Stack<int>();
  49.  
  50.  
  51. public void learn(int prog)
  52. {
  53. learned.Push(prog);
  54. }
  55.  
  56. public void rollback()
  57. {
  58. delearned.Push(learned.LastAction());
  59. learned.Pop();
  60. }
  61.  
  62. public void relearn()
  63. {
  64. learned.Push(delearned.LastAction());
  65. delearned.Pop();
  66. }
  67.  
  68. public string check()
  69. {
  70. if (learned.Count == 0)
  71. return "basic";
  72. return learned.LastAction().ToString();
  73. }
  74.  
  75. public object Clone()
  76. {
  77. var cloned = new CloneItem();
  78. cloned.learned = (Stack<int>)learned.Clone();
  79. cloned.delearned = (Stack<int>)this.delearned.Clone();
  80.  
  81. return cloned;
  82. }
  83. }
  84.  
  85. public class StackItem<T> : ICloneable
  86. {
  87. public int Value { get; set; }
  88. public StackItem<T> Next { get; set; }
  89. public StackItem<T> Previous { get; set; }
  90.  
  91. public object Clone()
  92. {
  93. var cloned = new StackItem<T>();
  94. cloned.Value = int.Parse(Value.ToString());
  95. cloned.Next = Next;
  96. cloned.Previous = Previous;
  97. return cloned;
  98. }
  99. }
  100.  
  101. public class Stack<T> : ICloneable
  102. {
  103. StackItem<T> head;
  104. StackItem<T> tail;
  105. int limit = 256;
  106. public int Count { get; set; }
  107.  
  108.  
  109. public void Push(int item)
  110. {
  111. if (Count == 0)
  112. {
  113. tail = head = new StackItem<T> { Value = item, Next = null, Previous = null };
  114. Count += 1;
  115. }
  116. else
  117. {
  118. var newItem = new StackItem<T> { Value = item, Next = null, Previous = tail };
  119. tail.Next = newItem;
  120. tail = newItem;
  121. Count += 1;
  122. }
  123.  
  124. if (Count > limit)
  125. {
  126. var newHead = head.Next;
  127. if (newHead != null)
  128. newHead.Previous = null;
  129. head = newHead;
  130. Count -= 1;
  131. }
  132. }
  133.  
  134. public int Pop()
  135. {
  136. if (tail == null) throw new InvalidOperationException();
  137. var result = tail.Value;
  138. tail = tail.Previous;
  139. if (tail == null)
  140. head = null;
  141. Count -= 1;
  142. return result;
  143. }
  144.  
  145. public int LastAction()
  146. {
  147. if (tail == null) throw new InvalidOperationException();
  148. return tail.Value;
  149. }
  150.  
  151. public object Clone()
  152. {
  153. return new Stack<T>() { head = head == null ? null : (StackItem<T>)this.head.Clone(), tail = tail == null ? null : (StackItem<T>)this.tail.Clone(), Count = this.Count };
  154. }
  155. }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement