using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ChamChat.Models { public class Loop { /// /// Loop /// /// Number of times to perform the steps in the loop /// First step in loop (index in Profile.Steps) /// Last step in loop (index in Profile.Steps) public Loop(Int32 N, Int32 First, Int32 Last) { _N = N; _First = First; _Last = Last; } private Int32 _N = 0; public Int32 N { get { return _N; } set { _N = value; } } private Int32 _First = 0; public Int32 First { get { return _First; } set { _First = value; } } private Int32 _Last = 0; public Int32 Last { get { return _Last; } set { _Last = value; } } public Boolean Equals(Loop l) { return l.ToString() == this.ToString(); } public override string ToString() { String s = string.Format("{2}x[s{0}..s{1}]", _First, _Last, _N); return s; } public Loop(String XML) { string fp = "<" + this.GetType().Namespace + "." + this.GetType().Name + ">"; if (!XML.Contains(fp)) throw new Exception(fp + " tag not found!"); try { _N = Int32.Parse(XML.GetTags("_N")); _First = Int32.Parse(XML.GetTags("_First")); _Last = Int32.Parse(XML.GetTags("_Last")); } catch { throw new Exception(fp +": could not create instance from input string!"); } } public String XML { get { string xml = ""; xml += "<"+this.GetType().Namespace + "." + this.GetType().Name + ">"; xml += String.Format("<_N>{0:0}", _N); xml += String.Format("<_First>{0:0}", _First); xml += String.Format("<_Last>{0:0}", _Last); xml += ""; return xml; } } } public static class ExtensionsToLoop { public static List OrderByLastAsc(this List list) { List result = new List(); List copy = new List(); if (list == null) return result; if (list.Count < 2) return list; foreach (Loop l in list) copy.Add(l); while (copy.Count > 0) { Loop lowest = copy[0]; foreach (Loop l in copy) { if (l.Last == lowest.Last) if (l.First > lowest.First) lowest = l; if (l.Last < lowest.Last) lowest = l; } result.Add(lowest); copy.Remove(lowest); } return result; } } }