Click or drag to resize

RankedSetT Class

Represents a collection of distinct items that can be accessed in sort order or by index.
Inheritance Hierarchy
SystemObject
  Kaos.CollectionsBtreeT
    Kaos.CollectionsRankedSetT

Namespace:  Kaos.Collections
Assembly:  KaosCollections (in KaosCollections.dll) Version: 4.2.0.0
Syntax
C#
[SerializableAttribute]
public class RankedSet<T> : Btree<T>, 
	ISet<T>, ICollection<T>, IEnumerable<T>, IEnumerable, ICollection, 
	IReadOnlyCollection<T>, ISerializable, IDeserializationCallback

Type Parameters

T
The type of the items in the set.

The RankedSetT type exposes the following members.

Constructors
  NameDescription
Public methodRankedSetT
Initializes a new set instance that uses the default item comparer.
Public methodCode exampleRankedSetT(IComparerT)
Initializes a new set instance that uses the supplied comparer.
Public methodRankedSetT(IEnumerableT)
Initializes a new set instance that contains items copied from the supplied collection.
Public methodRankedSetT(IEnumerableT, IComparerT)
Initializes a new set instance that contains items copied from the supplied collection.
Protected methodRankedSetT(SerializationInfo, StreamingContext)
Initializes a new set instance that contains serialized data.
Top
Properties
  NameDescription
Public propertyCapacity
Gets or sets the order of the underlying B+ tree structure.
(Inherited from BtreeT.)
Public propertyComparer
Returns a wrapper of the method used to order items in the set.
Public propertyCount
Gets the number of items in the set.
Public propertyMax
Gets the maximum item in the set per the comparer.
Public propertyMin
Gets the minimum item in the set per the comparer.
Top
Methods
  NameDescription
Public methodAdd
Adds an item to the set and returns a success indicator.
Public methodClear
Removes all items from the set.
Public methodContains
Determines whether the set contains the supplied item.
Public methodCopyTo(T)
Copies the set to a compatible array, starting at the beginning of the array.
Public methodCopyTo(T, Int32)
Copies the set to a compatible array, starting at the supplied position.
Public methodCopyTo(T, Int32, Int32)
Copies a supplied number of items to a compatible array, starting at the supplied position.
Public methodStatic memberCode exampleCreateSetComparer
Returns an equality comparer that can be used to create a collection that contains sets.
Public methodStatic memberCreateSetComparer(IEqualityComparerT)
Returns an equality comparer using a supplied comparer that can be used to create a collection that contains sets.
Public methodElementAt
Gets the item at the supplied index.
Public methodElementAtOrDefault
Gets the item at the supplied index or the default if the index is out of range.
Public methodElementsBetween
Returns an enumerator that iterates over a range with the supplied bounds.
Public methodElementsBetweenIndexes
Returns an enumerator that iterates over a range with the supplied index bounds.
Public methodElementsFrom
Returns an enumerator that iterates over a range with the supplied lower bound.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Public methodCode exampleExceptWith
Removes all items in the supplied collection from the set.
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodFirst
Gets the minimum item in the set per the comparer.
Public methodGetEnumerator
Returns an enumerator that iterates thru the set.
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Protected methodGetObjectData
Returns the data needed to serialize the set.
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodIndexOf
Gets the index of the supplied item.
Public methodCode exampleIntersectWith
Removes all items that are not in a supplied collection.
Public methodCode exampleIsProperSubsetOf
Determines whether the set is a proper subset of the supplied collection.
Public methodCode exampleIsProperSupersetOf
Determines whether the set is a proper superset of the supplied collection.
Public methodCode exampleIsSubsetOf
Determines whether the set is a subset of the supplied collection.
Public methodCode exampleIsSupersetOf
Determines whether a set is a superset of the supplied collection.
Public methodLast
Gets the maximum item in the set per the comparer.
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Protected methodOnDeserialization
Implements the deserialization callback and raises the deserialization event when completed.
Public methodCode exampleOverlaps
Determines whether the set and a supplied collection share common items.
Public methodRemove
Removes the supplied item from the set.
Public methodRemoveAt
Removes the item at the supplied index from the set.
Public methodCode exampleRemoveRange
Removes an index range of items from the set.
Public methodCode exampleRemoveWhere
Removes all items that match the condition defined by the supplied predicate from the set.
Public methodReplace(T)
Replace an item if present.
Public methodReplace(T, Boolean)
Replace an item or optionally add it if missing.
Public methodReverse
Returns an IEnumerable that iterates thru the set in reverse order.
Public methodCode exampleSetEquals
Determines whether the set and the supplied collection contain the same items.
Public methodCode exampleSkip
Bypasses a supplied number of items and yields the remaining items.
Public methodSkipWhile(FuncT, Boolean)
Bypasses elements as long as a supplied condition is true and yields the remaining items.
Public methodSkipWhile(FuncT, Int32, Boolean)
Bypasses elements as long as a supplied index-based condition is true and yields the remaining items.
Public methodCode exampleSymmetricExceptWith
Modifies the set so that it contains only items that are present either in itself or in the supplied collection, but not both.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodTryGet
Gets the actual item for the supplied search item.
Public methodTryGetGreaterThan
Gets the least item greater than the supplied item.
Public methodTryGetGreaterThanOrEqual
Gets the least item greater than or equal to the supplied item.
Public methodTryGetLessThan
Gets the greatest item that is less than the supplied item.
Public methodTryGetLessThanOrEqual
Gets the greatest item that is less than or equal to the supplied item.
Public methodCode exampleUnionWith
Add all items in other to this set that are not already in this set.
Top
Explicit Interface Implementations
  NameDescription
