TSubstringList Class

type TSubstringList = class

A list of Substrings kept in rows, each row a whole Substring.

The list allocates considerable amount of memory and is not meant to be used as the primary storage. It exists for the specific purpose to allow list operations inside of threads. Those operations need to be chunked.

Note: This class compares bytes on sorting and does not handle locale specific char handling. Derived class provided that capability. The sorting operations can be 100x faster when string case does not have to be considered.

Examples

Filling, sorting and reading a list.

delphi
uses Substr;

procedure Example;
var List: TSubstringList;
    s: Substring;
    Total: Integer;
begin
    List := TSubstringList.Create(100);
    try
        List.Add('pear');
        List.Add('apple');
        List.Add('fig');
        List.Sort;
        Assert(List[0] = 'apple');
        Assert(List[2] = 'pear');
        Total := 0;
        for s in List do
            Inc(Total, s.Length);
        Assert(Total = 12);
    finally
        List.Free;
    end;
end;

Refilling a list in a loop takes no memory.

delphi
uses Substr;

procedure Example;
var List: TSubstringList;
    Pass, k: Integer;
begin
    List := TSubstringList.Create(1000);
    try
        for Pass := 1 to 3 do
        begin
            List.Clear;
            for k := 1 to 1000 do
                List.Add(Substr.IntToStr(k * Pass));
            List.Sort;
        end;
        Assert(List.Count = 1000);
    finally
        List.Free;
    end;
end;

Views: an item read in place, stored in another item, and kept across Sort.

delphi
uses Substr;

procedure Example;
var List: TSubstringList;
    s: Substring;
begin
    List := TSubstringList.Create(4);
    try
        List.Add('beta');
        List.Add('alpha');
        s := List[0];
        List[1] := List[0];
        List.Items[0].UpperCaseAscii;
        Assert((s = 'BETA') and (List[1] = 'BETA'));
        List.Put(1, 'ALPHA');
        List.Sort;
        Assert((List[0] = 'ALPHA') and (s.Data = List[1].Data));
    finally
        List.Free;
    end;
end;

Constructors

NameDescription
Create (5)An empty list with Capacity 128.

Properties

NameTypeDescription
CapacityIntegerThe number of items the list holds without growing: a multiple of 128, at least 128.
ChunkSizeIntegerThe size of the pieces a load or save converts: ChunkSize bytes read, or ChunkSize div 2 characters written; 128 KB by default.
CommaTextSubstringDelimitedText with Delimiter ',' and QuoteChar '"'.
CountIntegerThe number of items. Setting it higher adds empty items, lower deletes the last ones.
DefaultEncodingTEncodingThe encoding of a load given none whose stream has no byte order mark, and of a save before any load; TEncoding.Default at first.
DelimitedTextSubstringThe items joined by Delimiter, each quoted by QuoteChar where it holds QuoteChar, Delimiter or, without StrictDelimiter, a character up to the space.
DelimiterCharThe delimiter of DelimitedText; ',' by default.
DuplicatesTDuplicatesWhat a sorted list's Add does with a text equal to an item: dupIgnore, dupAccept or dupError.
EncodingTEncodingThe encoding of the last load: the one its byte order mark named, else the one it was given, else DefaultEncoding; nil before the first load. SaveToFile and SaveToStream without an encoding write in it.
ItemsPSubstringItem Index (0..Count - 1) itself: every Substring member works on it in place.
LineBreakstringThe line break of Text; sLineBreak by default.
ObjectsTObjectThe object of item Index.
OwnsObjectsBooleanWhether deleting an item, Clear and Free free the items' objects.
QuoteCharCharThe quote of DelimitedText; '"' by default; #0 quotes nothing.
SortedBooleanWhether Add places each text at its sorted place; setting it True sorts the list.
StrictDelimiterBooleanWhether only Delimiter separates the fields of DelimitedText; False by default, so the characters up to the space separate them too.
StringsSubstringItem Index (0..Count - 1), read in place: no memory is taken and no character copied.
TextSubstringThe items as lines: each followed by LineBreak, the last one too while TrailingLineBreak is True.
TrailingLineBreakBooleanWhether Text ends with a LineBreak after the last item; True by default.
WriteBOMBooleanWhether a save writes the encoding's byte order mark first; True by default.

Methods

NameDescription
Add (2)Adds S: at the end, or, in a sorted list, at its place.
AddObjectAdds S with the object AObject, as Add.
ClearDeletes every item; the rows are kept, so the next adds take no memory.
CompactGives back the memory of the rows no item uses, and the memory loads and saves keep: Capacity becomes Count rounded up to a multiple of 128, at least 128, every item kept.
CustomSortSorts the list by Compare, stably; a sorted list is left as it is.
DeleteDeletes item Index; the later items move down by one.
DeleteRangeDeletes ACount items from Index, as TList<T>.DeleteRange.
DestroyFrees the list's rows, and the items' objects when OwnsObjects is True.
ExchangeSwaps items Index1 and Index2.
ExpandGrows the list when every row is in use, keeping every item as Resize does, by half its capacity rounded up to a multiple of 128.
ExtractRemoves the first item equal to S without freeing its object, and returns it.
ExtractItemExtract from either end: the first (FromBeginning) or last (FromEnd) item equal to S.
FindSearches a sorted list for S by bisection.
FirstThe first item.
GetEnumeratorThe enumerator of for s in List.
IndexOfThe index of the first item equal to S, or -1.
IndexOfItemIndexOf from either end: FromBeginning the first item equal to S, FromEnd the last; -1 when there is none.
IndexOfObjectThe index of the first item whose object is AObject, or -1.
InsertInserts S at Index (0..Count); the later items move up by one.
InsertObjectInserts S with the object AObject at Index, as Insert.
LastThe last item.
LoadFromFile (2)Replaces the items with the lines of file FileName: LoadFromStream on the file, in the encoding its byte order mark names, else DefaultEncoding.
LoadFromStream (2)Replaces the items with the lines of Stream from its position to its end, in the encoding its byte order mark names (UTF-8, UTF-16 or UTF-16 big-endian), else DefaultEncoding.
MoveMoves item CurIndex to NewIndex, the items between shifting by one.
PutWrites S into item Index; S takes every text kind.
RemoveDeletes the first item equal to S.
RemoveItemDeletes the first (FromBeginning) or last (FromEnd) item equal to S.
ResizeSets Capacity to NewCapacity, any size, rounded up to a multiple of 128 and at least 128, keeping the items that fit; setting Capacity keeps none.
SaveToFile (2)Writes Text to file FileName in Encoding (the encoding of the last load), else DefaultEncoding: SaveToStream on a new file.
SaveToStream (2)Writes Text to Stream in Encoding (the encoding of the last load), else DefaultEncoding.
SetCommaTextReplaces the items by the fields of Value, delimited by a comma and quoted by a double quote; Value takes every text kind.
SetDelimitedTextReplaces the items by the fields of Value; Value takes every text kind.
SetTextReplaces the items by the lines of Value; Value takes every text kind.
Sort (2)Sorts the list by its comparison, stably; a sorted list is left as it is.
SortAscendSort: ascending by the list's comparison.
SortDescendSorts descending by the list's comparison, stably; a sorted list is left as it is.
ToStringThe items as one string: Text.