Recent

Author Topic: TList  (Read 30370 times)

Rodney

  • New Member
  • *
  • Posts: 11
TList
« on: December 10, 2013, 01:27:05 pm »
Good afternoon
What I would like to do is very simple - Create a dynamic list of integers.
I assumed that the answer was some form of TList but my searches have failed to reveal any simple example of using TList.  (By simple I mean examples that I can understand!)

I would like to be able to add to, delete from, and sort an arbitrary list of numbers.

Any help would be appreciated.

Best regards
Rodney

eric

  • Sr. Member
  • ****
  • Posts: 267
Re: TList
« Reply #1 on: December 10, 2013, 01:44:19 pm »
Probably the easiest way would be to use a dynamic array:

Code: [Select]
var
  MyArray: array of Integer;
...
...
SetLength(MyArray, <Value>);      //Sets the length of the array to <Value> elements

Don't bother with sorting the array, just insert each new element in the right position after moving all the higher values up one place. To delete an element, move all the higher values down one place.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12314
  • FPC developer.
Re: TList
« Reply #2 on: December 10, 2013, 01:57:10 pm »
Real adventurous people might also look in unit FGL for generic solutions.

Rodney

  • New Member
  • *
  • Posts: 11
Re: TList
« Reply #3 on: December 10, 2013, 02:00:54 pm »
Thanks Eric.

I know about dynamic arrays but I was looking rather for a 'collection' where a 'delete' method would automatically handle all the necessary shuffling of the array elements, and as 'insert' method would allow you to add an integer at a particular place in the list.

Regards

Rodney

Rodney

  • New Member
  • *
  • Posts: 11
Re: TList
« Reply #4 on: December 10, 2013, 02:04:14 pm »
Marcov

Is there a simple user-friendly introduction to FGL?

I need a nice straightforward example where I can learn by building on.

Regards

Rodney

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12314
  • FPC developer.
Re: TList
« Reply #5 on: December 10, 2013, 02:28:52 pm »
Marcov

Is there a simple user-friendly introduction to FGL?

Not really. There is some information about generics syntax in the wiki though.

Quote
I need a nice straightforward example where I can learn by building on.

Well, the FGL mostly manages pointer types, so they wouldn't really apply directly. The FGL unit was more or less meant as an example, how to do a container type.

mse

  • Sr. Member
  • ****
  • Posts: 286
Re: TList
« Reply #6 on: December 10, 2013, 02:47:16 pm »

Rodney

  • New Member
  • *
  • Posts: 11
Re: TList
« Reply #7 on: December 10, 2013, 02:53:40 pm »
Thanks MSE

That looks interesting.  I will see if I can make use of it.

Regards
Rodney

BeniBela

  • Hero Member
  • *****
  • Posts: 928
    • homepage
Re: TList
« Reply #8 on: December 10, 2013, 05:06:49 pm »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8819
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: TList
« Reply #9 on: December 10, 2013, 05:31:22 pm »
Quote
Is there a simple user-friendly introduction to FGL?
I don't think any introduction is required for FGL, but for generics syntax is another thing and already done. Once you get it, you just need to know the interface. Simple example:
Code: [Select]
{$mode objfpc}
uses fgl;
type TIntegerList = specialize TFPGList<Integer>;
var IntegerList: TIntegerList;
begin
  IntegerList := TIntegerList.Create;
  // call .Add() .Find() .IndexOf() .Delete() .WhateverYouSeeInTheInterface()
  IntegerList.Free;
end;

Ocye

  • Hero Member
  • *****
  • Posts: 518
    • Scrabble3D
Re: TList
« Reply #10 on: December 12, 2013, 02:02:05 pm »
...for FGL, but for generics syntax is another thing and already done.
...and explained in detail at the wiki .

But maybe the OP want to learn how to use a TList with integer. Since TList handles only pointers (or objects) you have to allocate the items explicitly.

Code: [Select]
type
  pinteger=^integer;
var
  aList:TList;
  pi:pinteger;
  i:integer;
begin
  aList:=TList.Create;

  for i:=0 to 9 do
  begin
    New(pi);
    pi^:=i;
    aList.Add(pi);
  end;

  for i:=0 to aList.Count-1 do
    writeln(pinteger(aList[i])^);

  //take care about self created items
  for i:=aList.Count-1 downto 0 do
    Dispose(pinteger(aList[i]));

  aList.Clear;
  aList.Free;
end.

It's much easier with classes:

Code: [Select]
type
  TTest=class
    Value:integer;
  end;

var
  aList:TList;
  aTest:TTest;
  i:integer;
begin
  aList:=TList.Create;

  for i:=0 to 9 do
  begin
    aTest:=TTest.Create;
    aTest.Value:=i;
    aList.Add(aTest);
  end;

  for i:=0 to aList.Count-1 do
    writeln(TTest(aList[i]).Value);

  //take care about self created items
  for i:=aList.Count-1 downto 0 do
    TTest(aList[i]).Free;

  aList.Clear;
  aList.Free;
end.
Lazarus 1.7 (SVN) FPC 3.0.0

eny

  • Hero Member
  • *****
  • Posts: 1647
Re: TList
« Reply #11 on: December 12, 2013, 05:17:56 pm »
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: TList
« Reply #12 on: December 13, 2013, 03:45:01 am »
Based on info given in this thread, it should be possible make
Code: [Select]
TIntegerList = class(TList)
private
  function GetIntValue(index: integer): integer;
  procedure SetIntValue(index, value: integer);
public
  procedure Add(value: integer); // Allocate memory for int and add pointer to list
  property values[index: integer]: integer read GetIntValue write SetIntValue;
end;

If out of principle like me, you want to avoid 3rd party libraries for something like this.

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: TList
« Reply #13 on: December 13, 2013, 08:03:48 am »
Isn't it easier to to use pointer en integer.
Code: [Select]
TList.add(pointer(1))

value := integer(TList[0])
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TList
« Reply #14 on: December 13, 2013, 09:35:35 am »
Isn't it easier to to use pointer en integer.

What do you mean by "easier"?
All TList-based classes (whether standard or generic) necessarily involve casting to and from the value of the base pointer type. The whole point of developing derived classes such as TStringList, TIntegerList, TFPGList is to enable us to have type-safe TList functionality across all the common data types we want list functionality for without needing to do explicit (error-prone) casts every time the class is used (since the casting has been safely done in the wrapper class once and for all).

 

TinyPortal © 2005-2018