MapList
MapList<Key, Value> is a map that contains a list for each key.
var list = new MapList<string, int>();
Map list can be created with key comparer.
// Create maplist with comparer
var list = new MapList<string, int>(StringComparer.InvariantCultureIgnoreCase);
// Add to key
list.Add("key", 10);
list.Add("KEY", 11);
list.Add("keY", 12);
// Print values
foreach (int value in list["KeY"]) WriteLine(value);
MapList.Create(keyType, valueType) creates for key and value type arguments.
var list =
(MapList<string, int>)
MapList.Create(typeof(string), typeof(int));
MapList.Create(keyType, valueType, keyComparer) creates for key and value type arguments and key-comparer.
var list =
(MapList<string, int>)
MapList.Create(typeof(string), typeof(int), StringComparer.InvariantCultureIgnoreCase);
MapList can be created with initializer.
var list = new MapList<string, int>
{
{ "A", 01 },
{ "A", 02 },
{ "A", 03 },
{ "B", 11 },
{ "B", 12 },
{ "B", 13 },
};
MapList can be assigned read-only with extension method of IReadOnly.
list.SetReadOnly();
Writing to read-only map throws InvalidOperationException.
try
{
list.Add("A", 04);
}
catch (InvalidOperationException)
{
}
Multiple values can be added to one key.
// Add elements
list.Add("A", 01);
list.Add("A", 02);
list.Add("A", 03);
// Get values
IList<int> values = list["A"];
// Print values
foreach (int value in values) WriteLine(value);
MapList is not internally synchronized, so it must be externally locked with its .SyncRoot in concurrent use.
// Create list
var list = new MapList<int, int>();
// Run 50 tasks in parallel.
Parallel.For(0, 50,
(int i) => { lock (list.SyncRoot) list.Add(i % 5, i); }
);
// Print values
foreach (var kv in list) WriteLine($"{kv.Key},{string.Join(',', kv.Value)}");
// 2,7,37,42,47,32,27,22,17,12,2
// 3,33,38,43,48,8,3,28,23,18,13
// 4,34,39,44,49,9,29,24,19,14,4
// 0,35,40,45,0,10,5,30,25,20,15
// 1,36,41,46,1,11,31,26,21,16,6
Full Example
Full example
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Avalanche.Utilities;
using static System.Console;
public class maplist
{
public static void Run()
{
{
// <01>
var list = new MapList<string, int>();
// </01>
}
{
// <01B>
// Create maplist with comparer
var list = new MapList<string, int>(StringComparer.InvariantCultureIgnoreCase);
// Add to key
list.Add("key", 10);
list.Add("KEY", 11);
list.Add("keY", 12);
// Print values
foreach (int value in list["KeY"]) WriteLine(value);
// </01B>
}
{
// <02>
var list =
(MapList<string, int>)
MapList.Create(typeof(string), typeof(int));
// </02>
}
{
// <02B>
var list =
(MapList<string, int>)
MapList.Create(typeof(string), typeof(int), StringComparer.InvariantCultureIgnoreCase);
// </02B>
list.Add("key", 10);
list.Add("KEY", 11);
list.Add("keY", 12);
// Print values
foreach (int value in list["KeY"]) WriteLine(value);
}
{
// <03>
var list = new MapList<string, int>
{
{ "A", 01 },
{ "A", 02 },
{ "A", 03 },
{ "B", 11 },
{ "B", 12 },
{ "B", 13 },
};
// </03>
// <04>
list.SetReadOnly();
// </04>
// <05>
try
{
list.Add("A", 04);
}
catch (InvalidOperationException)
{
}
// </05>
}
{
// Create list
var list = new MapList<string, int>();
// <06>
// Add elements
list.Add("A", 01);
list.Add("A", 02);
list.Add("A", 03);
// Get values
IList<int> values = list["A"];
// Print values
foreach (int value in values) WriteLine(value);
// </06>
}
{
// <07>
// Create list
var list = new MapList<int, int>();
// Run 50 tasks in parallel.
Parallel.For(0, 50,
(int i) => { lock (list.SyncRoot) list.Add(i % 5, i); }
);
// Print values
foreach (var kv in list) WriteLine($"{kv.Key},{string.Join(',', kv.Value)}");
// 2,7,37,42,47,32,27,22,17,12,2
// 3,33,38,43,48,8,3,28,23,18,13
// 4,34,39,44,49,9,29,24,19,14,4
// 0,35,40,45,0,10,5,30,25,20,15
// 1,36,41,46,1,11,31,26,21,16,6
// </07>
}
}
}