1

Pr_Matrix で名前を付けた ConcurrentDictionary があります。

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

次のコードの目的は、data_set.Set_of_Point データ セット内のポイントの各ペア間の類似値をこのディクショナリに追加することです。

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));
   }
}

メインの ConcurrentDictionary に存在する ConcurrentDictionary を更新できません。

4

1 に答える 1

1

最初の問題は、AddOrUpdateメソッドがFloatデータ型を返すことです。ConcurrentDictionaryを明示的に返す必要があります。

  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; });

2番目の問題は、KeysおよびValuesコレクションが読み取り専用であり、ConcurrentDictionaryがCollection Initializerをサポートしていないため、 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; }
);
于 2012-09-16T14:04:47.880 に答える