public void SortHuge() { int[] array = GetRandomArray(5000, 10000); LinearList <int> list = new LinearList <int>(array); LinkedList <int> linkedList = new LinkedList <int>(array); Stopwatch sw1 = Stopwatch.StartNew(); list.Sort((i1, i2) => i1 < i2 ? -1 : 0); sw1.Stop(); Console.WriteLine($"Duration linear list: {sw1.ElapsedMilliseconds}ms"); Stopwatch sw2 = Stopwatch.StartNew(); var llSorted = linkedList.OrderBy(i => i, Comparer <int> .Default).ToArray(); sw2.Stop(); Console.WriteLine($"Duration linked list: {sw2.ElapsedMilliseconds}ms"); Stopwatch sw3 = Stopwatch.StartNew(); Array.Sort(array); sw3.Stop(); Console.WriteLine($"Duration array: {sw3.ElapsedMilliseconds}ms"); CollectionAssert.AreEqual(array, list.ToArray()); CollectionAssert.AreEqual(array, llSorted); }
public void SortWithZero() { LinearList <int> list = new LinearList <int>(new[] { 2, 0, 1 }); list.Sort((i1, i2) => i1.CompareTo(i2)); int[] sortedList = list.ToArray(); CollectionAssert.AreEqual(new[] { 0, 1, 2 }, sortedList); }
public void Sort() { LinearList <int> list = new LinearList <int>(new[] { 2, 5, 1, 7, 3, 4, 6 }); list.Sort((i1, i2) => i1.CompareTo(i2)); int[] sortedList = list.ToArray(); CollectionAssert.AreEqual(new[] { 1, 2, 3, 4, 5, 6, 7 }, sortedList); }
public void SortWithProperty() { IndexObject[] objetSet = new IndexObject[] { new IndexObject { Id = "eins", Index = 2 }, new IndexObject { Id = "zwei", Index = 0 }, new IndexObject { Id = "drei", Index = 1 } }; LinearList <IndexObject> list = new LinearList <IndexObject>(objetSet); list.Sort(item => item.Index); IndexObject[] sortedSet = list.ToArray(); CollectionAssert.AreEqual(new IndexObject[] { objetSet[1], objetSet[2], objetSet[0] }, sortedSet); }
/// <summary> /// Печать стека /// </summary> /// <returns>строковое представление стека</returns> public string Print() { if ((linlist != null) && (linlist.Length > 0)) { int count = this.Count; string result = ""; LinearList <T> lnold = new LinearList <T>(); for (int i = 0; linlist.Length > 0; i++) { result = result + this.Peek().ToString() + " "; lnold.AddFront(this.Pop()); } linlist = lnold; Count = count; return(result); } else { throw new InvalidOperationException("Стек пуст"); } }
/// <summary> /// Проверяет наличие элемента в стеке /// </summary> /// <param name="value">значение для поиска</param> /// <returns>true - если элемент найден. false - если элемент не найден</returns> public bool Contains(T value) { if ((linlist != null) && (linlist.Length > 0)) { int count = this.Count; bool result = false; LinearList <T> lnold = new LinearList <T>(); for (int i = 0; linlist.Length > 0; i++) { if (value.ToString() == this.Peek().ToString()) { result = true; } lnold.AddFront(this.Pop()); } linlist = lnold; Count = count; return(result); } else { throw new InvalidOperationException("Стек пуст"); } }
static void Main(string[] args) { RunConfig runConfig; if (!TryParseArguments(args, out runConfig)) { Usage(); return; } var code = File.ReadAllText(args[0]); var pageState = new GraphicsState(); var stack = new LinearList <IOperation>(); var execStack = new LinearList <IOperation>(); var dictStack = new LinearList <IDictionary <string, IOperation> >(); var system = new Dictionary <string, IOperation>(); system["add"] = new Add(); system["sub"] = new Substract(); system["mul"] = new Multiplicate(); system["div"] = new Divide(); system["mod"] = new Mod(); system["def"] = new Define(); system["for"] = new For(); system["dup"] = new Duplicate(); system["index"] = new Index(); system["pop"] = new Pop(); system["exch"] = new Exchange(); system["repeat"] = new Repeat(); system["array"] = new EmptyArray(); system["astore"] = new LoadArray(); system["rand"] = new Rand(); system["cvi"] = new ConvertToInteger(); system["copy"] = new Copy(); system["roll"] = new Roll(); system["get"] = new ArrayGet(); system["put"] = new ArrayPut(); system["ne"] = new NotEqual(); system["eq"] = new Equal(); system["or"] = new Or(); system["ifelse"] = new IfElse(); system["if"] = new If(); system["neg"] = new Neg(); system["not"] = new Not(); system["sqrt"] = new Sqrt(); system["lt"] = new LessThan(); system["ge"] = new GreaterOrEqualThan(); dictStack.Push(system); var graphics = new Dictionary <string, IOperation>(); graphics["newpath"] = new NewPath(pageState); graphics["closepath"] = new ClosePath(pageState); graphics["fillpath"] = new FillPath(pageState); graphics["setgray"] = new SetGray(pageState); graphics["setrgbcolor"] = new SetRGB(pageState); graphics["setlinewidth"] = new SetLineWidth(pageState); graphics["fill"] = new FillPath(pageState); graphics["showpage"] = new ShowPage(pageState); graphics["moveto"] = new MoveTo(pageState); graphics["lineto"] = new LineTo(pageState); graphics["rlineto"] = new RelativeLineTo(pageState); graphics["gsave"] = new SaveGraphicsState(pageState); graphics["grestore"] = new RestoreGraphicsState(pageState); graphics["stroke"] = new StrokePath(pageState); graphics["curveto"] = new CurveTo(pageState); graphics["arc"] = new Arc(pageState); dictStack.Push(graphics); dictStack.Push(new Dictionary <string, IOperation>()); //execStack.Push(start); CodeParser.LoadCode(execStack, @" /findfont { pop (somefont) } def /scalefont { exch pop } def /setfont { pop } def /setlinecap { pop } def /srand { pop } def "); CodeParser.LoadCode(execStack, code); var state = new PreScriptState(stack, execStack, dictStack); while (execStack.Count > 0) { var operation = execStack.Pop(); operation.Process(state); } }
private void CalculateDaily(string code) { if (string.IsNullOrEmpty(code)) { return; } var scripts = Config.Instance.INFO.ScriptSetting.Scripts; var macdsetting = Config.Instance.INFO.MACDSetting; var rsisetting = Config.Instance.INFO.RSISetting; // Delete MACD, RSI values if ((CheckTable <DBStkMACDEntity>() && scripts.ContainsKey("DEL_MACD_BY_CD")) && (CheckTable <DBStkRSIEntity>() && scripts.ContainsKey("DEL_RSI_BY_CD"))) { var lprm_macd = accessor.CreateParameter("CODE", code); var lcmd_macd = accessor.CreateCommand(scripts["DEL_MACD_BY_CD"], new List <DbParameter>() { lprm_macd }); var lprm_rsi = accessor.CreateParameter("CODE", code); var lcmd_rsi = accessor.CreateCommand(scripts["DEL_RSI_BY_CD"], new List <DbParameter>() { lprm_rsi }); if (accessor.ExecuteSQLCommand(new List <DbCommand>() { lcmd_macd, lcmd_rsi }) < 0) { logger.Write(TYPE.ERROR, string.Format("delete MACD and RSI data failed.({0})", code)); logger.Write(TYPE.ERROR, accessor.LastError); return; } } else { return; } // Retrieve Stocks var page = new EntityPage <DBTStkDailyEntity>( new Clause("Code = {Code}").AddParam("Code", code), new Sort().Add("ID", Sort.Orientation.asc), 100, accessor); List <DBTStkDailyEntity> lst_daily = null; DateTime ldt_date = DateTime.Today; MACD macd = null; RSI rsi = null; AverageValue daily30 = null; #region Cache For MACD SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> > cache1 = null; SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> > cache2 = null; LinearList <KeyValuePair <DateTime, decimal> > list1 = null; LinearList <KeyValuePair <DateTime, decimal> > list2 = null; #endregion #region Cache For AVG SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> > cache3 = null; SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> > cache4 = null; LinearList <KeyValuePair <DateTime, decimal> > list3 = null; LinearList <KeyValuePair <DateTime, decimal> > list4 = null; #endregion Func <KeyValuePair <DateTime, decimal>, decimal> fValue = (pair) => { return(pair.Value); }; Func <KeyValuePair <DateTime, decimal>, KeyValuePair <DateTime, decimal>, LinearList <KeyValuePair <DateTime, decimal> >, bool> fSwitch1 = (v1, v2, segment) => { switch (segment.Orietion) { case 0: return(false); case 1: if (v2.Value >= v1.Value) { return(false); } break; case -1: if (v2.Value <= v1.Value) { return(false); } break; } return(true); }; Func <LinearList <KeyValuePair <DateTime, decimal> >, LinearList <KeyValuePair <DateTime, decimal> >, bool> fSwitch2 = (values, segment) => { if (segment.Orietion == 0 || segment.Orietion == values.Orietion) { return(false); } return(values.Count >= 10); }; int pageno = 0; var count = 0; while ((lst_daily = page.Retrieve(++pageno)).Count > 0) { for (int i = 0; i < lst_daily.Count; i++, count++) { try { var daily = lst_daily[i]; ldt_date = daily.Date; if (pageno == 1 && i == 0) { macd = new MACD(daily.Close); rsi = new RSI(rsisetting.N1, rsisetting.N2, rsisetting.N3, daily.Close); daily30 = new AverageValue(30, 4); daily30.Add(daily.Close); cache1 = new SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> >( new LinearList <KeyValuePair <DateTime, decimal> >[] { new LinearList <KeyValuePair <DateTime, decimal> >(fValue), new LinearList <KeyValuePair <DateTime, decimal> >(fValue) }, fSwitch1, null); cache2 = new SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> >( new LinearList <KeyValuePair <DateTime, decimal> >[] { new LinearList <KeyValuePair <DateTime, decimal> >(fValue), new LinearList <KeyValuePair <DateTime, decimal> >(fValue) }, null, fSwitch2); cache3 = new SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> >( new LinearList <KeyValuePair <DateTime, decimal> >[] { new LinearList <KeyValuePair <DateTime, decimal> >(fValue), new LinearList <KeyValuePair <DateTime, decimal> >(fValue) }, fSwitch1, null); cache4 = new SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> >( new LinearList <KeyValuePair <DateTime, decimal> >[] { new LinearList <KeyValuePair <DateTime, decimal> >(fValue), new LinearList <KeyValuePair <DateTime, decimal> >(fValue) }, null, fSwitch2); } else { macd.Add(daily.Close); rsi.Add(daily.Close); daily30.Add(daily.Close); if (cache1.Add(new KeyValuePair <DateTime, decimal>(daily.Date, macd.DEA), out list1)) { cache2.Add(list1, out list2); } if (cache3.Add(new KeyValuePair <DateTime, decimal>(daily.Date, daily30.Average), out list3)) { cache4.Add(list3, out list4); } } var lent_macd = new DBStkMACDEntity(daily.ID); lent_macd.EMA12 = macd.EMA12; lent_macd.EMA26 = macd.EMA26; lent_macd.DIFF = macd.DIFF; lent_macd.DEA = macd.DEA; lent_macd.BAR = macd.BAR; var lent_rsi = new DBStkRSIEntity(daily.ID); lent_rsi.RSI1 = rsi.RSI1; lent_rsi.RSI2 = rsi.RSI2; lent_rsi.RSI3 = rsi.RSI3; lent_rsi.RSI1MaxEma = rsi.RSI1MaxEma; lent_rsi.RSI1ABSEma = rsi.RSI1ABSEma; lent_rsi.RSI2MaxEma = rsi.RSI2MaxEma; lent_rsi.RSI2ABSEma = rsi.RSI2ABSEma; lent_rsi.RSI3MaxEma = rsi.RSI3MaxEma; lent_rsi.RSI3ABSEma = rsi.RSI3ABSEma; accessor.SaveEntity(lent_macd, lent_rsi); accessor.Commit(); } catch { } } } if (count > 120) { cache2.Add(cache1.Segment, out list2); cache4.Add(cache3.Segment, out list4); var lent_sum = new DBStkSummaryResultEntity(); lent_sum.Code = code; lent_sum.Time = DateTime.Now; lent_sum.PAVG30_ORIENT = cache4.Segment.Orietion; lent_sum.PAVG30_DAYS = cache4.Segment.Count; lent_sum.PAVG30_VALUE = cache4.Segment.Slope; lent_sum.PAVG30_Date1 = cache4.Segment[0].Key; lent_sum.PAVG30_Date2 = cache4.Segment[cache4.Segment.Count - 1].Key; lent_sum.DEA_ORIENT = cache2.Segment.Orietion; lent_sum.DEA_DAYS = cache2.Segment.Count; lent_sum.DEA_VALUE = cache2.Segment.Slope; lent_sum.DEA_Date1 = cache2.Segment[0].Key; lent_sum.DEA_Date2 = cache2.Segment[cache2.Segment.Count - 1].Key; lent_sum.RSI_Date = ldt_date; lent_sum.RSI_VALUE1 = rsi.RSI1; lent_sum.RSI_VALUE2 = rsi.RSI2; lent_sum.RSI_VALUE3 = rsi.RSI3; if (CheckTable <DBStkSummaryResultEntity>()) { accessor.SetDBAccessor2(lent_sum); lent_sum.GenerateID(); lent_sum.Save(); } } if (page != null) { page.Dispose(); } if (cache1 != null) { cache1.Dispose(); } if (cache2 != null) { cache2.Dispose(); } if (cache3 != null) { cache3.Dispose(); } if (cache4 != null) { cache4.Dispose(); } if (daily30 != null) { daily30.Dispose(); } }
/// <summary> /// Инициализирует стек значениями по умолчанию /// </summary> public MyStack() { Count = 0; linlist = new LinearList <T>(); listviev = null; }