Pre-allocated TFPGList or similar

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Pre-allocated TFPGList or similar

Free Pascal - General mailing list
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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Pre-allocated TFPGList or similar

Free Pascal - General mailing list
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.

=== code begin ===

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

=== code end ===

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Pre-allocated TFPGList or similar

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.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Pre-allocated TFPGList or similar

Free Pascal - General mailing list
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.

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Pre-allocated TFPGList or similar

Michael Van Canneyt


On Sun, 24 Nov 2019, Sven Barth via fpc-pascal wrote:

> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Pre-allocated TFPGList or similar

Free Pascal - General mailing list
In reply to this post by Free Pascal - General mailing list


> On Nov 24, 2019, at 3:57 AM, Sven Barth via fpc-pascal <[hidden email]> 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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal