aboutsummaryrefslogtreecommitdiffstats
path: root/ServiceBasedPlugins/src/lib/collections/CollArray.pas
blob: a10ba905ea835106d53055fb673bc7c58f3c2809 (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
unit CollArray;

(*****************************************************************************
 * 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.2 $
 * $Log: D:\QVCS Repositories\Delphi Collections\CollArray.qbt $
 * 
 *   Colllection implementations based on arrays.
 * 
 * Revision 1.2  by: Matthew Greet  Rev date: 12/06/04 20:02:16
 *   Capacity property.
 * 
 * Revision 1.1  by: Matthew Greet  Rev date: 06/04/03 10:30:36
 *   Size property dropped. 
 *   Unused abstract functions still implemented.
 * 
 * Revision 1.0  by: Matthew Greet  Rev date: 01/03/03 10:50:02
 *   Initial revision.
 *
 * FPC compatibility fixes by: UltraStar Deluxe Team
 *
 * $Endlog$
 *****************************************************************************)

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

uses
    Collections;

type
    TArray = class(TAbstractList)
    private
        FArray: array of ICollectable;
    protected
        function TrueGetItem(Index: Integer): ICollectable; override;
        procedure TrueSetItem(Index: Integer; const Value: ICollectable); override;
        procedure TrueAppend(const Item: ICollectable); override;
        procedure TrueClear; override;
        function TrueDelete(Index: Integer): ICollectable; override;
        procedure TrueInsert(Index: Integer; const Item: ICollectable); override;
    public
        constructor Create(NaturalItemsOnly: Boolean); override;
        constructor Create(const ItemArray: array of ICollectable; NaturalItemsOnly: Boolean = false); override;
        constructor Create(Size: Integer; NaturalItemsOnly: Boolean = false); overload; virtual;
        constructor Create(const Collection: ICollection); override;
        destructor Destroy; override;
        function GetCapacity: Integer; override;
        procedure SetCapacity(Value: Integer); override;
        function GetFixedSize: Boolean; override;
        function GetSize: Integer; override;
    end;

implementation

constructor TArray.Create(NaturalItemsOnly: Boolean);
begin
    Create(0, NaturalItemsOnly);
end;

constructor TArray.Create(Size: Integer; NaturalItemsOnly: Boolean = false);
begin
    inherited Create(NaturalItemsOnly);
    SetLength(FArray, Size);
end;

constructor TArray.Create(const ItemArray: array of ICollectable; NaturalItemsOnly: Boolean);
var
    Item: ICollectable;
    ItemError: TCollectionError;
    I: Integer;
begin
    inherited Create(ItemArray, NaturalItemsOnly);
    SetLength(FArray, Length(ItemArray));
    for I := Low(ItemArray) to High(ItemArray) do
    begin
        Item := ItemArray[I];
        ItemError := ItemAllowed(Item);
        if ItemError <> ceOK then
        begin
            CollectionError(ItemError);
        end
        else
            Items[I] := Item;
    end;
end;

constructor TArray.Create(const Collection: ICollection);
var
    Iterator: IIterator;
    I: Integer;
begin
    inherited Create(Collection);
    SetLength(FArray, Collection.GetSize);
    Iterator := Collection.GetIterator;
    I := 0;
    while not Iterator.EOF do
    begin
        Items[I] := Iterator.CurrentItem;
        Inc(I);
        Iterator.Next;
    end;
end;

destructor TArray.Destroy;
var
    I: Integer;
begin
    // Delete interface references to all items
    for I := Low(FArray) to High(FArray) do
    begin
        FArray[I] := nil;
    end;
    inherited Destroy;
end;

function TArray.TrueGetItem(Index: Integer): ICollectable;
begin
    Result := FArray[Index];
end;

procedure TArray.TrueSetItem(Index: Integer; const Value: ICollectable);
begin
    FArray[Index] := Value;
end;

procedure TArray.TrueAppend(const Item: ICollectable);
begin
    // Ignored as collection is fixed size
end;

procedure TArray.TrueClear;
begin
    // Ignored as collection is fixed size
end;

function TArray.TrueDelete(Index: Integer): ICollectable;
begin
    // Ignored as collection is fixed size
end;

procedure TArray.TrueInsert(Index: Integer; const Item: ICollectable);
begin
    // Ignored as collection is fixed size
end;

function TArray.GetCapacity: Integer;
begin
    Result := Size;
end;

procedure TArray.SetCapacity(Value: Integer);
begin
    // Ignored
end;

function TArray.GetFixedSize: Boolean;
begin
    Result := true;
end;

function TArray.GetSize: Integer;
begin
    Result := Length(FArray);
end;

end.