Question

I have a ConcurrentDictionary that named with Pr_Matrix:

ConcurrentDictionary<int, ConcurrentDictionary<int, float>> Pr_Matrix = new ConcurrentDictionary<int, ConcurrentDictionary<int, float>>();

The purpose of following code is to add similarity value between each pair of points in the data_set.Set_of_Point data set to this dictionary.

foreach (var point_1 in data_set.Set_of_Point)
{
   foreach (var point_2 in data_set.Set_of_Point)
   {
       int point_id_1 = point_1.Key;
       int point_id_2 = point_2.Key;
       float similarity = selected_similarity_measure(point_1.Value, point_2.Value);

       Pr_Matrix.AddOrUpdate(point_id_1, 
       new ConcurrentDictionary<int, float>() { Keys = {  point_id_2 }, Values = { similarity } }, 
       (x, y) => y.AddOrUpdate(point_id_2, similarity, (m, n) => n));
   }
}

I can't update the ConcurrentDictionarys that exist in main ConcurrentDictionary.

Was it helpful?

Solution

The first problem is that the AddOrUpdate method returns a Float data type. You must return a ConcurrentDictionary explicitly:

  Pr_Matrix.AddOrUpdate(point_id_1, new ConcurrentDictionary<int, float>() { Keys = { point_id_2 }, Values = { similarity } }

                        , (x, y) => { y.AddOrUpdate(point_id_2, similarity, (m, n) => n); return y; });

and the secend problem is that the Keys and Values collections is read-only and ConcurrentDictionary does not support Collection Initializer ,so you must initialize it with something like a Dictionary:

Pr_Matrix.AddOrUpdate(
    point_id_1, 
    new ConcurrentDictionary<int, float>(new Dictionary<int, float> {{point_id_2, similarity}} ), 
    (x, y) => { y.AddOrUpdate(point_id_2, similarity, (m, n) => n); return y; }
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top