public async Task <bool> MoveNext(CancellationToken cancellationToken)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    if (_lookupEnumerator == null)
                    {
                        _lookupEnumerator
                            = (await _groupByAsyncEnumerable._source
                               .ToLookup(
                                   _groupByAsyncEnumerable._keySelector,
                                   e => e,
                                   _comparer,
                                   cancellationToken)).GetEnumerator();

                        _hasNext = _lookupEnumerator.MoveNext();
                    }

                    if (_hasNext)
                    {
                        var grouping = new Grouping <TKey, TElement>(_lookupEnumerator.Current.Key);

                        foreach (var item in _lookupEnumerator.Current)
                        {
                            grouping.Add(await _groupByAsyncEnumerable._elementSelector(item, cancellationToken));
                        }

                        Current = grouping;

                        _hasNext = _lookupEnumerator.MoveNext();

                        return(true);
                    }

                    return(false);
                }
 void CharacteristicDiscovered(object sender, CharacteristicsDiscoveredEventArgs e, Grouping <IService, ICharacteristic> grouping)
 {
     foreach (var characteristic in e.Characteristics)
     {
         grouping.Add(characteristic);
     }
 }
Example #3
0
        public static List <IGrouping <T, T> > GroupWhen <T>(this IEnumerable <T> collection, Func <T, bool> isGroupKey, bool includeKeyInGroup = false, bool initialGroup = false)
        {
            List <IGrouping <T, T> > result = new List <IGrouping <T, T> >();
            Grouping <T, T>?         group  = null;

            foreach (var item in collection)
            {
                if (isGroupKey(item))
                {
                    group = new Grouping <T, T>(item);
                    if (includeKeyInGroup)
                    {
                        group.Add(item);
                    }
                    result.Add(group);
                }
                else
                {
                    if (group == null)
                    {
                        if (!initialGroup)
                        {
                            throw new InvalidOperationException("Parameter initialGroup is false");
                        }

                        group = new Grouping <T, T>(default(T) !);
                        result.Add(group);
                    }

                    group.Add(item);
                }
            }

            return(result);
        }
        public IGrouping <IUnitFacade> CreateGrouping(IEnumerable members)
        {
            var units = members.ToUnitFacades();

            if (this.useLINQ)
            {
                //A somewhat cleaner solution, albeit due to the lack of covariance in .Net 3.5 we have to explicitly cast at the end.
                var groups = from u in units
                             group u by u.attributes into grps
                             select new OnePathForAllGroup(grps) as TransientGroup <IUnitFacade>;

                return(new Grouping <IUnitFacade>(groups));
            }
            else
            {
                var grpDict = new Dictionary <AttributeMask, OnePathForAllGroup>();

                var grouping = new Grouping <IUnitFacade>(1);
                foreach (var unit in units)
                {
                    OnePathForAllGroup grp;
                    if (!grpDict.TryGetValue(unit.attributes, out grp))
                    {
                        grp = new OnePathForAllGroup(1);
                        grpDict[unit.attributes] = grp;
                        grouping.Add(grp);
                    }

                    grp.Add(unit);
                }

                return(grouping);
            }
        }
Example #5
0
        private IEnumerable <AggregationResult> Add(TKey key, TElement element)
        {
            if (Equals(key, _defaultKey))
            {
                if (_defaultGroup == null)
                {
                    _defaultGroup = new Grouping(key);
                    _defaultGroup.Add(element);
                    return(new[] { AggregationResult.Added(_defaultGroup) });
                }
                _defaultGroup.Add(element);
                return(new[] { AggregationResult.Modified(_defaultGroup) });
            }

            Grouping group;

            if (!_groups.TryGetValue(key, out group))
            {
                group        = new Grouping(key);
                _groups[key] = group;

                group.Add(element);
                return(new[] { AggregationResult.Added(group) });
            }

            group.Add(element);
            return(new[] { AggregationResult.Modified(group) });
        }
Example #6
0
        public static IEnumerable <IGrouping <K, T> > GroupWhenChange <T, K>(this IEnumerable <T> collection, Func <T, K> getGroupKey)
        {
            Grouping <K, T> current = null;

            foreach (var item in collection)
            {
                if (current == null)
                {
                    current = new Grouping <K, T>(getGroupKey(item));
                    current.Add(item);
                }
                else if (current.Key.Equals(getGroupKey(item)))
                {
                    current.Add(item);
                }
                else
                {
                    yield return(current);

                    current = new Grouping <K, T>(getGroupKey(item));
                    current.Add(item);
                }
            }

            if (current != null)
            {
                yield return(current);
            }
        }
