-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoordinatesRanges.pas
252 lines (220 loc) · 6.36 KB
/
CoordinatesRanges.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
unit CoordinatesRanges;
interface
uses Math, SysUtils, Classes;
type
TCoordinate = Integer;
PCoordinatesRange = ^TCoordinatesRange;
TCoordinatesRange = record
start, ende: TCoordinate;
end;
TCoordinates = array of TCoordinate;
TCoordinatesProcedure = procedure(Coordinates: TCoordinates; var Cancel: Boolean) of object;
TCoordinatesRangeList = class
{ Looks like: [1-2:1-200:1-15] }
private
FRList: TList;
function GetCount: integer;
function GetRangeByIndex(Index: Integer): TCoordinatesRange;
procedure SetRangeByIndex(Index: Integer; aRange: TCoordinatesRange);
public
property Count: Integer read GetCount;
property Range[Index: Integer]: TCoordinatesRange
read GetRangeByIndex write SetRangeByIndex
; default;
function AddRange(Range: TCoordinatesRange): Integer;
procedure Clear;
procedure ReadFromString(s: string);
constructor Create;
destructor Destroy; override;
function GetAsString: String;
end;
function ValidInteger(const S:String): Boolean;
function RangeIntersection(r1, r2: TCoordinatesRange): TCoordinatesRange;
function StrToRange(s: string): TCoordinatesRange;
function EndofRange(s: string): Word;
function BeginofRange(s: string): word;
function ValidRange(const s: String): Boolean;
procedure ProcessRangeList(RangeList: TCoordinatesRangeList; proc: TCoordinatesProcedure);
function ElementOfRange(x: TCoordinate; Range: TCoordinatesRange): Boolean;
function ElementOfRangeList(x: TCoordinates; Ranges: TCoordinatesRangeList): Boolean;
function RangeToStr(Range: TCoordinatesRange): string;
procedure RangeListIntersection(rohling, maske: TCoordinatesRangeList);
implementation
function ValidRange(const s: String): Boolean;
var p: integer;
a,e: string;
begin
if UpperCase(s) = 'X' then
Result := true
else
if ValidInteger(s) then
Result := True
else
begin
p := pos('-',s);
a := copy(s,1,p-1);
e := copy(s,p+1,high(integer));
Result := (p > 0)and ValidInteger(a)and ValidInteger(e)and(StrToInt(a) < StrToInt(e));
end;
end;
function ValidInteger(const S:String): Boolean;
begin
Result := True;
try
StrToInt(s);
except
Result := False;
end;
end;
function BeginofRange(s: string): word;
var p: integer;
begin
if UpperCase(s) = 'X' then
Result := low(Result)
else
if ValidInteger(s) then
Result := StrToInt(s)
else
begin
p := pos('-',s);
Result := StrToInt(copy(s,1,p-1));
end;
end;
function EndofRange(s: string): Word;
var p: integer;
begin
if UpperCase(s) = 'X' then
Result := high(Result)
else
if ValidInteger(s) then
Result := StrToInt(s)
else
begin
p := pos('-',s);
Result := StrToInt(copy(s,p+1,length(s)-p));
end;
end;
function RangeIntersection(r1, r2: TCoordinatesRange): TCoordinatesRange;
begin
Result.start := IfThen((r1.start > r2.start),r1.start,r2.start);
Result.ende := IfThen((r1.ende < r2.ende ),r1.ende ,r2.ende );
end;
function StrToRange(s: string): TCoordinatesRange;
begin
Result.start := BeginofRange(s);
Result.ende := EndofRange(s);
end;
procedure ProcessRangeList(RangeList: TCoordinatesRangeList; proc: TCoordinatesProcedure);
var listlength: integer;
Coordinates: TCoordinates;
cancel: boolean;
procedure ProcessRange(nr: integer);
var i: word;
begin
for i := RangeList[nr].start to RangeList[nr].ende do //Wenn Start > Ende -> kein Durchlauf!
begin
Coordinates[nr] := i;
if (nr = listlength-1) then
proc(Coordinates,cancel)
else ProcessRange(nr+1);
if cancel then break;
end;
end;
begin
if not Assigned(proc) then raise Exception.Create('ProcessRangeList: proc is not set!');
listlength := RangeList.Count;
setlength(Coordinates,listlength);
cancel := False;
if listlength > 0 then ProcessRange(0);
end;
function ElementOfRange(x: TCoordinate; Range: TCoordinatesRange): Boolean;
begin
Result := (x >= Range.start)and(x <= Range.ende);
end;
function ElementOfRangeList(x: TCoordinates; Ranges: TCoordinatesRangeList): Boolean;
var cc, i: integer;
begin
cc := length(x);
if cc > Ranges.Count then raise Exception.Create('Coordinates different dimension!');
Result := True;
for i := 0 to cc-1 do
Result := Result and ElementOfRange(x[i],Ranges[i]);
end;
procedure TCoordinatesRangeList.ReadFromString(s: string);
var p: integer;
s1: string;
begin
Clear;
repeat
p := pos(':',s);
s1 := copy(s,1,IfThen((p > 0),p-1,high(integer)));
delete(s,1,p);
AddRange(StrToRange(s1));
until (p = 0);
end;
function RangeToStr(Range: TCoordinatesRange): string;
begin
if Range.start <> Range.ende then
Result := IntToStr(Range.start) + '-' + IntToStr(Range.ende)
else
Result := IntToStr(Range.start);
end;
function TCoordinatesRangeList.GetAsString: string;
var i: integer;
begin
Result := '';
for i := 0 to Count-1 do
begin
if i > 0 then Result := Result + ':';
Result := Result + RangeToStr(Range[i]);
end;
end;
procedure RangeListIntersection(rohling, maske: TCoordinatesRangeList);
var i,l: Integer;
begin
l := rohling.Count;
if maske.Count <> l then
raise Exception.Create('RangeListIntersection: lists are different in length!');
for i := 0 to l-1 do
rohling[i] := RangeIntersection(rohling[i], maske[i]);
end;
function TCoordinatesRangeList.AddRange(Range: TCoordinatesRange): Integer;
var p: PCoordinatesRange;
begin
New(p);
p^ := Range;
Result := FRList.Add(p);
end;
procedure TCoordinatesRangeList.Clear;
var i: Integer;
begin
for i := 0 to FRList.Count-1 do
Dispose(PCoordinatesRange(FRList[i]));
FRList.Clear;
end;
function TCoordinatesRangeList.GetCount: integer;
begin
Result := FRList.Count;
end;
function TCoordinatesRangeList.GetRangeByIndex(
Index: Integer): TCoordinatesRange;
begin
Result := TCoordinatesRange(FRList[Index]^);
end;
procedure TCoordinatesRangeList.SetRangeByIndex(Index: Integer;
aRange: TCoordinatesRange);
begin
TCoordinatesRange(FRList[Index]^) := aRange;
end;
constructor TCoordinatesRangeList.Create;
begin
inherited;
FRList := TList.Create;
end;
destructor TCoordinatesRangeList.Destroy;
begin
Clear;
FRList.Free;
inherited;
end;
end.