Click here to Skip to main content
14,333,069 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.
 
GeneralRe: when algorithms attack Pin
Jon McKee17-Aug-19 10:19
professionalJon McKee17-Aug-19 10:19 
GeneralRe: when algorithms attack Pin
Sander Rossel17-Aug-19 10:34
professionalSander Rossel17-Aug-19 10:34 
GeneralRe: when algorithms attack Pin
honey the codewitch16-Aug-19 10:30
memberhoney the codewitch16-Aug-19 10:30 
GeneralRe: when algorithms attack Pin
Sander Rossel16-Aug-19 10:59
professionalSander Rossel16-Aug-19 10:59 
GeneralRe: when algorithms attack Pin
honey the codewitch16-Aug-19 11:02
memberhoney the codewitch16-Aug-19 11:02 
GeneralRe: when algorithms attack Pin
Sander Rossel16-Aug-19 11:09
professionalSander Rossel16-Aug-19 11:09 
GeneralRe: when algorithms attack Pin
honey the codewitch16-Aug-19 11:16
memberhoney the codewitch16-Aug-19 11:16 
GeneralRe: when algorithms attack Pin
Sander Rossel17-Aug-19 1:35
professionalSander Rossel17-Aug-19 1:35 
I have a few questions:
1. Why use ++i rather than i++? i++ is far more common so I had to stop and think about what you were doing (it seems in this context ++i and i++ are the same).
2. Do you favor a for loop over foreach because of performance?
3. Why do you take an ICollection as parameter and immediately call ToArray? Most of the time, the implementation is an IList as well, and in that case you wouldn't need ToArray because it also supports indexing.

So without knowing anything about the code I've rewritten it a bit.
I couldn't test it, but I think it does the same as the original code, but with less nesting.
I've also got rid of the single line if-statement without braces by using progress?.Report.
And I've introduced a new single line if-statement by getting done = true; out of the if.
If this were my code I'd probably go even further by putting the remaining code inside the for loop in it's own method too.
Anyway, it's all a matter of style, I know plenty of people who'd prefer a single method, and maybe these extra method calls slow down your code to much, but here's how I'd handle it anyway Smile | :)
The comments in the code are specifically just for you Wink | ;)
static void _FillLRClosureInPlace(CfgDocument cfg, IProgress<CfgLalr1Progress> progress, ICollection<LRItem> result)
{
    var done = false;
    while (!done)
    {
        done = true;
        var l = result.ToArray();
        for (var i = 0; i < l.Length; i++)
        {
            progress?.Report(new CfgLalr1Progress(CfgLalr1Status.ComputingClosure, i));
            var item = l[i];
            var next = item.RightIndex < item.Rule.Right.Count ? item.Rule.Right[item.RightIndex] : null;
            if (item.RightIndex < item.Rule.Right.Count && cfg.IsNonTerminal(next))
            {
                done = HandleRules(cfg, next, result);
            }
        }
    }
}

private static bool HandleRules(CfgDocument cfg, object next, ICollection<LRItem> result)
{
    bool done = true;
    // cfg.Rules.Count doesn't change, does it?
    for (int j = 0; j < cfg.Rules.Count; j++)
    {
        var r = cfg.Rules[j];
        done = HandleRule(r, next, result);
    }
    return done;
}

private static bool HandleRule(Rule r, object next, ICollection<LRItem> result)
{
    bool done = true;
    if (r.Left == next)
    {
        var lri = new LRItem(r, 0);
        // I think this communicates intent more clearly.
        done = result.Contains(lri);
        if (!done)
        {
            result.Add(lri);
        }
    }
    return done;
}


GeneralRe: when algorithms attack Pin
honey the codewitch17-Aug-19 3:16
memberhoney the codewitch17-Aug-19 3:16 
GeneralRe: when algorithms attack Pin
Sander Rossel17-Aug-19 4:08
professionalSander Rossel17-Aug-19 4:08 
GeneralRe: when algorithms attack Pin
honey the codewitch17-Aug-19 4:14
memberhoney the codewitch17-Aug-19 4:14 
GeneralRe: when algorithms attack Pin
Sander Rossel17-Aug-19 4:27
professionalSander Rossel17-Aug-19 4:27 
GeneralRe: when algorithms attack Pin
honey the codewitch17-Aug-19 4:30
memberhoney the codewitch17-Aug-19 4:30 
GeneralRe: when algorithms attack Pin
Sander Rossel17-Aug-19 4:32
professionalSander Rossel17-Aug-19 4:32 
GeneralRe: when algorithms attack Pin
honey the codewitch17-Aug-19 4:41
memberhoney the codewitch17-Aug-19 4:41 
GeneralRe: when algorithms attack Pin
dandy7216-Aug-19 5:06
memberdandy7216-Aug-19 5:06 
GeneralRe: when algorithms attack Pin
honey the codewitch16-Aug-19 5:08
memberhoney the codewitch16-Aug-19 5:08 
GeneralRe: when algorithms attack Pin
dandy7216-Aug-19 5:11
memberdandy7216-Aug-19 5:11 
GeneralRe: when algorithms attack Pin
honey the codewitch16-Aug-19 5:12
memberhoney the codewitch16-Aug-19 5:12 
GeneralRe: when algorithms attack Pin
dandy7216-Aug-19 5:16
memberdandy7216-Aug-19 5:16 
GeneralRe: when algorithms attack Pin
honey the codewitch16-Aug-19 5:20
memberhoney the codewitch16-Aug-19 5:20 
GeneralRe: when algorithms attack Pin
Jon McKee16-Aug-19 9:31
professionalJon McKee16-Aug-19 9:31 
GeneralRe: when algorithms attack Pin
honey the codewitch16-Aug-19 9:35
memberhoney the codewitch16-Aug-19 9:35 
GeneralRe: when algorithms attack Pin
Jon McKee16-Aug-19 10:33
professionalJon McKee16-Aug-19 10:33 
GeneralRe: when algorithms attack Pin
honey the codewitch16-Aug-19 10:35
memberhoney the codewitch16-Aug-19 10:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.