Adding/updating concurrentDictionary<string,int[]> issues

Locked Adding/updating concurrentDictionary<string,int[]> issues

  • Friday, May 04, 2012 1:12 AM
     
      Has Code

    Hey everyone:

    I have the following code, but am stuck at the point in the code where I have the comments. I'm having difficulty trying to figure out how to add a value to a specified index in an integer array within a concurrent dictionary if the key doesn't yet exist; or update the value if the key in the concurrentdictionary already exists.

    How do I do this?

    Thanks!

                int intCount = 0;
                string[] arrFirstFileCombos;
                string[] arrSecondFileCombos;
                ConcurrentDictionary<string, int[]> arrCombosAndMatches;
                arrFirstFileCombos = File.ReadAllLines(strCompareFirstFile);
                arrSecondFileCombos = File.ReadAllLines(strCompareSecondFile);
                if (arrFirstFileCombos.Length > arrSecondFileCombos.Length)
                {
                    arrCombosAndMatches = new ConcurrentDictionary<string, int[]>(poParallelOptions.MaxDegreeOfParallelism, arrFirstFileCombos.Length);
                }
                else
                {
                    arrCombosAndMatches = new ConcurrentDictionary<string, int[]>(poParallelOptions.MaxDegreeOfParallelism, arrSecondFileCombos.Length);
                }
                Parallel.For(0, arrFirstFileCombos.Length, poParallelOptions, x =>
                {
                    Parallel.ForEach(arrSecondFileCombos, poParallelOptions, v2 =>
                    {
                        int intNumberSame = 0;
                        string[] arrTemp = v2.Trim().Split(charArrComma);
                        for (int t = 0; t < arrTemp.Length; t++)
                        {
                            intNumberSame += substr_count(arrFirstFileCombos[x].Trim(), arrTemp[t]);
                        }
                        //Here I want to:
                        //1) add v2 to arrCombosAndMatches if it doesn't yet exist with the value 1 in the intNumberSame index.
                        //2) update the intNumberSame index in the v2 key with its current value + 1 if v2 does exist in arrCombosAndMatches
                    });
                });

    _

All Replies

  • Friday, May 04, 2012 3:05 AM
     
      Has Code

    Just spent 20 minutes on this one but didn't test, here's a possible pattern:

     Parallel.ForEach(arrSecondFileCombos, poParallelOptions, v2 =>
                    {
                        int intNumberSame = 0;
                        string[] arrTemp = v2.Trim().Split(charArrComma);
                        for (int t = 0; t < arrTemp.Length; t++)
                        {
                            intNumberSame += substr_count(arrFirstFileCombos[x].Trim(), arrTemp[t]);
                        }
                        //Here I want to:
                        //1) add v2 to arrCombosAndMatches if it doesn't yet exist with the value 1 in the intNumberSame index.
                        var exist = from q in arrCombosAndMatches
                                    where q.Key.Contains(v2)
                                    select q.Key;
                        if(!exist.Count==1) arrCombosAndMatches.TryAdd(exist, v2);
                        //2) update the intNumberSame index in the v2 key with its current value + 1 if v2 does exist in arrCombosAndMatches
                    });


    JP Cowboy Coders Unite!

  • Friday, May 04, 2012 7:51 AM
     
     

    Check if this works:

    arrCombosAndMatches
             .AddOrUpdate( v2,
                           k =>
                           {
                               var a = new int[intNumberSame + 1];
                               a[intNumberSame] = 1;
                               return a;
                           },
                           ( k, a ) =>
                           {
                               if( a.Length <= intNumberSame ) Array.Resize( ref a, intNumberSame + 1 );
                               ++a[intNumberSame];
                               return a;
                           } );

    Maybe it is better to use a Dictionary of Dictionaries rather than Dictionary if Array.

  • Thursday, May 10, 2012 10:51 AM
     
     Proposed Answer
    Please use this forum: http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/threads

    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

    • Proposed As Answer by Syam S Thursday, May 10, 2012 11:16 AM
    •