-
Notifications
You must be signed in to change notification settings - Fork 0
/
uStrListNotDupl.pas
204 lines (184 loc) · 4.73 KB
/
uStrListNotDupl.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
unit uStrListNotDupl;
interface
uses Windows, Classes, SysUtils, AcedContainers;
type
TStrListNotDupl = class(TObject)
private
FCaseSensitive: Boolean;
FArray: TArrayList;
FCount: Integer;
function GetItem(const Key: string): Pointer;
public
constructor Create(ACaseSensitive:Boolean=False);
destructor Destroy; override;
function IndexOf(const aStr: string): Integer; overload;
function IndexOf(const aStr: string; var OIndex:Integer): Integer; overload;
function Add(const aStr: string):Boolean; overload;
function Add(const aStr: string; AValue:Pointer):Boolean; overload;
// function AddValueIfExists(const AStr: string):Pointer;
function SearchKey(const AKey:string; var AValue:Integer):Boolean;
procedure AddStrings(const aStrs: TStrings);
function Remove(const aStr: string):Boolean;
procedure LoadFromFile(const aFileName:string);
procedure SaveToFile(const aFileName:string);
procedure Clear;
property Count:Integer read FCount;
// property NotDupl:Integer read FNotDupl write FNotDupl;
property Items[const Key: string]: Pointer read GetItem;
end;
implementation
const
MAX_LIST_SIZE = 25000;
function TStrListNotDupl.Add(const aStr: string):Boolean;
begin
Result := Add(aStr, nil)
end;
function TStrListNotDupl.Add(const AStr: string;
AValue: Pointer): Boolean;
var sl : TStringAssociationList;
begin
Result := False;
if IndexOf(aStr)=-1 then
begin
sl := TStringAssociationList(FArray.PeekBack);
if sl.Count>=MAX_LIST_SIZE then
FArray.Add(TStringAssociationList.Create(FCaseSensitive, MAX_LIST_SIZE));
sl.Add(aStr, AValue);
Inc(FCount);
Result := True;
end
end;
function TStrListNotDupl.Remove(const aStr: string):Boolean;
var
sl : TStringAssociationList;
oi, i : Integer;
begin
Result := False;
i := IndexOf(aStr, oi);
if i<>-1 then
begin
sl := TStringAssociationList(FArray.ItemList^[oi]);
sl.RemoveAt(i);
if sl.Count=0 then
FArray.RemoveAt(oi) {Delete(oi)};
Dec(FCount);
Result := True;
end
end;
procedure TStrListNotDupl.AddStrings(const aStrs: TStrings);
var j:Integer;
begin
for j:=0 to aStrs.Count-1 do
Add(aStrs[j])
end;
procedure TStrListNotDupl.Clear;
begin
FArray.Clear();
FArray.Add(TStringAssociationList.Create(FCaseSensitive, MAX_LIST_SIZE));
end;
constructor TStrListNotDupl.Create(ACaseSensitive:Boolean);
begin
FCount := 0;
FCaseSensitive := ACaseSensitive;
FArray := TArrayList.Create(1024);
FArray.OwnItems := True;
Clear();
// FArray.Add(TStringAssociationList.Create(FCaseSensitive, MAX_LIST_SIZE));
end;
destructor TStrListNotDupl.Destroy;
begin
FArray.Free;
inherited;
end;
function TStrListNotDupl.GetItem(const Key: string): Pointer;
var i0,i2:Integer;
begin
i2 := IndexOf(Key, i0);
if (i0<>-1) and (i2<>-1) then
Result := TStringAssociationList(FArray.ItemList^[i0]).ValueList^[i2]
else
Result := nil;
end;
function TStrListNotDupl.IndexOf(const aStr: string;
var OIndex: Integer): Integer;
var
i,j : Integer;
aList : TStringAssociationList;
begin
Result := -1;
OIndex := -1;
for i:=0 to FArray.Count-1 do
begin
aList := TStringAssociationList(FArray.ItemList^[i]);
j := aList.IndexOf(aStr);
if j<>-1 then
begin
Result := j;
OIndex := i;
exit;
end;
end;
end;
function TStrListNotDupl.IndexOf(const aStr: string): Integer;
var oi:Integer;
begin
Result := IndexOf(aStr, oi)
end;
procedure TStrListNotDupl.LoadFromFile(const aFileName: string);
var
f : TextFile;
z : String;
begin
Clear();
if FileExists(aFileName) then
begin
AssignFile(f, aFileName);
Reset(f);
while not Eof(f) do
begin
Readln(f, z);
Add(z);
end;
CloseFile(f);
end;
end;
procedure TStrListNotDupl.SaveToFile(const aFileName: string);
var
f : TextFile;
i : Integer;
j : Integer;
l : TStringAssociationList;
begin
AssignFile(f, aFileName);
Rewrite(f);
for i:=0 to FArray.Count-1 do
begin
l := TStringAssociationList(FArray.ItemList^[i]);
for j:=0 to l.Count-1 do
begin
Writeln(f, l.KeyList^[j]);
end;
end;
CloseFile(f);
end;
function TStrListNotDupl.SearchKey(const AKey: string;
var AValue: Integer): Boolean;
var
i,j : Integer;
lst : TStringAssociationList;
begin
AValue := -1;
Result := False;
for i:=0 to FArray.Count-1 do
begin
lst := TStringAssociationList(FArray.ItemList^[i]);
j := lst.IndexOf(AKey);
if j<>-1 then
begin
Result := True;
AValue := Integer(lst.ValueList^[j]);
Exit;
end;
end;
end;
end.