コード例 #1
0
        static void Main(string[] args)
        {
            // Пример использования. 
            var dict = new CompositeKeyDictionary<Person, Address, House>();
            var ind = 1;
            var addressToSelect = new Address($"Country_{ind}", $"City{ind}", $"Street_{ind}", ind);
            Person personToSelect;

            FillDictionary(dict, out personToSelect);

            Console.WriteLine("All items:");
            foreach (var item in dict)
            {
                Console.WriteLine(item.ToString());
            }
            Console.WriteLine();
           
            Console.WriteLine($"Selected by address: {addressToSelect}");
            foreach (var item in dict.GetValuesByKey2(addressToSelect))
            {
                Console.WriteLine(item.ToString());
            }
            Console.WriteLine();

            Console.WriteLine($"Selected by person: {personToSelect}");
            foreach (var item in dict.GetValuesByKey1(personToSelect))
            {
                Console.WriteLine(item.ToString());
            }

            Console.ReadLine();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            // Пример использования.
            var    dict            = new CompositeKeyDictionary <Person, Address, House>();
            var    ind             = 1;
            var    addressToSelect = new Address($"Country_{ind}", $"City{ind}", $"Street_{ind}", ind);
            Person personToSelect;

            FillDictionary(dict, out personToSelect);

            Console.WriteLine("All items:");
            foreach (var item in dict)
            {
                Console.WriteLine(item.ToString());
            }
            Console.WriteLine();

            Console.WriteLine($"Selected by address: {addressToSelect}");
            foreach (var item in dict.GetValuesByKey2(addressToSelect))
            {
                Console.WriteLine(item.ToString());
            }
            Console.WriteLine();

            Console.WriteLine($"Selected by person: {personToSelect}");
            foreach (var item in dict.GetValuesByKey1(personToSelect))
            {
                Console.WriteLine(item.ToString());
            }

            Console.ReadLine();
        }
コード例 #3
0
        public void ConcurrencyTest()
        {
            //test adding elements
            var dictionary = new CompositeKeyDictionary<Guid, int, object>();
            var tasks = new Task[tasksCount];
            var ids = new Tuple<Guid,int>[tasksCount * stepsPerTask];
            for (int x = 0; x < tasksCount; ++x)
            {
                int xCopy = x;
                tasks[xCopy] = (new Task(() =>
                {
                    int step = 0;
                    foreach (var data in GetRandomData(stepsPerTask))
                    {
                        dictionary[data] = data;
                        //we need this collection for further removal
                        ids[xCopy * stepsPerTask + step] = data;
                        step += 1;
                    }
                }));
            }

            foreach(var t in tasks)
            {
                t.Start();
            }

            Task.WaitAll(tasks);
            Assert.AreEqual(stepsPerTask * tasksCount, dictionary.Count());

            //test removing elements
            for (int x = 0; x < tasksCount; x++)
            {
                int xCopy = x;
                tasks[xCopy] = (new Task(() =>
                {
                    int removalsRemaining = stepsPerTask;
                    while (removalsRemaining > 0)
                    {
                        bool result = dictionary.Remove(ids[stepsPerTask * xCopy + removalsRemaining - 1]);
                        if (result)
                        {
                            removalsRemaining -= 1;
                        }
                    }
                }));
            }

            foreach (var t in tasks)
            {
                t.Start();
            }

            Task.WaitAll(tasks);
            Assert.AreEqual(0, dictionary.Count());
        }
コード例 #4
0
        /// <summary>
        ///     Creates new instatnce of <see cref="CompositeKeyDictionary{UserId,string,User}" /> that contains 5 elements.
        /// </summary>
        /// <returns></returns>
        public static CompositeKeyDictionary <UserId, string, User> CreateDictionary()
        {
            var dictionary = new CompositeKeyDictionary <UserId, string, User>();

            foreach (var user in UserList)
            {
                dictionary.Add(user);
            }
            return(dictionary);
        }
