Example #1
0
        /// <summary>
        /// Performs the grouping operation for specified items.
        /// </summary>
        /// <param name="items">The items.</param>
        /// <param name="level">The level.</param>
        /// <param name="parent">The parent.</param>
        /// <returns></returns>
        public virtual GroupCollection <T> Perform(IReadOnlyCollection <T> items, int level, Group <T> parent)
        {
            GroupDescriptorCollection groupDescriptors = this.index.CollectionView.GroupDescriptors;

            if (level >= groupDescriptors.Count)
            {
                return(GroupCollection <T> .Empty);
            }
            IGroupFactory <T> groupFactory = this.index.CollectionView.GroupFactory;

            GroupCollection <T> cache = GetCachedGroups(items, level);

            if (!IsValid(level, groupDescriptors))
            {
                if (cache != null)
                {
                    cache.Dispose();
                }
                return(this.index.CollectionView.GroupFactory.CreateCollection(GroupBuilder <T> .Empty));
            }

            IComparer <Group <T> > newComparer = Activator.CreateInstance(this.Comparer.GetType()) as IComparer <Group <T> >;

            if (newComparer == null)
            {
                newComparer = this.Comparer;
            }
            if (newComparer is GroupComparer <T> )
            {
                ((GroupComparer <T>)newComparer).Directions = this.GetComparerDirections(level);
            }

            AvlTree <Group <T> > groupList = new AvlTree <Group <T> >(newComparer);
            AvlTree <Group <T> > list      = new AvlTree <Group <T> >(new GroupComparer <T>(this.GetComparerDirections(level)));

            foreach (T item in items)
            {
                object    key = this.GroupPredicate(item, level);
                Group <T> group, newGroup = new DataItemGroup <T>(key);
                group = list.Find(newGroup); groupList.Find(newGroup);
                if (group == null)
                {
                    group = GroupBuilder <T> .GetCachedGroup(cache, newGroup);

                    if (group == null)
                    {
                        group = groupFactory.CreateGroup(key, parent);
                        DataItemGroup <T> dataGroup = group as DataItemGroup <T>;
                        dataGroup.GroupBuilder = this;
                    }

                    list.Add(group);// groupList.Add(group);
                }

                group.Items.Add(item);
            }

            for (int i = 0; i < list.Count; i++)
            {
                groupList.Add(list[i]);
            }

            if (cache != null)
            {
                cache.Dispose();
            }

            return(groupFactory.CreateCollection(groupList));
        }