/// <summary> /// Builds an array of persons to be placed in the descendancy tree. /// </summary> /// <param name="gx">The input model for which the array of persons will be parsed and analyzed.</param> /// <returns>An array of persons to be placed in the descendancy tree.</returns> protected DescendancyNode BuildTree(Gedcomx gx) { DescendancyNode root = null; if (gx.Persons != null && gx.Persons.Count > 0) { List <DescendancyNode> rootArray = new List <DescendancyNode>(); foreach (Person person in gx.Persons) { if (person.DisplayExtension != null && person.DisplayExtension.DescendancyNumber != null) { String number = person.DisplayExtension.DescendancyNumber; bool spouse = number.EndsWith("-S") || number.EndsWith("-s"); if (spouse) { number = number.Substring(0, number.Length - 2); } int[] coordinates = ParseCoordinates(number); List <DescendancyNode> current = rootArray; int i = 0; DescendancyNode node = null; while (current != null) { int coordinate = coordinates[i]; while (current.Count < coordinate) { current.Add(null); } node = current[coordinate - 1]; if (node == null) { node = new DescendancyNode(); current[coordinate - 1] = node; } if (++i < coordinates.Length) { //if we still have another generation to descend, make sure the list is initialized. List <DescendancyNode> children = node.Children; if (children == null) { children = new List <DescendancyNode>(); node.Children = children; } current = children; } else { current = null; } } if (spouse) { node.Spouse = person; } else { node.Person = person; } } } if (rootArray.Count > 0) { root = rootArray[0]; } } return(root); }
/// <summary> /// Builds an array of persons to be placed in the descendancy tree. /// </summary> /// <param name="gx">The input model for which the array of persons will be parsed and analyzed.</param> /// <returns>An array of persons to be placed in the descendancy tree.</returns> protected DescendancyNode BuildTree(Gedcomx gx) { DescendancyNode root = null; if (gx.Persons != null && gx.Persons.Count > 0) { List<DescendancyNode> rootArray = new List<DescendancyNode>(); foreach (Person person in gx.Persons) { if (person.DisplayExtension != null && person.DisplayExtension.DescendancyNumber != null) { String number = person.DisplayExtension.DescendancyNumber; bool spouse = number.EndsWith("-S") || number.EndsWith("-s"); if (spouse) { number = number.Substring(0, number.Length - 2); } int[] coordinates = ParseCoordinates(number); List<DescendancyNode> current = rootArray; int i = 0; DescendancyNode node = null; while (current != null) { int coordinate = coordinates[i]; while (current.Count < coordinate) { current.Add(null); } node = current[coordinate - 1]; if (node == null) { node = new DescendancyNode(); current[coordinate - 1] = node; } if (++i < coordinates.Length) { //if we still have another generation to descend, make sure the list is initialized. List<DescendancyNode> children = node.Children; if (children == null) { children = new List<DescendancyNode>(); node.Children = children; } current = children; } else { current = null; } } if (spouse) { node.Spouse = person; } else { node.Person = person; } } } if (rootArray.Count > 0) { root = rootArray[0]; } } return root; }
/// <summary> /// Initializes a new instance of the <see cref="DescendancyTree"/> class. /// </summary> /// <param name="gx">The input model for which a descendancy model will be built.</param> public DescendancyTree(Gedcomx gx) { this.root = BuildTree(gx); }