Exemplo n.º 1
0
        public IEnumerable <T> Intersect(IEnumerable <T> array)
        {
            var stackListQueue = new StackListQueue <T>();
            var arrays         = new StackListQueue <StackListQueue <T> >(this)
            {
                new StackListQueue <T>(array)
            };

            foreach (var arr in arrays.Where(arr => !IsSorted(arr)))
            {
                arr.Sort(Comparer);
            }
            List <int> indexes = arrays.Select((a, i) => 0).ToList();
            List <int> counts  = arrays.Select((a, i) => a.Count).ToList();

            while (Enumerable.Range(0, 2).All(i => indexes[i] < counts[i]))
            {
                int value = Comparer.Compare(arrays[0][indexes[0]], arrays[1][indexes[1]]);
                if (value == 0)
                {
                    stackListQueue.Add(arrays[0][indexes[0]++]);
                    indexes[1]++;
                }
                else if (value < 0)
                {
                    indexes[0]++;
                }
                else
                {
                    indexes[1]++;
                }
            }
            return(stackListQueue);
        }
Exemplo n.º 2
0
        public new IEnumerable <T> Distinct()
        {
            var stackListQueue = new StackListQueue <T>();

            if (Count == 0)
            {
                return(stackListQueue);
            }
            var arrays = new StackListQueue <StackListQueue <T> >(this);

            foreach (var arr in arrays.Where(arr => !IsSorted(arr)))
            {
                arr.Sort(Comparer);
            }
            List <int> indexes = arrays.Select((a, i) => 0).ToList();
            List <int> counts  = arrays.Select((a, i) => a.Count).ToList();

            while (Enumerable.Range(0, 1).All(i => indexes[i] < counts[i] - 1))
            {
                int value = Comparer.Compare(arrays[0][indexes[0]], arrays[0][indexes[0] + 1]);
                if (value == 0)
                {
                    indexes[0]++;
                }
                else
                {
                    stackListQueue.Add(arrays[0][indexes[0]++]);
                }
            }
            stackListQueue.Add(arrays[0][counts[0] - 1]);
            return(stackListQueue);
        }
Exemplo n.º 3
0
        public virtual bool IsSorted(StackListQueue <T> collection)
        {
            List <T> list = collection.ToList();

            return(list.Count < 2 ||
                   Enumerable.Range(0, list.Count - 1)
                   .All(i => Comparer.Compare(list[i], list[i + 1]) <= 0));
        }
Exemplo n.º 4
0
        public void RemoveRange(IEnumerable <T> value)
        {
            if (!IsSorted(this))
            {
                Sort(Comparer);
            }
            var list = new StackListQueue <int>(value.Select(v => BinarySearch(v, Comparer))
                                                .Where(index => index >= 0));

            list.Sort();
            list.Reverse();
            foreach (int index in list)
            {
                RemoveAt(index);
            }
        }