Example #7
0
        private AggregationResult Add(IFact fact, TKey key, TElement element)
        {
            if (!_groups.TryGetValue(key, out var group))
            {
                group        = new Grouping(key);
                _groups[key] = group;

                group.Add(fact, element);
                return(AggregationResult.Added(group));
            }

            group.Add(fact, element);
            group.Key = key;
            return(AggregationResult.Modified(group));
        }
        private AggregationResult Add(TKey key, TElement element)
        {
            Grouping group;

            if (!_groups.TryGetValue(key, out group))
            {
                group        = new Grouping(key);
                _groups[key] = group;

                group.Add(element);
                return(AggregationResult.Added(group));
            }

            group.Add(element);
            return(AggregationResult.Modified(group));
        }
Example #9
0
    public static List <IGrouping <T, T> > GroupWhen <T>(this IEnumerable <T> collection, Func <T, bool> isGroupKey, BeforeFirstKey beforeFirstKey = BeforeFirstKey.Throw, bool includeKeyInGroup = false)
    {
        List <IGrouping <T, T> > result = new List <IGrouping <T, T> >();
        Grouping <T, T>?         group  = null;

        foreach (var item in collection)
        {
            if (isGroupKey(item))
            {
                group = new Grouping <T, T>(item);
                if (includeKeyInGroup)
                {
                    group.Add(item);
                }
                result.Add(group);
            }
            else
            {
                if (group == null)
                {
                    switch (beforeFirstKey)
                    {
                    case BeforeFirstKey.Throw:
                        throw new InvalidOperationException("First element should be a group key, or change the value in for beforeFirstKey");

                    case BeforeFirstKey.Skip:
                        break;

                    case BeforeFirstKey.DefaultGroup:
                        group = new Grouping <T, T>(default !);
                        result.Add(group);
                        group.Add(item);
                        break;

                    default:
                        throw new UnexpectedValueException(beforeFirstKey);
                    }
                }
                else
                {
                    group.Add(item);
                }
            }
        }

        return(result);
    }
        private Ensemble GenerateGroupings(Ensemble eIn, List <Books> books)
        {
            if (books.Count == 0)
            {
                return(eIn);
            }
            var e          = new Ensemble();
            var book       = books[0];
            var otherBooks = books.Skip(1).ToList();

            if (eIn.Count == 0)
            {
                var b = new Bundle();
                b.Add(book);
                var gAlone = new Grouping();
                gAlone.Add(b);
                e.Add(gAlone);
                return(GenerateGroupings(e, otherBooks));
            }
            else
            {
                foreach (var grouping in eIn)
                {
                    var nbBundles = grouping.Count();
                    for (var iBundle = 0; iBundle < nbBundles; iBundle++)
                    {
                        var bundle = grouping[iBundle];
                        if (bundle.CanAccept(book))
                        {
                            var enrichedGrouping = grouping.Clone();
                            var clonedBundle     = enrichedGrouping[iBundle];
                            clonedBundle.Add(book);
                            e.Add(enrichedGrouping);
                        }
                    }
                    var clonedGrouping = grouping.Clone();
                    var b = new Bundle();
                    b.Add(book);
                    clonedGrouping.Add(b);
                    e.Add(clonedGrouping);
                }
                //Console.WriteLine(e.Count);
                eIn.Clear();
                return(GenerateGroupings(e, otherBooks));
            }
        }
Example #11
0
                public async Task <bool> MoveNext(CancellationToken cancellationToken)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    if (_sourceEnumerator == null)
                    {
                        _sourceEnumerator = _groupByAsyncEnumerable._source.GetEnumerator();
                        _hasNext          = await _sourceEnumerator.MoveNext(cancellationToken);
                    }

                    if (_hasNext)
                    {
                        var currentKey = _groupByAsyncEnumerable._keySelector(_sourceEnumerator.Current);
                        var element    = _groupByAsyncEnumerable._elementSelector(_sourceEnumerator.Current);
                        var grouping   = new Grouping <TKey, TElement>(currentKey)
                        {
                            element
                        };

                        while (true)
                        {
                            _hasNext = await _sourceEnumerator.MoveNext(cancellationToken);

                            if (!_hasNext)
                            {
                                break;
                            }

                            if (!_comparer.Equals(
                                    currentKey,
                                    _groupByAsyncEnumerable._keySelector(_sourceEnumerator.Current)))
                            {
                                break;
                            }

                            grouping.Add(_groupByAsyncEnumerable._elementSelector(_sourceEnumerator.Current));
                        }

                        Current = grouping;

                        return(true);
                    }

                    return(false);
                }
