1 #Region "Microsoft.VisualBasic::ad3f839f9168701625cdb64b9795197f, Microsoft.VisualBasic.Core\ComponentModel\Algorithm\BinaryTree\Enumerable.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module Enumerable
35     
36     '         Function: ClusterMembers, PopulateNodes, PopulateSequence, (+2 Overloads) Values
37     
38     
39     ' /********************************************************************************/
40
41 #End Region
42
43 Imports System.Runtime.CompilerServices
44
45 Namespace ComponentModel.Algorithm.BinaryTree
46
47     Public Module Enumerable
48
49         ''' <summary>
50         ''' Populate an ASC sortted sequence from this binary tree 
51         ''' 
52         ''' ```
53         ''' left -> me -> right
54         ''' ```
55         ''' </summary>
56         ''' <typeparam name="K"></typeparam>
57         ''' <typeparam name="V"></typeparam>
58         ''' <param name="tree"></param>
59         ''' <returns></returns>
60         ''' <remarks>
61         ''' 这个函数是直接将键名和对应的值取出来,如果是需要取出聚类的簇成员,
62         ''' 应该使用<see cref="PopulateNodes(Of K, V)"/>方法
63         ''' </remarks>
64         <MethodImpl(MethodImplOptions.AggressiveInlining)>
65         <Extension>
66         Public Function PopulateSequence(Of K, V)(tree As BinaryTree(Of K, V)) As IEnumerable(Of Map(Of K, V))
67             Return tree.PopulateNodes.Select(Function(n) New Map(Of K, V)(n.Key, n.Value))
68         End Function
69
70         ''' <summary>
71         ''' 将一个给定的二叉树对象转换为一个数组序列
72         ''' </summary>
73         ''' <typeparam name="K"></typeparam>
74         ''' <typeparam name="V"></typeparam>
75         ''' <param name="tree"></param>
76         ''' <returns></returns>
77         <Extension>
78         Public Iterator Function PopulateNodes(Of K, V)(tree As BinaryTree(Of K, V)) As IEnumerable(Of BinaryTree(Of K, V))
79             If Not tree.Left Is Nothing Then
80                 For Each node In tree.Left.PopulateNodes
81                     Yield node
82                 Next
83             End If
84
85             Yield tree
86
87             If Not tree.Right Is Nothing Then
88                 For Each node In tree.Right.PopulateNodes
89                     Yield node
90                 Next
91             End If
92         End Function
93
94         <MethodImpl(MethodImplOptions.AggressiveInlining)>
95         <Extension>
96         Public Function Values(Of K, V)(tree As BinaryTree(Of K, V)) As IEnumerable(Of V)
97             Return tree.PopulateNodes.Values
98         End Function
99
100         <MethodImpl(MethodImplOptions.AggressiveInlining)>
101         <Extension>
102         Public Function Values(Of K, V)(nodes As IEnumerable(Of BinaryTree(Of K, V))) As IEnumerable(Of V)
103             Return nodes.Select(Function(n) n.Value)
104         End Function
105
106         <MethodImpl(MethodImplOptions.AggressiveInlining)>
107         <Extension>
108         Public Function ClusterMembers(Of K, V)(node As BinaryTree(Of K, V)) As V()
109             If node Is Nothing Then
110                 Return {}
111             Else
112                 Return DirectCast(node!values, List(Of V)).ToArray
113             End If
114         End Function
115     End Module
116 End Namespace