public static object SolverIDictsGroupBy(IList args) { var rows = (IList <IIndexedDict>)args[0]; var grpBy = (IList)args[1]; if (rows.Count == 0) { return(rows); // return empty list } var row0 = (IIndexedDict)rows[0]; var k2n = new Dictionary <string, int>(row0.Key2Ndx, StringComparer.OrdinalIgnoreCase); var keyNdxs = grpBy.Cast <object>().Select(o => k2n[Convert.ToString(o)]).ToArray(); { var lst = rows.ToArray(); IIndexedDictExtensions.SortByKeyTimed(lst, keyNdxs); rows = lst; } int n = row0.ValuesList.Length; var valNdxs = Enumerable.Range(0, n).Except(keyNdxs).ToArray(); var results = new OPs.ListOf <IIndexedDict>(); foreach (IIndexedDict row in rows) { var vals = row.ValuesList; int i = IIndexedDictExtensions.BinarySearch(results, keyNdxs, vals); if (i < 0) { var grp = new object[n]; foreach (int j in keyNdxs) { var to = vals[j] as ITimedObject; grp[j] = (to == null) ? vals[j] : to.Object; } foreach (int j in valNdxs) { var lst = new List <object>(); lst.Add(vals[j]); grp[j] = lst; } results.Insert(~i, ValuesDictionary.New(grp, k2n)); } else { var grp = results[i].ValuesList; foreach (int j in valNdxs) { var lst = (IList)grp[j]; lst.Add(vals[j]); } } } return(results); }
public static object IDictsGroupByFirstOrderedField([CanBeVector] object arg) { var rows = (IList)arg; if (rows.Count == 0) { return(rows); // return empty list } var results = new OPs.ListOf <IIndexedDict>(); var keyNdxs = new int[] { 0 }; foreach (IIndexedDict row in rows) { var vals = row.ValuesList; int n = vals.Length; int i = IIndexedDictExtensions.BinarySearch(results, keyNdxs, vals); if (i < 0) { var grp = new object[n]; { var to = vals[0] as ITimedObject; grp[0] = (to == null) ? vals[0] : to.Object; } for (int j = 1; j < n; j++) { var lst = new List <object>(); lst.Add(vals[j]); grp[j] = lst; } results.Insert(~i, ValuesDictionary.New(grp, row.Key2Ndx)); } else { var grp = results[i].ValuesList; for (int j = 1; j < n; j++) { var lst = (IList)grp[j]; lst.Add(vals[j]); } } } return(results); }