-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiDimMatrix.pas
476 lines (415 loc) · 10.9 KB
/
MultiDimMatrix.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
unit MultiDimMatrix;
interface
uses
System.Classes, System.Contnrs, MatrixTypes;
type
TColumnList = class(TList)
protected
procedure Notify(Ptr: Pointer; Action: TListNotification); override;
function Get(Index: Integer): pColumnItem;
public
function NewItem(AHeader : double) : pColumnItem;
function First : pColumnItem; reintroduce;
function Last : pColumnItem; reintroduce;
procedure Clear; override;
property Items[Index: Integer]: pColumnItem read Get; default;
end;
TRowList = class(TList)
protected
procedure Notify(Ptr: Pointer; Action: TListNotification); override;
function Get(Index: Integer): pRowItem;
public
function NewItem(ATag : SmallInt):pRowItem;
function First : pRowItem; reintroduce;
function Last : pRowItem; reintroduce;
procedure Clear; override;
property Items[Index: Integer]: pRowItem read Get; default;
end;
{Êëàññ ðåàëèçóåò ÿ÷åéêó ñ êîîðäèíàòàìè Col, Row - X è Y ñîîòâåòñòâåííî
Êëàññ ÿâëÿåòñÿ ðîäèòåëåì ÿ÷ååê-õðàíèëèù,
îïðåäåëÿåìûõ äàëüíåéøåé íåîáõîäèìîñòüþ}
TCell = class(TObject, IInterface, ICell)
strict private
fCol : pColumnItem;
fRow : pRowItem;
private
{ IInterface }
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
public
constructor Create(ACol : pColumnItem; ARow : pRowItem); virtual;
function Col : pColumnItem; stdcall;
function Row : pRowItem; stdcall;
end;
TCellClass = class of TCell;
{Êëàññ - õðàíèëèùå ÿ÷ååê TCell
è âûäà÷ó ññûëîê íà èíòåðôåéñ ICell }
TCellList = class(TObject)
strict private
fList : TObjectList;
fCellClass : TCellClass;
public
constructor Create(ACellClass : TCellClass);
destructor Destroy; override;
property CellClass : TCellClass read fCellClass;
function Count : integer;
procedure Delete(Index : integer);
procedure Clear;
function NewItem(ACol : pColumnItem; ARow : pRowItem) : ICell;
function QueryItem(AIndex : integer; const IID: TGUID; out Obj):boolean;
end;
{Êëàññ ìíîãîìåðíîé ìàòðèöû
Òèï âíóòðåííåãî íàáîðà îïðåäåëÿåòñÿ ïåðåäà÷åé
ññûëêè íà êëàññ â êîíñòðóêòîð èëè ïóòåì ïåðåêðûòèÿ ôóíêöèè DefaultCellClass
Îáåñïå÷èâàåò ïîèñê ïî èíäåêñó êîëîíêè, èíäåêñó ñòðîêè è ïî òèïó èíòåðôåéñà}
{Ñòðóêòóðà òàáëèöû TMultiDimMatrix
Col->°Ñ -60 |..| 85 | <-Header
\/Row
Tag 1 |TCellClass |..| TCellClass |
Tag 2 |TCellClass |..| TCellClass |
Tag 3 |TCellClass |..| TCellClass |
............
Tag 6 |TCellClass |..| TCellClass |
TCellClass - Ýêçåïëÿð îáúåêòà TCellClass
Ýòî ìîæåò áûòü ÿ÷åéêà êàê ñ ïðîñòûìè äàííûìè
òàê è ñîäåðæàòü â ñåáå îáúåêòû, âêëþ÷àÿ ñïèñêè
}
TMultiDimMatrix = class(TObject, IInterface, IMultiDimMatrix)
private
fCols : TColumnList;
fRows : TRowList;
fCells : TCellList;
{ IInterface }
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
protected
function DefaultCellClass : TCellClass; virtual;
property Cells : TCellList read fCells;
public
constructor Create overload; virtual;
constructor Create(ACellClass: TCellClass); {reintroduce;} overload; virtual;
destructor Destroy; override;
procedure Clear; stdcall;
function ColCount : integer; stdcall;
function RowCount : integer; stdcall;
function Header(ColumnIndex: byte): double; stdcall;
{Ôóíêöèÿ âåðíåò çíà÷åíèå çàãîëîâêà ïî åãî èíäåêñó
Èíäåêñ äîëæåí áûòü < ColCount}
function Tag(RowIndex: byte): SmallInt; stdcall;
{Ôóíêöèÿ âåðíåò çíà÷åíèå ñòðîêè ïî åãî èíäåêñó
Èíäåêñ äîëæåí áûòü < RowCount}
function FindCol(AHeader : double; var ACol : byte):Boolean;stdcall;
function AddCol(AHeader : double) : integer; stdcall;
function AddRow(ATag : SmallInt) : integer; stdcall;
{Ôóíêöèè AddCol è AddRow äîáàâëÿþò êîëîíêó è ñòðîêó ñ ïàðàìåòðàìè ñîîòâåòñòâåííî}
function FindRow(ATag: SmallInt; var ARow : byte): boolean; stdcall;
{Ôóíêöèÿ ïîèñêà ñòðîêè ïî ïàðàìåòðó Tag. Âåðíåò true ïðè óñïåøíîì ïîèñêå
 âûõîäíîé ïåðåìåííîé ARow áóäåò çíà÷åíèå èíäåêñà ñòðîêè}
