1 | #Region "Microsoft.VisualBasic::5fe28d8fe19788ad638c9172a49963b1, Microsoft.VisualBasic.Core\ComponentModel\Ranges\Selector\NumericTagged.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 | ' Structure NumericTagged |
35 | ' |
36 | ' Properties: IValueOf_Value |
37 | ' |
38 | ' Constructor: (+1 Overloads) Sub New |
39 | ' Function: (+3 Overloads) CompareTo, ToString |
40 | ' |
41 | ' |
42 | ' /********************************************************************************/ |
43 | |
44 | #End Region |
45 | |
46 | Imports Microsoft.VisualBasic.ComponentModel.TagData |
47 | Imports Microsoft.VisualBasic.Language |
48 | Imports Microsoft.VisualBasic.Serialization.JSON |
49 | |
50 | Namespace ComponentModel.Ranges |
51 | |
52 | ''' <summary> |
53 | ''' Almost equals to <see cref="DoubleTagged(Of T)"/>, but this object is a structure type. |
54 | ''' (作用几乎等同于<see cref="DoubleTagged(Of T)"/>,只不过这个是Structure类型,开销会小一些) |
55 | ''' </summary> |
56 | ''' <typeparam name="T"></typeparam> |
57 | Public Structure NumericTagged(Of T) : Implements IComparable(Of Double), IComparable, IComparable(Of NumericTagged(Of T)) |
58 | Implements Value(Of T).IValueOf |
59 | |
60 | Dim tag# |
61 | Dim value As T |
62 | |
63 | Private Property IValueOf_Value As T Implements Value(Of T).IValueOf.Value |
64 | |
65 | Sub New(tag#, value As T) |
66 | Me.tag = tag |
67 | Me.value = value |
68 | End Sub |
69 | |
70 | Public Overrides Function ToString() As String |
71 | Return $"#{tag} {value.GetJson}" |
72 | End Function |
73 | |
74 | ''' <summary> |
75 | ''' 比较的是<see cref="Tag"/>属性 |
76 | ''' </summary> |
77 | ''' <param name="other"></param> |
78 | ''' <returns></returns> |
79 | Public Function CompareTo(other As Double) As Integer Implements IComparable(Of Double).CompareTo |
80 | Dim d = tag - other |
81 | |
82 | If d = 0R Then |
83 | Return 0 |
84 | Else |
85 | Return Math.Sign(d) |
86 | End If |
87 | End Function |
88 | |
89 | Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo |
90 | If obj Is Nothing Then |
91 | Return 1 |
92 | Else |
93 | Dim type = obj.GetType |
94 | |
95 | If type = GetType(Double) Then |
96 | Return CompareTo(CDbl(obj)) |
97 | ElseIf type = GetType(NumericTagged(Of T)) Then |
98 | Return CompareTo(DirectCast(obj, NumericTagged(Of T)).tag) |
99 | Else |
100 | Return 1 |
101 | End If |
102 | End If |
103 | End Function |
104 | |
105 | Public Function CompareTo(other As NumericTagged(Of T)) As Integer Implements IComparable(Of NumericTagged(Of T)).CompareTo |
106 | Return CompareTo(other.tag) |
107 | End Function |
108 | End Structure |
109 | End Namespace |