Esempio n. 1
0
        private static IEnumerable <T> AscendantsCore <T, K>(IEnumerable <T> source, Func <T, K> keySelector, Func <T, K> parentKeySelector, Equality <K> equality, K startKey, bool ascendantsOnly = false, int?levelsCount = null)
            where T : class
            where K : class
        {
            PwrList <T> list = new PwrList <T>();
            T           item = source.SingleOrDefault(t => equality(keySelector(t), startKey));

            if (item != null && ascendantsOnly)
            {
                K parentKey = parentKeySelector(item);
                item = parentKey != null?source.SingleOrDefault(t => equality(keySelector(t), parentKey)) : null;
            }
            if (item != null)
            {
                list.Add(item);
            }
            for (int passCount = 1; (!levelsCount.HasValue || levelsCount.Value > passCount) && item != null; passCount++)
            {
                K parentKey = parentKeySelector(item);
                item = parentKey != null?source.SingleOrDefault(t => equality(keySelector(t), parentKey)) : null;

                if (item != null)
                {
                    list.Add(item);
                }
            }
            return(list.AsEnumerable());
        }
Esempio n. 2
0
        public PwrList <T> DequeRangeBack(K key, int count)
        {
            if (count < 0 || count > InnerList.Count)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            var list  = new PwrList <T>(count);
            var index = InnerList.BinarySearch(t => KeyComparison(KeySelector(t), key), SortingOption.Last);

            while (index >= 0 && index < InnerList.Count && KeyComparison(KeySelector(InnerList[index]), key) == 0 && count-- > 0)
            {
                list.Add(InnerList[index]);
                InnerList.RemoveAt(index--);
            }
            return(list);
        }