private void AddItem(RefCountItemContainer <T> container)
        {
            RefCountItemContainer <T> other;

            if (_containers.TryGetValue(container.Item, out other))
            {
                var wasIncluded = ShouldIncludeContainer(other);
                other.Increment(container);
                var isIncluded = ShouldIncludeContainer(other);
                if (wasIncluded && !isIncluded)
                {
                    _items.Remove(other.Item);
                }
                else if (!wasIncluded && isIncluded)
                {
                    _items.Add(other.Item);
                }
            }
            else
            {
                _containers[container.Item] = container;
                if (ShouldIncludeContainer(container))
                {
                    _items.Add(container.Item);
                }
            }
        }
        private bool RemoveItem(RefCountItemContainer <T> container)
        {
            RefCountItemContainer <T> other;

            if (!_containers.TryGetValue(container.Item, out other))
            {
                return(false);
            }
            var wasIncluded = ShouldIncludeContainer(other);

            other.Decrement(container);
            var isIncluded = ShouldIncludeContainer(other);

            if (other.CountOnFirst == 0 && other.CountOnSecond == 0)
            {
                _containers.Remove(other.Item);
            }
            if (wasIncluded && !isIncluded)
            {
                _items.Remove(other.Item);
            }
            else if (!wasIncluded && isIncluded)
            {
                _items.Add(other.Item);
            }
            return(true);
        }
 /// <summary>
 /// When implemented in a derived class, determines if the specified container's item should be included in the result.
 /// </summary>
 /// <param name="container">The container.</param>
 /// <returns><c>true</c> if <paramref name="container"/> belongs in the collection; otherwise, false.</returns>
 protected abstract bool ShouldIncludeContainer(RefCountItemContainer <T> container);
Пример #4
0
 /// <summary>
 /// Determines if the specified container's item should be included in the result.
 /// </summary>
 /// <param name="container">The container.</param>
 /// <returns>
 ///   <c>true</c> if <paramref name="container" /> belongs in the collection; otherwise, false.
 /// </returns>
 protected override bool ShouldIncludeContainer(RefCountItemContainer <T> container)
 {
     return(container.CountOnFirst > 0 && container.CountOnSecond == 0);
 }
Пример #5
0
        private static void AddItem(IDictionary <T, RefCountItemContainer <T> > containers, RefCountItemContainer <T> container)
        {
            RefCountItemContainer <T> other;

            if (containers.TryGetValue(container.Item, out other))
            {
                other.Increment(container);
            }
            else
            {
                containers[container.Item] = container;
            }
        }
Пример #6
0
        private static bool RemoveItem(IDictionary <T, RefCountItemContainer <T> > containers, RefCountItemContainer <T> container)
        {
            RefCountItemContainer <T> other;

            if (!containers.TryGetValue(container.Item, out other))
            {
                return(false);
            }
            other.Decrement(container);
            if (other.CountOnFirst == 0 && other.CountOnSecond == 0)
            {
                containers.Remove(other.Item);
            }
            return(true);
        }