Wintellect Power Collections Libraryを使う MultiDictionary編

Wintellect Power Collections LibraryのMultiDictionaryは、キーに対して複数の値を持つことができるデータ構造で、C++の標準テンプレートライブラリのmultimapのような機能を持つ。

MultiDictionaryクラスには、以下のようなメソッドがある。

Add
AddMany
AsReadOnly
Clear
Clone
CloneContents
Contains
ContainsKey
ConvertAll<(Of )>
CopyTo
CountWhere
Equals
Exists
FindAll
ForEach
GetEnumerator
GetHashCode
GetType
Remove
RemoveAll
RemoveMany
Replace
ReplaceMany
ToArray
ToString
TrueForAll

  • Add
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d = new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
            d.ForEach(delegate(KeyValuePair<Int32, ICollection<Int32>> o)
                {
                    foreach (Int32 v in o.Value)
                    {
                        Console.WriteLine("Key:{0}, Value:{1}", o.Key, v);
                    }
                });
    
        }
    }

  • AddMany
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                        new MultiDictionary<Int32, Int32>(true);
    
            List<Int32> l = new List<Int32>();
            for (Int32 i = 0; i < 3; i++)
            {
                l.Add(i);
            }
    
            d.AddMany(0, l);
            d.ForEach(delegate(KeyValuePair<Int32, ICollection<Int32>> o)
            {
                foreach (Int32 v in o.Value)
                {
                    Console.WriteLine("Key:{0}, Value:{1}", o.Key, v);
                }
            });
        }
    }


  • AsReadOnly
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
            ICollection<KeyValuePair<Int32, ICollection<Int32>>> col = d.AsReadOnly();
            foreach (KeyValuePair<Int32, ICollection<Int32>> kv in col)
            {
                foreach (Int32 value in kv.Value)
                {
                    Console.WriteLine("Key:{0}, Value:{1}", kv.Key, value);
                }
            }
        }
    }

  • Clear
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
            d.ForEach(delegate(KeyValuePair<Int32, ICollection<Int32>> o)
            {
                foreach (Int32 value in o.Value)
                {
                    Console.WriteLine("Key:{0}, Value:{1}", o.Key, value);
                }
            });
    
            d.Clear();
    
            Console.WriteLine("Item count after clear:{0}", d.Count);
        }
    }

  • Clone
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
            MultiDictionary<Int32, Int32> clone = d.Clone();
            clone.ForEach(delegate(KeyValuePair<Int32, ICollection<Int32>> o)
            {
                foreach (Int32 value in o.Value)
                {
                    Console.WriteLine("Key:{0}, Value:{1}", o.Key, value);
                }
            });
        }
    }


  • CloneContents
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
            MultiDictionary<Int32, Int32> deepClone = d.CloneContents();
            deepClone.ForEach(delegate(KeyValuePair<Int32, ICollection<Int32>> o)
            {
                foreach (Int32 value in o.Value)
                {
                    Console.WriteLine("Key:{0}, Value:{1}", o.Key, value);
                }
            });
        }
    }


  • Contains
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
            if (d.Contains(0, 0))
            {
                Console.WriteLine("Contains Key:0, Value:0");
            }
    
    
            KeyValuePair<Int32, ICollection<Int32>> kv1 =
                new KeyValuePair<int, ICollection<int>>(0, new Int32[] { 0, 0, 0 });
            if (d.Contains(kv1))
            {
                Console.WriteLine("Contains Key:0, Value:0,0,0");
            }
    
    
            KeyValuePair<Int32, ICollection<Int32>> kv2 =
                new KeyValuePair<int, ICollection<int>>(0, new Int32[] { 0, 0, 1 });
            if (d.Contains(kv2))
            {
                Console.WriteLine("Contains Key:{0}, Value:{0,0,1}");
            }
        }
    }


  • ContainsKey
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
            if (d.ContainsKey(0))
            {
                Console.WriteLine("Contains Key:0");
            }
    
            if (d.ContainsKey(5))
            {
                Console.WriteLine("Contains Key:5");
            }
        }
    }


  • ConvertAll
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
            IEnumerable<String> converted =
                d.ConvertAll<String>(delegate(KeyValuePair<Int32, ICollection<Int32>> o)
                {
                    List<String> values = new List<String>();
                    foreach (Int32 v in o.Value)
                    {
                        values.Add(v.ToString());
                    }
    
                    return String.Format("Key:{0}, Value:{1}", o.Key, String.Join(",", values.ToArray()));
                });
    
            foreach (String s in converted)
            {
                Console.WriteLine(s);
            }
        }
    }


  • CopyTo
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
            KeyValuePair<Int32, ICollection<Int32>>[] arr =
                new KeyValuePair<Int32, ICollection<Int32>>[d.Count];
            d.CopyTo(arr, 0);
    
            Array.ForEach(arr, delegate(KeyValuePair<Int32, ICollection<Int32>> o)
            {
                foreach (Int32 v in o.Value)
                {
                    Console.WriteLine("Key:{0}, Value:{1}", o.Key, v);
                }
            });
        }
    }


  • CountWhere
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
            Int32 evenKeys =
                d.CountWhere(delegate(KeyValuePair<Int32, ICollection<Int32>> o)
                {
                    return o.Key % 2 == 0;
                });
    
            Console.WriteLine("Even count:{0}", evenKeys);
        }
    }


  • Exists
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
            Boolean ret =
                d.Exists(delegate(KeyValuePair<Int32, ICollection<Int32>> o)
                {
                    return o.Key % 2 == 0;
                });
    
            Console.WriteLine("Exists even key:{0}", ret);
        }
    }


  • FindAll
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
            IEnumerable<KeyValuePair<Int32, ICollection<Int32>>> evens =
                d.FindAll(delegate(KeyValuePair<Int32, ICollection<Int32>> o)
                {
                    return o.Key % 2 == 0;
                });
    
            foreach (KeyValuePair<Int32, ICollection<Int32>> kv in evens)
            {
                foreach (Int32 v in kv.Value)
                {
                    Console.WriteLine("Key:{0}, Value:{1}", kv.Key, v);
                }
            }
        }
    }

  • ForEach
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
            d.ForEach(delegate(KeyValuePair<Int32, ICollection<Int32>> o)
            {
                foreach (Int32 v in o.Value)
                {
                    Console.WriteLine("Key:{0}, Value:{1}", o.Key, v);
                }
            });
        }
    }


  • Remove
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
    
            d.Remove(0);
            Console.WriteLine("After removed key 0");
            d.ForEach(delegate(KeyValuePair<Int32, ICollection<Int32>> o)
            {
                foreach (Int32 v in o.Value)
                {
                    Console.WriteLine("Key:{0}, Value:{1}", o.Key, v);
                }
            });
    
    
    
            d.Remove(new KeyValuePair<int, ICollection<int>>(1, new Int32[] { 1, 1, 1 }));
            Console.WriteLine("After removed key value 1->{1,1,1}");
            d.ForEach(delegate(KeyValuePair<Int32, ICollection<Int32>> o)
            {
                foreach (Int32 v in o.Value)
                {
                    Console.WriteLine("Key:{0}, Value:{1}", o.Key, v);
                }
            });
    
    
            d.Remove(2, 2);
            Console.WriteLine("After removed key value {2, 2}");
            d.ForEach(delegate(KeyValuePair<Int32, ICollection<Int32>> o)
            {
                foreach (Int32 v in o.Value)
                {
                    Console.WriteLine("Key:{0}, Value:{1}", o.Key, v);
                }
            });       
        }
    }

  • RemoveAll
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
            try
            {
                //削除すると、なぜか例外がスローされる?
                IEnumerable<KeyValuePair<Int32, ICollection<Int32>>> removed =
                    d.RemoveAll(delegate(KeyValuePair<Int32, ICollection<Int32>> o)
                    {
                        return true;
                    });
    
                foreach (KeyValuePair<Int32, ICollection<Int32>> kv in removed)
                {
                    foreach (Int32 v in kv.Value)
                    {
                        Console.WriteLine("Key:{0}, Value:{1}", kv.Key, v);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }   
        }
    }

  • RemoveMany
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
    
            d.RemoveMany(0, new Int32[] { 0, 0 });
            Console.WriteLine("After remove key value 0->(0,0)");
            d.ForEach(delegate(KeyValuePair<Int32, ICollection<Int32>> o)
            {
                foreach (Int32 v in o.Value)
                {
                    Console.WriteLine("Key:{0}, Value:{1}", o.Key, v);
                }
            });
    
            d.RemoveMany(new Int32[] { 1, 2 });
            Console.WriteLine("After remove key {1,2}");
            d.ForEach(delegate(KeyValuePair<Int32, ICollection<Int32>> o)
            {
                foreach (Int32 v in o.Value)
                {
                    Console.WriteLine("Key:{0}, Value:{1}", o.Key, v);
                }
            });  
        }
    }

  • Replace
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
            d.Replace(0, 100);
            d.ForEach(delegate(KeyValuePair<Int32, ICollection<Int32>> o)
            {
                foreach (Int32 v in o.Value)
                {
                    Console.WriteLine("Key:{0}, Value:{1}", o.Key, v);
                }
            });
        }
    }

  • ReplaceMany
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
            d.ReplaceMany(0, new Int32[] { 100, 100, 100 });
            d.ForEach(delegate(KeyValuePair<Int32, ICollection<Int32>> o)
            {
                foreach (Int32 v in o.Value)
                {
                    Console.WriteLine("Key:{0}, Value:{1}", o.Key, v);
                }
            });
        }
    }

  • ToArray
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
            KeyValuePair<Int32, ICollection<Int32>>[] arr = d.ToArray();
            foreach (KeyValuePair<Int32, ICollection<Int32>> kv in arr)
            {
                foreach (Int32 v in kv.Value)
                {
                    Console.WriteLine("Key:{0}, Value:{1}", kv.Key, v);
                }
            }
        }
    }

  • ToString
  • using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using Wintellect.PowerCollections;
    using System.Reflection;
    
    public static class WintellectPowerCollections
    {
        public static void Main()
        {
            MultiDictionary<Int32, Int32> d =
                new MultiDictionary<Int32, Int32>(true);
    
            for (Int32 i = 0; i < 3; i++)
            {
                for (Int32 j = 0; j < 3; j++)
                {
                    d.Add(i, i);
                }
            }
    
            Console.WriteLine(d.ToString());
        }
    }
    //ToStringの実行結果
    {1->(1,1,1), 0->(0,0,0), 2->(2,2,2)}