C# Splitting Data Tree into smaller chunks- incorrect output

Hi,
I am trying to split a data Tree into smaller chunks.
the Code seems to be working all fine, You can see here:

private void RunScript(DataTree<object> inputTree, ref object A)
  {
    var microTower = SplitBranchLists(inputTree);

    A = microTower;

  }

  // <Custom additional code> 

  public DataTree<object> SplitBranchLists(DataTree < object > inputDataTree)
  {
    DataTree<object> splitBranchLists = new DataTree<object>();
    int splitSize = 3;
    for(int i = 0; i < inputDataTree.BranchCount; i++)
    {
      var dataPath = inputDataTree.Paths[i];
      var dataList = inputDataTree.Branches[i];
      var remainder = dataList.Count % splitSize;
      var divisor = dataList.Count / splitSize;

      for (int n = 0; n < divisor; n++)
      {
        for ( int k = 0; k < dataList.Count - remainder; k += splitSize)
        {
          var tmpsum = 0;
          var startIndex = k + tmpsum;
          splitBranchLists.AddRange(dataList.GetRange(startIndex, splitSize), new GH_Path(dataPath.AppendElement(n)));
          Print(dataList.GetRange(startIndex, splitSize).Count.ToString());
          tmpsum += splitSize;

        }
      }
    }
    return splitBranchLists;
  }

But the Output is not what is expected.

Here is the Script also:
SplitDataTree.gh (9.5 KB)
What is missing, any Idea?

FYI, I enclosed your code in tripple backticks to render better in your post.

1 Like

Hi Su
try this:

 public DataTree<object> SplitBranchLists(DataTree < object > inputDataTree, int splitSize)
  {
    DataTree<object> splitBranchLists = new DataTree<object>();
    for(int i = 0; i < inputDataTree.BranchCount; i++)
    {
      var dataPath = inputDataTree.Paths[i];
      var dataList = inputDataTree.Branches[i];
      var remainder = dataList.Count % splitSize;
      var divisor = dataList.Count / splitSize;

      int tmpsum = 0;
      for (int n = 0; n < divisor; n++)
      {
        List<object> data = null;
        data = (dataList.GetRange(tmpsum, splitSize));
        splitBranchLists.AddRange(data, new GH_Path(dataPath.AppendElement(n)));
        tmpsum += splitSize;
      }
      if (remainder > 0)
      {
        splitBranchLists.AddRange(dataList.GetRange(tmpsum, remainder), new GH_Path(dataPath.AppendElement(divisor)));
      }
    }
    return splitBranchLists;
  }

SplitDataTree .gh (13.7 KB)

@603419608 . I see that the 2nd loop was not necessary and I had also left the ‘tmpsum’ inside the loop. Thanks! That was really helpful.

Hey, I’ve been struggling with this code for a few days. I would like to split a DataTree with a list of int.
The problem here is that the paths should not be completely renumbered, but should be divided into sub-branches. e.g.
if the path {4; 0} has 15 items and should be split up with another index from the list e.g. int i = 4,
then this should create a new subbranch {4; 1}.

So everything like in your great code, only with a list of int. Similar to SplitList Component

I would be very happy about your help!
SplitDataTreeX.gh (19.3 KB)

Hi @marta-neu
Think you have to change the GH_Path for the remainder>0

splitBranchLists.AddRange(dataList.GetRange(tmpsum, remainder), new GH_Path(dataPath));

I tend to not change the PathOrder and let it Grow as per the codes need. But if you want to, you can create the new Path based on the int i, n in the loops.

I hope this helps you.
SplitDataTreeX.gh (17.6 KB)

Hej

Thank you very much for your quick help. That definitely helps. To get the exact length of paths, I will now try to implement your tip. A new path via int i and n definitely sounds clean.
W

GH_Path branch = new GH_Path(i,0); // for Branch {4;0}
GH_Path branch = new GH_Path(i,n); // for subBranch {4;1}

with this approach I am missing something …

@marta-neu
Currently the n only gives an output of 0 because its increment if the splitsize, so for the sub branch you need to do
GH_Path branch = new GH_Path(i,n+1); // for subBranch {4;1}

I just noticed that the list is not split correctly here either.