IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 Delphi Discussion :

TObjectBucketList indexé sur une string


Sujet :

Delphi

  1. #1
    Membre du Club
    Développeur informatique
    Inscrit en
    Juillet 2005
    Messages
    87
    Détails du profil
    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2005
    Messages : 87
    Points : 60
    Points
    60
    Par défaut TObjectBucketList indexé sur une string
    Bonjour,

    J'essai d'avoir plus ou moins l'équivalent d'un HashMap en Java, c'est à dire une liste de clé / valeur avec clé et valeur qui sont des objets.
    J'ai donc utilisé un TObjectBucketList, en clé je voudrais passer une string et un objet en valeur.
    Hors, voilà mon problème, alors que pour java, deux poiteurs vers une string identiques sont égaux, je ne retrouve pas le même comportement en Pascal. Ce qui fait que si j'utilise une string pour clé; si ce n'est pas exactement le même objet ça ne marche pas Y aurait-il moyen de reproduire le comportement de java car c'est bien pratique !

  2. #2
    Expert éminent sénior
    Avatar de ShaiLeTroll
    Homme Profil pro
    Développeur C++\Delphi
    Inscrit en
    Juillet 2006
    Messages
    13 612
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Développeur C++\Delphi
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2006
    Messages : 13 612
    Points : 25 303
    Points
    25 303
    Par défaut
    J'ai aussi tenté d'utiliser la TObjectBucketList un jour mais cela ne m'avait pas convaincu, j'ai fait cette classe THashStringList (assez mal nommé, puisqu'en réalité il n'y a pas de Hash, j'aurais du l'appeler TAssociativeStringList ...)

    Le Sujet d'Origine était : TStrings et dictionnaire (clé/value)

    J'ai donc fait cette THashStringList et en plus j'ai ajouté TTreeHashingObjectList dans le sujet "Temps pour parser un fichier de + de 3 millions de lignes"

    TTreeHashingObjectList est encore plus rapide que THashStringList, cela se base en un arbre pour répartir les objets (une lettre par niveau), attention, plus la chaine est longue plus l'arbre est grand et lourd en mémoire ... Attention, cela ne fonctionne que pour les AnsiString !

    Voici la toute dernière Version
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    {* -----------------------------------------------------------------------------
    Unité : Liste de Hashage
    @author S******* P***** aka ShaiLeTroll
    @version 0.6
    ------------------------------------------------------------------------------ }
     
    unit uHashList;
     
    interface
     
    uses
      Windows, Classes;
     
    type
      EHashStringListError = class(EStringListError);
      {*
      Classe interne des items d'une THashStringList
      }
      THashStringItem = class;
      {*
      Classe qui permet d'associer une Clé de Hashage, une Valeur, une libellé, un Tag et un Objet
      }
      THashStringList = class(TObject)
      private
        FInternalList: TStringList; /// Champ Interne
        FUniqueTags: Boolean;
        function FindCode(const HashCode: string; IndexRequired: Boolean = False): Integer; /// Recherche un Code
        function FindTag(Tag: Integer; IndexRequired: Boolean = False): Integer; /// Recherche un Tag
        function GetItemsFromIndex(Index: Integer): THashStringItem;
        function GetCodeSorted: Boolean;
        procedure SetCodeSorted(const Value: Boolean);
        function GetCaseSensitive: Boolean;
        procedure SetCaseSensitive(const Value: Boolean);
      protected
        function GetCodesFromIndex(Index: Integer): string; /// Accesseur en Lecture de CodesFromIndex
        function GetCodesFromTag(Tag: Integer): string; /// Accesseur en Lecture de CodesFromTag
     
        function GetTagFromIndex(Index: Integer): Integer; /// Accesseur en Lecture de TagFromIndex
        procedure SetTagFromIndex(Index: Integer; Tag: Integer); /// Accesseur en Ecriture de TagFromIndex, Mise à Jour uniquement
        function GetTagFromCode(const HashCode: string): Integer; /// Accesseur en Lecture de TagFromCode
        procedure SetTagFromCode(const HashCode: string; Tag: Integer); /// Accesseur en Ecriture de TagFromCode, Ajoute le Code si il n'existe pas !!!
     
        function GetValuesFromIndex(Index: Integer): string; /// Accesseur en Lecture de ValuesFromIndex
        procedure SetValuesFromIndex(Index: Integer; const Value: string); /// Accesseur en Ecriture de ValuesFromTag, Mise à Jour uniquement
        function GetValuesFromCode(const HashCode: string): string; /// Accesseur en Lecture de ValuesFromCode
        procedure SetValuesFromCode(const HashCode: string; const Value: string); /// Accesseur en Ecriture de ValuesFromCode, Ajoute le Code si il n'existe pas !!!
        function GetValuesFromTag(Tag: Integer): string; /// Accesseur en Lecture de ValuesFromTag
        procedure SetValuesFromTag(Tag: Integer; const Value: string); /// Accesseur en Ecriture de ValuesFromTag, Mise à Jour uniquement
     
        function GetCaptionsFromIndex(Index: Integer): string; /// Accesseur en Lecture de CaptionsFromCode
        procedure SetCaptionsFromIndex(Index: Integer; const Caption: string); /// Accesseur en Ecriture de CaptionsFromCode, Mise à Jour uniquement
        function GetCaptionsFromCode(const HashCode: string): string; /// Accesseur en Lecture de CaptionsFromCode
        procedure SetCaptionsFromCode(const HashCode, Caption: string); /// Accesseur en Ecriture de CaptionsFromCode, Ajoute le Code si il n'existe pas !!!
        function GetCaptionsFromTag(Tag: Integer): string; /// Accesseur en Lecture de CaptionsFromTag
        procedure SetCaptionsFromTag(Tag: Integer; const Caption: string); /// Accesseur en Ecriture de CaptionsFromTag, Mise à Jour uniquement
     
        function GetObjectsFromIndex(Index: Integer): TObject; /// Accesseur en Lecture de ObjectsFromIndex
        procedure SetObjectsFromIndex(Index: Integer; Obj: TObject); /// Accesseur en Ecriture de ObjectsFromIndex, Mise à Jour uniquement
        function GetObjectsFromCode(const HashCode: string): TObject; /// Accesseur en Lecture de ObjectsFromCode
        procedure SetObjectsFromCode(const HashCode: string; Obj: TObject); /// Accesseur en Ecriture de ObjectsFromCode, Mise à Jour uniquement
        function GetObjectsFromTag(Tag: Integer): TObject; /// Accesseur en Lecture de ObjectsFromTag
        procedure SetObjectsFromTag(Tag: Integer; Obj: TObject); /// Accesseur en Ecriture de ObjectsFromTag, Mise à Jour uniquement
     
        function GetDataFromIndex(Index: Integer): Pointer; /// Accesseur en Lecture de DataFromIndex
        procedure SetDataFromIndex(Index: Integer; Obj: Pointer); /// Accesseur en Ecriture de DataFromIndex, Mise à Jour uniquement
        function GetDataFromCode(const HashCode: string): Pointer; /// Accesseur en Lecture de DataFromCode
        procedure SetDataFromCode(const HashCode: string; Obj: Pointer); /// Accesseur en Ecriture de DataFromCode, Mise à Jour uniquement
        function GetDataFromTag(Tag: Integer): Pointer; /// Accesseur en Lecture de DataFromTag
        procedure SetDataFromTag(Tag: Integer; Obj: Pointer); /// Accesseur en Ecriture de DataFromTag, Mise à Jour uniquement
     
        function GetCount(): Integer; /// Accesseur en Lecture de Count
     
        property ItemsFromIndex[Index: Integer]: THashStringItem read GetItemsFromIndex; // Accès aux éléments internes de la liste
      public
        constructor Create(); /// Constructeur
        destructor Destroy; override; /// Destructeur
        procedure Clear(); /// Clear
        procedure Assign(Source: THashStringList);
     
        property CodesFromIndex[Index: Integer]: string read GetCodesFromIndex; /// Hash par Index // Liste Triée En Lecture Seule
        property CodesFromTag[Tag: Integer]: string read GetCodesFromTag; /// Hash par Tag // Liste Triée En Lecture Seule
     
        property TagFromIndex[Index: Integer]: Integer read GetTagFromIndex write SetTagFromIndex; /// Tag par Index
        property TagFromCode[const HashCode: string]: Integer read GetTagFromCode write SetTagFromCode; /// Tag par Hash
     
        property ValuesFromIndex[Index: Integer]: string read GetValuesFromIndex write SetValuesFromIndex; /// Valeur par Index
        property ValuesFromCode[const HashCode: string]: string read GetValuesFromCode write SetValuesFromCode; default; /// Valeur par Hash
        property ValuesFromTag[Tag: Integer]: string read GetValuesFromTag write SetValuesFromTag; /// Valeur par Tag
     
        property CaptionsFromIndex[Index: Integer]: string read GetCaptionsFromIndex write SetCaptionsFromIndex; /// Libellé par Index
        property CaptionsFromCode[const HashCode: string]: string read GetCaptionsFromCode write SetCaptionsFromCode; /// Libellé par Hash
        property CaptionsFromTag[Tag: Integer]: string read GetCaptionsFromTag write SetCaptionsFromTag; /// Libellé par Tag
     
        property ObjectsFromIndex[Index: Integer]: TObject read GetObjectsFromIndex write SetObjectsFromIndex; /// Objet par Index
        property ObjectsFromCode[const HashCode: string]: TObject read GetObjectsFromCode write SetObjectsFromCode; /// Objet par Hash
        property ObjectsFromTag[Tag: Integer]: TObject read GetObjectsFromTag write SetObjectsFromTag; /// Objet par Tag
     
        property DataFromIndex[Index: Integer]: Pointer read GetDataFromIndex write SetDataFromIndex; /// Data par Index
        property DataFromCode[const HashCode: string]: Pointer read GetDataFromCode write SetDataFromCode; /// Data par Hash
        property DataFromTag[Tag: Integer]: Pointer read GetDataFromTag write SetDataFromTag; /// Data par Tag
     
        property Count: Integer read GetCount; /// Count
        property CodeSorted: Boolean read GetCodeSorted write SetCodeSorted; /// CodeSorted, trie les codes de Hash, cela améliore les performances !
        property CaseSensitive: Boolean read GetCaseSensitive write SetCaseSensitive;
        property UniqueTags: Boolean read FUniqueTags write FUniqueTags; /// DuplicateTags, ne vérifie pas les tags déjà existants !
      end;
     
      THashStringItem = class(TObject)
      private
        FValue: string;
        FCaption: string;
        FTag: Integer;
        FSubObject: TObject;
        FData: Pointer;
        constructor Create(const AValue: string; const ACaption: string); overload;
        constructor Create(ATag: Integer); overload;
      end;
     
      {*
      Classe interne des items d'une TTreeHashingObjectList
      }
      TTreeHashingObjectItem = class;
      {*
      Classe qui permet d'associer une Clé de Hashage et un Objet
      }
      TTreeHashingObjectList = class(TObject)
      private
        FIndexArray: array[Char] of TTreeHashingObjectItem;
        procedure AddCode(const HashCode: string; Level: Integer; LevelItem: TTreeHashingObjectItem; const Value: TObject);
        function FindCode(const HashCode: string; out Level: Integer; out LevelItem: TTreeHashingObjectItem): Boolean;
      protected
        function GetObjectsFromCode(const HashCode: string): TObject; /// Accesseur en Lecture de ObjectsFromCode
        procedure SetObjectsFromCode(const HashCode: string; const Obj: TObject); /// Accesseur en Ecriture de ObjectsFromCode, Mise à Jour uniquement
      public
        constructor Create(); /// Constructeur
        destructor Destroy; override; /// Destructeur
        procedure Clear(); /// Clear
     
        property ObjectsFromCode[const HashCode: string]: TObject read GetObjectsFromCode write SetObjectsFromCode; default; /// Objet par Hash
      end;
     
      TTreeHashingObjectItem = class(TObject)
      private
        FIndexArray: array of TTreeHashingObjectItem;
        FObject: TObject;
        FChar: Char;
     
        procedure AddCode(const HashCode: string; Level: Integer; const Obj: TObject);
        function FindCode(C: Char): TTreeHashingObjectItem;
      public
        constructor Create(AChar: Char; AObject: TObject); /// Constructeur
        destructor Destroy; override; /// Destructeur
        procedure Clear(); /// Clear
     
      end;
     
    implementation
     
    resourcestring
      SDuplicateTag = 'Tag : %d déjà affecté !';
      SNotUniqueTag = 'Tag n''est pas déclaré commme clé unique !'#13'Accès Refusé !';
      SCodeSortedChangeListNotEmpty = 'Le Tri sur le Code de Hashage ne peut être changé que sur une Liste Vide !';
      SCaseSensitiveChangeListNotEmpty = 'Le Sensibilité à la Casse ne peut être changé que sur une Liste Vide !';
      SNotExistsCode = 'Le Code "%s" n''existe pas dans la liste !';
      SNotExistsTag = 'Le Tag %d n''existe pas dans la liste !';
     
    { THashStringList }
     
    { THashStringList - Constructeurs }
     
    {* ----------------------------------------------------------------------------}
    constructor THashStringList.Create();
    begin
      inherited;
     
      FInternalList := TStringList.Create();
     
      CaseSensitive := False; // Par Défaut, on facilite l'utilisation de la liste en ignorant la Casse, mettre à True pourrait améliorer les performances.
      CodeSorted := True; // Par Défaut, on utilise cette liste pour le Hashage, le tri améliore les performances.
      UniqueTags := False; // Tag Libre par défaut, mais rend l'accès par le tag impossible car non pertinent !
    end;
     
    {* ----------------------------------------------------------------------------}
    destructor THashStringList.Destroy;
    begin
      Clear();
     
      FInternalList.Free();
      FInternalList := nil;
     
      inherited;
    end;
     
    { THashStringList - Méthodes Publiques }
     
    {* ----------------------------------------------------------------------------}
    procedure THashStringList.Clear();
    var
      Index: Integer;
    begin
      if Self <> nil then
      begin
        for Index := 0 to Count - 1 do
           ItemsFromIndex[Index].Free(); // Libère l'Objet Hash mais pas le SubObject ni le Data
     
        FInternalList.Clear();
      end;
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure THashStringList.Assign(Source: THashStringList);
    var
      Index: Integer;
    begin
      Clear();
     
      Self.CodeSorted := Source.CodeSorted;
      Self.CaseSensitive := Source.CaseSensitive;
      Self.UniqueTags := Source.UniqueTags;
     
      for Index := 0 to Source.Count - 1 do
      begin
        Self[Source.CodesFromIndex[Index]] := Source.ValuesFromIndex[Index];
        Self.TagFromIndex[Index] := Source.TagFromIndex[Index];
        Self.CaptionsFromIndex[Index] := Source.CaptionsFromIndex[Index];
        Self.ObjectsFromIndex[Index] := Source.ObjectsFromIndex[Index];
        Self.DataFromIndex[Index] := Source.DataFromIndex[Index];
      end;
    end;
     
    { THashStringList - Méthodes Accesseurs }
     
    { THashStringList - Méthodes Accesseurs - Codes }
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetCodesFromIndex(Index: Integer): string;
    begin
      Result := FInternalList.Strings[Index];
    end;
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetCodesFromTag(Tag: Integer): string;
    begin
      Result := FInternalList.Strings[FindTag(Tag, True)];
    end;
     
    { THashStringList - Méthodes Accesseurs - Tag }
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetTagFromIndex(Index: Integer): Integer;
    begin
      Result := ItemsFromIndex[Index].FTag;
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure THashStringList.SetTagFromIndex(Index: Integer; Tag: Integer);
    begin
      if UniqueTags and (FindTag(Tag) >= 0) then
        raise EHashStringListError.CreateFmt(SDuplicateTag, [Tag]);
     
      ItemsFromIndex[Index].FTag := Tag;
    end;
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetTagFromCode(const HashCode: string): Integer;
    begin
      Result := ItemsFromIndex[FindCode(HashCode, True)].FTag;
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure THashStringList.SetTagFromCode(const HashCode: string; Tag: Integer);
    var
      Index: Integer;
    begin
      if UniqueTags and (FindTag(Tag) >= 0) then
        raise EHashStringListError.CreateFmt(SDuplicateTag, [Tag]);
     
      Index := FindCode(HashCode);
      if Index >= 0 then
      begin
         TagFromIndex[Index] := Tag;
      end
      else
      begin
         FInternalList.AddObject(HashCode, THashStringItem.Create(Tag));
      end;
    end;
     
    { THashStringList - Méthodes Accesseurs - Values }
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetValuesFromIndex(Index: Integer): string;
    begin
      Result := ItemsFromIndex[Index].FValue;
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure THashStringList.SetValuesFromIndex(Index: Integer; const Value: string);
    begin
      ItemsFromIndex[Index].FValue := Value;
    end;
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetValuesFromCode(const HashCode: string): string;
    begin
      Result := ValuesFromIndex[FindCode(HashCode, True)];
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure THashStringList.SetValuesFromCode(const HashCode: string; const Value: string);
    var
      Index: Integer;
    begin
      Index := FindCode(HashCode);
      if Index >= 0 then
      begin
         ValuesFromIndex[Index] := Value;
      end
      else
      begin
         FInternalList.AddObject(HashCode, THashStringItem.Create(Value, ''));
      end;
    end;
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetValuesFromTag(Tag: Integer): string;
    begin
       Result := ValuesFromIndex[FindTag(Tag, True)];
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure THashStringList.SetValuesFromTag(Tag: Integer; const Value: string);
    begin
       ValuesFromIndex[FindTag(Tag, True)] := Value;
    end;
     
    { THashStringList - Méthodes Accesseurs - Captions }
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetCaptionsFromIndex(Index: Integer): string;
    begin
      Result := ItemsFromIndex[Index].FCaption;
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure THashStringList.SetCaptionsFromIndex(Index: Integer; const Caption: string);
    begin
      ItemsFromIndex[Index].FCaption := Caption;
    end;
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetCaptionsFromCode(const HashCode: string): string;
    begin
      Result := CaptionsFromIndex[FindCode(HashCode, True)];
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure THashStringList.SetCaptionsFromCode(const HashCode, Caption: string);
    var
      Index: Integer;
    begin
      Index := FindCode(HashCode);
      if Index >= 0 then
      begin
         ItemsFromIndex[Index].FCaption := Caption;
      end
      else
      begin
        FInternalList.AddObject(HashCode, THashStringItem.Create('', Caption));
      end;
    end;
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetCaptionsFromTag(Tag: Integer): string;
    begin
       Result := CaptionsFromIndex[FindTag(Tag, True)];
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure THashStringList.SetCaptionsFromTag(Tag: Integer; const Caption: string);
    begin
       CaptionsFromIndex[FindTag(Tag, True)] := Caption;
    end;
     
    { THashStringList - Méthodes Accesseurs - Objects }
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetObjectsFromIndex(Index: Integer): TObject;
    begin
      Result := ItemsFromIndex[Index].FSubObject;
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure THashStringList.SetObjectsFromIndex(Index: Integer; Obj: TObject);
    begin
      ItemsFromIndex[Index].FSubObject := Obj;
    end;
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetObjectsFromCode(const HashCode: string): TObject;
    begin
      Result := ObjectsFromIndex[FindCode(HashCode, True)];
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure THashStringList.SetObjectsFromCode(const HashCode: string; Obj: TObject);
    begin
      ObjectsFromIndex[FindCode(HashCode, True)] := Obj;
    end;
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetObjectsFromTag(Tag: Integer): TObject;
    begin
      Result := ObjectsFromIndex[FindTag(Tag, True)];
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure THashStringList.SetObjectsFromTag(Tag: Integer; Obj: TObject);
    begin
      ObjectsFromIndex[FindTag(Tag, True)] := Obj;
    end;
     
    { THashStringList - Méthodes Accesseurs - Data }
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetDataFromIndex(Index: Integer): Pointer;
    begin
      Result := ItemsFromIndex[Index].FData;
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure THashStringList.SetDataFromIndex(Index: Integer; Obj: Pointer);
    begin
      ItemsFromIndex[Index].FData := Obj;
    end;
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetDataFromCode(const HashCode: string): Pointer;
    begin
      Result := DataFromIndex[FindCode(HashCode, True)];
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure THashStringList.SetDataFromCode(const HashCode: string; Obj: Pointer);
    begin
      DataFromIndex[FindCode(HashCode, True)] := Obj;
    end;
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetDataFromTag(Tag: Integer): Pointer;
    begin
      Result := DataFromIndex[FindTag(Tag, True)];
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure THashStringList.SetDataFromTag(Tag: Integer; Obj: Pointer);
    begin
      DataFromIndex[FindTag(Tag, True)] := Obj;
    end;
     
    { THashStringList - Méthodes Accesseurs - Count }
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetCount(): Integer;
    begin
      Result := FInternalList.Count;
    end;
     
    { THashStringList - Méthodes Privés }
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.FindCode(const HashCode: string; IndexRequired: Boolean = False): Integer;
    begin
      Result := FInternalList.IndexOf(HashCode);
     
      if IndexRequired and (Result < 0) then
        raise EHashStringListError.CreateFmt(SNotExistsCode, [HashCode]);
    end;
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.FindTag(Tag: Integer; IndexRequired: Boolean = False): Integer;
    begin
      if not FUniqueTags then
        raise EHashStringListError.Create(SNotUniqueTag);
     
      for Result := 0 to Count - 1 do
        if ItemsFromIndex[Result].FTag = Tag then
          Exit;
     
      if IndexRequired then
        raise EHashStringListError.CreateFmt(SNotExistsTag, [Tag])
      else
        Result := -1;
    end;
     
    { THashStringList - Accesseurs Internes }
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetItemsFromIndex(Index: Integer): THashStringItem;
    begin
      Result := THashStringItem(FInternalList.Objects[Index]);
    end;
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetCodeSorted: Boolean;
    begin
      Result := FInternalList.Sorted;
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure THashStringList.SetCodeSorted(const Value: Boolean);
    begin
      if FInternalList.Count > 0 then
        raise EHashStringListError.Create(SCodeSortedChangeListNotEmpty);
     
      FInternalList.Sorted := Value;
      // Impossible d'insérer/modifier deux fois le même élément, les accesseurs normalement sécurisent cela !
      if Value then
        FInternalList.Duplicates := dupError;
    end;
     
    {* ----------------------------------------------------------------------------}
    function THashStringList.GetCaseSensitive: Boolean;
    begin
      Result := FInternalList.CaseSensitive;
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure THashStringList.SetCaseSensitive(const Value: Boolean);
    begin
      if FInternalList.Count > 0 then
        raise EHashStringListError.Create(SCaseSensitiveChangeListNotEmpty);
     
      FInternalList.CaseSensitive := Value;
    end;
     
    { THashStringItem }
     
    { THashStringItem - Constructeurs }
     
    {* ----------------------------------------------------------------------------}
    constructor THashStringItem.Create(const AValue: string; const ACaption: string);
    begin
      inherited Create();
     
      FValue := AValue;
      FCaption := ACaption;
    end;
     
    {* ----------------------------------------------------------------------------}
    constructor THashStringItem.Create(ATag: Integer);
    begin
      inherited Create();
     
      FValue := '';
      FCaption := '';
      FTag := ATag;
    end;
     
    { TTreeHashingObjectList }
     
    { TTreeHashingObjectList - Constructeurs }
     
    {* ----------------------------------------------------------------------------}
    constructor TTreeHashingObjectList.Create();
    begin
      inherited;
    end;
     
    {* ----------------------------------------------------------------------------}
    destructor TTreeHashingObjectList.Destroy;
    begin
      Clear();
     
      inherited;
    end;
     
    { TTreeHashingObjectList - Méthodes Publiques }
     
    {* ----------------------------------------------------------------------------}
    procedure TTreeHashingObjectList.Clear();
    var
      C: Char;
    begin
      if Self <> nil then
      begin
        for C := Low(FIndexArray) to High(FIndexArray) do
          FIndexArray[C].Free();
     
        ZeroMemory(@FIndexArray, SizeOf(FIndexArray)); // Tableau Statique
      end;
    end;
     
    { TTreeHashingObjectList - Méthodes Accesseurs - Objects }
     
    {* ----------------------------------------------------------------------------}
    function TTreeHashingObjectList.GetObjectsFromCode(const HashCode: string): TObject;
    var
      Item: TTreeHashingObjectItem;
      Level: Integer;
    begin
      if FindCode(HashCode, Level, Item) then
        Result := Item.FObject
      else
        Result := nil;
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure TTreeHashingObjectList.SetObjectsFromCode(const HashCode: string; const Obj: TObject);
    var
      Item: TTreeHashingObjectItem;
      Level: Integer;
    begin
      if FindCode(HashCode, Level, Item) then
      begin
        Item.FObject := Obj;
      end
      else
      begin
        AddCode(HashCode, Level, Item, Obj);
      end;
    end;
     
    { TTreeHashingObjectList - Méthodes Privées }
     
    {* ----------------------------------------------------------------------------}
    procedure TTreeHashingObjectList.AddCode(const HashCode: string; Level: Integer; LevelItem: TTreeHashingObjectItem; const Value: TObject);
    var
      LevelChar: Char;
    begin
      if not Assigned(LevelItem) then
      begin
        Level := 1;
        LevelChar := HashCode[Level];
        LevelItem := FIndexArray[LevelChar];
        if not Assigned(LevelItem) then
        begin
          LevelItem := TTreeHashingObjectItem.Create(LevelChar, Value);
          FIndexArray[LevelChar] := LevelItem;
        end;
      end;
     
      if Level < Length(HashCode) then
        LevelItem.AddCode(HashCode, Level + 1, Value);
    end;
     
    {* ----------------------------------------------------------------------------}
    function TTreeHashingObjectList.FindCode(const HashCode: string; out Level: Integer; out LevelItem: TTreeHashingObjectItem): Boolean;
    var
      LenCode: Integer;
      SubLevelItem: TTreeHashingObjectItem;
    begin
      LenCode := Length(HashCode);
      Result := LenCode > 0;
      if Result then
      begin
        Level := 1;
        LevelItem := FIndexArray[HashCode[Level]];
        while (Level < LenCode) and Assigned(LevelItem) do
        begin
          Inc(Level);
          SubLevelItem := LevelItem.FindCode(HashCode[Level]);
          if Assigned(SubLevelItem) then
          begin
            LevelItem := SubLevelItem
          end
          else
          begin
            Result := False;
            Dec(Level);
            Exit;
          end;
        end;
        Result := Assigned(LevelItem);
      end;
    end;
     
    { TTreeHashingObjectItem }
     
    { TTreeHashingObjectItem - Constructeurs }
     
    {* ----------------------------------------------------------------------------}
    constructor TTreeHashingObjectItem.Create(AChar: Char; AObject: TObject);
    begin
      inherited Create();
     
      FChar := AChar;
      FObject := AObject;
    end;
     
    {* ----------------------------------------------------------------------------}
    destructor TTreeHashingObjectItem.Destroy;
    begin
      Clear();
     
      inherited;
    end;
     
    {* ----------------------------------------------------------------------------}
    procedure TTreeHashingObjectItem.Clear;
    var
      I: Integer;
    begin
      for I := Low(FIndexArray) to High(FIndexArray) do
        FIndexArray[I].Free();
     
      SetLength(FIndexArray, 0);
    end;
     
    { TTreeHashingObjectItem - Méthodes Privées }
     
    {* ----------------------------------------------------------------------------}
    procedure TTreeHashingObjectItem.AddCode(const HashCode: string; Level: Integer; const Obj: TObject);
    var
      IdxLen: Integer;
      LenCode: Integer;
    begin
      IdxLen := Length(FIndexArray);
      SetLength(FIndexArray, IdxLen + 1);
     
      LenCode := Length(HashCode);
      if Level < LenCode then
      begin
        FIndexArray[IdxLen] := TTreeHashingObjectItem.Create(HashCode[Level], nil);
        FIndexArray[IdxLen].AddCode(HashCode, Level + 1, Obj);
      end
      else
        FIndexArray[IdxLen] := TTreeHashingObjectItem.Create(HashCode[Level], Obj);
    end;
     
    {* ----------------------------------------------------------------------------}
    function TTreeHashingObjectItem.FindCode(C: Char): TTreeHashingObjectItem;
    var
      I: Integer;
    begin
      for I := Low(FIndexArray) to High(FIndexArray) do
      begin
        Result := FIndexArray[I];
        if Result.FChar = C then
          Exit;
      end;
     
      Result := nil;
    end;
     
    end.

  3. #3
    Membre du Club
    Développeur informatique
    Inscrit en
    Juillet 2005
    Messages
    87
    Détails du profil
    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2005
    Messages : 87
    Points : 60
    Points
    60
    Par défaut
    Merci et effectivement ça semble intéressant. Pour l'instant j'ai opté pour une autre méthode :
    Utiliser un TStringList ! On peut affecter un objet pour chaque entrées donc c'est ce que je cherche. Je ne sais pas trop au niveau des perfs mais ce n'est pas mon problème car ce sont de petites listes.

    Mais quand j'aurais le temps je regarderai tes objets !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. MySQL - Probleme avec 2 index sur une table
    Par xG-Hannibal dans le forum Outils
    Réponses: 7
    Dernier message: 31/03/2006, 14h08
  2. Pb d'index sur une base Access
    Par chakir dans le forum Bases de données
    Réponses: 1
    Dernier message: 09/03/2006, 12h24
  3. Problème avec les indexes sur une base de données.
    Par osoudee dans le forum MS SQL Server
    Réponses: 1
    Dernier message: 09/02/2006, 09h24
  4. Index sur une colonne Date
    Par sjaeger dans le forum Oracle
    Réponses: 11
    Dernier message: 10/11/2005, 14h55
  5. Index sur une col. de type 'booléen": utile?
    Par Atreides dans le forum Oracle
    Réponses: 2
    Dernier message: 28/01/2005, 13h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo