Share via


Map Generic Class

Contains a map, which is an unordered collection mapping keys to elements. Map values are immutable. See MapContainer for a mutable version of Map.

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

Usage

'Usage

Syntax

'Declaration
[SerializableAttribute] 
[DefaultMemberAttribute("Item")] 
public class Map<D,R> : CompoundValue, IDictionary<D,R>, ICollection<KeyValuePair<D,R>>, 
    IEnumerable<KeyValuePair<D,R>>, IEnumerable

GenericParameters

  • D
     
  • R
     

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 Map type ignores user-provided equality and hashcode methods for element types; this is to make Map suitable for reasoning in modeling.

Instead, the Map type uses a fixed equality for its elements, which is defined as follows:

  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.

Map values support most LINQ constructs directly.

Maps can be constructed in Cord expressions using the maplet syntax (key -> value).

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

The Map 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.Map

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

Map Members
Microsoft.Modeling Namespace
MapContainer