Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2019-11

Pre-allocated TFPGList or similar

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ryan Joseph

I need a pre-grown list which I can put (not insert!) items into at indexes without getting "List index out of bounds" errors. For example I want to start with a list that has 10 empty indexes:

list := TList.Create(10); // 10 empty slots!
list[5] := someItem;

Is this possible using any list type in the RTL? Actually I'd rather just disable all the out of bounds errors because I need some performant which isn't making more checks than need be. I want to use a heap-based list because the it may need to grow later.

Thanks guys.

Regards,
Ryan Joseph

Sven Barth
to Ryan Joseph
Ryan Joseph wrote 7 lines
I need a pre-grown list which I can put (not insert!) items into at indexes without getting "List index out of bounds" errors. For example I want to start with a list that has 10 empty indexes: list := TList.Create(10); // 10 empty slots! list[5] := someItem; Is this possible using any list type in the RTL? Actually I'd rather just disable all the out of bounds errors because I need some performant which isn't making more checks than need be. I want to use a heap-based list because the it may need to grow later.

What you're looking for is the Count property. Setting it is supported by the untyped lists in Classes, the generic ones in FGL as well as those in Generics.Collections.

=== code begin ===

list := TList.Create;
list.Count := 10;
list[5] := someItem;

=== code end ===

Regards,
Sven

Michael Van Canneyt
to Sven Barth
Sven Barth wrote 16 lines
Am 23.11.2019 um 23:42 schrieb Ryan Joseph via fpc-pascal: > I need a pre-grown list which I can put (not insert!) items into at indexes without getting "List index out of bounds" errors. For example I want to start with a list that has 10 empty indexes: > > list := TList.Create(10); // 10 empty slots! > > list[5] := someItem; > > Is this possible using any list type in the RTL? Actually I'd rather just disable all the out of bounds errors because I need some performant which isn't making more checks than need be. I want to use a heap-based list because the it may need to grow later. What you're looking for is the Count property. Setting it is supported by the untyped lists in Classes, the generic ones in FGL as well as those in Generics.Collections.

Better yet, use Capacity.

Count is meant to set the number of slots actually used. Capacity is the number of allocated slots.

Michael.

Sven Barth
to Michael Van Canneyt
Michael Van Canneyt wrote 27 lines
On Sun, 24 Nov 2019, Sven Barth via fpc-pascal wrote: > Am 23.11.2019 um 23:42 schrieb Ryan Joseph via fpc-pascal: >> I need a pre-grown list which I can put (not insert!) items into at >> indexes > without getting "List index out of bounds" errors. For example I want > to start with a list that has 10 empty indexes: >> >> list := TList.Create(10); // 10 empty slots! >> >> list[5] := someItem; >> >> Is this possible using any list type in the RTL? Actually I'd rather >> just > disable all the out of bounds errors because  I need some performant > which isn't making more checks than need be. I want to use a > heap-based list because the it may need to grow later. > What you're looking for is the Count property. Setting it is > supported by the untyped lists in Classes, the generic ones in FGL as > well as those in Generics.Collections. Better yet, use Capacity. Count is meant to set the number of slots actually used. Capacity is the number of allocated slots.

But the setter for the items checks against Count, not Capacity, so the code I showed will fail if used with Capacity instead of Count. At least both properties should be set to work correctly.

Regards,
Sven

Michael Van Canneyt
to Sven Barth
Sven Barth wrote 31 lines
Am 24.11.2019 um 10:14 schrieb Michael Van Canneyt: > > > On Sun, 24 Nov 2019, Sven Barth via fpc-pascal wrote: > >> Am 23.11.2019 um 23:42 schrieb Ryan Joseph via fpc-pascal: >>> I need a pre-grown list which I can put (not insert!) items into at >>> indexes >> without getting "List index out of bounds" errors. For example I want >> to start with a list that has 10 empty indexes: >>> >>> list := TList.Create(10); // 10 empty slots! >>> >>> list[5] := someItem; >>> >>> Is this possible using any list type in the RTL? Actually I'd rather >>> just >> disable all the out of bounds errors because  I need some performant >> which isn't making more checks than need be. I want to use a >> heap-based list because the it may need to grow later. >> What you're looking for is the Count property. Setting it is >> supported by the untyped lists in Classes, the generic ones in FGL as >> well as those in Generics.Collections. > > Better yet, use Capacity. > > Count is meant to set the number of slots actually used. > Capacity is the number of allocated slots. But the setter for the items checks against Count, not Capacity, so the code I showed will fail if used with Capacity instead of Count. At least both properties should be set to work correctly.

You are correct.

I meant to indicate that it is the capacity property that controls the pre-allocation.

Michael.

Ryan Joseph
to Sven Barth: What you're looking for is the Count property. Setting it is supported by the untyped…
Show quoted text 9 lines
On Nov 24, 2019, at 3:57 AM, Sven Barth via fpc-pascal wrote: What you're looking for is the Count property. Setting it is supported by the untyped lists in Classes, the generic ones in FGL as well as those in Generics.Collections. === code begin === list := TList.Create; list.Count := 10; list[5] := someItem;

I was trying that but with capacity and not count. Thanks.

Regards,
Ryan Joseph