forked from Sovos-Compliance/convey-public-libs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
uStringRing.pas
43 lines (36 loc) · 822 Bytes
/
uStringRing.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
unit uStringRing;
interface
type
TStringRing = class
private
FStrings : array of String;
FCurIndex : Integer;
public
constructor Create(AElementCount : Cardinal);
function NextString(const s: String): String;
end;
implementation
uses
Windows;
constructor TStringRing.Create(AElementCount : Cardinal);
begin
inherited Create;
SetLength(FStrings, AElementCount);
FCurIndex := -1;
end;
function TStringRing.NextString(const s: String): String;
var
ACurIndex : Integer;
begin
repeat
ACurIndex := InterlockedIncrement(FCurIndex);
if ACurIndex > high(FStrings) then
begin
InterlockedCompareExchange(FCurIndex, -1, ACurIndex);
continue;
end;
until ACurIndex <= high(FStrings);
FStrings[ACurIndex] := s;
Result := FStrings[ACurIndex];
end;
end.