Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Clones
- {
- public class CloneVersionSystem : ICloneVersionSystem
- {
- private List<Clone> clones;
- public string Execute(string query)
- {
- var allCommands = query.Split();
- var commandName = allCommands[0];
- var cloneId = int.Parse(allCommands[1]);
- var programId = allCommands.Length > 2 ? allCommands[2] : "";
- if (clones.Count < cloneId) return null;
- return RunCommand(commandName, clones[cloneId - 1], programId);
- }
- public CloneVersionSystem()
- {
- clones = new List<Clone>{new Clone()};
- }
- private string RunCommand(string commandName, Clone clone, string programId)
- {
- string result = null;
- switch (commandName)
- {
- case "learn":
- LearnClone(clone, programId);
- break;
- case "rollback":
- RollbackClone(clone);
- break;
- case "relearn":
- RelearnClone(clone);
- break;
- case "clone":
- ToCloneClone(clone);
- break;
- case "check":
- result = CheckClone(clone);
- break;
- }
- return result;
- }
- private void LearnClone(Clone clone, string programId)
- {
- clone.HistoryStack.Clear();
- clone.ProgramsStack.Push(int.Parse(programId));
- }
- private void RollbackClone(Clone clone)
- {
- clone.HistoryStack.Push(clone.ProgramsStack.Pop());
- }
- private void RelearnClone(Clone clone)
- {
- clone.ProgramsStack.Push(clone.HistoryStack.Pop());
- }
- private string CheckClone(Clone clone)
- {
- if (clone.ProgramsStack.IsEmpty()) return "basic";
- return clone.ProgramsStack.Peek()+"";
- }
- private void ToCloneClone(Clone clone)
- {
- var newClone = clone.CloneThisClone();
- clones.Add(newClone);
- }
- }
- public class Clone
- {
- public Stack ProgramsStack;
- public Stack HistoryStack;
- public Clone()
- {
- ProgramsStack = new Stack();
- HistoryStack = new Stack();
- }
- public Clone CloneThisClone()
- {
- var other = new Clone
- {
- ProgramsStack = new Stack(ProgramsStack),
- HistoryStack = new Stack(HistoryStack)
- };
- return other;
- }
- public class Stack
- {
- private StackItem last;
- public Stack()
- {
- }
- public Stack(Stack stack)
- {
- last = stack.last;
- }
- public void Push(int value)
- {
- last = new StackItem(value, last);
- }
- using System;
- using System.Collections.Generic;
- namespace Clones
- {
- public class CloneVersionSystem : ICloneVersionSystem
- {
- private List<Clone> clones;
- public string Execute(string query)
- {
- var allCommands = query.Split();
- var commandName = allCommands[0];
- var cloneId = int.Parse(allCommands[1]);
- var programId = allCommands.Length > 2 ? allCommands[2] : "";
- if (clones.Count < cloneId) return null;
- return RunCommand(commandName, clones[cloneId - 1], programId);
- }
- public CloneVersionSystem()
- {
- clones = new List<Clone>{new Clone()};
- }
- private string RunCommand(string commandName, Clone clone, string programId)
- {
- string result = null;
- switch (commandName)
- {
- case "learn":
- LearnClone(clone, programId);
- break;
- case "rollback":
- RollbackClone(clone);
- break;
- case "relearn":
- RelearnClone(clone);
- break;
- case "clone":
- ToCloneClone(clone);
- break;
- case "check":
- result = CheckClone(clone);
- break;
- }
- return result;
- }
- private void LearnClone(Clone clone, string programId)
- {
- clone.HistoryStack.Clear();
- clone.ProgramsStack.Push(int.Parse(programId));
- }
- private void RollbackClone(Clone clone)
- {
- clone.HistoryStack.Push(clone.ProgramsStack.Pop());
- }
- private void RelearnClone(Clone clone)
- {
- clone.ProgramsStack.Push(clone.HistoryStack.Pop());
- }
- private string CheckClone(Clone clone)
- {
- if (clone.ProgramsStack.IsEmpty()) return "basic";
- return clone.ProgramsStack.Peek()+"";
- }
- private void ToCloneClone(Clone clone)
- {
- var newClone = clone.CloneThisClone();
- clones.Add(newClone);
- }
- }
- public class Clone
- {
- public Stack ProgramsStack;
- public Stack HistoryStack;
- public Clone()
- {
- ProgramsStack = new Stack();
- HistoryStack = new Stack();
- }
- public Clone CloneThisClone()
- {
- var other = new Clone
- {
- ProgramsStack = new Stack(ProgramsStack),
- HistoryStack = new Stack(HistoryStack)
- };
- return other;
- }
- public class Stack
- {
- private StackItem last;
- public Stack()
- {
- }
- public Stack(Stack stack)
- {
- last = stack.last;
- }
- public void Push(int value)
- {
- last = new StackItem(value, last);
- }
- public int Pop()
- {
- var value = last.Value;
- last = last.Previous;
- return value;
- }
- public void Clear()
- {
- last = null;
- }
- public int Peek()
- {
- return last.Value;
- }
- public bool IsEmpty()
- {
- return last == null;
- }
- }
- public class StackItem
- {
- public readonly int Value;
- public readonly StackItem Previous;
- public StackItem(int value, StackItem previous)
- {
- Value = value;
- Previous = previous;
- }
- }
- }
- }
- public int Pop()
- {
- var value = last.Value;
- last = last.Previous;
- return value;
- }
- public void Clear()
- {
- last = null;
- }
- public int Peek()
- {
- return last.Value;
- }
- public bool IsEmpty()
- {
- return last == null;
- }
- }
- public class StackItem
- {
- public readonly int Value;
- public readonly StackItem Previous;
- public StackItem(int value, StackItem previous)
- {
- Value = value;
- Previous = previous;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement