| 1 |
unit objlist; |
| 2 |
|
| 3 |
{$mode objfpc}{$H+} |
| 4 |
|
| 5 |
interface |
| 6 |
uses base; |
| 7 |
|
| 8 |
const |
| 9 |
MaxListSize = Maxint div 16; |
| 10 |
type |
| 11 |
PObjectArray = ^TObjectArray; |
| 12 |
TObjectArray = array[0..MaxListSize - 1] of TObject; |
| 13 |
|
| 14 |
TObjectList = class(TObject) |
| 15 |
private |
| 16 |
FList: PObjectArray; |
| 17 |
FCount: Integer; |
| 18 |
FCapacity: Integer; |
| 19 |
function Get(Index: Integer): TObject; |
| 20 |
procedure Grow; |
| 21 |
procedure Put(Index: Integer; Item: TObject); |
| 22 |
procedure SetCapacity(NewCapacity: Integer); |
| 23 |
procedure SetCount(NewCount: Integer); |
| 24 |
procedure Clear; |
| 25 |
function Expand: TObjectList; |
| 26 |
public |
| 27 |
constructor create(IniSize:integer); |
| 28 |
destructor Destroy; override; |
| 29 |
function Add(Item: TObject): Integer; |
| 30 |
procedure Insert(Index: Integer; Item: TObject); |
| 31 |
procedure Delete(Index: Integer); |
| 32 |
procedure deleteall; |
| 33 |
procedure FreeItem(item:TObject); |
| 34 |
procedure FreeAll; |
| 35 |
property Capacity: Integer read FCapacity write SetCapacity; |
| 36 |
property Count: Integer read FCount write SetCount; |
| 37 |
property Items[Index: Integer]: TObject read Get write Put; default; |
| 38 |
//property List: PObjectArray read FList; |
| 39 |
function KeyOf(item:TObject):AnsiString;virtual; abstract; |
| 40 |
function search(const key:AnsiString; var index:integer):boolean; |
| 41 |
end; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
implementation |
| 47 |
|
| 48 |
|
| 49 |
destructor TObjectList.Destroy; |
| 50 |
begin |
| 51 |
FreeAll; |
| 52 |
Clear; |
| 53 |
inherited Destroy; |
| 54 |
end; |
| 55 |
|
| 56 |
constructor TObjectList.create(IniSize:integer); |
| 57 |
begin |
| 58 |
inherited create; |
| 59 |
setCapacity(IniSize); |
| 60 |
end; |
| 61 |
|
| 62 |
function TObjectList.Add(Item: TObject): Integer; |
| 63 |
begin |
| 64 |
Result := FCount; |
| 65 |
if Result = FCapacity then Grow; |
| 66 |
FList^[Result] := Item; |
| 67 |
Inc(FCount); |
| 68 |
end; |
| 69 |
|
| 70 |
procedure TObjectList.Insert(Index: Integer; Item: TObject); |
| 71 |
begin |
| 72 |
if (Index < 0) or (Index > FCount) then exit; //Error(SListIndexError, Index); |
| 73 |
if FCount = FCapacity then Grow; |
| 74 |
if Index < FCount then |
| 75 |
System.Move(FList^[Index], FList^[Index + 1], |
| 76 |
(FCount - Index) * SizeOf(Pointer)); |
| 77 |
FList^[Index] := Item; |
| 78 |
Inc(FCount); |
| 79 |
end; |
| 80 |
|
| 81 |
procedure TObjectList.Clear; |
| 82 |
begin |
| 83 |
SetCount(0); |
| 84 |
SetCapacity(0); |
| 85 |
end; |
| 86 |
|
| 87 |
procedure TObjectList.Delete(Index: Integer); |
| 88 |
begin |
| 89 |
if (Index < 0) or (Index >= FCount) then Exit; |
| 90 |
Dec(FCount); |
| 91 |
if Index < FCount then |
| 92 |
System.Move(FList^[Index + 1], FList^[Index], |
| 93 |
(FCount - Index) * SizeOf(Pointer)); |
| 94 |
end; |
| 95 |
|
| 96 |
function TObjectList.Expand: TObjectList; |
| 97 |
begin |
| 98 |
if FCount = FCapacity then Grow; |
| 99 |
Result := Self; |
| 100 |
end; |
| 101 |
|
| 102 |
function TObjectList.Get(Index: Integer): TObject; |
| 103 |
begin |
| 104 |
// if (Index < 0) or (Index >= FCount) then Error(SListIndexError, Index); |
| 105 |
Result := FList^[Index]; |
| 106 |
end; |
| 107 |
|
| 108 |
procedure TObjectList.Grow; |
| 109 |
var |
| 110 |
Delta: Integer; |
| 111 |
begin |
| 112 |
if Fcapacity >= 16384 then |
| 113 |
Delta:=16384 |
| 114 |
else if FCapacity >= 16 then |
| 115 |
Delta := FCapacity |
| 116 |
else Delta := 4 ; |
| 117 |
SetCapacity(FCapacity + Delta); |
| 118 |
end; |
| 119 |
|
| 120 |
procedure TObjectList.Put(Index: Integer; Item: TObject); |
| 121 |
begin |
| 122 |
FList^[Index] := Item; |
| 123 |
end; |
| 124 |
|
| 125 |
procedure TObjectList.SetCapacity(NewCapacity: Integer); |
| 126 |
begin |
| 127 |
if (NewCapacity<FCount) or (NewCapacity > MaxListSize) then |
| 128 |
setexception(5000); |
| 129 |
if NewCapacity mod 4 <>0 then NewCapacity:=(((NewCapacity div 4)+1)*4); |
| 130 |
if NewCapacity <> FCapacity then |
| 131 |
begin |
| 132 |
ReallocMem(FList, NewCapacity * SizeOf(TObject)); |
| 133 |
FCapacity := NewCapacity; |
| 134 |
end; |
| 135 |
end; |
| 136 |
|
| 137 |
procedure TObjectList.SetCount(NewCount: Integer); |
| 138 |
begin |
| 139 |
if NewCount > FCapacity then SetCapacity(NewCount); |
| 140 |
if NewCount > FCount then |
| 141 |
FillChar(FList^[FCount], (NewCount - FCount) * SizeOf(Pointer), 0); |
| 142 |
FCount := NewCount; |
| 143 |
end; |
| 144 |
|
| 145 |
procedure TObjectList.deleteAll; |
| 146 |
var |
| 147 |
i:integer; |
| 148 |
begin |
| 149 |
for i:=count-1 downto 0 do |
| 150 |
Delete(i); |
| 151 |
end; |
| 152 |
|
| 153 |
procedure TObjectList.FreeItem(item:TObject); |
| 154 |
begin |
| 155 |
Item.Free; |
| 156 |
end; |
| 157 |
|
| 158 |
procedure TObjectList.FreeAll; |
| 159 |
var |
| 160 |
Temp: TObject; |
| 161 |
i:integer; |
| 162 |
begin |
| 163 |
for i:=count-1 downto 0 do |
| 164 |
begin |
| 165 |
Temp := Items[i]; |
| 166 |
FreeItem(Temp); { Delete Item } |
| 167 |
Delete(i); { Free item from list } |
| 168 |
end; |
| 169 |
end; |
| 170 |
|
| 171 |
function TObjectList.search(const key:AnsiString; var index:integer):boolean; |
| 172 |
var |
| 173 |
found:boolean; |
| 174 |
begin |
| 175 |
index:=0; |
| 176 |
found:=false; |
| 177 |
while (index<count) and not found do |
| 178 |
begin |
| 179 |
if key=KeyOf(items[index]) then |
| 180 |
found:=true |
| 181 |
else |
| 182 |
index:=index+1; |
| 183 |
end; |
| 184 |
search:=found; |
| 185 |
end; |
| 186 |
|
| 187 |
|
| 188 |
begin |
| 189 |
|
| 190 |
|
| 191 |
end. |
| 192 |
|