0

i have 3 files that i need to compare to 100 other files (by using some functions i wrote). i take each file from the three and in loop compare them to each one of the 100 files. i want to use the functions sequentially, after i finish to compare the 3 files to the 100 files i want to activate the next function, how can i do it ( the signature of the functions are the same so i can use delegate, but i don't know how) here is a pseudo code for the comparison:

for(i=0;i<3;i++)
{
 for(j=0;j<100;j++)
   compare(filesArr1[i],filesArr2[j]);
}

After the two loops ended i want to activate the next compare function...

4 Answers 4

3

Okay, so basically you're trying to parameterise a common method by the comparison. Something like this should work for you:

public void CompareFiles(IEnumerable<string> firstFiles,
                         IEnumerable<string> secondFiles,
                         Action<string, string> comparison)
{
    foreach (string file1 in firstFiles)
    {
        foreach (string file2 in secondFiles)
        {
            comparison(file1, file2);
        }
    }
}

Then call it with:

CompareFiles(arrFiles1, arrFiles2, FirstComparison);
CompareFiles(arrFiles1, arrFiles2, SecondComparison);

That's assuming the comparison method already takes any appropriate action. If it needs to return something, you'll probably want to use a Func of some description - more details would be useful.

Sign up to request clarification or add additional context in comments.

3 Comments

such a fan of Actions and Funcs.
Is there a particular reason why you use IList<T> and not IEnumerable<T>?
@Oliver: Not sure... I suspect there was when I started writing it, but not by the end :) Editing...
2

The following code will run the loop you provided twice; the first time calling comp1, and the second time calling comp2. You can increase the number of comparison functions by a) writing more, and b) adding their names to the following line:

ComparisonDelegate[] comparisons = new ComparisonDelegate[] { comp1, comp2 };

    delegate bool ComparisonDelegate(string file1, string file2);

    static bool comp1(string file1, string file2) { return true; }
    static bool comp2(string file1, string file2) { return false; }

    ComparisonDelegate[] comparisons = new ComparisonDelegate[] { comp1, comp2 };

    void runComparisons()
    {
        foreach (ComparisonDelegate cd in comparisons)
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 100; j++)
                {
                    bool retVal = cd(filesArr1[i], filesArr2[j]);
                }
            }
        }
    }

Hopefully you can adapt this to suit

1 Comment

@aharont, they do not have to be statics. Delegates can capture the "receiver" of an instance method.
1

1st of all it would be much faster if your main and only loop will go through 100 files and inside it you will compare your 3 files ;) Like this:

for( int index = 0; index < 100; index++ )
{
  // then all actions here
}

Next. If you need to switch to another comparison function after some steps, you need to change your working delegate switching to some other function. But there's no difference on whether you switch your delegate or call another function, you just need another function call for 3rd file in your case. You need better formalization why you need to switch and on what premises this depends.

5 Comments

Could you explain your first sentence in more detail? It's not at all clear - especially as there are two loops... As far as I can tell, the OP wants to compare each of three files to each of 100 files with one comparison, then do the whole thing again with a different comparison function.
@Jon Just added code sample in my answer. I'm proposing to change the attitude and formalize the task first of all. It may be that there's no big difference with loops order from performance POV, but one thing actually is clear: OP can just call one function for a 3rd file while using another one for the first two, with all code clear and only one loop. And yes, he can use common function providing 2 different delegates as comparison providers.
As far as I can tell he's saying instead of having an outer loop, just do all three comparisons("then all actions here") procedurally inside the 100 loop. Which only eliminates what, 3 increment and comparison operations of the outer loop. I don't think that qualifies as "much faster"
I agree with aaronls: this doesn't merit the claim of "much faster" and you're also assuming that the ordering doesn't matter. What if his second comparison requires that all the first comparisons have been done first?
@Jon: ok, here comes some business logic :) That's one of the thing I'm trying to make clear here. On all other premises and concerning this delegates-attitude question, your answer is preferable.
0

If you know after how many iterations you want to change the compare function, you can do a basic if (or switch ), no need for delegates:

  if ( 0 < j ) && ( j < 2)
      compare1()
  else
  if (2 < j) && ( j < 10)
     compare2()
  else
     compare3()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.