Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Clones
- {
- public class CloneVersionSystem : ICloneVersionSystem
- {
- public static List<Clone> clones;
- public string Execute(string query)
- {
- if (query.Split()[0] == "learn")
- {
- var newClone = new Clone
- {
- CloneID = int.Parse(query.Split()[1]),
- ProgrammID = query.Split()[2]
- };
- clones.Add(newClone);
- }
- if (query.Split()[0] == "rollback")
- {
- foreach (var clone in clones)
- {
- if (clone.CloneID == int.Parse(query.Split()[1]))
- {
- clone.SuccessfulActionStack.RemoveLast();
- clone.ProgrammID = clone.SuccessfulActionStack.Last().ProgrammID;
- clone.FailedActionStack.Push(new ActionInfo("rollback", clone.CloneID, clone.ProgrammID));
- }
- }
- }
- if (query.Split()[0] == "relearn")
- {
- foreach (var clone in clones)
- {
- if (clone.CloneID == int.Parse(query.Split()[1]))
- {
- clone.ProgrammID = clone.FailedActionStack.Last().ProgrammID;
- clone.FailedActionStack.Pop();
- }
- }
- }
- if (query.Split()[0] == "clone")
- {
- var newClone = new Clone() { CloneID = int.Parse(query.Split()[1] + 1) };
- foreach (var clone in clones)
- if (clone.CloneID == int.Parse(query.Split()[1]))
- newClone.ProgrammID = clone.ProgrammID;
- clones.Add(newClone);
- }
- if (query.Split()[0] == "check")
- {
- foreach (var clone in clones)
- if (clone.CloneID == int.Parse(query.Split()[1]))
- return clone.ProgrammID;
- }
- return null;
- }
- }
- public class Clone
- {
- public int CloneID { get; set; }
- public string ProgrammID { get; set; }
- public LimitedSizeStack<ActionInfo> SuccessfulActionStack { get; set; }
- public LimitedSizeStack<ActionInfo> FailedActionStack { get; set; }
- public Clone()
- {
- }
- }
- public class ActionInfo
- {
- public string ActionName { get; set; }
- public int CloneID { get; set; }
- public string ProgrammID { get; set; }
- public ActionInfo(string actionName, int cloneID, string programmID)
- {
- ActionName = actionName;
- CloneID = cloneID;
- ProgrammID = programmID;
- }
- }
- public class LimitedSizeStack<T>
- {
- private int StackLimit { get; set; }
- private static LinkedList<T> linkedList;
- public LimitedSizeStack(int limit)
- {
- StackLimit = limit;
- linkedList = new LinkedList<T>();
- }
- public void Push(T item)
- {
- if (this.Count + 1 > StackLimit)
- linkedList.RemoveFirst();
- linkedList.AddLast(item);
- }
- public T Pop()
- {
- var last = linkedList.Last;
- linkedList.RemoveLast();
- return last.Value;
- }
- public void RemoveLast()
- {
- linkedList.RemoveLast();
- }
- public int Count
- {
- get { return linkedList.Count; }
- }
- public T Last()
- {
- return linkedList.Last.Value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement