private PivotedProperties(ItemProperties itemProperties, IEnumerable <SeriesGroup> seriesGroups) { ItemProperties = itemProperties; SeriesGroups = ImmutableList.ValueOf(seriesGroups); if (!SeriesGroups.SelectMany(group => group.SeriesList) .All(series => ReferenceEquals(series.ItemProperties, itemProperties))) { throw new ArgumentException(@"Wrong set of properties", nameof(seriesGroups)); } }
/// <summary> /// Reorder the ItemProperties collection so that the ungrouped properties come first, /// followed by the grouped properties. /// If a group contains multiple series, the properties from those series are interleaved /// with each other. /// </summary> /// <returns></returns> public PivotedProperties ReorderItemProperties() { var groupedPropertyIndexes = SeriesGroups .SelectMany(group => group.SeriesList.SelectMany(series => series.PropertyIndexes)).ToHashSet(); var newOrder = new List <int>(); newOrder.AddRange(Enumerable.Range(0, ItemProperties.Count).Where(i => !groupedPropertyIndexes.Contains(i))); newOrder.AddRange(SeriesGroups.SelectMany(group => Enumerable.Range(0, group.PivotKeys.Count) .SelectMany(i => group.SeriesList.Select(series => series.PropertyIndexes[i])))); var newNumbering = new int[newOrder.Count]; for (int i = 0; i < newOrder.Count; i++) { newNumbering[newOrder[i]] = i; } var newItemProperties = new ItemProperties(newOrder.Select(i => ItemProperties[i])); var result = new PivotedProperties(newItemProperties, SeriesGroups.Select(group => group.RenumberProperties(newItemProperties, newNumbering))); #if DEBUG Debug.Assert(ItemProperties.ToHashSet().SetEquals(result.ItemProperties.ToHashSet())); Debug.Assert(SeriesGroups.Count == result.SeriesGroups.Count); for (int iGroup = 0; iGroup < SeriesGroups.Count; iGroup++) { Debug.Assert(SeriesGroups[iGroup].SeriesList.Count == result.SeriesGroups[iGroup].SeriesList.Count); Debug.Assert(SeriesGroups[iGroup].PivotKeys.SequenceEqual(result.SeriesGroups[iGroup].PivotKeys)); for (int iSeries = 0; iSeries < SeriesGroups[iGroup].SeriesList.Count; iSeries++) { var resultSeries = result.SeriesGroups[iGroup].SeriesList[iSeries]; Debug.Assert(resultSeries.PropertyIndexes.OrderBy(i => i).SequenceEqual(resultSeries.PropertyIndexes)); var series = SeriesGroups[iGroup].SeriesList[iSeries]; Debug.Assert(series.PropertyIndexes.Select(i => ItemProperties[i]) .SequenceEqual(resultSeries.PropertyIndexes.Select(i => result.ItemProperties[i]))); } } #endif return(result); }