@@ -60,6 +60,47 @@ public static class TopicExtensions {
6060
6161 }
6262
63+ /*==========================================================================================================================
64+ | METHOD: FIND FIRST PARENT
65+ \-------------------------------------------------------------------------------------------------------------------------*/
66+ /// <summary>
67+ /// Finds the first instance of <see cref="Topic"/> among the parents that satisfies the delegate.
68+ /// </summary>
69+ /// <param name="topic">The instance of the <see cref="Topic"/> to operate against; populated automatically by .NET.</param>
70+ /// <param name="predicate">The function to validate whether a <see cref="Topic"/> should be included in the output.</param>
71+ /// <returns>The first instance of a parent topic to be satisfied.</returns>
72+ public static Topic ? FindFirstParent ( this Topic topic , Func < Topic , bool > predicate ) {
73+
74+ /*------------------------------------------------------------------------------------------------------------------------
75+ | Validate contracts
76+ \-----------------------------------------------------------------------------------------------------------------------*/
77+ Contract . Requires ( topic , nameof ( topic ) ) ;
78+ Contract . Requires ( predicate , nameof ( predicate ) ) ;
79+
80+ /*------------------------------------------------------------------------------------------------------------------------
81+ | Search attributes
82+ \-----------------------------------------------------------------------------------------------------------------------*/
83+ if ( predicate ( topic ) ) {
84+ return topic ;
85+ }
86+
87+ /*------------------------------------------------------------------------------------------------------------------------
88+ | Find lowest common root
89+ \-----------------------------------------------------------------------------------------------------------------------*/
90+ while ( topic . Parent != null ) {
91+ if ( predicate ( topic . Parent ) ) {
92+ return topic . Parent ;
93+ }
94+ topic = topic . Parent ;
95+ }
96+
97+ /*------------------------------------------------------------------------------------------------------------------------
98+ | Indicate no results found
99+ \-----------------------------------------------------------------------------------------------------------------------*/
100+ return null ;
101+
102+ }
103+
63104 /*==========================================================================================================================
64105 | METHOD: FIND ALL
65106 \-------------------------------------------------------------------------------------------------------------------------*/
0 commit comments