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
- {
- List<Clone> clones;
- public string Execute(string query)
- {
- string[] commands = query.Split();
- clones = new List<Clone>();
- switch (commands[0])
- {
- case "learn":
- {
- Learn(Int32.Parse(commands[1])-1, commands[2]);
- break;
- }
- case "rollback":
- {
- RollBack(Int32.Parse(commands[1])-1);
- break;
- }
- case "relearn":
- {
- Relearn(Int32.Parse(commands[1])-1);
- break;
- }
- case "clone":
- {
- MakeClone(Int32.Parse(commands[1])-1);
- break;
- }
- case "check":
- {
- return Check(Int32.Parse(commands[1])-1);
- }
- }
- return null;
- }
- private void Learn(int index, string command){
- if(index >= clones.Count){
- clones.Insert(index, new Clone(index));
- }
- clones[index].Learned.Push(command);
- }
- private void RollBack(int index){
- var lastProgramm = clones[index].Learned.Pop();
- clones[index].Canceled.Push(lastProgramm);
- }
- private void Relearn(int index){
- var lastProgramm = clones[index].Canceled.Pop();
- clones[index].Learned.Push(lastProgramm);
- }
- private void MakeClone(int index){
- clones.Add(new Clone(clones.Count));
- clones[clones.Count].Learned.Head = clones[index].Learned.Head;
- clones[clones.Count].Canceled.Head = clones[index].Canceled.Head;
- }
- private string Check(int index){
- if(clones[index].Learned.Head == null)
- return "basic";
- return clones[index].Learned.Head.Value;
- }
- }
- public class Clone{
- public CloneStack Learned;
- public CloneStack Canceled;
- int Number;
- public Clone(int number){
- Number = number;
- Learned = new CloneStack();
- Canceled = new CloneStack();
- }
- }
- public class StackItem{
- public string Value {get; set; }
- public StackItem Next{get; set;}
- }
- public class CloneStack{
- public StackItem Head;
- public void Push(string value){
- if(Head == null){
- Head = new StackItem { Value = value, Next = null };
- }
- else
- {
- Head = new StackItem { Value = value, Next = Head };
- }
- }
- public string Pop(){
- if (Head == null) throw new InvalidOperationException();
- var result = Head.Value;
- Head = Head.Next;
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement