Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Clones
- {
- //interface ICommand
- //{
- //}
- public class CloneVersionSystem : ICloneVersionSystem
- {
- // enum command { learn , rollback, relearn, clone, check };
- List<CloneItem> clones = new List<CloneItem>() { new CloneItem() };
- public string Execute(string query)
- {
- var commands = query.Split();
- var cloneIndex = int.Parse(commands[1]) - 1;
- switch (commands[0])
- {
- case "learn":
- clones[cloneIndex].learn(int.Parse(commands[2]));
- break;
- case "rollback":
- clones[cloneIndex].rollback();
- break;
- case "relearn":
- clones[cloneIndex].relearn();
- break;
- case "clone":
- clones.Add((CloneItem)clones[cloneIndex].Clone());
- break;
- case "check":
- return clones[cloneIndex].check().ToString();
- }
- return null;
- }
- }
- public class CloneItem : ICloneable
- {
- public Stack<int> learned = new Stack<int>();
- public Stack<int> delearned = new Stack<int>();
- public void learn(int prog)
- {
- learned.Push(prog);
- }
- public void rollback()
- {
- delearned.Push(learned.LastAction());
- learned.Pop();
- }
- public void relearn()
- {
- learned.Push(delearned.LastAction());
- delearned.Pop();
- }
- public string check()
- {
- if (learned.Count == 0)
- return "basic";
- return learned.LastAction().ToString();
- }
- public object Clone()
- {
- var cloned = new CloneItem();
- cloned.learned = (Stack<int>)learned.Clone();
- cloned.delearned = (Stack<int>)this.delearned.Clone();
- return cloned;
- }
- }
- public class StackItem<T> : ICloneable
- {
- public int Value { get; set; }
- public StackItem<T> Next { get; set; }
- public StackItem<T> Previous { get; set; }
- public object Clone()
- {
- var cloned = new StackItem<T>();
- cloned.Value = int.Parse(Value.ToString());
- cloned.Next = Next;
- cloned.Previous = Previous;
- return cloned;
- }
- }
- public class Stack<T> : ICloneable
- {
- StackItem<T> head;
- StackItem<T> tail;
- int limit = 256;
- public int Count { get; set; }
- public void Push(int item)
- {
- if (Count == 0)
- {
- tail = head = new StackItem<T> { Value = item, Next = null, Previous = null };
- Count += 1;
- }
- else
- {
- var newItem = new StackItem<T> { Value = item, Next = null, Previous = tail };
- tail.Next = newItem;
- tail = newItem;
- Count += 1;
- }
- if (Count > limit)
- {
- var newHead = head.Next;
- if (newHead != null)
- newHead.Previous = null;
- head = newHead;
- Count -= 1;
- }
- }
- public int Pop()
- {
- if (tail == null) throw new InvalidOperationException();
- var result = tail.Value;
- tail = tail.Previous;
- if (tail == null)
- head = null;
- Count -= 1;
- return result;
- }
- public int LastAction()
- {
- if (tail == null) throw new InvalidOperationException();
- return tail.Value;
- }
- public object Clone()
- {
- 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 };
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement