Partager via


Set Generic Class

Contains a set, an unordered collection of elements without repetitions. Set values are immutable. See SetContainer for a mutable version of sets.

Namespace: Microsoft.Modeling
Assembly: Microsoft.Xrt.Runtime (in Microsoft.Xrt.Runtime.dll)

Usage

'Usage

Syntax

'Declaration
[SerializableAttribute] 
public class Set<T> : CompoundValue, ICollection<T>, IEnumerable<T>, 
    IEnumerable

GenericParameters

  • T

Example

The following example shows how to use the collection types Map, Set, and Sequence. The first block is the Cord code for initializing and exploring the types; the second block is the model code.

config ParameterCombination: Main 
{
    action abstract static bool Implementation.AddKey(string key)
        where key in { "green", "yellow", "red" };

    action abstract static bool Implementation.RemoveKey(string key)
        where key in { "green", "yellow", "cyan" };

    action abstract static void
        Implementation.SetValues(Map<string, int> valuesMap)
        where valuesMap in {
            Map<string, int>{ "green"->1, "blue"->3, "cyan"->5 },
            Map<string, int>{ "green"->5, "red"->4, "yellow"->7 },
            Map<string, int>{} };

    action abstract static Map<string, int>
        Implementation.GetValues(Set<string> keySet)
        where keySet in {
            Set<string>{ "green", "blue" },
            Set<string>{ "red", "orange" },
            Set<string>{ "cyan" },
            Set<string>{} };    

    action abstract static Sequence<int>
        Implementation.RemoveDuplicateElements(Sequence<int> queue)
        where queue in {
            Sequence<int>{ 1, 3, 2 },
            Sequence<int>{ 2, 2, 2 },
            Sequence<int>{ 1 },
            Sequence<int>{} };
}

namespace CollectionInitializers
{
    static class PropertiesModel
    {
        private static MapContainer<string, int> properties =
            new MapContainer<string, int>();

        [Rule]
        static bool AddKey(string key)
        {
            if (properties.ContainsKey(key) || key == null)
            {
                return false;
            }
            else
            {
                properties[key] = 0;
                return true;
            }
        }

        [Rule]
        static bool RemoveKey(string key)
        {
            return properties.Remove(key);
        }

        [Rule]
        static Set<string> GetKeys()
        {
            return new Set<string>(properties.Keys);
        }

        [Rule]
        static void SetValues(Map<string, int> valuesMap)
        {
            if (valuesMap == null) return;
            foreach (string key in valuesMap.Keys)
            {
                if (properties.ContainsKey(key))
                {
                    properties[key] = valuesMap[key];
                }
            }
        }

        [Rule]
        static Map<string, int> GetValues(Set<string> keySet)
        {
            if (keySet == null) return new Map<string, int>();
            MapContainer<string, int> tempMap = new MapContainer<string, int>();
            foreach (string key in keySet)
            {
                if (properties.ContainsKey(key))
                {
                    tempMap.Add(key, properties[key]);
                }
            }
            return new Map<string, int>(tempMap);
        }

        [Rule]
        static void Clear()
        {
            properties.Clear();
        }

        [Rule]
        static Sequence<int> RemoveDuplicateElements(Sequence<int> queue)
        {
            return (queue != null) ?
                new Sequence<int>(queue.Distinct()) : new Sequence<int>();
        }
    }
}

Remarks

The Set type ignores user-provided equality and hashcode for element types; this is to make Set suitable for reasoning in modeling.

Instead, the Set type uses a fixed equality for set elements, which is defined as follows: all primitive types and value types (struct) are equal by structure, reference types that derive from CompoundValue are equal by structure as well, and all other reference types are equal by object identity only.

  1. All primitive types and value types (struct) are equal by structure.

  2. Reference types that derive from CompoundValue are equal by structure.

  3. All other reference types are equal by object identity.

Set values support most LINQ constructs.

The Set type is immutable, thus methods that would change the contents of a Set will create a new instance.

The Set type obeys value equality in state comparison; two collection values are identical in two states if their content is identical.

Inheritance Hierarchy

System.Object
   Microsoft.Modeling.CompoundValue
    Microsoft.Modeling.Set
       Microsoft.Modeling.SetGroup

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Platforms

Development Platforms

Microsoft Windows 7, Microsoft Windows Vista, Microsoft Windows XP SP2 or later, Microsoft Windows Server 2008, Microsoft Windows Server 2003

Change History

See Also

Reference

Set Members
Microsoft.Modeling Namespace
SetContainer