Explicit interface implementationPrivate methodICollectionTAdd
Adds an item to the set.
Explicit interface implementationPrivate methodICollectionCopyTo
Copies the set to a compatible array, starting at the supplied array index.
Explicit interface implementationPrivate methodIEnumerableTGetEnumerator
Returns an enumerator that iterates thru the set.
Explicit interface implementationPrivate methodIEnumerableGetEnumerator
Returns an enumerator that iterates thru the collection.
Explicit interface implementationPrivate methodISerializableGetObjectData
Returns the data needed to serialize the set.
Explicit interface implementationPrivate propertyICollectionTIsReadOnly
Indicates that the collection is not read-only.
Explicit interface implementationPrivate propertyICollectionIsSynchronized
Indicates that the collection is not thread safe.
Explicit interface implementationPrivate methodIDeserializationCallbackOnDeserialization
Implements the deserialization callback and raises the deserialization event when completed.
Explicit interface implementationPrivate propertyICollectionSyncRoot
Gets an object that can be used to synchronize access to the collection.
Top
Remarks
Examples

This program shows some basic operations of this class.

C#
using System;
using Kaos.Collections;

namespace ExampleApp
{
    class RsExample01
    {
        static bool IsPolynymous (string name) => name.Contains (" ");

        static void Main()
        {
            var musicians = new RankedSet<string> (StringComparer.InvariantCultureIgnoreCase);

            foreach (var m1 in new string[] { "Falco", "k.d. lang", "Madonna", "Tom Petty",
                                              "Joni Mitchell", "Grimes", "Warren Zevon" })
                musicians.Add (m1);

            Console.WriteLine ("Candidates:");
            foreach (var item in musicians)
                Console.WriteLine ($"  {item}");

            musicians.Remove ("Falco");
            musicians.RemoveWhere (IsPolynymous);
            musicians.RemoveRange (1, musicians.Count-1);

            Console.WriteLine ("\nFavorite:");
            foreach (var item in musicians)
                Console.WriteLine ($"  {item}");
        }

        /* Output:

        Candidates:
          Falco
          Grimes
          Joni Mitchell
          k.d. lang
          Madonna
          Tom Petty
          Warren Zevon

        Favorite:
          Grimes

        */
    }
}

Next is an example showing binary serialization round tripped.

Note: Serialization is not supported in .NET Standard 1.0.

C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using Kaos.Collections;

namespace ExampleApp
{
    [Serializable]
    public class PersonComparer : Comparer<Person>
    {
        public override int Compare (Person x, Person y)
        { return x==null? (y==null? 0 : -1) : (y==null? 1 : String.Compare (x.ToString(), y.ToString())); }
    }

    [Serializable]
    public class Person : ISerializable
    {
        public string First { get; private set; }
        public string Last { get; private set; }

        public Person (string first, string last)
        { this.First = first; this.Last = last; }

        protected Person (SerializationInfo info, StreamingContext context)
        {
            this.First = (String) info.GetValue ("First", typeof (String));
            this.Last = (String) info.GetValue ("Last", typeof (String));
        }

        public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
        {
            info.AddValue ("First", First, typeof (String));
            info.AddValue ("Last", Last, typeof (String));
        }

        public override string ToString() => Last + ", " + First;
    }


    class RsExample05
    {
        static void Main()
        {
            var set1 = new RankedSet<Person> (new PersonComparer());
            set1.Add (new Person ("Hugh", "Mann"));
            set1.Add (new Person ("Hammond", "Egger"));

            string fileName = "Persons.bin";
            IFormatter formatter = new BinaryFormatter();

            SerializePersons (fileName, set1, formatter);
            Console.WriteLine ($"Wrote {set1.Count} items to file '{fileName}'.");
            Console.WriteLine ();

            RankedSet<Person> set2 = DeserializePersons (fileName, formatter);
            Console.WriteLine ($"Read back {set2.Count} items:");

            foreach (var p2 in set2)
                Console.WriteLine (p2);
        }

        public static void SerializePersons (string fn, RankedSet<Person> set, IFormatter formatter)
        {
            using (var fs = new FileStream (fn, FileMode.Create))
            { formatter.Serialize (fs, set); }
        }

        static RankedSet<Person> DeserializePersons (string fn, IFormatter formatter)
        {
            using (var fs = new FileStream (fn, FileMode.Open))
            { return (RankedSet<Person>) formatter.Deserialize (fs); }
        }

        /* Output:

        Wrote 2 items to file 'Persons.bin'.

        Read back 2 items:
        Egger, Hammond
        Mann, Hugh

        */
    }
}
See Also