aboutsummaryrefslogtreecommitdiffstats
path: root/Lua/src/lib/collections/CollList.pas
blob: 68aa0d6681af22d1a5368d968133b1e07b0a16ee (plain) (blame)
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
unit CollList;

(*****************************************************************************
 * Copyright 2003 by Matthew Greet
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details. (http://opensource.org/licenses/lgpl-license.php)
 * 
 * See http://www.warmachine.u-net.com/delphi_collections for updates and downloads.
 *
 * $Version: v1.0.3 $
 * $Revision: 1.1.1.2 $
 * $Log: D:\QVCS Repositories\Delphi Collections\CollList.qbt $
 * 
 *   Collection implementations based on sorted TPArrayList instances.
 * 
 * Revision 1.1.1.2  by: Matthew Greet  Rev date: 12/06/04 20:05:54
 *   Capacity property.
 * 
 * Revision 1.1.1.1  by: Matthew Greet  Rev date: 14/02/04 17:45:38
 *   v1.0 branch.
 * 
 * Revision 1.1  by: Matthew Greet  Rev date: 06/04/03 10:41:52
 *   Uses TExposedPArrayList to improve performance.
 * 
 * Revision 1.0  by: Matthew Greet  Rev date: 01/03/03 10:50:02
 *   Initial revision.
 * 
 * FPC compatibility fixes by: UltraStar Deluxe Team
 *
 * $Endlog$
 *****************************************************************************)

interface

{$IFDEF FPC}
  {$MODE Delphi}{$H+}
{$ENDIF}

uses
    Collections, CollPArray;

type
    TListSet = class(TAbstractSet)
    private
        FList: TExposedPArrayList;
    protected
        function GetPosition(const Item: ICollectable): TCollectionPosition; override;
        procedure TrueAdd2(Position: TCollectionPosition; const Item: ICollectable); override;
        procedure TrueClear; override;
        function TrueGet(Position: TCollectionPosition): ICollectable; override;
        procedure TrueRemove2(Position: TCollectionPosition); override;
    public
        constructor Create(NaturalItemsOnly: Boolean); override;
        destructor Destroy; override;
        function GetCapacity: Integer; override;
        procedure SetCapacity(Value: Integer); override;
        function GetIterator: IIterator; override;
        function GetNaturalItemIID: TGUID; override;
        function GetSize: Integer; override;
    end;

    TListMap = class(TAbstractMap)
    private
        FList: TExposedPArrayList;
    protected
        function GetAssociationIterator: IMapIterator; override;
        function GetKeyPosition(const Key: ICollectable): TCollectionPosition; override;
        procedure TrueClear; override;
        function TrueGet(Position: TCollectionPosition): IAssociation; override;
        function TruePut(Position: TCollectionPosition; const Association: IAssociation): IAssociation; override;
        function TrueRemove2(Position: TCollectionPosition): IAssociation; override;
    public
        constructor Create(NaturalItemsOnly: Boolean; NaturalKeysOnly: Boolean); override;
        destructor Destroy; override;
        function GetCapacity: Integer; override;
        procedure SetCapacity(Value: Integer); override;
        procedure SetKeyComparator(const Value: IComparator); override;
        function GetNaturalKeyIID: TGUID; override;
        function GetSize: Integer; override;
    end;

implementation

type
    TListPosition = class(TCollectionPosition)
    private
        FSearchResult: TSearchResult;
    public
        constructor Create(Found: Boolean; SearchResult: TSearchResult);
        property SearchResult: TSearchResult read FSearchResult;
    end;

constructor TListSet.Create(NaturalItemsOnly: Boolean);
begin
    inherited Create(NaturalItemsOnly);
    FList := TExposedPArrayList.Create(NaturalItemsOnly);
    FList.Comparator := Comparator;
    FList.Sort;
end;

destructor TListSet.Destroy;
begin
    FList.Free;
    inherited Destroy;
end;

function TListSet.GetPosition(const Item: ICollectable): TCollectionPosition;
var
    SearchResult: TSearchResult;
begin
    SearchResult := FList.Search(Item);
    Result := TListPosition.Create((SearchResult.ResultType = srFoundAtIndex), SearchResult);
end;

procedure TListSet.TrueAdd2(Position: TCollectionPosition; const Item: ICollectable);
var
    SearchResult: TSearchResult;
    Index: Integer;
begin
    SearchResult := TListPosition(Position).SearchResult;
    Index := SearchResult.Index;
    if SearchResult.ResultType = srBeforeIndex then
        FList.TrueInsert(Index, Item)
    else
        FList.TrueAppend(Item);
end;

procedure TListSet.TrueClear;
begin
    FList.Clear;
end;

function TListSet.TrueGet(Position: TCollectionPosition): ICollectable;
begin
    Result := FList.Items[TListPosition(Position).SearchResult.Index];
end;

procedure TListSet.TrueRemove2(Position: TCollectionPosition);
begin
    FList.Delete(TListPosition(Position).SearchResult.Index);
end;

function TListSet.GetCapacity: Integer;
begin
    Result := FList.Capacity;
end;

procedure TListSet.SetCapacity(Value: Integer);
begin
    FList.Capacity := Value;
end;

function TListSet.GetIterator: IIterator;
begin
    Result := FList.GetIterator;
end;

function TListSet.GetNaturalItemIID: TGUID;
begin
    Result := ComparableIID;
end;

function TListSet.GetSize: Integer;
begin
    Result := FList.Size;
end;

constructor TListMap.Create(NaturalItemsOnly: Boolean; NaturalKeysOnly: Boolean);
begin
    inherited Create(NaturalItemsOnly, NaturalKeysOnly);
    FList := TExposedPArrayList.Create(false);
    FList.Comparator := AssociationComparator;
    FList.Sort;
end;

destructor TListMap.Destroy;
begin
    FList.Free;
    inherited Destroy;
end;

function TListMap.GetAssociationIterator: IMapIterator;
begin
    Result := TAssociationIterator.Create(FList.GetIterator);
end;

function TListMap.GetKeyPosition(const Key: ICollectable): TCollectionPosition;
var
    Association: IAssociation;
    SearchResult: TSearchResult;
begin
    Association := TAssociation.Create(Key, nil);
    SearchResult := FList.Search(Association);
    Result := TListPosition.Create((SearchResult.ResultType = srFoundAtIndex), SearchResult);
end;

procedure TListMap.TrueClear;
begin
    FList.Clear;
end;

function TListMap.TrueGet(Position: TCollectionPosition): IAssociation;
begin
    Result := (FList.Items[TListPosition(Position).SearchResult.Index]) as IAssociation;
end;

function TListMap.TruePut(Position: TCollectionPosition; const Association: IAssociation): IAssociation;
var
    SearchResult: TSearchResult;
    Index: Integer;
begin
    SearchResult := TListPosition(Position).SearchResult;
    Index := SearchResult.Index;
    if SearchResult.ResultType = srFoundAtIndex then
    begin
        Result := (FList.Items[Index]) as IAssociation;
        FList.Items[Index] := Association;
    end
    else if SearchResult.ResultType = srBeforeIndex then
        FList.TrueInsert(Index, Association)
    else
        FList.TrueAppend(Association);
end;

function TListMap.TrueRemove2(Position: TCollectionPosition): IAssociation;
begin
    Result := (FList.Items[TListPosition(Position).SearchResult.Index]) as IAssociation;
    FList.Delete(TListPosition(Position).SearchResult.Index);
end;

procedure TListMap.SetKeyComparator(const Value: IComparator);
begin
    inherited SetKeyComparator(Value);
    FList.Sort;
end;

function TListMap.GetCapacity: Integer;
begin
    Result := FList.Capacity;
end;

procedure TListMap.SetCapacity(Value: Integer);
begin
    FList.Capacity := Value;
end;

function TListMap.GetNaturalKeyIID: TGUID;
begin
    Result := ComparableIID;
end;

function TListMap.GetSize: Integer;
begin
    Result := FList.Size;
end;

constructor TListPosition.Create(Found: Boolean; SearchResult: TSearchResult);
begin
    inherited Create(Found);
    FSearchResult := SearchResult;
end;

end.