123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079 |
- {
- This file is part of the Free Pascal run time library.
- Copyright (c) 2006 by Micha Nelissen
- member of the Free Pascal development team
- It contains the Free Pascal generics library
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- **********************************************************************}
- {$mode objfpc}
- {$define FGLINLINE}
- {$ifdef FGLINLINE}
- {$inline on}
- {$endif FGLINLINE}
- unit fgl;
- interface
- uses
- types, sysutils;
- {$IF defined(VER2_4)}
- {$DEFINE OldSyntax}
- {$IFEND}
- const
- MaxListSize = Maxint div 16;
- type
- EListError = class(Exception);
- TFPSList = class;
- TFPSListCompareFunc = function(Key1, Key2: Pointer): Integer of object;
- { TFPSList }
- TFPSList = class(TObject)
- protected
- FList: PByte;
- FCount: Integer;
- FCapacity: Integer; { list has room for capacity+1 items, contains room for a temporary item }
- FItemSize: Integer;
- procedure CopyItem(Src, Dest: Pointer); virtual;
- procedure CopyItems(Src, Dest: Pointer; aCount : Integer); virtual;
- procedure Deref(Item: Pointer); virtual; overload;
- procedure Deref(FromIndex, ToIndex: Integer); overload;
- function Get(Index: Integer): Pointer;
- procedure InternalExchange(Index1, Index2: Integer);
- function InternalGet(Index: Integer): Pointer; {$ifdef FGLINLINE} inline; {$endif}
- procedure InternalPut(Index: Integer; NewItem: Pointer);
- procedure Put(Index: Integer; Item: Pointer);
- procedure QuickSort(L, R: Integer; Compare: TFPSListCompareFunc);
- procedure SetCapacity(NewCapacity: Integer);
- procedure SetCount(NewCount: Integer);
- procedure RaiseIndexError(Index : Integer);
- property InternalItems[Index: Integer]: Pointer read InternalGet write InternalPut;
- function GetLast: Pointer;
- procedure SetLast(const Value: Pointer);
- function GetFirst: Pointer;
- procedure SetFirst(const Value: Pointer);
- Procedure CheckIndex(AIndex : Integer); inline;
- public
- constructor Create(AItemSize: Integer = sizeof(Pointer));
- destructor Destroy; override;
- class Function ItemIsManaged : Boolean; virtual;
- function Add(Item: Pointer): Integer;
- procedure Clear;
- procedure Delete(Index: Integer);
- procedure DeleteRange(IndexFrom, IndexTo : Integer);
- class procedure Error(const Msg: string; Data: PtrInt);
- procedure Exchange(Index1, Index2: Integer);
- function Expand: TFPSList;
- procedure Extract(Item: Pointer; ResultPtr: Pointer);
- function IndexOf(Item: Pointer): Integer;
- procedure Insert(Index: Integer; Item: Pointer);
- function Insert(Index: Integer): Pointer;
- procedure Move(CurIndex, NewIndex: Integer);
- procedure Assign(Obj: TFPSList);
- procedure AddList(Obj: TFPSList);
- function Remove(Item: Pointer): Integer;
- procedure Pack;
- procedure Sort(Compare: TFPSListCompareFunc);
- property Capacity: Integer read FCapacity write SetCapacity;
- property Count: Integer read FCount write SetCount;
- property Items[Index: Integer]: Pointer read Get write Put; default;
- property ItemSize: Integer read FItemSize;
- property List: PByte read FList;
- property First: Pointer read GetFirst write SetFirst;
- property Last: Pointer read GetLast write SetLast;
- end;
- const
- {$ifdef cpu16}
- MaxGListSize = {MaxInt div} 1024 deprecated;
- {$else cpu16}
- MaxGListSize = MaxInt div 1024 deprecated;
- {$endif cpu16}
- type
- generic TFPGListEnumerator<T> = class(TObject)
- protected
- FList: TFPSList;
- FPosition: Integer;
- function GetCurrent: T;
- public
- constructor Create(AList: TFPSList);
- function MoveNext: Boolean;
- property Current: T read GetCurrent;
- end;
- { TFPGList }
- generic TFPGList<T> = class(TFPSList)
- private
- type
- TCompareFunc = function(const Item1, Item2: T): Integer;
- PT = ^T;
- TTypeList = PT;
- PTypeList = ^TTypeList;
- {$ifndef OldSyntax}protected var{$else}var protected{$endif}
- FOnCompare: TCompareFunc;
- procedure CopyItem(Src, Dest: Pointer); override;
- procedure Deref(Item: Pointer); override;
- function Get(Index: Integer): T; {$ifdef FGLINLINE} inline; {$endif}
- function GetList: PTypeList; {$ifdef FGLINLINE} inline; {$endif}
- function ItemPtrCompare(Item1, Item2: Pointer): Integer;
- procedure Put(Index: Integer; const Item: T); {$ifdef FGLINLINE} inline; {$endif}
- function GetLast: T; {$ifdef FGLINLINE} inline; {$endif}
- procedure SetLast(const Value: T); {$ifdef FGLINLINE} inline; {$endif}
- function GetFirst: T; {$ifdef FGLINLINE} inline; {$endif}
- procedure SetFirst(const Value: T); {$ifdef FGLINLINE} inline; {$endif}
- public
- Type
- TFPGListEnumeratorSpec = specialize TFPGListEnumerator<T>;
- constructor Create;
- class Function ItemIsManaged : Boolean; override;
- function Add(const Item: T): Integer; {$ifdef FGLINLINE} inline; {$endif}
- function Extract(const Item: T): T; {$ifdef FGLINLINE} inline; {$endif}
- property First: T read GetFirst write SetFirst;
- function GetEnumerator: TFPGListEnumeratorSpec; {$ifdef FGLINLINE} inline; {$endif}
- function IndexOf(const Item: T): Integer;
- procedure Insert(Index: Integer; const Item: T); {$ifdef FGLINLINE} inline; {$endif}
- property Last: T read GetLast write SetLast;
- {$ifndef VER2_4}
- procedure Assign(Source: TFPGList);
- procedure AddList(Source: TFPGList);
- {$endif VER2_4}
- function Remove(const Item: T): Integer; {$ifdef FGLINLINE} inline; {$endif}
- procedure Sort(Compare: TCompareFunc);
- property Items[Index: Integer]: T read Get write Put; default;
- property List: PTypeList read GetList;
- end;
- generic TFPGObjectList<T: TObject> = class(TFPSList)
- private
- type
- TCompareFunc = function(const Item1, Item2: T): Integer;
- PT = ^T;
- TTypeList = PT;
- PTypeList = ^TTypeList;
- TFPGListEnumeratorSpec = specialize TFPGListEnumerator<T>;
- {$ifndef OldSyntax}protected var{$else}var protected{$endif}
- FOnCompare: TCompareFunc;
- FFreeObjects: Boolean;
- procedure CopyItem(Src, Dest: Pointer); override;
- procedure Deref(Item: Pointer); override;
- function Get(Index: Integer): T; {$ifdef FGLINLINE} inline; {$endif}
- function GetList: PTypeList; {$ifdef FGLINLINE} inline; {$endif}
- function ItemPtrCompare(Item1, Item2: Pointer): Integer;
- procedure Put(Index: Integer; const Item: T); {$ifdef FGLINLINE} inline; {$endif}
- function GetLast: T; {$ifdef FGLINLINE} inline; {$endif}
- procedure SetLast(const Value: T); {$ifdef FGLINLINE} inline; {$endif}
- function GetFirst: T; {$ifdef FGLINLINE} inline; {$endif}
- procedure SetFirst(const Value: T); {$ifdef FGLINLINE} inline; {$endif}
- public
- constructor Create(FreeObjects: Boolean = True);
- function Add(const Item: T): Integer; {$ifdef FGLINLINE} inline; {$endif}
- function Extract(const Item: T): T; {$ifdef FGLINLINE} inline; {$endif}
- property First: T read GetFirst write SetFirst;
- function GetEnumerator: TFPGListEnumeratorSpec; {$ifdef FGLINLINE} inline; {$endif}
- function IndexOf(const Item: T): Integer;
- procedure Insert(Index: Integer; const Item: T); {$ifdef FGLINLINE} inline; {$endif}
- property Last: T read GetLast write SetLast;
- {$ifndef VER2_4}
- procedure AddList(Source: TFPGObjectList);
- procedure Assign(Source: TFPGObjectList);
- {$endif VER2_4}
- function Remove(const Item: T): Integer; {$ifdef FGLINLINE} inline; {$endif}
- procedure Sort(Compare: TCompareFunc);
- property Items[Index: Integer]: T read Get write Put; default;
- property List: PTypeList read GetList;
- property FreeObjects: Boolean read FFreeObjects write FFreeObjects;
- end;
- generic TFPGInterfacedObjectList<T> = class(TFPSList)
- private
- type
- TCompareFunc = function(const Item1, Item2: T): Integer;
- PT = ^T;
- TTypeList = PT;
- PTypeList = ^TTypeList;
- TFPGListEnumeratorSpec = specialize TFPGListEnumerator<T>;
- {$ifndef OldSyntax}protected var{$else}var protected{$endif}
- FOnCompare: TCompareFunc;
- procedure CopyItem(Src, Dest: Pointer); override;
- procedure Deref(Item: Pointer); override;
- function Get(Index: Integer): T; {$ifdef FGLINLINE} inline; {$endif}
- function GetList: PTypeList; {$ifdef FGLINLINE} inline; {$endif}
- function ItemPtrCompare(Item1, Item2: Pointer): Integer;
- procedure Put(Index: Integer; const Item: T); {$ifdef FGLINLINE} inline; {$endif}
- function GetLast: T; {$ifdef FGLINLINE} inline; {$endif}
- procedure SetLast(const Value: T); {$ifdef FGLINLINE} inline; {$endif}
- function GetFirst: T; {$ifdef FGLINLINE} inline; {$endif}
- procedure SetFirst(const Value: T); {$ifdef FGLINLINE} inline; {$endif}
- public
- constructor Create;
- function Add(const Item: T): Integer; {$ifdef FGLINLINE} inline; {$endif}
- function Extract(const Item: T): T; {$ifdef FGLINLINE} inline; {$endif}
- property First: T read GetFirst write SetFirst;
- function GetEnumerator: TFPGListEnumeratorSpec; {$ifdef FGLINLINE} inline; {$endif}
- function IndexOf(const Item: T): Integer;
- procedure Insert(Index: Integer; const Item: T); {$ifdef FGLINLINE} inline; {$endif}
- property Last: T read GetLast write SetLast;
- {$ifndef VER2_4}
- procedure Assign(Source: TFPGInterfacedObjectList);
- procedure AddList(Source: TFPGInterfacedObjectList);
- {$endif VER2_4}
- function Remove(const Item: T): Integer; {$ifdef FGLINLINE} inline; {$endif}
- procedure Sort(Compare: TCompareFunc);
- property Items[Index: Integer]: T read Get write Put; default;
- property List: PTypeList read GetList;
- end;
- TFPSMap = class(TFPSList)
- private
- FKeySize: Integer;
- FDataSize: Integer;
- FDuplicates: TDuplicates;
- FSorted: Boolean;
- FOnKeyPtrCompare: TFPSListCompareFunc;
- FOnDataPtrCompare: TFPSListCompareFunc;
- procedure SetSorted(Value: Boolean);
- protected
- function BinaryCompareKey(Key1, Key2: Pointer): Integer;
- function BinaryCompareData(Data1, Data2: Pointer): Integer;
- procedure SetOnKeyPtrCompare(Proc: TFPSListCompareFunc);
- procedure SetOnDataPtrCompare(Proc: TFPSListCompareFunc);
- procedure InitOnPtrCompare; virtual;
- procedure CopyKey(Src, Dest: Pointer); virtual;
- procedure CopyData(Src, Dest: Pointer); virtual;
- function GetKey(Index: Integer): Pointer;
- function GetKeyData(AKey: Pointer): Pointer;
- function GetData(Index: Integer): Pointer;
- function LinearIndexOf(AKey: Pointer): Integer;
- procedure PutKey(Index: Integer; AKey: Pointer);
- procedure PutKeyData(AKey: Pointer; NewData: Pointer);
- procedure PutData(Index: Integer; AData: Pointer);
- public
- constructor Create(AKeySize: Integer = sizeof(Pointer);
- ADataSize: integer = sizeof(Pointer));
- function Add(AKey, AData: Pointer): Integer;
- function Add(AKey: Pointer): Integer;
- function Find(AKey: Pointer; out Index: Integer): Boolean;
- function IndexOf(AKey: Pointer): Integer;
- function IndexOfData(AData: Pointer): Integer;
- function Insert(Index: Integer): Pointer;
- procedure Insert(Index: Integer; out AKey, AData: Pointer);
- procedure InsertKey(Index: Integer; AKey: Pointer);
- procedure InsertKeyData(Index: Integer; AKey, AData: Pointer);
- function Remove(AKey: Pointer): Integer;
- procedure Sort;
- property Duplicates: TDuplicates read FDuplicates write FDuplicates;
- property KeySize: Integer read FKeySize;
- property DataSize: Integer read FDataSize;
- property Keys[Index: Integer]: Pointer read GetKey write PutKey;
- property Data[Index: Integer]: Pointer read GetData write PutData;
- property KeyData[Key: Pointer]: Pointer read GetKeyData write PutKeyData; default;
- property Sorted: Boolean read FSorted write SetSorted;
- property OnPtrCompare: TFPSListCompareFunc read FOnKeyPtrCompare write SetOnKeyPtrCompare; //deprecated;
- property OnKeyPtrCompare: TFPSListCompareFunc read FOnKeyPtrCompare write SetOnKeyPtrCompare;
- property OnDataPtrCompare: TFPSListCompareFunc read FOnDataPtrCompare write SetOnDataPtrCompare;
- end;
- generic TFPGMap<TKey, TData> = class(TFPSMap)
- private
- type
- TKeyCompareFunc = function(const Key1, Key2: TKey): Integer;
- TDataCompareFunc = function(const Data1, Data2: TData): Integer;
- PKey = ^TKey;
- // unsed PData = ^TData;
- {$ifndef OldSyntax}protected var{$else}var protected{$endif}
- FOnKeyCompare: TKeyCompareFunc;
- FOnDataCompare: TDataCompareFunc;
- procedure CopyItem(Src, Dest: Pointer); override;
- procedure CopyKey(Src, Dest: Pointer); override;
- procedure CopyData(Src, Dest: Pointer); override;
- procedure Deref(Item: Pointer); override;
- procedure InitOnPtrCompare; override;
- function GetKey(Index: Integer): TKey; {$ifdef FGLINLINE} inline; {$endif}
- function GetKeyData(const AKey: TKey): TData; {$ifdef FGLINLINE} inline; {$endif}
- function GetData(Index: Integer): TData; {$ifdef FGLINLINE} inline; {$endif}
- function KeyCompare(Key1, Key2: Pointer): Integer;
- function KeyCustomCompare(Key1, Key2: Pointer): Integer;
- //function DataCompare(Data1, Data2: Pointer): Integer;
- function DataCustomCompare(Data1, Data2: Pointer): Integer;
- procedure PutKey(Index: Integer; const NewKey: TKey); {$ifdef FGLINLINE} inline; {$endif}
- procedure PutKeyData(const AKey: TKey; const NewData: TData); {$ifdef FGLINLINE} inline; {$endif}
- procedure PutData(Index: Integer; const NewData: TData); {$ifdef FGLINLINE} inline; {$endif}
- procedure SetOnKeyCompare(NewCompare: TKeyCompareFunc);
- procedure SetOnDataCompare(NewCompare: TDataCompareFunc);
- public
- constructor Create;
- function Add(const AKey: TKey; const AData: TData): Integer; {$ifdef FGLINLINE} inline; {$endif}
- function Add(const AKey: TKey): Integer; {$ifdef FGLINLINE} inline; {$endif}
- function Find(const AKey: TKey; out Index: Integer): Boolean; {$ifdef FGLINLINE} inline; {$endif}
- function TryGetData(const AKey: TKey; out AData: TData): Boolean; {$ifdef FGLINLINE} inline; {$endif}
- procedure AddOrSetData(const AKey: TKey; const AData: TData); {$ifdef FGLINLINE} inline; {$endif}
- function IndexOf(const AKey: TKey): Integer; {$ifdef FGLINLINE} inline; {$endif}
- function IndexOfData(const AData: TData): Integer;
- procedure InsertKey(Index: Integer; const AKey: TKey);
- procedure InsertKeyData(Index: Integer; const AKey: TKey; const AData: TData);
- function Remove(const AKey: TKey): Integer;
- property Keys[Index: Integer]: TKey read GetKey write PutKey;
- property Data[Index: Integer]: TData read GetData write PutData;
- property KeyData[const AKey: TKey]: TData read GetKeyData write PutKeyData; default;
- property OnCompare: TKeyCompareFunc read FOnKeyCompare write SetOnKeyCompare; //deprecated;
- property OnKeyCompare: TKeyCompareFunc read FOnKeyCompare write SetOnKeyCompare;
- property OnDataCompare: TDataCompareFunc read FOnDataCompare write SetOnDataCompare;
- end;
- generic TFPGMapObject<TKey; TData: TObject> = class(TFPSMap)
- private
- type
- TKeyCompareFunc = function(const Key1, Key2: TKey): Integer;
- TDataCompareFunc = function(const Data1, Data2: TData): Integer;
- PKey = ^TKey;
- // unsed PData = ^TData;
- {$ifndef OldSyntax}protected var{$else}var protected{$endif}
- FOnKeyCompare: TKeyCompareFunc;
- FOnDataCompare: TDataCompareFunc;
- FFreeObjects: Boolean;
- procedure CopyItem(Src, Dest: Pointer); override;
- procedure CopyKey(Src, Dest: Pointer); override;
- procedure CopyData(Src, Dest: Pointer); override;
- procedure Deref(Item: Pointer); override;
- procedure InitOnPtrCompare; override;
- function GetKey(Index: Integer): TKey; {$ifdef FGLINLINE} inline; {$endif}
- function GetKeyData(const AKey: TKey): TData; {$ifdef FGLINLINE} inline; {$endif}
- function GetData(Index: Integer): TData; {$ifdef FGLINLINE} inline; {$endif}
- function KeyCompare(Key1, Key2: Pointer): Integer;
- function KeyCustomCompare(Key1, Key2: Pointer): Integer;
- //function DataCompare(Data1, Data2: Pointer): Integer;
- function DataCustomCompare(Data1, Data2: Pointer): Integer;
- procedure PutKey(Index: Integer; const NewKey: TKey); {$ifdef FGLINLINE} inline; {$endif}
- procedure PutKeyData(const AKey: TKey; const NewData: TData); {$ifdef FGLINLINE} inline; {$endif}
- procedure PutData(Index: Integer; const NewData: TData); {$ifdef FGLINLINE} inline; {$endif}
- procedure SetOnKeyCompare(NewCompare: TKeyCompareFunc);
- procedure SetOnDataCompare(NewCompare: TDataCompareFunc);
- public
- constructor Create(AFreeObjects: Boolean);
- constructor Create;
- function Add(const AKey: TKey; const AData: TData): Integer; {$ifdef FGLINLINE} inline; {$endif}
- function Add(const AKey: TKey): Integer; {$ifdef FGLINLINE} inline; {$endif}
- function Find(const AKey: TKey; out Index: Integer): Boolean; {$ifdef FGLINLINE} inline; {$endif}
- function TryGetData(const AKey: TKey; out AData: TData): Boolean; {$ifdef FGLINLINE} inline; {$endif}
- procedure AddOrSetData(const AKey: TKey; const AData: TData); {$ifdef FGLINLINE} inline; {$endif}
- function IndexOf(const AKey: TKey): Integer; {$ifdef FGLINLINE} inline; {$endif}
- function IndexOfData(const AData: TData): Integer;
- procedure InsertKey(Index: Integer; const AKey: TKey);
- procedure InsertKeyData(Index: Integer; const AKey: TKey; const AData: TData);
- function Remove(const AKey: TKey): Integer;
- property Keys[Index: Integer]: TKey read GetKey write PutKey;
- property Data[Index: Integer]: TData read GetData write PutData;
- property KeyData[const AKey: TKey]: TData read GetKeyData write PutKeyData; default;
- property OnCompare: TKeyCompareFunc read FOnKeyCompare write SetOnKeyCompare; //deprecated;
- property OnKeyCompare: TKeyCompareFunc read FOnKeyCompare write SetOnKeyCompare;
- property OnDataCompare: TDataCompareFunc read FOnDataCompare write SetOnDataCompare;
- end;
- generic TFPGMapInterfacedObjectData<TKey, TData> = class(TFPSMap)
- private
- type
- TKeyCompareFunc = function(const Key1, Key2: TKey): Integer;
- TDataCompareFunc = function(const Data1, Data2: TData): Integer;
- PKey = ^TKey;
- // unsed PData = ^TData;
- {$ifndef OldSyntax}protected var{$else}var protected{$endif}
- FOnKeyCompare: TKeyCompareFunc;
- FOnDataCompare: TDataCompareFunc;
- procedure CopyItem(Src, Dest: Pointer); override;
- procedure CopyKey(Src, Dest: Pointer); override;
- procedure CopyData(Src, Dest: Pointer); override;
- procedure Deref(Item: Pointer); override;
- procedure InitOnPtrCompare; override;
- function GetKey(Index: Integer): TKey; {$ifdef FGLINLINE} inline; {$endif}
- function GetKeyData(const AKey: TKey): TData; {$ifdef FGLINLINE} inline; {$endif}
- function GetData(Index: Integer): TData; {$ifdef FGLINLINE} inline; {$endif}
- function KeyCompare(Key1, Key2: Pointer): Integer;
- function KeyCustomCompare(Key1, Key2: Pointer): Integer;
- //function DataCompare(Data1, Data2: Pointer): Integer;
- function DataCustomCompare(Data1, Data2: Pointer): Integer;
- procedure PutKey(Index: Integer; const NewKey: TKey); {$ifdef FGLINLINE} inline; {$endif}
- procedure PutKeyData(const AKey: TKey; const NewData: TData); {$ifdef FGLINLINE} inline; {$endif}
- procedure PutData(Index: Integer; const NewData: TData); {$ifdef FGLINLINE} inline; {$endif}
- procedure SetOnKeyCompare(NewCompare: TKeyCompareFunc);
- procedure SetOnDataCompare(NewCompare: TDataCompareFunc);
- public
- constructor Create;
- function Add(const AKey: TKey; const AData: TData): Integer; {$ifdef FGLINLINE} inline; {$endif}
- function Add(const AKey: TKey): Integer; {$ifdef FGLINLINE} inline; {$endif}
- function Find(const AKey: TKey; out Index: Integer): Boolean; {$ifdef FGLINLINE} inline; {$endif}
- function TryGetData(const AKey: TKey; out AData: TData): Boolean; {$ifdef FGLINLINE} inline; {$endif}
- procedure AddOrSetData(const AKey: TKey; const AData: TData); {$ifdef FGLINLINE} inline; {$endif}
- function IndexOf(const AKey: TKey): Integer; {$ifdef FGLINLINE} inline; {$endif}
- function IndexOfData(const AData: TData): Integer;
- procedure InsertKey(Index: Integer; const AKey: TKey);
- procedure InsertKeyData(Index: Integer; const AKey: TKey; const AData: TData);
- function Remove(const AKey: TKey): Integer;
- property Keys[Index: Integer]: TKey read GetKey write PutKey;
- property Data[Index: Integer]: TData read GetData write PutData;
- property KeyData[const AKey: TKey]: TData read GetKeyData write PutKeyData; default;
- property OnCompare: TKeyCompareFunc read FOnKeyCompare write SetOnKeyCompare; //deprecated;
- property OnKeyCompare: TKeyCompareFunc read FOnKeyCompare write SetOnKeyCompare;
- property OnDataCompare: TDataCompareFunc read FOnDataCompare write SetOnDataCompare;
- end;
- implementation
- uses
- rtlconsts;
- {****************************************************************************
- TFPSList
- ****************************************************************************}
- constructor TFPSList.Create(AItemSize: integer);
- begin
- inherited Create;
- FItemSize := AItemSize;
- end;
- destructor TFPSList.Destroy;
- begin
- Clear;
- // Clear() does not clear the whole list; there is always a single temp entry
- // at the end which is never freed. Take care of that one here.
- FreeMem(FList);
- inherited Destroy;
- end;
- procedure TFPSList.CopyItem(Src, Dest: Pointer);
- begin
- System.Move(Src^, Dest^, FItemSize);
- end;
- procedure TFPSList.CopyItems(Src, Dest: Pointer; aCount: Integer);
- begin
- System.Move(Src^, Dest^, FItemSize*aCount);
- end;
- procedure TFPSList.RaiseIndexError(Index : Integer);
- begin
- Error(SListIndexError, Index);
- end;
- function TFPSList.InternalGet(Index: Integer): Pointer;
- begin
- Result:=FList+Index*ItemSize;
- end;
- procedure TFPSList.InternalPut(Index: Integer; NewItem: Pointer);
- var
- ListItem: Pointer;
- begin
- ListItem := InternalItems[Index];
- CopyItem(NewItem, ListItem);
- end;
- function TFPSList.Get(Index: Integer): Pointer;
- begin
- CheckIndex(Index);
- Result := InternalItems[Index];
- end;
- procedure TFPSList.Put(Index: Integer; Item: Pointer);
- var p : Pointer;
- begin
- CheckIndex(Index);
- p:=InternalItems[Index];
- if assigned(p) then
- DeRef(p);
- InternalItems[Index] := Item;
- end;
- procedure TFPSList.SetCapacity(NewCapacity: Integer);
- begin
- if (NewCapacity < FCount) or (NewCapacity > MaxListSize) then
- Error(SListCapacityError, NewCapacity);
- if NewCapacity = FCapacity then
- exit;
- ReallocMem(FList, (NewCapacity+1) * FItemSize);
- FillChar(InternalItems[FCapacity]^, (NewCapacity+1-FCapacity) * FItemSize, #0);
- FCapacity := NewCapacity;
- end;
- procedure TFPSList.Deref(Item: Pointer);
- begin
- end;
- procedure TFPSList.Deref(FromIndex, ToIndex: Integer);
- var
- ListItem, ListItemLast: Pointer;
- begin
- ListItem := InternalItems[FromIndex];
- ListItemLast := InternalItems[ToIndex];
- repeat
- Deref(ListItem);
- if ListItem = ListItemLast then
- break;
- ListItem := PByte(ListItem) + ItemSize;
- until false;
- end;
- procedure TFPSList.SetCount(NewCount: Integer);
- begin
- if (NewCount < 0) or (NewCount > MaxListSize) then
- Error(SListCountError, NewCount);
- if NewCount > FCapacity then
- SetCapacity(NewCount);
- if NewCount > FCount then
- FillByte(InternalItems[FCount]^, (NewCount-FCount) * FItemSize, 0)
- else if NewCount < FCount then
- Deref(NewCount, FCount-1);
- FCount := NewCount;
- end;
- function TFPSList.Add(Item: Pointer): Integer;
- begin
- if FCount = FCapacity then
- Self.Expand;
- CopyItem(Item, InternalItems[FCount]);
- Result := FCount;
- Inc(FCount);
- end;
- procedure TFPSList.CheckIndex(AIndex : Integer);
- begin
- if (AIndex < 0) or (AIndex >= FCount) then
- Error(SListIndexError, AIndex);
- end;
- class function TFPSList.ItemIsManaged: Boolean;
- begin
- Result:=False;
- end;
- procedure TFPSList.Clear;
- begin
- if Assigned(FList) then
- begin
- SetCount(0);
- SetCapacity(0);
- end;
- end;
- procedure TFPSList.Delete(Index: Integer);
- var
- ListItem: Pointer;
- begin
- CheckIndex(Index);
- Dec(FCount);
- ListItem := InternalItems[Index];
- Deref(ListItem);
- System.Move(InternalItems[Index+1]^, ListItem^, (FCount - Index) * FItemSize);
- // Shrink the list if appropriate
- if (FCapacity > 256) and (FCount < FCapacity shr 2) then
- begin
- FCapacity := FCapacity shr 1;
- ReallocMem(FList, (FCapacity+1) * FItemSize);
- end;
- { Keep the ending of the list filled with zeros, don't leave garbage data
- there. Otherwise, we could accidentally have there a copy of some item
- on the list, and accidentally Deref it too soon.
- See http://bugs.freepascal.org/view.php?id=20005. }
- FillChar(InternalItems[FCount]^, (FCapacity+1-FCount) * FItemSize, #0);
- end;
- procedure TFPSList.DeleteRange(IndexFrom, IndexTo : Integer);
- var
- ListItem: Pointer;
- I: Integer;
- OldCnt : Integer;
- begin
- CheckIndex(IndexTo);
- CheckIndex(IndexFrom);
- OldCnt:=FCount;
- Dec(FCount,IndexTo-IndexFrom+1);
- For I :=IndexFrom To Indexto Do
- begin
- ListItem := InternalItems[I];
- Deref(ListItem);
- end;
- System.Move(InternalItems[IndexTo+1]^, InternalItems[IndexFrom]^, (OldCnt - IndexTo-1) * FItemSize);
- // Shrink the list if appropriate
- if (FCapacity > 256) and (FCount < FCapacity shr 2) then
- begin
- FCapacity := FCapacity shr 1;
- ReallocMem(FList, (FCapacity+1) * FItemSize);
- end;
- { Keep the ending of the list filled with zeros, don't leave garbage data
- there. Otherwise, we could accidentally have there a copy of some item
- on the list, and accidentally Deref it too soon.
- See http://bugs.freepascal.org/view.php?id=20005. }
- FillChar(InternalItems[FCount]^, (FCapacity+1-FCount) * FItemSize, #0);
- end;
- procedure TFPSList.Extract(Item: Pointer; ResultPtr: Pointer);
- var
- i : Integer;
- ListItemPtr : Pointer;
- begin
- i := IndexOf(Item);
- if i >= 0 then
- begin
- ListItemPtr := InternalItems[i];
- System.Move(ListItemPtr^, ResultPtr^, FItemSize);
- { fill with zeros, to avoid freeing/decreasing reference on following Delete }
- System.FillByte(ListItemPtr^, FItemSize, 0);
- Delete(i);
- end else
- System.FillByte(ResultPtr^, FItemSize, 0);
- end;
- class procedure TFPSList.Error(const Msg: string; Data: PtrInt);
- begin
- raise EListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame), get_caller_frame(get_frame);
- end;
- procedure TFPSList.Exchange(Index1, Index2: Integer);
- begin
- CheckIndex(Index1);
- CheckIndex(Index2);
- InternalExchange(Index1, Index2);
- end;
- procedure TFPSList.InternalExchange(Index1, Index2: Integer);
- begin
- System.Move(InternalItems[Index1]^, InternalItems[FCapacity]^, FItemSize);
- System.Move(InternalItems[Index2]^, InternalItems[Index1]^, FItemSize);
- System.Move(InternalItems[FCapacity]^, InternalItems[Index2]^, FItemSize);
- end;
- function TFPSList.Expand: TFPSList;
- var
- IncSize : Longint;
- begin
- if FCount < FCapacity then exit;
- IncSize := 4;
- if FCapacity > 3 then IncSize := IncSize + 4;
- if FCapacity > 8 then IncSize := IncSize + 8;
- if FCapacity > 127 then Inc(IncSize, FCapacity shr 2);
- SetCapacity(FCapacity + IncSize);
- Result := Self;
- end;
- function TFPSList.GetFirst: Pointer;
- begin
- If FCount = 0 then
- Result := Nil
- else
- Result := InternalItems[0];
- end;
- procedure TFPSList.SetFirst(const Value: Pointer);
- begin
- Put(0, Value);
- end;
- function TFPSList.IndexOf(Item: Pointer): Integer;
- var
- ListItem: Pointer;
- begin
- Result := 0;
- ListItem := First;
- while (Result < FCount) and (CompareByte(ListItem^, Item^, FItemSize) <> 0) do
- begin
- Inc(Result);
- ListItem := PByte(ListItem)+FItemSize;
- end;
- if Result = FCount then Result := -1;
- end;
- function TFPSList.Insert(Index: Integer): Pointer;
- begin
- if (Index < 0) or (Index > FCount) then
- Error(SListIndexError, Index);
- if FCount = FCapacity then Self.Expand;
- Result := InternalItems[Index];
- if Index<FCount then
- begin
- System.Move(Result^, (Result+FItemSize)^, (FCount - Index) * FItemSize);
- { clear for compiler assisted types }
- System.FillByte(Result^, FItemSize, 0);
- end;
- Inc(FCount);
- end;
- procedure TFPSList.Insert(Index: Integer; Item: Pointer);
- begin
- CopyItem(Item, Insert(Index));
- end;
- function TFPSList.GetLast: Pointer;
- begin
- if FCount = 0 then
- Result := nil
- else
- Result := InternalItems[FCount - 1];
- end;
- procedure TFPSList.SetLast(const Value: Pointer);
- begin
- Put(FCount - 1, Value);
- end;
- procedure TFPSList.Move(CurIndex, NewIndex: Integer);
- var
- CurItem, NewItem, TmpItem, Src, Dest: Pointer;
- MoveCount: Integer;
- begin
- CheckIndex(CurIndex);
- CheckIndex(NewIndex);
- if CurIndex = NewIndex then
- exit;
- CurItem := InternalItems[CurIndex];
- NewItem := InternalItems[NewIndex];
- TmpItem := InternalItems[FCapacity];
- System.Move(CurItem^, TmpItem^, FItemSize);
- if NewIndex > CurIndex then
- begin
- Src := InternalItems[CurIndex+1];
- Dest := CurItem;
- MoveCount := NewIndex - CurIndex;
- end else begin
- Src := NewItem;
- Dest := InternalItems[NewIndex+1];
- MoveCount := CurIndex - NewIndex;
- end;
- System.Move(Src^, Dest^, MoveCount * FItemSize);
- System.Move(TmpItem^, NewItem^, FItemSize);
- end;
- function TFPSList.Remove(Item: Pointer): Integer;
- begin
- Result := IndexOf(Item);
- if Result <> -1 then
- Delete(Result);
- end;
- const LocalThreshold = 64;
- procedure TFPSList.Pack;
- var
- LItemSize : integer;
- NewCount,
- i : integer;
- pdest,
- psrc : Pointer;
- localnul : array[0..LocalThreshold-1] of byte;
- pnul : pointer;
- begin
- LItemSize:=FItemSize;
- pnul:=@localnul;
- if LItemSize>Localthreshold then
- getmem(pnul,LItemSize);
- fillchar(pnul^,LItemSize,#0);
- NewCount:=0;
- psrc:=First;
- pdest:=psrc;
- For I:=0 To FCount-1 Do
- begin
- if not CompareMem(psrc,pnul,LItemSize) then
- begin
- System.Move(psrc^, pdest^, LItemSize);
- inc(pdest,LItemSIze);
- inc(NewCount);
- end
- else
- deref(psrc);
- inc(psrc,LitemSize);
- end;
- if LItemSize>Localthreshold then
- FreeMem(pnul,LItemSize);
- FCount:=NewCount;
- end;
- // Needed by Sort method.
- procedure TFPSList.QuickSort(L, R: Integer; Compare: TFPSListCompareFunc);
- var
- I, J, P: Integer;
- PivotItem: Pointer;
- begin
- repeat
- I := L;
- J := R;
- { cast to dword to avoid overflow to a negative number during addition which
- would result again in a negative number when being divided }
- P := (dword(L) + dword(R)) div 2;
- repeat
- PivotItem := InternalItems[P];
- while Compare(PivotItem, InternalItems[I]) > 0 do
- Inc(I);
- while Compare(PivotItem, InternalItems[J]) < 0 do
- Dec(J);
- if I <= J then
- begin
- InternalExchange(I, J);
- if P = I then
- P := J
- else if P = J then
- P := I;
- Inc(I);
- Dec(J);
- end;
- until I > J;
- if L < J then
- QuickSort(L, J, Compare);
- L := I;
- until I >= R;
- end;
- procedure TFPSList.Sort(Compare: TFPSListCompareFunc);
- begin
- if not Assigned(FList) or (FCount < 2) then exit;
- QuickSort(0, FCount-1, Compare);
- end;
- procedure TFPSList.AddList(Obj: TFPSList);
- var
- i: Integer;
- begin
- if Obj.ItemSize <> FItemSize then
- Error(SListItemSizeError, 0);
- // Do this now.
- Capacity:=Capacity+Obj.Count;
- if ItemIsManaged then
- begin
- // nothing for it, need to do it manually to give deref a chance.
- For I:=0 to Obj.Count-1 do
- Add(Obj[i])
- end
- else
- begin
- if Obj.Count=0 then
- exit;
- CopyItems(Obj.InternalItems[0],InternalItems[FCount],Obj.Count);
- FCount:=FCount+Obj.Count;
- end
- end;
- procedure TFPSList.Assign(Obj: TFPSList);
- begin
- // We must do this check here, to avoid clearing the list.
- if Obj.ItemSize <> FItemSize then
- Error(SListItemSizeError, 0);
- Clear;
- AddList(Obj);
- end;
- {****************************************************************************}
- {* TFPGListEnumerator *}
- {****************************************************************************}
- function TFPGListEnumerator.GetCurrent: T;
- begin
- Result := T(FList.Items[FPosition]^);
- end;
- constructor TFPGListEnumerator.Create(AList: TFPSList);
- begin
- inherited Create;
- FList := AList;
- FPosition := -1;
- end;
- function TFPGListEnumerator.MoveNext: Boolean;
- begin
- inc(FPosition);
- Result := FPosition < FList.Count;
- end;
- {****************************************************************************}
- {* TFPGList *}
- {****************************************************************************}
- constructor TFPGList.Create;
- begin
- inherited Create(sizeof(T));
- end;
- procedure TFPGList.CopyItem(Src, Dest: Pointer);
- begin
- T(Dest^) := T(Src^);
- end;
- procedure TFPGList.Deref(Item: Pointer);
- begin
- Finalize(T(Item^));
- end;
- function TFPGList.Get(Index: Integer): T;
- begin
- Result := T(inherited Get(Index)^);
- end;
- function TFPGList.GetList: PTypeList;
- begin
- Result := PTypeList(@FList);
- end;
- function TFPGList.ItemPtrCompare(Item1, Item2: Pointer): Integer;
- begin
- Result := FOnCompare(T(Item1^), T(Item2^));
- end;
- procedure TFPGList.Put(Index: Integer; const Item: T);
- begin
- inherited Put(Index, @Item);
- end;
- function TFPGList.Add(const Item: T): Integer;
- begin
- Result := inherited Add(@Item);
- end;
- function TFPGList.Extract(const Item: T): T;
- begin
- inherited Extract(@Item, @Result);
- end;
- function TFPGList.GetFirst: T;
- begin
- if FCount<>0 then
- Result := T(inherited GetFirst^)
- else
- Result:=Default(T);
- end;
- procedure TFPGList.SetFirst(const Value: T);
- begin
- inherited SetFirst(@Value);
- end;
- class function TFPGList.ItemIsManaged: Boolean;
- begin
- {$IFNDEF VER3_0}
- Result:=IsManagedType(T);
- {$ELSE}
- Result:=True; // Fallback to old behaviour
- {$ENDIF}
- end;
- function TFPGList.GetEnumerator: TFPGListEnumeratorSpec;
- begin
- Result := TFPGListEnumeratorSpec.Create(Self);
- end;
- function TFPGList.IndexOf(const Item: T): Integer;
- begin
- Result := 0;
- {$info TODO: fix inlining to work! InternalItems[Result]^}
- while (Result < FCount) and (PT(FList)[Result] <> Item) do
- Inc(Result);
- if Result = FCount then
- Result := -1;
- end;
- procedure TFPGList.Insert(Index: Integer; const Item: T);
- begin
- T(inherited Insert(Index)^) := Item;
- end;
- function TFPGList.GetLast: T;
- begin
- if FCount<>0 then
- Result := T(inherited GetLast^)
- else
- result:=Default(T);
- end;
- procedure TFPGList.SetLast(const Value: T);
- begin
- inherited SetLast(@Value);
- end;
- {$ifndef VER2_4}
- procedure TFPGList.AddList(Source: TFPGList);
- var
- i: Integer;
-
- begin
- if ItemIsManaged then
- begin
- Capacity:=Capacity+Source.Count;
- for I := 0 to Source.Count - 1 do
- Add(Source[i]);
- end
- else
- Inherited AddList(TFPSList(source))
- end;
- procedure TFPGList.Assign(Source: TFPGList);
- begin
- if ItemIsManaged then
- begin
- Clear;
- AddList(Source);
- end
- else
- Inherited Assign(TFPSList(source))
- end;
- {$endif VER2_4}
- function TFPGList.Remove(const Item: T): Integer;
- begin
- Result := IndexOf(Item);
- if Result >= 0 then
- Delete(Result);
- end;
- procedure TFPGList.Sort(Compare: TCompareFunc);
- begin
- FOnCompare := Compare;
- inherited Sort(@ItemPtrCompare);
- end;
- {****************************************************************************}
- {* TFPGObjectList *}
- {****************************************************************************}
- constructor TFPGObjectList.Create(FreeObjects: Boolean);
- begin
- inherited Create;
- FFreeObjects := FreeObjects;
- end;
- procedure TFPGObjectList.CopyItem(Src, Dest: Pointer);
- begin
- T(Dest^) := T(Src^);
- end;
- procedure TFPGObjectList.Deref(Item: Pointer);
- begin
- if FFreeObjects then
- T(Item^).Free;
- end;
- function TFPGObjectList.Get(Index: Integer): T;
- begin
- Result := T(inherited Get(Index)^);
- end;
- function TFPGObjectList.GetList: PTypeList;
- begin
- Result := PTypeList(@FList);
- end;
- function TFPGObjectList.ItemPtrCompare(Item1, Item2: Pointer): Integer;
- begin
- Result := FOnCompare(T(Item1^), T(Item2^));
- end;
- procedure TFPGObjectList.Put(Index: Integer; const Item: T);
- begin
- inherited Put(Index, @Item);
- end;
- function TFPGObjectList.Add(const Item: T): Integer;
- begin
- Result := inherited Add(@Item);
- end;
- function TFPGObjectList.Extract(const Item: T): T;
- begin
- inherited Extract(@Item, @Result);
- end;
- function TFPGObjectList.GetFirst: T;
- Var
- P: Pointer;
- begin
- if FCount<>0 then
- Result := T(inherited GetFirst^)
- else
- Result := Default(T)
- end;
- procedure TFPGObjectList.SetFirst(const Value: T);
- begin
- inherited SetFirst(@Value);
- end;
- function TFPGObjectList.GetEnumerator: TFPGListEnumeratorSpec;
- begin
- Result := TFPGListEnumeratorSpec.Create(Self);
- end;
- function TFPGObjectList.IndexOf(const Item: T): Integer;
- begin
- Result := 0;
- {$info TODO: fix inlining to work! InternalItems[Result]^}
- while (Result < FCount) and (PT(FList)[Result] <> Item) do
- Inc(Result);
- if Result = FCount then
- Result := -1;
- end;
- procedure TFPGObjectList.Insert(Index: Integer; const Item: T);
- begin
- T(inherited Insert(Index)^) := Item;
- end;
- function TFPGObjectList.GetLast: T;
- begin
- if FCount<>0 then
- Result := T(inherited GetLast^)
- else
- Result :=Default(T);
- end;
- procedure TFPGObjectList.SetLast(const Value: T);
- begin
- inherited SetLast(@Value);
- end;
- {$ifndef VER2_4}
- procedure TFPGObjectList.AddList(Source: TFPGObjectList);
- var
- i: Integer;
- begin
- for I := 0 to Source.Count - 1 do
- Add(Source[i]);
- end;
- procedure TFPGObjectList.Assign(Source: TFPGObjectList);
- begin
- Clear;
- AddList(Source);
- end;
- {$endif VER2_4}
- function TFPGObjectList.Remove(const Item: T): Integer;
- begin
- Result := IndexOf(Item);
- if Result >= 0 then
- Delete(Result);
- end;
- procedure TFPGObjectList.Sort(Compare: TCompareFunc);
- begin
- FOnCompare := Compare;
- inherited Sort(@ItemPtrCompare);
- end;
- {****************************************************************************}
- {* TFPGInterfacedObjectList *}
- {****************************************************************************}
- constructor TFPGInterfacedObjectList.Create;
- begin
- inherited Create;
- end;
- procedure TFPGInterfacedObjectList.CopyItem(Src, Dest: Pointer);
- begin
- if Assigned(Pointer(Dest^)) then
- T(Dest^)._Release;
- Pointer(Dest^) := Pointer(Src^);
- if Assigned(Pointer(Dest^)) then
- T(Dest^)._AddRef;
- end;
- procedure TFPGInterfacedObjectList.Deref(Item: Pointer);
- begin
- if Assigned(Pointer(Item^)) then
- T(Item^)._Release;
- end;
- function TFPGInterfacedObjectList.Get(Index: Integer): T;
- begin
- Result := T(inherited Get(Index)^);
- end;
- function TFPGInterfacedObjectList.GetList: PTypeList;
- begin
- Result := PTypeList(@FList);
- end;
- function TFPGInterfacedObjectList.ItemPtrCompare(Item1, Item2: Pointer): Integer;
- begin
- Result := FOnCompare(T(Item1^), T(Item2^));
- end;
- procedure TFPGInterfacedObjectList.Put(Index: Integer; const Item: T);
- begin
- CheckIndex(Index);
- InternalItems[Index] := @Item; // eventually calls copyitem()
- end;
- function TFPGInterfacedObjectList.Add(const Item: T): Integer;
- begin
- Result := inherited Add(@Item);
- end;
- function TFPGInterfacedObjectList.Extract(const Item: T): T;
- begin
- inherited Extract(@Item, @Result);
- end;
- function TFPGInterfacedObjectList.GetFirst: T;
- begin
- Result := T(inherited GetFirst^);
- end;
- procedure TFPGInterfacedObjectList.SetFirst(const Value: T);
- begin
- inherited SetFirst(@Value);
- end;
- function TFPGInterfacedObjectList.GetEnumerator: TFPGListEnumeratorSpec;
- begin
- Result := TFPGListEnumeratorSpec.Create(Self);
- end;
- function TFPGInterfacedObjectList.IndexOf(const Item: T): Integer;
- begin
- Result := 0;
- {$info TODO: fix inlining to work! InternalItems[Result]^}
- while (Result < FCount) and (PT(FList)[Result] <> Item) do
- Inc(Result);
- if Result = FCount then
- Result := -1;
- end;
- procedure TFPGInterfacedObjectList.Insert(Index: Integer; const Item: T);
- begin
- T(inherited Insert(Index)^) := Item;
- end;
- function TFPGInterfacedObjectList.GetLast: T;
- begin
- Result := T(inherited GetLast^);
- end;
- procedure TFPGInterfacedObjectList.SetLast(const Value: T);
- begin
- inherited SetLast(@Value);
- end;
- {$ifndef VER2_4}
- procedure TFPGInterfacedObjectList.Assign(Source: TFPGInterfacedObjectList);
- begin
- Clear;
- AddList(Source);
- end;
- procedure TFPGInterfacedObjectList.AddList(Source: TFPGInterfacedObjectList);
- var
- i: Integer;
- begin
- for I := 0 to Source.Count - 1 do
- Add(Source[i]);
- end;
- {$endif VER2_4}
- function TFPGInterfacedObjectList.Remove(const Item: T): Integer;
- begin
- Result := IndexOf(Item);
- if Result >= 0 then
- Delete(Result);
- end;
- procedure TFPGInterfacedObjectList.Sort(Compare: TCompareFunc);
- begin
- FOnCompare := Compare;
- inherited Sort(@ItemPtrCompare);
- end;
- {****************************************************************************
- TFPSMap
- ****************************************************************************}
- constructor TFPSMap.Create(AKeySize: Integer; ADataSize: integer);
- begin
- inherited Create(AKeySize+ADataSize);
- FKeySize := AKeySize;
- FDataSize := ADataSize;
- InitOnPtrCompare;
- end;
- procedure TFPSMap.CopyKey(Src, Dest: Pointer);
- begin
- System.Move(Src^, Dest^, FKeySize);
- end;
- procedure TFPSMap.CopyData(Src, Dest: Pointer);
- begin
- System.Move(Src^, Dest^, FDataSize);
- end;
- function TFPSMap.GetKey(Index: Integer): Pointer;
- begin
- Result := Items[Index];
- end;
- function TFPSMap.GetData(Index: Integer): Pointer;
- begin
- Result := PByte(Items[Index])+FKeySize;
- end;
- function TFPSMap.GetKeyData(AKey: Pointer): Pointer;
- var
- I: Integer;
- begin
- I := IndexOf(AKey);
- if I >= 0 then
- Result := InternalItems[I]+FKeySize
- else
- Error(SMapKeyError, PtrUInt(AKey));
- end;
- function TFPSMap.BinaryCompareKey(Key1, Key2: Pointer): Integer;
- begin
- Result := CompareByte(Key1^, Key2^, FKeySize);
- end;
- function TFPSMap.BinaryCompareData(Data1, Data2: Pointer): Integer;
- begin
- Result := CompareByte(Data1^, Data2^, FDataSize);
- end;
- procedure TFPSMap.SetOnKeyPtrCompare(Proc: TFPSListCompareFunc);
- begin
- if Proc <> nil then
- FOnKeyPtrCompare := Proc
- else
- FOnKeyPtrCompare := @BinaryCompareKey;
- end;
- procedure TFPSMap.SetOnDataPtrCompare(Proc: TFPSListCompareFunc);
- begin
- if Proc <> nil then
- FOnDataPtrCompare := Proc
- else
- FOnDataPtrCompare := @BinaryCompareData;
- end;
- procedure TFPSMap.InitOnPtrCompare;
- begin
- SetOnKeyPtrCompare(nil);
- SetOnDataPtrCompare(nil);
- end;
- procedure TFPSMap.PutKey(Index: Integer; AKey: Pointer);
- begin
- if FSorted then
- Error(SSortedListError, 0);
- CopyKey(AKey, Items[Index]);
- end;
- procedure TFPSMap.PutData(Index: Integer; AData: Pointer);
- begin
- CopyData(AData, PByte(Items[Index])+FKeySize);
- end;
- procedure TFPSMap.PutKeyData(AKey: Pointer; NewData: Pointer);
- var
- I: Integer;
- begin
- I := IndexOf(AKey);
- if I >= 0 then
- Data[I] := NewData
- else
- Add(AKey, NewData);
- end;
- procedure TFPSMap.SetSorted(Value: Boolean);
- begin
- if Value = FSorted then exit;
- FSorted := Value;
- if Value then Sort;
- end;
- function TFPSMap.Add(AKey: Pointer): Integer;
- begin
- if Sorted then
- begin
- if Find(AKey, Result) then
- case Duplicates of
- dupIgnore: exit;
- dupError: Error(SDuplicateItem, 0)
- end;
- end else
- Result := Count;
- CopyKey(AKey, inherited Insert(Result));
- end;
- function TFPSMap.Add(AKey, AData: Pointer): Integer;
- begin
- Result := Add(AKey);
- Data[Result] := AData;
- end;
- function TFPSMap.Find(AKey: Pointer; out Index: Integer): Boolean;
- { Searches for the first item <= Key, returns True if exact match,
- sets index to the index of the found string. }
- var
- I,L,R,Dir: Integer;
- begin
- Result := false;
- Index := -1;
- if not Sorted then
- raise EListError.Create(SErrFindNeedsSortedList);
- // Use binary search.
- L := 0;
- R := FCount-1;
- while L<=R do
- begin
- I := L + (R - L) div 2;
- Dir := FOnKeyPtrCompare(Items[I], AKey);
- if Dir < 0 then
- L := I+1
- else begin
- R := I-1;
- if Dir = 0 then
- begin
- Result := true;
- if Duplicates <> dupAccept then
- L := I;
- end;
- end;
- end;
- Index := L;
- end;
- function TFPSMap.LinearIndexOf(AKey: Pointer): Integer;
- var
- ListItem: Pointer;
- begin
- Result := 0;
- ListItem := First;
- while (Result < FCount) and (FOnKeyPtrCompare(ListItem, AKey) <> 0) do
- begin
- Inc(Result);
- ListItem := PByte(ListItem)+FItemSize;
- end;
- if Result = FCount then Result := -1;
- end;
- function TFPSMap.IndexOf(AKey: Pointer): Integer;
- begin
- if Sorted then
- begin
- if not Find(AKey, Result) then
- Result := -1;
- end else
- Result := LinearIndexOf(AKey);
- end;
- function TFPSMap.IndexOfData(AData: Pointer): Integer;
- var
- ListItem: Pointer;
- begin
- Result := 0;
- ListItem := First+FKeySize;
- while (Result < FCount) and (FOnDataPtrCompare(ListItem, AData) <> 0) do
- begin
- Inc(Result);
- ListItem := PByte(ListItem)+FItemSize;
- end;
- if Result = FCount then Result := -1;
- end;
- function TFPSMap.Insert(Index: Integer): Pointer;
- begin
- if FSorted then
- Error(SSortedListError, 0);
- Result := inherited Insert(Index);
- end;
- procedure TFPSMap.Insert(Index: Integer; out AKey, AData: Pointer);
- begin
- AKey := Insert(Index);
- AData := PByte(AKey) + FKeySize;
- end;
- procedure TFPSMap.InsertKey(Index: Integer; AKey: Pointer);
- begin
- CopyKey(AKey, Insert(Index));
- end;
- procedure TFPSMap.InsertKeyData(Index: Integer; AKey, AData: Pointer);
- var
- ListItem: Pointer;
- begin
- ListItem := Insert(Index);
- CopyKey(AKey, ListItem);
- CopyData(AData, PByte(ListItem)+FKeySize);
- end;
- function TFPSMap.Remove(AKey: Pointer): Integer;
- begin
- Result := IndexOf(AKey);
- if Result >= 0 then
- Delete(Result);
- end;
- procedure TFPSMap.Sort;
- begin
- inherited Sort(FOnKeyPtrCompare);
- end;
- {****************************************************************************
- TFPGMap
- ****************************************************************************}
- constructor TFPGMap.Create;
- begin
- inherited Create(SizeOf(TKey), SizeOf(TData));
- end;
- procedure TFPGMap.CopyItem(Src, Dest: Pointer);
- begin
- CopyKey(Src, Dest);
- CopyData(PByte(Src)+KeySize, PByte(Dest)+KeySize);
- end;
- procedure TFPGMap.CopyKey(Src, Dest: Pointer);
- begin
- TKey(Dest^) := TKey(Src^);
- end;
- procedure TFPGMap.CopyData(Src, Dest: Pointer);
- begin
- TData(Dest^) := TData(Src^);
- end;
- procedure TFPGMap.Deref(Item: Pointer);
- begin
- Finalize(TKey(Item^));
- Finalize(TData(Pointer(PByte(Item)+KeySize)^));
- end;
- function TFPGMap.GetKey(Index: Integer): TKey;
- begin
- Result := TKey(inherited GetKey(Index)^);
- end;
- function TFPGMap.GetData(Index: Integer): TData;
- begin
- Result := TData(inherited GetData(Index)^);
- end;
- function TFPGMap.GetKeyData(const AKey: TKey): TData;
- begin
- Result := TData(inherited GetKeyData(@AKey)^);
- end;
- function TFPGMap.KeyCompare(Key1, Key2: Pointer): Integer;
- begin
- if PKey(Key1)^ < PKey(Key2)^ then
- Result := -1
- else if PKey(Key1)^ > PKey(Key2)^ then
- Result := 1
- else
- Result := 0;
- end;
- {function TFPGMap.DataCompare(Data1, Data2: Pointer): Integer;
- begin
- if PData(Data1)^ < PData(Data2)^ then
- Result := -1
- else if PData(Data1)^ > PData(Data2)^ then
- Result := 1
- else
- Result := 0;
- end;}
- function TFPGMap.KeyCustomCompare(Key1, Key2: Pointer): Integer;
- begin
- Result := FOnKeyCompare(TKey(Key1^), TKey(Key2^));
- end;
- function TFPGMap.DataCustomCompare(Data1, Data2: Pointer): Integer;
- begin
- Result := FOnDataCompare(TData(Data1^), TData(Data2^));
- end;
- procedure TFPGMap.SetOnKeyCompare(NewCompare: TKeyCompareFunc);
- begin
- FOnKeyCompare := NewCompare;
- if NewCompare <> nil then
- OnKeyPtrCompare := @KeyCustomCompare
- else
- OnKeyPtrCompare := @KeyCompare;
- end;
- procedure TFPGMap.SetOnDataCompare(NewCompare: TDataCompareFunc);
- begin
- FOnDataCompare := NewCompare;
- if NewCompare <> nil then
- OnDataPtrCompare := @DataCustomCompare
- else
- OnDataPtrCompare := nil;
- end;
- procedure TFPGMap.InitOnPtrCompare;
- begin
- SetOnKeyCompare(nil);
- SetOnDataCompare(nil);
- end;
- procedure TFPGMap.PutKey(Index: Integer; const NewKey: TKey);
- begin
- inherited PutKey(Index, @NewKey);
- end;
- procedure TFPGMap.PutData(Index: Integer; const NewData: TData);
- begin
- inherited PutData(Index, @NewData);
- end;
- procedure TFPGMap.PutKeyData(const AKey: TKey; const NewData: TData);
- begin
- inherited PutKeyData(@AKey, @NewData);
- end;
- function TFPGMap.Add(const AKey: TKey): Integer;
- begin
- Result := inherited Add(@AKey);
- end;
- function TFPGMap.Add(const AKey: TKey; const AData: TData): Integer;
- begin
- Result := inherited Add(@AKey, @AData);
- end;
- function TFPGMap.Find(const AKey: TKey; out Index: Integer): Boolean;
- begin
- Result := inherited Find(@AKey, Index);
- end;
- function TFPGMap.TryGetData(const AKey: TKey; out AData: TData): Boolean;
- var
- I: Integer;
- begin
- I := IndexOf(AKey);
- Result := (I >= 0);
- if Result then
- AData := TData(inherited GetData(I)^)
- else
- AData := Default(TData);
- end;
- procedure TFPGMap.AddOrSetData(const AKey: TKey; const AData: TData);
- begin
- inherited PutKeyData(@AKey, @AData);
- end;
- function TFPGMap.IndexOf(const AKey: TKey): Integer;
- begin
- Result := inherited IndexOf(@AKey);
- end;
- function TFPGMap.IndexOfData(const AData: TData): Integer;
- begin
- { TODO: loop ? }
- Result := inherited IndexOfData(@AData);
- end;
- procedure TFPGMap.InsertKey(Index: Integer; const AKey: TKey);
- begin
- inherited InsertKey(Index, @AKey);
- end;
- procedure TFPGMap.InsertKeyData(Index: Integer; const AKey: TKey; const AData: TData);
- begin
- inherited InsertKeyData(Index, @AKey, @AData);
- end;
- function TFPGMap.Remove(const AKey: TKey): Integer;
- begin
- Result := inherited Remove(@AKey);
- end;
- {****************************************************************************
- TFPGMapObject
- ****************************************************************************}
- constructor TFPGMapObject.Create(AFreeObjects: Boolean);
- begin
- inherited Create(SizeOf(TKey), SizeOf(TData));
- FFreeObjects := AFreeObjects;
- end;
- constructor TFPGMapObject.Create;
- begin
- Create(True);
- end;
- procedure TFPGMapObject.CopyItem(Src, Dest: Pointer);
- begin
- CopyKey(Src, Dest);
- CopyData(PByte(Src)+KeySize, PByte(Dest)+KeySize);
- end;
- procedure TFPGMapObject.CopyKey(Src, Dest: Pointer);
- begin
- TKey(Dest^) := TKey(Src^);
- end;
- procedure TFPGMapObject.CopyData(Src, Dest: Pointer);
- begin
- if Assigned(Pointer(Dest^)) And FFreeObjects then
- TData(Dest^).Free;
- TData(Dest^) := TData(Src^);
- end;
- procedure TFPGMapObject.Deref(Item: Pointer);
- begin
- Finalize(TKey(Item^));
- if Assigned(PPointer(PByte(Item)+KeySize)^) and FFreeObjects then
- TData(Pointer(PByte(Item)+KeySize)^).Free;
- end;
- function TFPGMapObject.GetKey(Index: Integer): TKey;
- begin
- Result := TKey(inherited GetKey(Index)^);
- end;
- function TFPGMapObject.GetData(Index: Integer): TData;
- begin
- Result := TData(inherited GetData(Index)^);
- end;
- function TFPGMapObject.GetKeyData(const AKey: TKey): TData;
- begin
- Result := TData(inherited GetKeyData(@AKey)^);
- end;
- function TFPGMapObject.KeyCompare(Key1, Key2: Pointer): Integer;
- begin
- if PKey(Key1)^ < PKey(Key2)^ then
- Result := -1
- else if PKey(Key1)^ > PKey(Key2)^ then
- Result := 1
- else
- Result := 0;
- end;
- {function TFPGMapObject.DataCompare(Data1, Data2: Pointer): Integer;
- begin
- if PData(Data1)^ < PData(Data2)^ then
- Result := -1
- else if PData(Data1)^ > PData(Data2)^ then
- Result := 1
- else
- Result := 0;
- end;}
- function TFPGMapObject.KeyCustomCompare(Key1, Key2: Pointer): Integer;
- begin
- Result := FOnKeyCompare(TKey(Key1^), TKey(Key2^));
- end;
- function TFPGMapObject.DataCustomCompare(Data1, Data2: Pointer): Integer;
- begin
- Result := FOnDataCompare(TData(Data1^), TData(Data2^));
- end;
- procedure TFPGMapObject.SetOnKeyCompare(NewCompare: TKeyCompareFunc);
- begin
- FOnKeyCompare := NewCompare;
- if NewCompare <> nil then
- OnKeyPtrCompare := @KeyCustomCompare
- else
- OnKeyPtrCompare := @KeyCompare;
- end;
- procedure TFPGMapObject.SetOnDataCompare(NewCompare: TDataCompareFunc);
- begin
- FOnDataCompare := NewCompare;
- if NewCompare <> nil then
- OnDataPtrCompare := @DataCustomCompare
- else
- OnDataPtrCompare := nil;
- end;
- procedure TFPGMapObject.InitOnPtrCompare;
- begin
- SetOnKeyCompare(nil);
- SetOnDataCompare(nil);
- end;
- procedure TFPGMapObject.PutKey(Index: Integer; const NewKey: TKey);
- begin
- inherited PutKey(Index, @NewKey);
- end;
- procedure TFPGMapObject.PutData(Index: Integer; const NewData: TData);
- begin
- inherited PutData(Index, @NewData);
- end;
- procedure TFPGMapObject.PutKeyData(const AKey: TKey; const NewData: TData);
- begin
- inherited PutKeyData(@AKey, @NewData);
- end;
- function TFPGMapObject.Add(const AKey: TKey): Integer;
- begin
- Result := inherited Add(@AKey);
- end;
- function TFPGMapObject.Add(const AKey: TKey; const AData: TData): Integer;
- begin
- Result := inherited Add(@AKey, @AData);
- end;
- function TFPGMapObject.Find(const AKey: TKey; out Index: Integer): Boolean;
- begin
- Result := inherited Find(@AKey, Index);
- end;
- function TFPGMapObject.TryGetData(const AKey: TKey; out AData: TData): Boolean;
- var
- I: Integer;
- begin
- I := IndexOf(AKey);
- Result := (I >= 0);
- if Result then
- AData := TData(inherited GetData(I)^)
- else
- AData := Default(TData);
- end;
- procedure TFPGMapObject.AddOrSetData(const AKey: TKey; const AData: TData);
- begin
- inherited PutKeyData(@AKey, @AData);
- end;
- function TFPGMapObject.IndexOf(const AKey: TKey): Integer;
- begin
- Result := inherited IndexOf(@AKey);
- end;
- function TFPGMapObject.IndexOfData(const AData: TData): Integer;
- begin
- { TODO: loop ? }
- Result := inherited IndexOfData(@AData);
- end;
- procedure TFPGMapObject.InsertKey(Index: Integer; const AKey: TKey);
- begin
- inherited InsertKey(Index, @AKey);
- end;
- procedure TFPGMapObject.InsertKeyData(Index: Integer; const AKey: TKey; const AData: TData);
- begin
- inherited InsertKeyData(Index, @AKey, @AData);
- end;
- function TFPGMapObject.Remove(const AKey: TKey): Integer;
- begin
- Result := inherited Remove(@AKey);
- end;
- {****************************************************************************
- TFPGMapInterfacedObjectData
- ****************************************************************************}
- constructor TFPGMapInterfacedObjectData.Create;
- begin
- inherited Create(SizeOf(TKey), SizeOf(TData));
- end;
- procedure TFPGMapInterfacedObjectData.CopyItem(Src, Dest: Pointer);
- begin
- CopyKey(Src, Dest);
- CopyData(PByte(Src)+KeySize, PByte(Dest)+KeySize);
- end;
- procedure TFPGMapInterfacedObjectData.CopyKey(Src, Dest: Pointer);
- begin
- TKey(Dest^) := TKey(Src^);
- end;
- procedure TFPGMapInterfacedObjectData.CopyData(Src, Dest: Pointer);
- begin
- if Assigned(Pointer(Dest^)) then
- TData(Dest^)._Release;
- TData(Dest^) := TData(Src^);
- if Assigned(Pointer(Dest^)) then
- TData(Dest^)._AddRef;
- end;
- procedure TFPGMapInterfacedObjectData.Deref(Item: Pointer);
- begin
- Finalize(TKey(Item^));
- if Assigned(PPointer(PByte(Item)+KeySize)^) then
- TData(Pointer(PByte(Item)+KeySize)^)._Release;
- end;
- function TFPGMapInterfacedObjectData.GetKey(Index: Integer): TKey;
- begin
- Result := TKey(inherited GetKey(Index)^);
- end;
- function TFPGMapInterfacedObjectData.GetData(Index: Integer): TData;
- begin
- Result := TData(inherited GetData(Index)^);
- end;
- function TFPGMapInterfacedObjectData.GetKeyData(const AKey: TKey): TData;
- begin
- Result := TData(inherited GetKeyData(@AKey)^);
- end;
- function TFPGMapInterfacedObjectData.KeyCompare(Key1, Key2: Pointer): Integer;
- begin
- if PKey(Key1)^ < PKey(Key2)^ then
- Result := -1
- else if PKey(Key1)^ > PKey(Key2)^ then
- Result := 1
- else
- Result := 0;
- end;
- {function TFPGMapInterfacedObjectData.DataCompare(Data1, Data2: Pointer): Integer;
- begin
- if PData(Data1)^ < PData(Data2)^ then
- Result := -1
- else if PData(Data1)^ > PData(Data2)^ then
- Result := 1
- else
- Result := 0;
- end;}
- function TFPGMapInterfacedObjectData.KeyCustomCompare(Key1, Key2: Pointer): Integer;
- begin
- Result := FOnKeyCompare(TKey(Key1^), TKey(Key2^));
- end;
- function TFPGMapInterfacedObjectData.DataCustomCompare(Data1, Data2: Pointer): Integer;
- begin
- Result := FOnDataCompare(TData(Data1^), TData(Data2^));
- end;
- procedure TFPGMapInterfacedObjectData.SetOnKeyCompare(NewCompare: TKeyCompareFunc);
- begin
- FOnKeyCompare := NewCompare;
- if NewCompare <> nil then
- OnKeyPtrCompare := @KeyCustomCompare
- else
- OnKeyPtrCompare := @KeyCompare;
- end;
- procedure TFPGMapInterfacedObjectData.SetOnDataCompare(NewCompare: TDataCompareFunc);
- begin
- FOnDataCompare := NewCompare;
- if NewCompare <> nil then
- OnDataPtrCompare := @DataCustomCompare
- else
- OnDataPtrCompare := nil;
- end;
- procedure TFPGMapInterfacedObjectData.InitOnPtrCompare;
- begin
- SetOnKeyCompare(nil);
- SetOnDataCompare(nil);
- end;
- procedure TFPGMapInterfacedObjectData.PutKey(Index: Integer; const NewKey: TKey);
- begin
- inherited PutKey(Index, @NewKey);
- end;
- procedure TFPGMapInterfacedObjectData.PutData(Index: Integer; const NewData: TData);
- begin
- inherited PutData(Index, @NewData);
- end;
- procedure TFPGMapInterfacedObjectData.PutKeyData(const AKey: TKey; const NewData: TData);
- begin
- inherited PutKeyData(@AKey, @NewData);
- end;
- function TFPGMapInterfacedObjectData.Add(const AKey: TKey): Integer;
- begin
- Result := inherited Add(@AKey);
- end;
- function TFPGMapInterfacedObjectData.Add(const AKey: TKey; const AData: TData): Integer;
- begin
- Result := inherited Add(@AKey, @AData);
- end;
- function TFPGMapInterfacedObjectData.Find(const AKey: TKey; out Index: Integer): Boolean;
- begin
- Result := inherited Find(@AKey, Index);
- end;
- function TFPGMapInterfacedObjectData.TryGetData(const AKey: TKey; out AData: TData): Boolean;
- var
- I: Integer;
- begin
- I := IndexOf(AKey);
- Result := (I >= 0);
- if Result then
- AData := TData(inherited GetData(I)^)
- else
- AData := Default(TData);
- end;
- procedure TFPGMapInterfacedObjectData.AddOrSetData(const AKey: TKey;
- const AData: TData);
- begin
- inherited PutKeyData(@AKey, @AData);
- end;
- function TFPGMapInterfacedObjectData.IndexOf(const AKey: TKey): Integer;
- begin
- Result := inherited IndexOf(@AKey);
- end;
- function TFPGMapInterfacedObjectData.IndexOfData(const AData: TData): Integer;
- begin
- { TODO: loop ? }
- Result := inherited IndexOfData(@AData);
- end;
- procedure TFPGMapInterfacedObjectData.InsertKey(Index: Integer; const AKey: TKey);
- begin
- inherited InsertKey(Index, @AKey);
- end;
- procedure TFPGMapInterfacedObjectData.InsertKeyData(Index: Integer; const AKey: TKey; const AData: TData);
- begin
- inherited InsertKeyData(Index, @AKey, @AData);
- end;
- function TFPGMapInterfacedObjectData.Remove(const AKey: TKey): Integer;
- begin
- Result := inherited Remove(@AKey);
- end;
- end.
|