Example #12
0
        private void UpdateItem(UpdateModel updateModel)
        {
            Grouping <string, ItemPreview> currentItems = null;

            if (Items != null)
            {
                currentItems = Items.Where(k => k.Key == updateModel.ItemPreview.ItemType.ToPluralString())
                               .FirstOrDefault();
            }
            else if (Items is null)
            {
                Items = new ObservableCollection <Grouping <string, ItemPreview> >();
            }
            if (currentItems is null)//that means no items of that type are in list
            {
                //add new item
                DisplayMsg("", false);//make sure there is no msg
                Items.Add(new Grouping <string, ItemPreview>(updateModel.ItemPreview.ItemType.ToPluralString(), new List <ItemPreview>()
                {
                    updateModel.ItemPreview
                }));
            }
            else if (updateModel.UpdateType == TypeOfUpdates.Create)
            {
                currentItems.Add(updateModel.ItemPreview);
            }
            else if (updateModel.UpdateType == TypeOfUpdates.Modify)
            {//get current item that will be modified later
                var itemToBeModified = currentItems.FirstOrDefault(s => s.Id == updateModel.ItemPreview.Id);
                if (itemToBeModified != null)
                {
                    int index = currentItems.IndexOf(itemToBeModified);//get index of the item from the list
                    currentItems.SetNewItem(index, updateModel.ItemPreview);
                }
            }
            else if (updateModel.UpdateType == TypeOfUpdates.Delete)
            {
                Delete(updateModel.ItemPreview.Id);
            }
            if (IsUwp)
            {
                Items = UpdateItems(Items);//update the list with itself(bug: (when updating an item it will not be displayed so i updated the current list))
            }
        }
        // ReSharper disable once InconsistentNaming
        private static IEnumerable <IGrouping <TKey, TElement> > _GroupBy <TSource, TKey, TElement>(
            IEnumerable <TSource> source,
            Func <TSource, TKey> keySelector,
            Func <TSource, TElement> elementSelector)
        {
            using (var sourceEnumerator = source.GetEnumerator())
            {
                var comparer = EqualityComparer <TKey> .Default;
                var hasNext  = sourceEnumerator.MoveNext();

                while (hasNext)
                {
                    var currentKey = keySelector(sourceEnumerator.Current);
                    var element    = elementSelector(sourceEnumerator.Current);
                    var grouping   = new Grouping <TKey, TElement>(currentKey)
                    {
                        element
                    };

                    while (true)
                    {
                        hasNext = sourceEnumerator.MoveNext();

                        if (!hasNext)
                        {
                            break;
                        }

                        if (!comparer.Equals(currentKey, keySelector(sourceEnumerator.Current)))
                        {
                            break;
                        }

                        grouping.Add(elementSelector(sourceEnumerator.Current));
                    }

                    yield return(grouping);
                }
            }
        }
		void CharacteristicDiscovered (object sender, CharacteristicDiscoveredEventArgs e, Grouping<IService, ICharacteristic> grouping)
		{
			grouping.Add(e.Characteristic);
		}
