public void GetFirst_OneValueAppended_ReturnsThatValue(int value) { IMyList list = CreateSUT(); list.Append(value); Assert.Equal(value, list.GetFirst()); }
public PrgState(IDictionary <string, int> dict, IExeStack <IStatement> stack, IMyList <int> outList, FileTable <int, FileData> fileTable) { this.dict = dict; this.stack = stack; this.outList = outList; this.fileTable = fileTable; }
public Engine() { this.addCollection = new AddCollection <string>(); this.addRemoveCollection = new AddRemoveCollection <string>(); this.myList = new MyList <string>(); this.stringBuilder = new StringBuilder(); }
public bool Equals(IMyList <T> that) { if (this == that) { return(true); } if (that == null || this.Count != that.Count) { return(false); } Node thisnode = this.first; IEnumerator <T> thatenm = that.GetEnumerator(); while (thisnode != null) { if (!thatenm.MoveNext()) { throw new ApplicationException("Impossible: LinkedList<T>.Equals"); } // assert MoveNext() was true (because of the above size test) if (!thisnode.item.Equals(thatenm.Current)) { return(false); } thisnode = thisnode.next; } // assert !MoveNext(); // because of the size test return(true); }
static void Main(string[] args) { Dog aDog = new Dog(); // succeed Dog 继承自 Animal 所以转换正常(协变) Animal aAnimal = aDog; List <Dog> listDogs = new List <Dog>(); // error List<Dog> 并没有继承自 List<Animal> 所以转换失败 //List<Animal> listAnimals = listDogs; // 复杂的强制类型转换 List <Animal> listAnimals2 = listDogs.Select(d => (Animal)d).ToList(); #region 协变 OUT /* * [TypeDependencyAttribute("System.SZArrayHelper")] * public interface IEnumerable<out T> : IEnumerable * * T 用 out 标记,所以代表了输出,也就是只能作为结果返回 */ IEnumerable <Dog> someDogs = new List <Dog>(); // 因为T只能做结果返回,所以T不会被修改, 编译器就可以推断下面的语句强制转换合法 IEnumerable <Animal> someAnimals = someDogs; #endregion #region 逆变 IN Action <Animal> actionAnimal = new Action <Animal>(a => { /*让动物叫*/ Console.WriteLine("让动物叫"); }); /* * In 关键字:逆变,代表输入,代表着只能被使用,不能作为返回值, * 所以C#编译器可以根据in关键字推断这个泛型类型只能被使用, * 所以Action<Dog> actionDog = actionAnimal;可以通过编译器的检查。 */ Action <Dog> actionDog = actionAnimal; actionDog(aDog); #endregion #region 协变2 OUT IMyList <Dog> myDogs = new MyList <Dog>(); IMyList <Animal> myAnimals = myDogs; #endregion #region 逆变2 IN //IMyList<Animal> myAnimals = new MyList<Animal>(); //IMyList<Dog> myDogs = myAnimals; #endregion }
public Engine() { this.addcollection = new AddCollection <string>(); this.addRemoveCollection = new AddRemoveCollection <string>(); this.myList = new MyList <string>(); this.resultingOutput = new StringBuilder(); }
public void IndexOf(IndexOfScenario scenario) { IMyList <TestItem> list = (IMyList <TestItem>)scenario.List; int index = list.IndexOf(scenario.ToFind); Assert.AreEqual(scenario.ExpectedIndex, index); }
public int GetGoldenTarget(IMyList targetPoints) { if (targetPoints.Count < 2) { throw new ArgumentException("points.Count < 2", nameof(targetPoints)); } return((targetPoints.GetFirst() + targetPoints.ElementAt(targetPoints.Count - 1)) / 2); }
public PrgState(IMyStack <IStmt> exe, IMyDictionary <String, int> d, IMyList <int> l, IMyFileTable <int, FileData> ft, IStmt orig) { exeStack = exe; symTable = d; messages = l; fileTable = ft; originalPrg = orig; exeStack.Push(orig); }
public Engine( IAddCollection addCollection, IAddRemoveCollection addRemoveCollection, IMyList myList) { this.addCollection = addCollection; this.addRemoveCollection = addRemoveCollection; this.myList = myList; }
static void Main22() { //IMyList<Dog> myDogs = new MyList<Dog>(); //IMyList<Animal> myAnimals = myDogs; IMyList <Animal> myAnimals = new MyList <Animal>(); IMyList <Dog> myDogs = myAnimals; }
public PrgState Execute(PrgState state) { IMyDictionary <string, int> sym = state.SymTable; IMyList <int> msg = state.Messages; int res = expr.Eval(sym); msg.Add(res); return(state); }
public ProgramState(IMyStack<IMyStatement> executionStack, IMyDictionary<string, int> myDictionary, IMyList<string> output, IHeap<int, int> heap, IMyStatement originalProgram) { this.executionStack = executionStack; this.myDictionary = myDictionary; this.output = output; this.originalProgram = originalProgram; this.executionStack.push(originalProgram); this.heap = heap; this.stateId = globalStateId++; }
public static string RemoveAmountOfStringsML(IMyList myList, int amountOfRemoveOperations) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < amountOfRemoveOperations; i++) { sb.Append(myList.Remove() + " "); } return(sb.ToString().Trim()); }
/// <summary> /// Прямой итератор /// </summary> /// <typeparam name="T">Параметр типа списка</typeparam> /// <param name="theList">Исходный список</param> /// <param name="finalState">Делегат, сигнализирующий об окончании итерации</param> /// <param name="incrementer">Делегат, выполняющий инкрементирование последовательности</param> /// <returns>Прямой перечислитель элементов</returns> public static IEnumerable <T> GeneralIterator <T>(this IMyList <T> theList, Func <IMyList <T>, bool> finalState, Func <IMyList <T>, IMyList <T> > incrementer) { while (!finalState(theList)) { yield return(theList.Head); theList = incrementer(theList); } }
public void Count_NValuesAppended_ReturnsN(int[] values) { IMyList list = CreateSUT(); //Usually avoid adding loops foreach (var value in values) { list.Append(value); } Assert.Equal(values.Length, list.Count); }
public void ElementAt_NValuesAppended_ReturnsValueAtPositionK(int[] values, int index, int expectedValue) { IMyList list = CreateSUT(); //Usually avoid adding loops foreach (var value in values) { list.Append(value); } Assert.Equal(expectedValue, list.ElementAt(index)); }
public MyBinaryHeap(T[] initializer) { this.comparer = Comparer <T> .Default; this.array = new MyList <T>(); if (initializer != null) { for (int i = 0; i < initializer.Length; i++) { this.Add(initializer[i]); } } }
public void TryExecuteCommand(string command) { string[] cmdArgs = command.Split(); string cmd = cmdArgs[0]; switch (cmd) { case "Add": this.list.Add(cmdArgs[1]); break; case "Remove": this.list.Remove(int.Parse(cmdArgs[1])); break; case "Contains": Console.WriteLine(this.list.Contains(cmdArgs[1])); break; case "Swap": this.list.Swap(int.Parse(cmdArgs[1]), int.Parse(cmdArgs[2])); break; case "Greater": Console.WriteLine(this.list.CountGreaterThan(cmdArgs[1])); break; case "Max": Console.WriteLine(this.list.Max()); break; case "Min": Console.WriteLine(this.list.Min()); break; case "Sort": this.list = Sorter <string> .Sort(this.list); break; case "Print": foreach (string item in this.list) { Console.WriteLine(item); } break; } }
public static void Main(String[] args) { LinkedList <double> dLst = new LinkedList <double>(7.0, 9.0, 13.0, 0.0); foreach (double d in dLst) { Console.Write("{0} ", d); } Console.WriteLine(); IMyList <int> iLst = dLst.Map <int>(Math.Sign); foreach (int i in iLst) { Console.Write("{0} ", i); } Console.WriteLine(); IMyList <String> sLst1 = dLst.Map <String>(delegate(double d) { return("s" + d); }); foreach (String s in sLst1) { Console.Write("{0} ", s); } Console.WriteLine(); IMyList <String> sLst2 = dLst.Map <String>(d => "s" + d); foreach (String s in sLst2) { Console.Write("{0} ", s); } Console.WriteLine(); // Testing SortedList<MyString> SortedList <MyString> sortedLst = new SortedList <MyString>(); sortedLst.Insert(new MyString("New York")); sortedLst.Insert(new MyString("Rome")); sortedLst.Insert(new MyString("Dublin")); sortedLst.Insert(new MyString("Riyadh")); sortedLst.Insert(new MyString("Tokyo")); foreach (MyString s in sortedLst) { Console.Write("{0} ", s.Value); } Console.WriteLine(); // MyList equality Console.WriteLine(dLst.Equals(dLst)); // True Console.WriteLine(dLst.Equals(sLst1)); // False Console.WriteLine(sLst1.Equals(sLst2)); // True Console.WriteLine(sLst1.Equals(null)); // False }
public IEnumerator <T> GetEnumerator() { IMyList <IEnumerator <T> > enumerators = new MyList <IEnumerator <T> >(); for (int i = 0; i < this.buckets.Length; i++) { IMyList <T> bucket = this.buckets[i]; if (bucket != null && bucket.Count > 0) { enumerators.Add(bucket.GetEnumerator()); } } return(new MyHashTableEnumerator <T>(enumerators)); }
static void Test1() { try { Console.WriteLine("----Test1"); LinkedList <double> dLst = new LinkedList <double>(7.0, 9.0, 13.0, 0.0); foreach (double d in dLst) { Console.WriteLine("{0} ", d); } Console.WriteLine("-----1"); IMyList <int> iLst = dLst.Map <int>(new Mapper <double, int>(Math.Sign)); foreach (int i in iLst) { Console.WriteLine("{0} ", i); } Console.WriteLine("-----2"); IMyList <String> sLst = dLst.Map <String>(delegate(double d) { return("s" + d); }); foreach (String s in sLst) { Console.WriteLine("{0} ", s); } Console.WriteLine("-----3"); // Testing SortedList<MyString> SortedList <MyString> sortedLst = new SortedList <MyString>(); sortedLst.Insert(new MyString("New York")); sortedLst.Insert(new MyString("Rome")); sortedLst.Insert(new MyString("Dublin")); sortedLst.Insert(new MyString("Riyadh")); sortedLst.Insert(new MyString("Tokyo")); foreach (MyString s in sortedLst) { Console.WriteLine("{0} ", s.Value); } Console.WriteLine("-----4"); Console.WriteLine("ok"); } catch (Exception e) { Console.WriteLine(e.GetType()); } }
public void Indexer(IndexerScenario scenario) { IMyList <TestItem> list = (IMyList <TestItem>)scenario.List; try { TestItem testItem = list[scenario.Index]; Assert.AreEqual(testItem, scenario.ExceptedItem); } catch (Exception ex) { if (scenario.ExpectedExceptionType == null) { throw; } Assert.IsInstanceOfType(scenario.ExpectedExceptionType, ex); } }
public void AddTargetPoints(IMyList toArray, int count) { if (count < 0) { throw new ArgumentException($"{nameof(count)} cannot be negative."); } int month = DateTime.Today.Month; var factor = CalculateScalingFactor(); int[] points = new int[count]; for (int i = 0; i < count; i++) { points[i] = 3 + factor * i; } toArray.AppendMany(points); }
public void RemoveAt(RemoveAtScenario scenario) { IMyList <TestItem> list = (IMyList <TestItem>)scenario.List; try { list.RemoveAt(scenario.ToRemove); } catch (Exception ex) { if (scenario.ExpectedExceptionType == null) { throw; } Assert.IsInstanceOfType(scenario.ExpectedExceptionType, ex); } AssertHelper.AreCollectionSame(scenario.ExpectedElements, list); }
public static IMyList <R> Select <T, R>(this IMyList <T> theList, Func <T, R> selector) { Func <IMyList <R> > selectorTailFunc = null; selectorTailFunc = () => { IMyList <R> result = null; if (theList.Tail == null) { result = new MyList <R>(default(R), null); } else { result = new MyList <R>(selector(theList.Head), selectorTailFunc); } theList = theList.Tail; return(result); }; return(selectorTailFunc()); }
public static IMyList <T> Take <T>(this IMyList <T> theList, int takeCount) { Func <IMyList <T> > takeTailFunc = null; takeTailFunc = () => { IMyList <T> result = null; if (theList.Tail == null || takeCount-- == 0) { result = new MyList <T>(default(T), null); } else { result = new MyList <T>(theList.Head, takeTailFunc); } theList = theList.Tail; return(result); }; return(takeTailFunc()); }
public void Insert(InsertScenario scenario) { IMyList <TestItem> list = (IMyList <TestItem>)scenario.List; try { foreach (var toInsert in scenario.ToInsert) { list.Insert(toInsert.Key, toInsert.Value); } } catch (Exception ex) { if (scenario.ExpectedExceptionType == null) { throw; } Assert.IsInstanceOfType(scenario.ExpectedExceptionType, ex); } AssertHelper.AreCollectionSame(scenario.ExpectedElements, list); }
public static IMyList <T> Where <T>(this IMyList <T> theList, Func <T, bool> predicate) { Func <IMyList <T> > whereTailFunc = null; whereTailFunc = () => { IMyList <T> result = null; if (theList.Tail == null) { result = new MyList <T>(default(T), null); } if (predicate(theList.Head)) { result = new MyList <T>(theList.Head, whereTailFunc); } theList = theList.Tail; if (result == null) { result = whereTailFunc(); } return(result); }; return(whereTailFunc()); }
static void checkCollection(IMyList collection, int count) { middleObject middleObject = new middleObject(); Console.WriteLine("Check collection - " + collection.GetName()); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); collection.AddEnd(count); stopWatch.Stop(); Console.WriteLine("Time spent on the addition {0} objects to the end - {1} ms", count, stopWatch.ElapsedMilliseconds); stopWatch.Restart(); collection.AddBegin(count); stopWatch.Stop(); Console.WriteLine("Time spent on the addition {0} objects to the top - {1} ms", count, stopWatch.ElapsedMilliseconds); stopWatch.Restart(); collection.AddMiddle(count); stopWatch.Stop(); Console.WriteLine("Time spent on the addition {0} objects to the middle - {1} ms", count, stopWatch.ElapsedMilliseconds); stopWatch.Restart(); collection.DeleteEnd(count); stopWatch.Stop(); Console.WriteLine("Time spent on the removal {0} objects of the end of Collection- {1} ms", count, stopWatch.ElapsedMilliseconds); stopWatch.Restart(); collection.DeleteMiddle(count); stopWatch.Stop(); Console.WriteLine("Time spent on the removal {0} objects of the middle of collection- {1} ms", count, stopWatch.ElapsedMilliseconds); stopWatch.Restart(); collection.DeleteBegin(count); stopWatch.Stop(); Console.WriteLine("Time spent on the removal {0} objects of the top of collection- {1} ms", count, stopWatch.ElapsedMilliseconds); stopWatch.Restart(); collection.GetMiddleElem(count, middleObject); stopWatch.Stop(); Console.WriteLine("Time spent on the getting {0} objects from the middle of collection- {1} ms", count, stopWatch.ElapsedMilliseconds); Console.WriteLine("==============================================================================================="); }
public void createProgram(IMyStack<IMyStatement> mExecutionStack, IMyDictionary<String, int> myDictionary, IMyList<String> mOutput, IHeap<int, int> heap, IMyStatement mInitialProgram) { programStateList.Add(new ProgramState(mExecutionStack, myDictionary, mOutput, heap, mInitialProgram)); }
public CommandInterpreter() { this.myList = new MyList<string>(); }
public void setOutput(IMyList<String> output) { this.output = output; }
public MyHashTableEnumerator(IMyList <IEnumerator <T> > enumerators) { this.enumerators = enumerators; this.Reset(); }