0

I've seen a lot of different examples of how to do this and am well aware that I could write out a loop that iterates my entire tree of classes to find the maximum depth, but I cannot help but think there has to be a simpler way.

Basically I have two classes that I developed to host all my applications settings, SettingGroup which is exactly what it sounds like, basically a folder, and Setting which is the setting itself and the configurations that allow the application to know what the setting is for and how to display it. The reason I dont just use a fixed class and write out field for my settings is that the application is plugin driven and I wish the settings to remain centralized is plugins are added or removed and not have to worry about decentralized data from the plugins.

When dynamically creating the settings page it is necessary to know the maximum depth of any particular SettingGroup so I know if the root should first organized by tabs, pages, sections etc...

Long story short, is there a reasonably lightweight way to determine the groups maximum depth?

public enum DescriptionVisibility { None, SubText, ToolTip };
public enum SettingType { Bool, Integer, Decimal, Text };
public enum SettingControl { Checkbox, Textbox, Slider, Radio, Dropdown, TextField, Color};

public class SettingGroup
{
    public string name { get; set; }
    public string description { get; set; }
    public List<SettingGroup> groups { get; set; }
    public List<Setting> settings { get; set; }
}
public class Setting
{
    public string name { get; set; }
    public string description { get; set; }
    public DescriptionVisibility descriptionVisibility { get; set; }
    public Dictionary<string, dynamic> configuration { get; set; }
    public dynamic settingValue { get; set; }
    public SettingType settingType { get; set; }
    public SettingControl settingControl { get; set; }

}

Edit: this is untested, but this is what I am currently considering using;

    private static int getDepth(this SettingGroup group, int depth = 0)
    {
        if (group.groups == null)
            return depth;
        if (group.groups.Count == 0)
            return depth;

        int returnDepth = depth;

        foreach (SettingGroup subGroup in group.groups)
        {
            int subGroupDepth = subGroup.getDepth(depth + 1);
            if (subGroupDepth > returnDepth)
                returnDepth = subGroupDepth;
        }

        return returnDepth;
    }

Its pretty basic so it couldn't be TOO slow, but still seems bulky, is there not a LINQ way to do this perhaps?

Wobbles
  • 3,033
  • 1
  • 25
  • 51
  • I think you're almost there, but the way to do it is recursion. It's a standard interview question to measure the depth of a tree. – Carlos Jan 15 '16 at 20:40
  • Recursion can't be done with simple linq – Tom B. Jan 15 '16 at 20:41
  • @Carlos the above IS recursive, see the internal call to getDepth from within. – Wobbles Jan 15 '16 at 20:44
  • 1
    @TomB. Actually, its possible. See: http://stackoverflow.com/questions/11830174/how-to-flatten-tree-via-linq – Caramiriel Jan 15 '16 at 20:45
  • @Wobbles how does `Setting` reference back to `SettingGroup`? Ie. the property that leads to recursion. – Caramiriel Jan 15 '16 at 20:47
  • @Caramiriel It wouldn't have to, Setting is the "File" in this scenario, SettingGroup is the hierarchical class and does reference itself in `groups` as a list. – Wobbles Jan 15 '16 at 20:49
  • What really are you searching for? e.g. faster code, less code, etc. – Ivan Stoev Jan 15 '16 at 20:59
  • @IvanStoev right now just weather or not I overlooked something that can do this with either less code or faster code, speed likely being the primary concern though. – Wobbles Jan 15 '16 at 21:02
  • The code is fine. It can be shortened, but that will not make it faster (in fact will probably make it slower:). There is no way to make it faster w/o introducing and maintaining some state inside. – Ivan Stoev Jan 15 '16 at 21:11
  • Ah good point. Didn't spot it. – Carlos Jan 15 '16 at 21:51

0 Answers0