Author Topic: DataNode children Insert  (Read 304 times)

devomage

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 62
  • Posts: 232
    • View Profile
DataNode children Insert
« on: May 16, 2017, 10:54:07 PM »
Any chance of adding a convenience method to deal with reordering DataNode children?

I do not see a canned method to achieve this...



Something like:
  1. System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();
  2. list.Insert(index, item);
  3.  

devomage

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 62
  • Posts: 232
    • View Profile
Re: DataNode children Insert
« Reply #1 on: May 16, 2017, 11:03:55 PM »
  1. list.RemoveAt(index);
  2.  

devomage

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 62
  • Posts: 232
    • View Profile
Re: DataNode children Insert
« Reply #2 on: May 16, 2017, 11:20:14 PM »
I guess not so much the remove function - RemoveChild works fine for this.  Insert though is still an issue...

This is currently working:
  1. private DataNode InsertAt(DataNode list, int index, DataNode item)
  2. {
  3.     bool isSet = false;
  4.  
  5.     DataNode new_list = new DataNode("character_list");
  6.  
  7.     for (int i = 0; i < list.children.Count; i++)
  8.     {
  9.         DataNode node = list.children[i];
  10.  
  11.         if (i == index)
  12.         {
  13.             isSet = true;
  14.  
  15.             new_list.SetHierarchy(item.name, item);
  16.         }
  17.  
  18.         new_list.SetHierarchy(node.name, node);
  19.     }
  20.  
  21.     if (!isSet)
  22.     {
  23.         new_list.SetHierarchy(item.name, item);
  24.     }
  25.  
  26.     return new_list;
  27. }

devomage

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 62
  • Posts: 232
    • View Profile
Re: DataNode children Insert
« Reply #3 on: May 17, 2017, 07:20:38 PM »
I went a different route, but this is a nice extension...
  1. public static DataNode InsertAt(this DataNode list, int index, DataNode item)
  2. {
  3.         bool isSet = false;
  4.  
  5.         DataNode new_list = new DataNode(list.name);
  6.  
  7.         for (int i = 0; i < list.children.Count; i++)
  8.         {
  9.                 DataNode node = list.children[i];
  10.  
  11.                 if (i == index)
  12.                 {
  13.                         isSet = true;
  14.  
  15.                         new_list.SetHierarchy(item.name, item);
  16.                 }
  17.  
  18.                 new_list.SetHierarchy(node.name, node);
  19.         }
  20.  
  21.         if (!isSet)
  22.         {
  23.                 new_list.SetHierarchy(item.name, item);
  24.         }
  25.  
  26.         return new_list;
  27. }

cmifwdll

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 98
  • Posts: 170
  • TNet Alchemist
    • View Profile
Re: DataNode children Insert
« Reply #4 on: May 17, 2017, 11:09:09 PM »
Since children is just a List<DataNode> which is basically a DataNode[] wouldn't normal re-ordering / insertion work?

  1. TNet.DataNode node = new TNet.DataNode("NodeRoot", 0);
  2. node.AddChild("ChildOne", 1);
  3. node.AddChild("ChildTwo", 2);
  4. node.AddChild("ChildThree", 3);
  5. node.AddChild("ChildFour", 4);
  6. node.AddChild("ChildFive", 5);
  7.  
  8. TNet.DataNode tempChildThree = node.children[2];
  9. node.children[2] = node.children[4];
  10. node.children[4] = tempChildThree;
  11.  
  12. node.children.Insert(2, new TNet.DataNode("InsertedChildThree", 3));
  13. Debug.Log("Test DataNode: " + node.ToString());
  14.  
Produces:
  1. Test DataNode: NodeRoot = 0
  2.         ChildOne = 1
  3.         ChildTwo = 2
  4.         InsertedChildThree = 3
  5.         ChildFive = 5
  6.         ChildFour = 4
  7.         ChildThree = 3
  8.  

This seems to be what the SetHiearchy function does, except it traverses the tree to find the specified child at the passed path.

devomage

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 62
  • Posts: 232
    • View Profile
Re: DataNode children Insert
« Reply #5 on: May 18, 2017, 05:20:51 PM »
wow, for the life of me I didnt see Insert there when I posted this thread...

thanks cmifwdll, Insert is def the way to go.