-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
SequentialVectors.pas
1295 lines (1078 loc) · 43.5 KB
/
SequentialVectors.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
SequentialVectors
This unit provides base class (TSequentialVector) that can be used to
implement both FIFO (queue) and LIFO (stack) vectors with items/entries
of specific type.
Generics were not used because of backward compatibility with old
compilers - instead a template is provided. Also a complete derived vector
classes with items of type Integer are provided (you can refer to them for
more details).
Note that any of the vectors can be created as invariant-size circular/ring
buffer (if vector is full, its oldest item is replaced by any newly added
one). You only need to set constructor's CircularCapacity parameter to
desired capacity of the buffer (a number larger than zero).
WARNING - if the vector is created in this mode, you cannot change its
capacity allocated during creation.
Version 1.0 (2024-07-15)
Last change (2024-07-15)
©2024 František Milt
Contacts:
František Milt: frantisek.milt@gmail.com
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.SequentialVectors
Dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
* AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
* BinaryStreamingLite - github.com/TheLazyTomcat/Lib.BinaryStreamingLite
StrRect - github.com/TheLazyTomcat/Lib.StrRect
Library AuxExceptions is required only when rebasing local exception classes
(see symbol SequentialVectors_UseAuxExceptions for details).
BinaryStreamingLite can be replaced by full BinaryStreaming.
Library AuxExceptions might also be required as an indirect dependency.
Indirect dependencies:
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
{
Use the following template to create a derived classes for specific item type.
Simply copy the code and replace occurences of string @ClassName@ with a name
of common class (common to both FIFO and LIFO), @FIFOClassName@ with a name
for FIFO class, @LIFOClassName@ with a name for LIFO class and finally @Type@
with an identifier of type you want to use for items.
Integer vectors provided by this unit were completely created using this
template, so you can look there for an example.
Also note that, if you do not need both FIFO and LIFO vectors, you can create
complete implementation directly, without using two-level inheritance. All
you need is a small tweak to the constructor - remove the OperationMode
argument, and, in the constructor implementation, select one of the vector
types directly by passing omFIFO or omLIFO to inherited constructor.
}
(*******************************************************************************
== Declaration ===============================================================
--------------------------------------------------------------------------------
@ClassName@ = class(TSequentialVector)
protected
Function GetItem(Index: Integer): @Type@; reintroduce;
procedure SetItem(Index: Integer; NewValue: @Type@); reintroduce;
//procedure ItemFinal(Item: Pointer); override;
//procedure ItemAssign(SrcItem,DstItem: Pointer); override;
Function ItemCompare(Item1,Item2: Pointer): Integer; override;
//Function ItemEquals(Item1,Item2: Pointer): Boolean; override;
//procedure ItemWrite(Item: Pointer; Stream: TStream); override;
//procedure ItemRead(Item: Pointer; Stream: TStream); override;
//class Function ManagedItemStreaming: Boolean; override;
public
constructor Create(OperationMode: TSVOperationMode; CircularCapacity: Integer = -1);
Function IndexOf(Item: @Type@): Integer; reintroduce;
Function Find(Item: @Type@; out Index: Integer): Boolean; reintroduce;
procedure Push(Item: @Type@); reintroduce;
Function Peek: @Type@; reintroduce;
Function Pop: @Type@; reintroduce;
Function Pick(Index: Integer): @Type@; reintroduce;
property Items[Index: Integer]: @Type@ read GetItem write SetItem;
end;
@FIFOClassName@ = class(@ClassName@)
public
constructor Create(CircularCapacity: Integer = -1);
end;
@LIFOClassName@ = class(@ClassName@)
public
constructor Create(CircularCapacity: Integer = -1);
end;
== Implementation ============================================================
--------------------------------------------------------------------------------
Function @ClassName@.GetItem(Index: Integer): @Type@;
begin
inherited GetItem(Index,@Result);
end;
//------------------------------------------------------------------------------
procedure @ClassName@.SetItem(Index: Integer; NewValue: @Type@);
begin
inherited SetItem(Index,@NewValue);
end;
//------------------------------------------------------------------------------
// Itemfinal is called whenever any item is implicitly (without explicit
// action of the user) removed from the vector - this includes actions such as
// clearing, freeing non-empty list or loading the list (where existing items
// are discarded). It is not called when Pop-ing, as that is functionally
// equivalent to Extract in lists.
// Default action is a no-op. Implement only when the items really need to be
// finalized (objects, interfaces, dynamically allocated memory, dynamic
// arrays, strings, etc.).
//procedure @ClassName@.ItemFinal(Item: Pointer);
//begin
//end;
//------------------------------------------------------------------------------
// ItemAssign is called when value of an item is assigned.
// Default implementation uses RTL procedure Move to copy the item's memory,
// but you can reimplement it to provide simpler and faster assigning (eg. for
// primitive types like integers or floats, where it is enough to just use
// assignment operator := and compiler optimizes the operation).
//procedure @ClassName@.ItemAssign(SrcItem,DstItem: Pointer);
//begin
//end;
//------------------------------------------------------------------------------
// ItemCompare is used to compare values of two items. This method must be
// always implemented to suit the actual type. There is no default
// implementation as it is an abstract method.
// When Item1 is larger than Item2, the function must return a positive value,
// when they are equal, zero must be returned, and when Item1 is smaller than
// Item2, then a negative value must be returned.
Function @ClassName@.ItemCompare(Item1,Item2: Pointer): Integer;
begin
{$MESSAGE ERROR 'Implement for actual type!'}
end;
//------------------------------------------------------------------------------
// ItemEquals is used to compare value of two items for equality.
// Default implementation uses ItemCompare to do the comparison. Implement it
// when full value comparison is not needed or direct equality check can be
// done much faster.
// When the two items have values that can be considered equal, return True,
// otherwise set the result to False.
//Function @ClassName@.ItemEquals(Item1,Item2: Pointer): Boolean;
//begin
//end;
//------------------------------------------------------------------------------
// ItemWrite is called for each item that is being written to a stream (or
// file), but only when method ManagedItemStreaming (see further) returns true.
// Default implementation merely stores item's memory as is. Implement this
// method if you wish to eg. ensure endianness, store complex objects that
// cannot be written directly, and so on.
//procedure @ClassName@.ItemWrite(Item: Pointer; Stream: TStream);
//begin
//end;
//------------------------------------------------------------------------------
// ItemRead is called for each item that is being read from a stream (or file),
// but only when method ManagedItemStreaming returns true.
// Default implementation reads the stream directly into the item's memory,
// without any further processing. You can use this method to do reading of
// complex objects or when binary compatibility (eg. endianness) needs to be
// ensured.
//procedure @ClassName@.ItemRead(Item: Pointer; Stream: TStream);
//begin
//end;
//------------------------------------------------------------------------------
// If you want items to be streamed using methods ItemWrite and ItemRead,
// return true. If false is returned (default behavior), then the items are
// streamed directly from/to memory, without calling mentioned functions.
//class Function @ClassName@.ManagedItemStreaming: Boolean;
//begin
//end;
//==============================================================================
constructor @ClassName@.Create(OperationMode: TSVOperationMode; CircularCapacity: Integer = -1);
begin
inherited Create(OperationMode,SizeOf(@Type@),CircularCapacity);
end;
//------------------------------------------------------------------------------
Function @ClassName@.IndexOf(Item: @Type@): Integer;
begin
Result := inherited IndexOf(@Item);
end;
//------------------------------------------------------------------------------
Function @ClassName@.Find(Item: @Type@; out Index: Integer): Boolean;
begin
Result := inherited Find(@Item,Index);
end;
//------------------------------------------------------------------------------
procedure @ClassName@.Push(Item: @Type@);
begin
inherited Push(@Item);
end;
//------------------------------------------------------------------------------
Function @ClassName@.Peek: @Type@;
begin
inherited Peek(@Result);
end;
//------------------------------------------------------------------------------
Function @ClassName@.Pop: @Type@;
begin
inherited Pop(@Result);
end;
//------------------------------------------------------------------------------
Function @ClassName@.Pick(Index: Integer): @Type@;
begin
inherited Pick(Index,@Result);
end;
//==============================================================================
constructor @FIFOClassName@.Create(CircularCapacity: Integer = -1);
begin
inherited Create(omFIFO,CircularCapacity);
end;
//==============================================================================
constructor @LIFOClassName@.Create(CircularCapacity: Integer = -1);
begin
inherited Create(omLIFO,CircularCapacity);
end;
*******************************************************************************)
unit SequentialVectors;
{
SequentialVectors_UseAuxExceptions
If you want library-specific exceptions to be based on more advanced classes
provided by AuxExceptions library instead of basic Exception class, and don't
want to or cannot change code in this unit, you can define global symbol
SequentialVectors_UseAuxExceptions to achieve this.
}
{$IF Defined(SequentialVectors_UseAuxExceptions)}
{$DEFINE UseAuxExceptions}
{$IFEND}
//------------------------------------------------------------------------------
{$IFDEF FPC}
{$MODE ObjFPC}
{$MODESWITCH DuplicateLocals+}
{$MODESWITCH ClassicProcVars+}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{$ENDIF}
{$H+}
interface
uses
SysUtils, Classes,
AuxTypes, AuxClasses{$IFDEF UseAuxExceptions}, AuxExceptions{$ENDIF};
{===============================================================================
Library-specific exceptions
===============================================================================}
type
ESVException = class({$IFDEF UseAuxExceptions}EAEGeneralException{$ELSE}Exception{$ENDIF});
ESVIndexOutOfBounds = class(ESVException);
ESVInvalidOperation = class(ESVException);
ESVInvalidValue = class(ESVException);
ESVNoItem = class(ESVException);
{===============================================================================
--------------------------------------------------------------------------------
TSequentialVector
--------------------------------------------------------------------------------
===============================================================================}
type
TSVOperationMode = (omFIFO,omLIFO);
{===============================================================================
TSequentialVector - class declaration
===============================================================================}
type
TSequentialVector = class(TCustomListObject)
protected
fOperationMode: TSVOperationMode;
fPeekMethod: procedure(ItemPtr: Pointer) of object;
fPopMethod: procedure(ItemPtr: Pointer) of object;
fItemSize: TMemSize;
fMemory: Pointer;
fMemorySize: TMemSize;
fCapacity: Integer;
fCount: Integer;
fHighMemory: Pointer;
fFirstItemPosition: Integer;
fCircularCapacity: Integer;
fUpdateCounter: Integer;
fChanged: Boolean;
fOnChangeEvent: TNotifyEvent;
fOnChangeCallback: TNotifyCallback;
// getters, setters
Function GetItemPtr(Index: Integer): Pointer; virtual;
procedure GetItem(Index: Integer; DstPtr: Pointer); virtual;
procedure SetItem(Index: Integer; SrcPtr: Pointer); virtual;
// inherited list methods
Function GetCapacity: Integer; override;
procedure SetCapacity(Value: Integer); override;
Function GetCount: Integer; override;
procedure SetCount(Value: Integer); override;
// items management
procedure ItemFinal(Item: Pointer); virtual;
procedure ItemAssign(SrcItem,DstItem: Pointer); virtual;
Function ItemCompare(Item1,Item2: Pointer): Integer; virtual; abstract;
Function ItemEquals(Item1,Item2: Pointer): Boolean; virtual;
procedure ItemWrite(Item: Pointer; Stream: TStream); virtual;
procedure ItemRead(Item: Pointer; Stream: TStream); virtual;
// init/final
procedure Initialize(OperationMode: TSVOperationMode; ItemSize: TMemSize; CircularCapacity: Integer); virtual;
procedure Finalize; virtual;
// internals
Function ItemsMemorySize(Count: Integer): TMemSize; virtual;
Function NextItemPtr(ItemPtr: Pointer): Pointer; virtual;
procedure DoChange; virtual;
procedure FinalizeAllItems; virtual;
class Function ManagedItemStreaming: Boolean; virtual;
procedure PeekFirst(ItemPtr: Pointer); virtual;
procedure PeekLast(ItemPtr: Pointer); virtual;
procedure PopFirst(ItemPtr: Pointer); virtual;
procedure PopLast(ItemPtr: Pointer); virtual;
procedure InternalReadFromStream(Stream: TStream); virtual;
public
constructor Create(OperationMode: TSVOperationMode; ItemSize: TMemSize; CircularCapacity: Integer = -1);
destructor Destroy; override;
procedure BeginUpdate; virtual;
procedure EndUpdate; virtual;
Function LowIndex: Integer; override;
Function HighIndex: Integer; override;
// vector control
Function IndexOf(ItemPtr: Pointer): Integer; virtual;
Function Find(ItemPtr: Pointer; out Index: Integer): Boolean; virtual;
procedure Push(ItemPtr: Pointer); virtual;
procedure Peek(ItemPtr: Pointer); virtual;
procedure Pop(ItemPtr: Pointer); virtual;
procedure Pick(Index: Integer; ItemPtr: Pointer); virtual;
procedure Clear; virtual;
// I/O
procedure WriteToStream(Stream: TStream); virtual;
procedure ReadFromStream(Stream: TStream); virtual;
procedure SaveToStream(Stream: TStream); virtual;
procedure LoadFromStream(Stream: TStream); virtual;
procedure WriteToFile(const FileName: String); virtual;
procedure ReadFromFile(const FileName: String); virtual;
procedure SaveToFile(const FileName: String); virtual;
procedure LoadFromFile(const FileName: String); virtual;
// properties
// change inherited list RW properties to read-only
property Capacity read GetCapacity;
property Count read GetCount;
property ItemSize: TMemSize read fItemSize;
property Memory: Pointer read fMemory;
property MemorySize: TMemSize read fMemorySize;
property CircularCapacity: Integer read fCircularCapacity;
property Pointers[Index: Integer]: Pointer read GetItemPtr;
property OnChange: TNotifyEvent read fOnChangeEvent write fOnChangeEvent;
property OnChangeEvent: TNotifyEvent read fOnChangeEvent write fOnChangeEvent;
property OnChangeCallback: TNotifyCallback read fOnChangeCallback write fOnChangeCallback;
end;
{===============================================================================
--------------------------------------------------------------------------------
TIntegerSequentialVector
--------------------------------------------------------------------------------
===============================================================================}
{$IF SizeOf(Integer) <> 4}
{$MESSAGE WARN 'Incompatible integer size (expected 4B).'}
{$IFEND}
{===============================================================================
TIntegerSequentialVector - class declaration
===============================================================================}
type
TIntegerSequentialVector = class(TSequentialVector)
protected
Function GetItem(Index: Integer): Integer; reintroduce;
procedure SetItem(Index: Integer; NewValue: Integer); reintroduce;
procedure ItemAssign(SrcItem,DstItem: Pointer); override;
Function ItemCompare(Item1,Item2: Pointer): Integer; override;
Function ItemEquals(Item1,Item2: Pointer): Boolean; override;
procedure ItemWrite(Item: Pointer; Stream: TStream); override;
procedure ItemRead(Item: Pointer; Stream: TStream); override;
class Function ManagedItemStreaming: Boolean; override;
public
constructor Create(OperationMode: TSVOperationMode; CircularCapacity: Integer = -1);
Function IndexOf(Item: Integer): Integer; reintroduce;
Function Find(Item: Integer; out Index: Integer): Boolean; reintroduce;
procedure Push(Item: Integer); reintroduce;
Function Peek: Integer; reintroduce;
Function Pop: Integer; reintroduce;
Function Pick(Index: Integer): Integer; reintroduce;
property Items[Index: Integer]: Integer read GetItem write SetItem;
end;
{===============================================================================
--------------------------------------------------------------------------------
TIntegerFIFOVector
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TIntegerFIFOVector - class declaration
===============================================================================}
type
TIntegerFIFOVector = class(TIntegerSequentialVector)
public
constructor Create(CircularCapacity: Integer = -1);
end;
// aliasses
TIntegerFirstInFirstOutVector = TIntegerFIFOVector;
TIntegerQueueVector = TIntegerFIFOVector;
{===============================================================================
--------------------------------------------------------------------------------
TIntegerLIFOVector
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TIntegerLIFOVector - class declaration
===============================================================================}
type
TIntegerLIFOVector = class(TIntegerSequentialVector)
public
constructor Create(CircularCapacity: Integer = -1);
end;
// aliasses
TIntegerLastInFirstOutVector = TIntegerLIFOVector;
TIntegerStackVector = TIntegerLIFOVector;
implementation
uses
StrRect, BinaryStreamingLite;
{$IFDEF FPC_DisableWarns}
{$DEFINE FPCDWM}
{$DEFINE W4055:={$WARN 4055 OFF}} // Conversion between ordinals and pointers is not portable
{$DEFINE W5024:={$WARN 5024 OFF}} // Parameter "$1" not used
{$ENDIF}
{===============================================================================
--------------------------------------------------------------------------------
TSequentialVector
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TSequentialVector - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TSequentialVector - protected methods
-------------------------------------------------------------------------------}
Function TSequentialVector.GetItemPtr(Index: Integer): Pointer;
begin
If CheckIndex(Index) then
begin
// convert item index to item position and then to its address
If Index >= (fCapacity - fFirstItemPosition) then
{$IFDEF FPCDWM}{$PUSH}W4055{$ENDIF}
Result := Pointer(PtrUInt(fMemory) + (PtrUInt(ItemsMemorySize(Index - (fCapacity - fFirstItemPosition)))))
else
Result := Pointer(PtrUInt(fMemory) + (PtrUInt(ItemsMemorySize(fFirstItemPosition + Index))));
{$IFDEF FPCDWM}{$POP}{$ENDIF}
end
else raise ESVIndexOutOfBounds.CreateFmt('TSequentialVector.GetItemPtr: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.GetItem(Index: Integer; DstPtr: Pointer);
begin
ItemAssign(GetItemPtr(Index),DstPtr);
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.SetItem(Index: Integer; SrcPtr: Pointer);
var
ItemPtr: Pointer;
begin
ItemPtr := GetItemPtr(Index);
If not ItemEquals(ItemPtr,SrcPtr) then
begin
ItemAssign(SrcPtr,ItemPtr);
DoChange;
end;
end;
//------------------------------------------------------------------------------
Function TSequentialVector.GetCapacity: Integer;
begin
Result := fCapacity;
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.SetCapacity(Value: Integer);
var
ItemsToMove: Integer;
begin
If Value < 0 then
raise ESVInvalidValue.CreateFmt('TSequentialVector.SetCapacity: Invalid new capacity (%d).',[Value]);
If Value < fCount then
raise ESVInvalidOperation.CreateFmt('TSequentialVector.SetCapacity: Cannot lower capacity (%d) below count (%d).',[Value,fCount]);
If (Value <> fCapacity) and (fCircularCapacity <= 0) then
begin
If fCount > 0 then
begin
// there are some items, we need to copy them to newly allocated memory
If fCount > (fCapacity - fFirstItemPosition) then
ItemsToMove := fCapacity - fFirstItemPosition
else
ItemsToMove := 0;
fMemorySize := ItemsMemorySize(Value);
ReallocMem(fMemory,fMemorySize);
fCapacity := Value;
{$IFDEF FPCDWM}{$PUSH}W4055{$ENDIF}
fHighMemory := Pointer(PtrUInt(fMemory) + fMemorySize);
If ItemsToMove > 0 then
Move(Pointer(PtrUInt(fMemory) + ItemsMemorySize(fFirstItemPosition))^,
Pointer(PtrUInt(PtrUInt(fHighMemory) - PtrUInt(ItemsMemorySize(ItemsToMove))))^,
ItemsMemorySize(ItemsToMove));
{$IFDEF FPCDWM}{$POP}{$ENDIF}
fFirstItemPosition := fCapacity - ItemsToMove;
end
else
begin
// do not make copy if there is no item
FreeMem(fMemory,fMemorySize);
fMemorySize := ItemsMemorySize(Value);
fMemory := AllocMem(fMemorySize);
fCapacity := Value;
{$IFDEF FPCDWM}{$PUSH}W4055{$ENDIF}
fHighMemory := Pointer(PtrUInt(fMemory) + fMemorySize);
{$IFDEF FPCDWM}{$POP}{$ENDIF}
fFirstItemPosition := 0;
end;
end;
end;
//------------------------------------------------------------------------------
Function TSequentialVector.GetCount: Integer;
begin
Result := fCount;
end;
//------------------------------------------------------------------------------
{$IFDEF FPCDWM}{$PUSH}W5024{$ENDIF}
procedure TSequentialVector.SetCount(Value: Integer);
begin
raise ESVInvalidOperation.Create('TSequentialVector.SetCount: Explicitly setting count is not allowed.');
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
//------------------------------------------------------------------------------
{$IFDEF FPCDWM}{$PUSH}W5024{$ENDIF}
procedure TSequentialVector.ItemFinal(Item: Pointer);
begin
// do nothing
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
//------------------------------------------------------------------------------
procedure TSequentialVector.ItemAssign(SrcItem,DstItem: Pointer);
begin
Move(SrcItem^,DstItem^,fItemSize);
end;
//------------------------------------------------------------------------------
Function TSequentialVector.ItemEquals(Item1,Item2: Pointer): Boolean;
begin
Result := ItemCompare(Item1,Item2) = 0;
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.ItemWrite(Item: Pointer; Stream: TStream);
begin
Stream.WriteBuffer(Item^,fItemSize);
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.ItemRead(Item: Pointer; Stream: TStream);
begin
Stream.ReadBuffer(Item^,fItemSize);
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.Initialize(OperationMode: TSVOperationMode; ItemSize: TMemSize; CircularCapacity: Integer);
begin
fOperationMode := OperationMode;
case fOperationMode of
omFIFO: begin
fPeekMethod := PeekFirst;
fPopMethod := PopFirst;
end;
omLIFO: begin
fPeekMethod := PeekLast;
fPopMethod := PopLast;
end;
else
raise ESVInvalidValue.CreateFmt('TSequentialVector.Initialize: Invalid operation mode (%d).',[Ord(fOperationMode)]);
end;
fItemSize := ItemSize;
fMemory := nil;
fMemorySize := 0;
fCapacity := 0;
fCount := 0;
fHighMemory := fMemory;
fFirstItemPosition := 0;
fCircularCapacity := 0;
If CircularCapacity > 0 then
SetCapacity(CircularCapacity);
fCircularCapacity := CircularCapacity;
fUpdateCounter := 0;
fChanged := False;
fOnChangeEvent := nil;
fOnChangeCallback := nil;
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.Finalize;
begin
FinalizeAllItems;
FreeMem(fMemory,fMemorySize);
end;
//------------------------------------------------------------------------------
Function TSequentialVector.ItemsMemorySize(Count: Integer): TMemSize;
begin
Result := PtrUInt(Count) * PtrUInt(fItemSize);
end;
//------------------------------------------------------------------------------
Function TSequentialVector.NextItemPtr(ItemPtr: Pointer): Pointer;
begin
{$IFDEF FPCDWM}{$PUSH}W4055{$ENDIF}
Result := Pointer(PtrUInt(ItemPtr) + PtrUInt(fItemSize));
If PtrUInt(Result) >= PtrUInt(fHighMemory) then
Result := fMemory;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.DoChange;
begin
fChanged := True;
If (fUpdateCounter <= 0) then
begin
If Assigned(fOnChangeEvent) then
fOnChangeEvent(Self)
else If Assigned(fOnChangeCallback) then
fOnChangeCallback(Self);
end;
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.FinalizeAllItems;
var
i: Integer;
TempPtr: Pointer;
begin
If fCount > 0 then
begin
TempPtr := GetItemPtr(LowIndex);
For i := 1 to fCount do
begin
ItemFinal(TempPtr);
TempPtr := NextItemPtr(TempPtr);
end;
end;
end;
//------------------------------------------------------------------------------
class Function TSequentialVector.ManagedItemStreaming: Boolean;
begin
Result := False;
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.PeekFirst(ItemPtr: Pointer);
begin
ItemAssign(GetItemPtr(LowIndex),ItemPtr);
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.PeekLast(ItemPtr: Pointer);
begin
ItemAssign(GetItemPtr(HighIndex),ItemPtr);
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.PopFirst(ItemPtr: Pointer);
begin
ItemAssign(GetItemPtr(LowIndex),ItemPtr);
Inc(fFirstItemPosition);
Dec(fCount);
If (fFirstItemPosition >= fCapacity) or (fCount <= 0) then
fFirstItemPosition := 0;
Shrink;
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.PopLast(ItemPtr: Pointer);
begin
ItemAssign(GetItemPtr(HighIndex),ItemPtr);
Dec(fCount);
If fCount <= 0 then
fFirstItemPosition := 0;
Shrink;
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.InternalReadFromStream(Stream: TStream);
var
i: Integer;
TempPtr: Pointer;
begin
// there should be no valid item in the vector by now...
fFirstItemPosition := 0;
If ManagedItemStreaming then
begin
// managed IO
TempPtr := GetItemPtr(LowIndex);
For i := 1 to fCount do
begin
ItemRead(TempPtr,Stream);
TempPtr := NextItemPtr(TempPtr);
end;
end
// unmanaged IO, read everything in one go
else Stream.ReadBuffer(fMemory^,ItemsMemorySize(fCount));
DoChange;
end;
{-------------------------------------------------------------------------------
TSequentialVector - public methods
-------------------------------------------------------------------------------}
constructor TSequentialVector.Create(OperationMode: TSVOperationMode; ItemSize: TMemSize; CircularCapacity: Integer = -1);
begin
inherited Create;
Initialize(OperationMode,ItemSize,CircularCapacity);
end;
//------------------------------------------------------------------------------
destructor TSequentialVector.Destroy;
begin
Finalize;
inherited;
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.BeginUpdate;
begin
If fUpdateCounter <= 0 then
begin
fUpdateCounter := 0;
fChanged := False;
end;
Inc(fUpdateCounter);
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.EndUpdate;
begin
Dec(fUpdateCounter);
If fUpdateCounter <= 0 then
begin
fUpdateCounter := 0;
If fChanged then
DoChange;
fChanged := False;
end;
end;
//------------------------------------------------------------------------------
Function TSequentialVector.LowIndex: Integer;
begin
Result := 0;
end;
//------------------------------------------------------------------------------
Function TSequentialVector.HighIndex: Integer;
begin
Result := Pred(fCount);
end;
//------------------------------------------------------------------------------
Function TSequentialVector.IndexOf(ItemPtr: Pointer): Integer;
var
i: Integer;
TempPtr: Pointer;
begin
Result := -1;
If fCount > 0 then
begin
TempPtr := GetItemPtr(LowIndex);
For i := 1 to fCount do
If ItemEquals(ItemPtr,TempPtr) then
begin
Result := i;
Break{For i};
end
else TempPtr := NextItemPtr(TempPtr);
end;
end;
//------------------------------------------------------------------------------
Function TSequentialVector.Find(ItemPtr: Pointer; out Index: Integer): Boolean;
begin
Index := IndexOf(ItemPtr);
Result := CheckIndex(Index);
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.Push(ItemPtr: Pointer);
var
ChangedItem: Pointer;
begin
// push is the same for lifo and fifo
If (fCircularCapacity > 0) and (fCount >= fCircularCapacity) and (fCircularCapacity = fCapacity) then
begin
{
Operating as circular buffer with limited size - remove oldest item and
replace it with the new one. Remember to do implicit item cleanup.
}
ChangedItem := GetItemPtr(LowIndex);
ItemFinal(ChangedItem);
ItemAssign(ItemPtr,ChangedItem);
Inc(fFirstItemPosition);
If fFirstItemPosition >= fCapacity then
fFirstItemPosition := 0;
DoChange;
end
else
begin
// vanilla stuff
Grow;
Inc(fCount);
ItemAssign(ItemPtr,GetItemPtr(HighIndex));
DoChange;
end;
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.Peek(ItemPtr: Pointer);
begin
If fCount > 0 then
fPeekMethod(ItemPtr)
else
raise ESVNoItem.Create('TSequentialVector.Peek: Cannot peek an empty vector.');
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.Pop(ItemPtr: Pointer);
begin
If fCount > 0 then
begin
fPopMethod(ItemPtr);
DoChange;
end
else raise ESVNoItem.Create('TSequentialVector.Pop: Cannot pop an empty vector.');
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.Pick(Index: Integer; ItemPtr: Pointer);
var
CurrItem: Pointer;
NextItem: Pointer;
i: Integer;
begin
If CheckIndex(Index) then
begin
If Index = LowIndex then
PopFirst(ItemPtr)
else If Index = HighIndex then
PopLast(ItemPtr)
else
begin
// if we are here, it means there are at least 3 items!
ItemAssign(GetItemPtr(Index),ItemPtr);
CurrItem := GetItemPtr(Index);
NextItem := NextItemPtr(CurrItem);
For i := Index to Pred(HighIndex) do
begin
Move(NextItem^,CurrItem^,fItemSize);
// following should be faster than constantly calling GetItemPtr
CurrItem := NextItem;
NextItem := NextItemPtr(CurrItem);
end;
Dec(fCount);
Shrink;
end;
DoChange;
end
else raise ESVIndexOutOfBounds.CreateFmt('TSequentialVector.Pick: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
procedure TSequentialVector.Clear;
begin