function ByIndex(ACol, ARow : byte; const IID: TGUID; out Obj): boolean; stdcall;
{Ôóíêöèÿ ïîèñêà ïî èíäåêñàì ACol, ARow è GUID-ó ÿ÷åéêè.
Âîçâðàòèò true ïðè íàéäåííîé ÿ÷åéêå.  âûõîäíîé ïåðåìåííîé Obj áóäåò ññûëêà íà èíòåðôåéñ ÿ÷åéêè}
function ByParams(ACol : byte; ATag : SmallInt; const IID: TGUID; out Obj): boolean; stdcall;
{Ôóíêöèÿ ïîèñêà ïî èíäåêñó ACol, ïàðàìåòðó ARow è GUID-ó ÿ÷åéêè.
Âîçâðàòèò true ïðè íàéäåííîé ÿ÷åéêå.  âûõîäíîé ïåðåìåííîé Obj áóäåò ññûëêà íà èíòåðôåéñ ÿ÷åéêè}
end;
TMultiDimMatrixClass = class of TMultiDimMatrix;
implementation
uses System.SysUtils, System.Math;
{ TColumnList }
function TColumnList.NewItem(AHeader: double): pColumnItem;
begin
New(result);
result.Index := byte(self.Count);
result.Header := AHeader;
inherited Add(result);
end;
procedure TColumnList.Clear;
begin
while count > 0 do
delete(count -1);
inherited;
end;
function TColumnList.First: pColumnItem;
begin
result := Get(0);
end;
function TColumnList.Get(Index: Integer): pColumnItem;
begin
Result := pColumnItem(inherited Get(Index));
end;
function TColumnList.Last: pColumnItem;
begin
result := Get(Count - 1);
end;
procedure TColumnList.Notify(Ptr: Pointer; Action: TListNotification);
begin
if Action = lnDeleted then
Dispose(Ptr);
inherited Notify(Ptr, Action);
end;
{ TRowList }
function TRowList.NewItem(ATag: SmallInt): pRowItem;
begin
New(result);
result.Index := byte(self.Count);
result.M_Tag := ATag;
inherited Add(result);
end;
procedure TRowList.Clear;
begin
while count > 0 do
delete(count -1);
inherited;
end;
function TRowList.First: pRowItem;
begin
result := Get(0);
end;
function TRowList.Get(Index: Integer): pRowItem;
begin
Result := pRowItem(inherited Get(Index));
end;
function TRowList.Last: pRowItem;
begin
result := Get(Count - 1);
end;
procedure TRowList.Notify(Ptr: Pointer; Action: TListNotification);
begin
if Action = lnDeleted then
Dispose(Ptr);
inherited Notify(Ptr, Action);
end;
{ T2DimCellList }
function TCellList.NewItem(ACol : pColumnItem; ARow : pRowItem): ICell;
var
vTmp : TCell;
begin
vTmp := fCellClass.Create(ACol, ARow);
fList.Add(vTmp);
result := vTmp as ICell;
end;
function TCellList.QueryItem(AIndex: integer; const IID: TGUID;
out Obj): boolean;
begin
result:= (AIndex > -1)
and (AIndex < fList.Count)
and assigned(fList.Items[AIndex])
and Supports(fList.Items[AIndex], IID, Obj);
end;
procedure TCellList.Clear;
begin
fList.Clear;
end;
function TCellList.Count: integer;
begin
result := fList.Count;
end;
constructor TCellList.Create(ACellClass: TCellClass);
begin
fList := TObjectList.Create;
fCellClass := ACellClass;
end;
procedure TCellList.Delete(Index: integer);
begin
fList.Delete(Index);
end;
destructor TCellList.Destroy;
begin
fList.Clear;
FreeAndNil(fList);
inherited Destroy;
end;
{ TMultiDimMatrix }
constructor TMultiDimMatrix.Create;
begin
Create(DefaultCellClass);
end;
constructor TMultiDimMatrix.Create(ACellClass: TCellClass);
begin
fCols := TColumnList.Create;
fRows := TRowList.Create;
fCells := TCellList.Create(ACellClass);
end;
destructor TMultiDimMatrix.Destroy;
begin
Clear;
FreeAndNil(fCells);
FreeAndNil(fCols);
FreeAndNil(fRows);
inherited Destroy;
end;
procedure TMultiDimMatrix.Clear;
begin
fCells.Clear;
fRows.Clear;
fCols.Clear;
end;
function TMultiDimMatrix.FindCol(AHeader: double; var ACol : byte): Boolean;
begin
ACol := 0;
while ACol < fCols.Count do
begin
if SameValue(fCols[ACol].Header, AHeader, 0.5) then
Break;
inc(ACol);
end;
Result := ACol < fCols.Count;
end;
function TMultiDimMatrix.AddCol(AHeader: double): integer;
var
i : integer;
vRes : byte;
v : ICell;
begin
if FindCol(AHeader, vRes) then
begin
result := vRes;
exit;
end;
fCols.NewItem(AHeader);
i := 0;
while i < fRows.Count do
begin
v := fCells.NewItem(fCols.Last, fRows[i]);
v := nil;
inc(i);
end;
result := fCols.Count - 1;
end;
function TMultiDimMatrix.AddRow(ATag: SmallInt): integer;
var
i : integer;
vRes : byte;
v : ICell;
begin
if FindRow(ATag, vRes) then
begin
result := vRes;
exit;
end;
fRows.NewItem(ATag);
i := 0;
while i < fCols.Count do
begin
v := fCells.NewItem(fCols[i], fRows.Last);
v := nil;
inc(i);
end;
result := fRows.Count - 1;
end;
function TMultiDimMatrix.ColCount: integer;
begin
result := fCols.Count;
end;
function TMultiDimMatrix.RowCount: integer;
begin
result := fRows.Count;
end;
function TMultiDimMatrix.Header(ColumnIndex: byte): double;
begin
result := fCols[ColumnIndex]^.Header;
end;
function TMultiDimMatrix.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
if GetInterface(IID, Obj) then
Result := S_OK
else
Result := E_NOINTERFACE;
end;
function TMultiDimMatrix.Tag(RowIndex: byte): SmallInt;
begin
result := fRows[RowIndex]^.M_Tag;
end;
function TMultiDimMatrix._AddRef: Integer;
begin
Result := -1;
end;
function TMultiDimMatrix._Release: Integer;
begin
Result := -1;
end;
function TMultiDimMatrix.DefaultCellClass: TCellClass;
begin
result := TCell;
end;
function TMultiDimMatrix.FindRow(ATag: SmallInt; var ARow : byte): boolean;
begin
ARow := 0;
while ARow < fRows.Count do
begin
if (fRows.Items[ARow]^.M_Tag = ATag) then
break;
inc(ARow);
end;
result := ARow < fRows.Count;
end;
function TMultiDimMatrix.ByIndex(ACol, ARow: byte; const IID: TGUID;
out Obj): boolean;
var
i : integer;
v : ICell;
begin
i := 0;
while (i < fCells.Count) do
begin
v := nil;
if (fCells.QueryItem(i, ICell, v))
and (v.Row^.Index = ARow)
and (v.Col^.Index = ACol) then
break;
inc(i);
end;
result := (i < fCells.Count) and Assigned(v) and (v.QueryInterface(IID, Obj) = S_OK);
v := nil;
end;
function TMultiDimMatrix.ByParams(ACol : byte; ATag: SmallInt; const IID: TGUID;
out Obj): boolean;
var
i : integer;
v : ICell;
begin
i := 0;
while (i < fCells.Count) do
begin
v := nil;
if (fCells.QueryItem(i, ICell, v))
and (v.Row^.M_Tag = ATag)
and (v.Col^.Index = ACol) then
break;
inc(i);
end;
result := (i < fCells.Count) and Assigned(v) and (v.QueryInterface(IID, Obj) = S_OK);
v := nil;
end;
{ TCell }
function TCell.Col: pColumnItem;
begin
result := fCol;
end;
constructor TCell.Create(ACol : pColumnItem; ARow : pRowItem);
begin
fCol := ACol;
fRow := ARow;
end;
function TCell.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
if GetInterface(IID, Obj) then
Result := S_OK
else
Result := E_NOINTERFACE;
end;
function TCell.Row: pRowItem;
begin
result := fRow;
end;
function TCell._AddRef: Integer;
begin
Result := -1;
end;
function TCell._Release: Integer;
begin
Result := -1;
end;
end.