-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgenericutils.pas
More file actions
150 lines (122 loc) · 4.04 KB
/
genericutils.pas
File metadata and controls
150 lines (122 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit genericutils;
interface
type
generic DefaultUtils <T> = record
class function Equals(const A, B: T): Boolean; static; inline;
class function LessThan(const A, B: T): Boolean; static; inline;
class function GreaterThan(const A, B: T): Boolean; static; inline;
class function Compare(const A, B: T): Int64; static; inline;
end;
generic DefaultNumericUtils <T> = record
class function Equals(const A, B: T): Boolean; static; inline;
class function LessThan(const A, B: T): Boolean; static; inline;
class function GreaterThan(const A, B: T): Boolean; static; inline;
class function Compare(const A, B: T): Int64; static; inline;
end;
generic DefaultUnorderedUtils <T> = record
class function Equals(const A, B: T): Boolean; static; inline;
class function LessThan(const A, B: T): Boolean; static; inline;
class function GreaterThan(const A, B: T): Boolean; static; inline;
class function Compare(const A, B: T): Int64; static; inline;
end;
generic IncomparableUtils <T> = record
class function Equals(const A, B: T): Boolean; static; inline;
class function LessThan(const A, B: T): Boolean; static; inline;
class function GreaterThan(const A, B: T): Boolean; static; inline;
class function Compare(const A, B: T): Int64; static; inline;
end;
TObjectUtils = specialize DefaultUnorderedUtils <TObject>;
WordUtils = specialize DefaultNumericUtils <Word>;
CardinalUtils = specialize DefaultNumericUtils <Cardinal>;
LongIntUtils = specialize DefaultNumericUtils <LongInt>;
IntegerUtils = specialize DefaultNumericUtils <Integer>;
PointerUtils = specialize DefaultNumericUtils <Pointer>;
PtrUIntUtils = specialize DefaultNumericUtils <PtrUInt>;
DoubleUtils = specialize DefaultUtils <Double>;
RawByteStringUtils = specialize DefaultUtils <RawByteString>;
// for UTF8StringUtils, see stringutils.pas
implementation
uses
sysutils;
class function DefaultUtils.Equals(const A, B: T): Boolean;
begin
Result := A = B;
end;
class function DefaultUtils.LessThan(const A, B: T): Boolean;
begin
Result := A < B;
end;
class function DefaultUtils.GreaterThan(const A, B: T): Boolean;
begin
Result := A > B;
end;
class function DefaultUtils.Compare(const A, B: T): Int64;
begin
if (A < B) then
Result := -1
else
if (A > B) then
Result := 1
else
Result := 0;
end;
class function DefaultNumericUtils.Equals(const A, B: T): Boolean;
begin
Result := A = B;
end;
class function DefaultNumericUtils.LessThan(const A, B: T): Boolean;
begin
Result := A < B;
end;
class function DefaultNumericUtils.GreaterThan(const A, B: T): Boolean;
begin
Result := A > B;
end;
class function DefaultNumericUtils.Compare(const A, B: T): Int64;
begin
{$PUSH}
{$POINTERMATH ON}
Result := A - B; // $R-
{$POP}
end;
class function DefaultUnorderedUtils.Equals(const A, B: T): Boolean;
begin
Result := A = B;
end;
class function DefaultUnorderedUtils.LessThan(const A, B: T): Boolean;
begin
raise Exception.Create('tried to compare unordered data');
Result := False;
end;
class function DefaultUnorderedUtils.GreaterThan(const A, B: T): Boolean;
begin
raise Exception.Create('tried to compare unordered data');
Result := False;
end;
class function DefaultUnorderedUtils.Compare(const A, B: T): Int64;
begin
raise Exception.Create('tried to compare unordered data');
Result := 0;
end;
class function IncomparableUtils.Equals(const A, B: T): Boolean;
begin
Result := False;
end;
class function IncomparableUtils.LessThan(const A, B: T): Boolean;
begin
raise Exception.Create('tried to compare unordered data');
Result := False;
end;
class function IncomparableUtils.GreaterThan(const A, B: T): Boolean;
begin
raise Exception.Create('tried to compare unordered data');
Result := False;
end;
class function IncomparableUtils.Compare(const A, B: T): Int64;
begin
raise Exception.Create('tried to compare unordered data');
Result := 0;
end;
end.