Mike Barnes
Mike Barnes

Reputation: 4305

Recursively adding to a list within a list

I want to create a recursive function that will add to the nestedContainer object listed below no matter the length based on a multi-dimensional array. The root will be zero and as long as you work from the root you can add as meany as you want. so 0 will contain 0.0, 0.1, 0.2 and 0.0 will contain 0.0.0, 0.0.1, 0.0.2 and so on. The parent must be created before the child can exist.

    public class Container()
    {
         public List<Container> container {get; set;}
         public string containerName {get; set;}
         public string index {get; set;}
    } 

    public class ContainerBuilder()
    {
         //This is the main container that will contain all of the children
         Container nestedContainer = new Container();

         //This method will take in the parent index value and then will add the new container into the parent container list based on the value specified
         public void AddContainer(string parentIndex, string containerName, string index)
         {
              Container container = new Container() 
              {
                 index = index,
                 name = name,
                 container = new List<Container>()
              }

              SetContainer(parentIndex, index, container);
          }

          private void SetContainer(string, parentIndex, string index, Container container)
          {
              //Recurive function that will add the new container in the parent container working its way back from the parent   

             //Get the root container, a starting point to add the children
             var rootContainer = nestedContainer .contains[int.Parse(parentIndex.Split('.')[0])];   
          }
     }

Implementation

ContainerBuilder builder = new ContainerBuilder();

builder.AddContainer("0", "Parent 0", "0"); 
builder.AddContainer("0", "Child of parent", "0.x"); 
builder.AddContainer("0.x", "child of child", "0.x.x"); 
builder.AddContainer("0.x.x", "child of a child of a child", "0.x.x.x"); 

You see the index should be irrelivant as long as the path matches so no matter which way you go about it one can add as many children as you want.

Upvotes: 1

Views: 203

Answers (1)

Tim Rutter
Tim Rutter

Reputation: 4679

Each Container needs to know its parent. The top level parent will have parent set to null. Each container will also have a list of Container

public class Container
{
    private Container _parent = null;

    public Container(Container parent, int index)
    {
        _parent = parent;
        Containers = new List<Container>();
        Index = index;
    }

    public List<Container> Containers { get; set; }
    public string ContainerName { get; set; }
    public string Index { get; set; }
}

Then you can recurse in either direction

Edit: Here are a couple of functions to add to Container class that I think might get you started:

//Returns the path of the this container prepending all parent indeces
public string GetPath()
{
    string ret = "";
    if (_parent != null)
    {
        ret = _parent.GetPath();
        ret += String.Format("{0}.", Index);
    }
    return ret;
}

//Gets a child ensuring all container lists contain enough elements
public Container GetChild(string indexPath)
{
    string[] pathParts = indexPath.Split(new[] { '.' }, 2);
    if (pathParts.Any())
    {
        int index;
        if (int.TryParse(pathParts[0], out index))
        {
            //make sure there's enough containers
            Containers = Enumerable.Range(0, index +1).Select(i => new Container(this,i)).ToList();
            if (pathParts.Count() == 2)
            {
                //more sub children so recursively add...
                return Containers[index].GetChild(pathParts[1]);
            }
            return Containers[index];
        }
    }
    return null;
}

Tested with:

Container c = new Container(null,0);
Console.WriteLine(c.GetChild("2.2.2").GetPath());
Console.WriteLine(c.Containers[0].GetPath());
Console.WriteLine(c.Containers[1].GetPath());
Console.WriteLine(c.Containers[2].GetPath());
Console.WriteLine(c.Containers[2].Containers[0].GetPath());
Console.WriteLine(c.Containers[2].Containers[1].GetPath());
Console.WriteLine(c.Containers[2].Containers[2].GetPath());
Console.WriteLine(c.Containers[2].Containers[2].Containers[0].GetPath());
Console.WriteLine(c.Containers[2].Containers[2].Containers[1].GetPath());
Console.WriteLine(c.Containers[2].Containers[2].Containers[2].GetPath());

Which outputs

2.2.2.
0.
1.
2.
2.0.
2.1.
2.2.
2.2.0.
2.2.1.
2.2.2.

Just need to remove that last "."

Upvotes: 2

Related Questions