Example #15
0
            public override void OnNext(TSource value)
            {
                TKey key;

                try
                {
                    key = _keySelector(value);
                }
                catch (Exception exception)
                {
                    Error(exception);
                    return;
                }

                var fireNewMapEntry = false;
                Subject <TElement>?writer;

                try
                {
                    //
                    // Note: The box instruction in the IL will be erased by the JIT in case T is
                    //       a value type. In fact, the whole if block will go away and we'll end
                    //       up with nothing but the TryGetValue check below.
                    //
                    //       // var fireNewMapEntry = false;
                    //       C:\Projects\Rx\Rx\Experimental\Main\Source\Rx\System.Reactive.Linq\Reactive\Linq\Observable\GroupBy.cs @ 67:
                    //       000007fb`6d544b80 48c7452800000000 mov     qword ptr [rbp+28h],0
                    //
                    //       // var writer = default(ISubject<TElement>);
                    //       C:\Projects\Rx\Rx\Experimental\Main\Source\Rx\System.Reactive.Linq\Reactive\Linq\Observable\GroupBy.cs @ 66:
                    //       000007fb`6d544b88 c6453400        mov     byte ptr [rbp+34h],0
                    //
                    //       // if (!_map.TryGetValue(key, out writer))
                    //       C:\Projects\Rx\Rx\Experimental\Main\Source\Rx\System.Reactive.Linq\Reactive\Linq\Observable\GroupBy.cs @ 86:
                    //       000007fb`6d544b8c 488b4560        mov     rax,qword ptr [rbp+60h]
                    //       ...
                    //
                    if (key == null)
                    {
                        if (_null == null)
                        {
                            _null           = new Subject <TElement>();
                            fireNewMapEntry = true;
                        }

                        writer = _null;
                    }
                    else
                    {
                        if (!_map.TryGetValue(key, out writer))
                        {
                            writer = new Subject <TElement>();
                            _map.Add(key, writer);
                            fireNewMapEntry = true;
                        }
                    }
                }
                catch (Exception exception)
                {
                    Error(exception);
                    return;
                }

                if (fireNewMapEntry)
                {
                    var group = new GroupedObservable <TKey, TElement>(key, writer, _refCountDisposable !); // NB: _refCountDisposable is set in Run.
                    ForwardOnNext(group);
                }

                TElement element;

                try
                {
                    element = _elementSelector(value);
                }
                catch (Exception exception)
                {
                    Error(exception);
                    return;
                }

                writer.OnNext(element);
            }
Example #16
0
 void CharacteristicDiscovered(object sender, CharacteristicDiscoveredEventArgs e, Grouping <IService, ICharacteristic> grouping)
 {
     grouping.Add(e.Characteristic);
 }
Example #17
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        /// <param name="text"></param>
        /// <remarks>
        /// When parsing text, it is always assumed as the start that the rule is an
        /// alternating group of groups, and when a grouping token is encountered the same
        /// is assumed.  The simplify method is called at the end
        /// </remarks>
        public void AddRule(string name, string text)
        {
            int      matchedGroup;
            Grouping currentAlternation = new Grouping(Alternation.Instance)
            {
                ProductionName = name
            };
            Grouping currentGroup = new Grouping(Concatenation.Instance);

            currentAlternation.Add(currentGroup);

            foreach (Match match in ruleTokenizer.Matches(text))
            {
                matchedGroup = match.Groups.Cast <System.Text.RegularExpressions.Group>()
                               .Select((g, i) => new { Group = g, Index = i })
                               .FirstOrDefault(g => g.Group.Success && g.Index > 0).Index;

                switch (matchedGroup)
                {
                case 1:
                    currentGroup.Add(new Referrer(match.Value));
                    break;

                case 2:
                    currentGroup.Add(Literal.FromQuoted(match.Value));
                    break;

                case 3:
                    switch (match.Value)
                    {
                    case "(":
                        currentAlternation = new Grouping(Alternation.Instance);
                        currentGroup.Add(currentAlternation);
                        currentGroup = new Grouping(Concatenation.Instance);
                        currentAlternation.Add(currentGroup);
                        break;

                    case ")":
                        if (currentAlternation.IsException)
                        {
                            currentGroup       = currentAlternation.Parent;
                            currentAlternation = currentGroup.Parent;
                        }
                        currentGroup       = currentAlternation.Parent;
                        currentAlternation = currentGroup.Parent;
                        break;

                    case "|":
                        if (currentAlternation.IsException)
                        {
                            currentGroup       = currentAlternation.Parent;
                            currentAlternation = currentGroup.Parent;
                        }
                        currentGroup = new Grouping(Concatenation.Instance);
                        currentAlternation.Add(currentGroup);
                        break;

                    case "-":
                        currentAlternation = new Grouping(Alternation.Instance)
                        {
                            IsException = true
                        };
                        currentGroup.Add(currentAlternation);
                        currentGroup = new Grouping(Concatenation.Instance);
                        currentAlternation.Add(currentGroup);
                        break;
                    }
                    break;

                case 4:
                    currentGroup.Last().Quantifier = match.Value;
                    break;

                case 6:
                    throw new Exception(String.Format("Invalid token \"{0}\" at {1}", match.Value,
                                                      match.Index));
                }
            }

            Add(Simplify(currentAlternation));
        }