You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.8 KiB

  1. // Copyright (c) 2020 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Collections.Generic;
  20. namespace ICSharpCode.TreeView
  21. {
  22. /// <summary>
  23. /// Static helper methods for traversing trees.
  24. /// </summary>
  25. static class TreeTraversal
  26. {
  27. /// <summary>
  28. /// Converts a tree data structure into a flat list by traversing it in pre-order.
  29. /// </summary>
  30. /// <param name="root">The root element of the tree.</param>
  31. /// <param name="recursion">The function that gets the children of an element.</param>
  32. /// <returns>Iterator that enumerates the tree structure in pre-order.</returns>
  33. public static IEnumerable<T> PreOrder<T>(T root, Func<T, IEnumerable<T>> recursion)
  34. {
  35. return PreOrder(new T[] { root }, recursion);
  36. }
  37. /// <summary>
  38. /// Converts a tree data structure into a flat list by traversing it in pre-order.
  39. /// </summary>
  40. /// <param name="input">The root elements of the forest.</param>
  41. /// <param name="recursion">The function that gets the children of an element.</param>
  42. /// <returns>Iterator that enumerates the tree structure in pre-order.</returns>
  43. public static IEnumerable<T> PreOrder<T>(IEnumerable<T> input, Func<T, IEnumerable<T>> recursion)
  44. {
  45. Stack<IEnumerator<T>> stack = new Stack<IEnumerator<T>>();
  46. try
  47. {
  48. stack.Push(input.GetEnumerator());
  49. while (stack.Count > 0)
  50. {
  51. while (stack.Peek().MoveNext())
  52. {
  53. T element = stack.Peek().Current;
  54. yield return element;
  55. IEnumerable<T> children = recursion(element);
  56. if (children != null)
  57. {
  58. stack.Push(children.GetEnumerator());
  59. }
  60. }
  61. stack.Pop().Dispose();
  62. }
  63. }
  64. finally
  65. {
  66. while (stack.Count > 0)
  67. {
  68. stack.Pop().Dispose();
  69. }
  70. }
  71. }
  72. }
  73. }