From Delphi to Go - List

Keywords: Programming Delphi Mobile Go

The most basic list in Delphi is TList class and TList < T > generic class, as well as thread safe TThreadList class and TThreadList < T > generic class. The underlying implementation is array. Go uses the container/list package, and the internal implementation is a two-way linked list.

Delphi

TList

In TList, there is a pointer. When using, pay attention to handle the pointer.

//statement
var l: TList;
//structure
l := TList.Create;
//Add to
l.Add(p);
//Element number
n := l.Count;
//List capacity
cap := l.Capacity;
//Value
p1 := l.Items[0];
p2 := l.Extract(p1); //Find the pointer p1 and take it out of the list. There will be no p1 in the list. If there is any element after it, move forward to fill in the vacancy.
p := l.First;        //Take the first element
p := l.Last;         //Take the last element
//Index of lookup element
i := l.IndexOf(p);
//modify
l.Items[0] := p2;
//delete
l.Delete(0);
l.Remove(p2);
//empty
l.Clear;             //Only the internal pointer array is cleared, and the memory pointed to by the pointer is not released.
//release
l.Free;

The TObjectList class and TComponentList class can be used after referencing the system.contrs cell. The operation method is the same as TList, except that the elements can be released automatically after deleting or clearing the list.

TThreadList

TThreadList is a TList with lock, so it is thread safe when multithreading.

//statement
var tl: TThreadList;
//structure
tl := TThreadList.Create;
//Add to
tl.Add(p);
//delete
tl.Remove(p);
//Lock up
l := tl.LockList; //Return to TList for more detailed operation
//Unlock
tl.UnlockList;
//empty
tl.Clear;         //The same as TList, only the internal pointer array is cleared, and the memory pointed to by the pointer is not released.
//release
tl.Free;

TList<T>

You need to refer to the system.genetics.collections unit in a similar way to TList.

//Operation elements take records as an example
TR = record
  i: Integer;
  s: string;
end;
//statement
var rl: TList<TR>;
//structure
rl := TList<TR>.Create;
//Add to
rl.Add(R1);           //Return to the added location
rl.Insert(1, R2);     //Insert at specified location
//delete
rl.Remove(R1);        //Delete by element
rl.Delete(0);         //Delete an element by index
rl.DeleteRange(0, n); //Delete consecutive elements by index
R := rl.Extract(R2);  //Extract elements
R := rl.ExtractAt(0); //Extract elements by index
//empty
rl.Clear;             //The memory occupied by the element is automatically released
//release
rl.Free;

TThreadList<T>

Tthreadlist < T > is TList < T > with lock, which is thread safe when multithreading.

//statement
var rtl: TThreadList<TR>;
//structure
rtl := TThreadList<TR>.Create;
//Add to
rtl.Add(R);
//delete
rtl.Remove(R);
//Lock up
rl := rtl.LockList; //Return TList < tr > for more detailed operation
//Unlock
rtl.UnlockList;
//empty
rtl.Clear;
//release
rtl.Free;

Go

//statement
var l list.List                //l is the List structure
l := list.New()                //l is a pointer to the List structure
//Add to
e1 := l.PushFront(123)         //Add elements to the head
e2 := l.PushBack("abc")        //Add elements at the end
e3 := l.InsertAfter("xyz", e1) //Add a new element after the specified element
e4 := l.InsertBefore(456, e2)  //Add a new element before the specified element
//Value
e5 := l.Front()                //Returns the first element
e6 := l.Back()                 //Returns the last element
//Traversal from beginning to end
for i := e5; i != nil; i = i.Next() {
  fmt.Println(i.Value)
}
//Traversal from tail in reverse order
for i := e6; i != nil; i = i.Prev() {
  fmt.Println(i.Value)
}
//Mobile element
l.MoveToFront(e3)              //Move e3 to the head
l.MoveToBack(e4)               //Move e4 to the tail
l.MoveAfter(e1, e3)            //Move e1 behind e3
l.MoveBefore(e2, e1)           //Move e2 to the front of e1
//delete
l.Remove(e3)
//Element number
n := l.Len()
//empty
l.Init()

Because of the grammar sugar of Go language, the usage of pointer and the object pointed by pointer is exactly the same.

Posted by fredi_bieging on Wed, 30 Oct 2019 11:27:29 -0700