-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRanges.pas
213 lines (189 loc) · 5.69 KB
/
Ranges.pas
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
unit Ranges;
////////////////////////////////////////////////////////////////////////////////
//
// Author: Jaap Baak
// https://github.com/transportmodelling/Utils
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
interface
////////////////////////////////////////////////////////////////////////////////
Uses
SysUtils, Parse, ArrayHlp;
Type
TRange = record
private
FMin,FMax: Integer;
public
Constructor Create(const Min,Max: Integer);
Function Count: Integer;
Function Contains(const Value: Integer): Boolean;
Function Values: TArray<Integer>;
Function Split(NRanges: Integer): TArray<TRange>;
public
Property Min: Integer read FMin;
Property Max: Integer read FMax;
end;
TRanges = record
private
FRanges: array of TRange;
Function GetRanges(Range: Integer): TRange; inline;
public
Class Operator Implicit(const Values: array of Integer): TRanges;
Class Operator Implicit(Ranges: String): TRanges;
Class Operator Implicit(Ranges: TRanges): String;
public
Constructor Create(const Values: array of Integer); overload;
Constructor Create(const Ranges: array of TRange); overload;
Constructor Create(const Ranges: string); overload;
Function Count: Integer;
Function Contains(const Value: Integer): Boolean;
Function Values: TArray<Integer>;
Function AsString: String;
public
Property Ranges[Range: Integer]: TRange read GetRanges; default;
end;
////////////////////////////////////////////////////////////////////////////////
implementation
////////////////////////////////////////////////////////////////////////////////
Constructor TRange.Create(const Min,Max: Integer);
begin
if Min <= Max then
begin
FMin := Min;
FMax := Max;
end else
raise Exception.Create('Min greater than Max');
end;
Function TRange.Count: Integer;
begin
Result := Max-Min+1;
end;
Function TRange.Contains(const Value: Integer): Boolean;
begin
Result := (Value >= FMin) and (Value <= FMax);
end;
Function TRange.Values: TArray<Integer>;
begin
SetLength(Result,Count);
for var Value := Min to Max do Result[Value-Min] := Value;
end;
Function TRange.Split(NRanges: Integer): TArray<TRange>;
begin
// Ensure each range has at least 1 element
if NRanges > Count then NRanges := Count;
// Calculate number of elements in result ranges
var CountDivNRanges := Count div NRanges;
var Remainder := Count - NRanges*CountDivNRanges;
// Set result
var Last := FMin-1;
SetLength(Result,NRanges);
for var Range := 0 to NRanges-1 do
begin
Result[Range].FMin := Last+1;
if Range < Remainder then
Result[Range].FMax := Last+CountDivNRanges+1
else
Result[Range].FMax := Last+CountDivNRanges;
Inc(Last,Result[Range].Count);
end;
end;
////////////////////////////////////////////////////////////////////////////////
Class Operator TRanges.Implicit(const Values: array of Integer): TRanges;
begin
if Length(Values) > 0 then
begin
var Arr := TArray<Integer>.Create(Values);
// Sort values
Arr.Sort;
// Initialize first range
var Value := Arr[0];
var Range := TRange.Create(Value,Value);
// Iterate values
for var Index := 1 to Arr.Length-1 do
if Arr[Index] = Value+1 then Inc(Value) else
begin
// Finalize range
Range.FMax := Arr[Index-1];
Result.FRanges := Result.FRanges + [Range];
// Initialize next range
Value := Arr[Index];
Range.FMin := Value;
end;
// Finalize last range
Range.FMax := Arr[Arr.Length-1];
Result.FRanges := Result.FRanges + [Range];
end;
end;
Class Operator TRanges.Implicit(Ranges: String): TRanges;
begin
var Parser := TStringParser.Create(Comma,Ranges);
SetLength(Result.FRanges,Parser.Count);
for var Range := 0 to Parser.Count-1 do
begin
var P := Pos('-',Parser[Range]);
if P = 0 then
begin
var Value := Parser[Range].ToInt;
Result.FRanges[Range].FMin := Value;
Result.FRanges[Range].FMax := Value;
end else
begin
Result.FRanges[Range].FMin := StrToInt(Copy(Parser[Range],1,P-1));
Result.FRanges[Range].FMax := StrToInt(Copy(Parser[Range],P+1,MaxInt));
end;
end;
end;
Class Operator TRanges.Implicit(Ranges: TRanges): String;
begin
Result := Ranges.AsString;
end;
Constructor TRanges.Create(const Values: array of Integer);
begin
Self := Values;
end;
Constructor TRanges.Create(const Ranges: array of TRange);
begin
SetLength(FRanges,Length(Ranges));
for var Range := low(Ranges) to high(Ranges) do FRanges[Range] := Ranges[Range];
end;
Constructor TRanges.Create(const Ranges: string);
begin
Self := Ranges;
end;
Function TRanges.GetRanges(Range: Integer): TRange;
begin
Result := FRanges[Range];
end;
Function TRanges.Count: Integer;
begin
Result := Length(FRanges);
end;
Function TRanges.Contains(const Value: Integer): Boolean;
begin
Result := false;
for var Range := 0 to Count-1 do
if FRanges[Range].Contains(Value) then Exit(true);
end;
Function TRanges.Values: TArray<Integer>;
begin
Result := [];
for var Range := 0 to Count-1 do Result := Result + FRanges[Range].Values;
end;
Function TRanges.AsString: string;
begin
Result := '';
var Separator := '';
for var Range := 0 to Count-1 do
begin
if FRanges[Range].Count = 1 then
Result := Result + Separator + FRanges[Range].FMin.ToString
else
if FRanges[Range].FMax = FRanges[Range].FMin+1 then
Result := Result + Separator + FRanges[Range].FMin.ToString + ',' + FRanges[Range].FMax.ToString
else
Result := Result + Separator + FRanges[Range].FMin.ToString + '-' + FRanges[Range].FMax.ToString;
Separator := ',';
end;
end;
end.