コード例 #5
0
 private static void FillDictionary(CompositeKeyDictionary<Person, Address, House> dict, out Person personToSelect)
 {
     Person person = new Person(0, $"Name_{0}");
     personToSelect = person;
     const int cycleConst = 5;
     for (int i = 1; i <= 15; ++i)
     {
         if (i % cycleConst == 0)
         {
             person = new Person(i / cycleConst, $"Name_{i / cycleConst}");
         }
         dict.Add(person,
             new Address($"Country_{i % cycleConst}", $"City{i % cycleConst}", $"Street_{i % cycleConst}", i % cycleConst),
             new House($"Color_{i}", i));
     }
 }
コード例 #6
0
        private static void FillDictionary(CompositeKeyDictionary <Person, Address, House> dict, out Person personToSelect)
        {
            Person person = new Person(0, $"Name_{0}");

            personToSelect = person;
            const int cycleConst = 5;

            for (int i = 1; i <= 15; ++i)
            {
                if (i % cycleConst == 0)
                {
                    person = new Person(i / cycleConst, $"Name_{i / cycleConst}");
                }
                dict.Add(person,
                         new Address($"Country_{i % cycleConst}", $"City{i % cycleConst}", $"Street_{i % cycleConst}", i % cycleConst),
                         new House($"Color_{i}", i));
            }
        }
コード例 #7
0
        public void TestIndiciesPerformance()
        {
            var dictionary = new CompositeKeyDictionary<Guid, int, object>();
            var data = GetRandomData(dictSize);

            var stopWatch = new Stopwatch();
            stopWatch.Start();
            foreach (var x in data)
            {
                dictionary.Add(x, x);
            }

            var timeToBuiltDictionaryWithIndicies = stopWatch.ElapsedMilliseconds;

            // Time to build Dict without indicies
            dictionary.Clear();
            dictionary.UseIndices = false;
            stopWatch.Restart();
            foreach (var x in data)
            {
                dictionary.Add(x, x);
            }
            var timeToBuiltDictionaryWithOutIndicies = stopWatch.ElapsedMilliseconds;

            //Time required to rebuilt two indicies
            stopWatch.Restart();
            dictionary.UseIndices = true;
            var timeToRebuiltIndices = stopWatch.ElapsedMilliseconds;

            // Get speed with index access
            stopWatch.Restart();
            var xs = dictionary[data.First().Item2].ToList();
            var timeToAccessWithIndices = stopWatch.ElapsedMilliseconds;

            // Now turn it off
            dictionary.UseIndices = false;
            stopWatch.Restart();
            xs = dictionary[data.First().Item2].ToList();
            var timeToAccessWithOutIndices = stopWatch.ElapsedMilliseconds;

            Trace.WriteLine(string.Format("Performance results for dictSize = {0} and busketSize = {1}", dictSize, busketSize));
            Trace.WriteLine("timeToBuiltDictionaryWithIndicies: " + timeToBuiltDictionaryWithIndicies);
            Trace.WriteLine("timeToBuiltDictionaryWithOutIndicies: " + timeToBuiltDictionaryWithOutIndicies);
            Trace.WriteLine("timeToRebuiltIndices: " + timeToRebuiltIndices);
            Trace.WriteLine("timeToAccessWithIndices: " + timeToAccessWithIndices);
            Trace.WriteLine("timeToAccessWithOutIndices: " + timeToAccessWithOutIndices);
            Trace.WriteLine("----------------------------------------------------------------");
        }
コード例 #8
0
 public void MyTestCleanup()
 {
     _dict = null;
 }
コード例 #9
0
 public void MyTestInitialize()
 {
     _dict = new CompositeKeyDictionary <Person, Address, House>();
 }
コード例 #10
0
  public void MyTestCleanup()
 {
     _dict = null;
 }
コード例 #11
0
 public void MyTestInitialize()
 {
     _dict = new CompositeKeyDictionary<Person, Address, House>();
 }