1 #Region "Microsoft.VisualBasic::ed80ae1d0ca0e7020a5aaa66aa250019, Microsoft.VisualBasic.Core\ComponentModel\Count.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     '     Class Counter
35     
36     '         Constructor: (+2 OverloadsSub New
37     
38     '         Function: Hit
39     
40     '         Sub: Add
41     
42     '     Module CounterExtensions
43     
44     '         FunctionAsInteger, AsNumeric
45     
46     
47     ' /********************************************************************************/
48
49 #End Region
50
51 Imports System.Runtime.CompilerServices
52 Imports Microsoft.VisualBasic.Language
53
54 Namespace ComponentModel
55
56     ''' <summary>
57     ''' The object counter
58     ''' </summary>
59     Public Class Counter : Inherits int
60
61         ''' <summary>
62         ''' Create a new integer counter start from ZERO.(新建一个计数器)
63         ''' </summary>
64         Sub New()
65             MyBase.New(0)
66         End Sub
67
68         <MethodImpl(MethodImplOptions.AggressiveInlining)>
69         Sub New(hits As Integer)
70             Call MyBase.New(x:=hits)
71         End Sub
72
73         Public Sub Add(n As Integer)
74             Value += n
75         End Sub
76
77         ''' <summary>
78         ''' ``++i``
79         ''' </summary>
80         <MethodImpl(MethodImplOptions.AggressiveInlining)>
81         Public Function Hit() As Integer
82             Return ++Me
83         End Function
84
85         <MethodImpl(MethodImplOptions.AggressiveInlining)>
86         Public Overloads Shared Widening Operator CType(c As IntegerAs Counter
87             Return New Counter(hits:=c)
88         End Operator
89
90         <MethodImpl(MethodImplOptions.AggressiveInlining)>
91         Public Overloads Shared Narrowing Operator CType(c As Counter) As Integer
92             Return c.Value
93         End Operator
94
95         <MethodImpl(MethodImplOptions.AggressiveInlining)>
96         Public Overloads Shared Narrowing Operator CType(c As Counter) As Double
97             Return CDbl(c.Value)
98         End Operator
99     End Class
100
101     Public Module CounterExtensions
102
103         <MethodImpl(MethodImplOptions.AggressiveInlining)>
104         <Extension>
105         Public Function AsNumeric(Of K)(counter As Dictionary(Of K, Counter)) As Dictionary(Of K, Double)
106             Return counter.ToDictionary(Function(c) c.Key, Function(c) CDbl(c.Value))
107         End Function
108
109         <MethodImpl(MethodImplOptions.AggressiveInlining)>
110         <Extension>
111         Public Function AsInteger(Of K)(counter As Dictionary(Of K, Counter)) As Dictionary(Of K, Integer)
112             Return counter.ToDictionary(Function(c) c.Key, Function(c) CInt(c.Value))
113         End Function
114     End Module
115 End Namespace