Click or drag to resize

RankedBagTRetainAll Method

Removes any elements that are not in the supplied collection from the bag.

Namespace:  Kaos.Collections
Assembly:  KaosCollections (in KaosCollections.dll) Version: 4.2.0.0
Syntax
C#
public int RetainAll(
	IEnumerable<T> other
)

Parameters

other
Type: System.Collections.GenericIEnumerableT
The elements to retain.

Return Value

Type: Int32
The number of elements removed from the bag.
Exceptions
ExceptionCondition
ArgumentNullExceptionWhen other is null.
Remarks
Cardinality is respected by this operation so that the occurrences of each item retained is the number of occurrences of that item in other. In precise terms, this operation removes max(n-m,0) occurrences of a given item where the bag contains n occurrences and other contains m occurrences.
Examples
C#
using System;
using Kaos.Collections;

namespace ExampleApp
{
    class RbExample01
    {
        static void Main()
        {
            var crayons = new RankedBag<string> (StringComparer.InvariantCultureIgnoreCase)
            { "red", "yellow", "black", "BLACK" };

            crayons.Add ("blue");

            Console.WriteLine ($"There are {crayons.Count} total crayons:");
            foreach (var crayon in crayons)
                Console.WriteLine ($"  {crayon}");

            Console.WriteLine ($"\nThere are {crayons.GetDistinctCount()} distinct colors:");
            foreach (var crayon in crayons.Distinct())
                Console.WriteLine ($"  {crayon}");

            Console.WriteLine ($"\nGot 'gold' crayon? {crayons.Contains ("gold")}");

            // RetainAll respects cardinality so the oldest 'black' is removed:
            crayons.RetainAll (new string[] { "white", "grey", "Black", "red" });

            Console.WriteLine ("\nAfter RetainAll: ");
            foreach (var crayon in crayons)
                Console.WriteLine ($"  {crayon}");
        }

        /* Output:

        There are 5 total crayons:
          black
          BLACK
          blue
          red
          yellow

        There are 4 distinct colors:
          black
          blue
          red
          yellow

        Got 'gold' crayon? False

        After RetainAll:
          BLACK
          red

        */
    }
}
See Also