Menu

[r4453]: / branches / parsnip / Src / Lib / DelphiColl / Collections.BidiMaps.pas  Maximize  Restore  History

Download this file

1051 lines (878 with data), 48.2 kB

   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
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
(*
* Copyright (c) 2009-2011, Ciobanu Alexandru
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*)
unit Collections.BidiMaps;
interface
uses SysUtils,
Generics.Defaults,
Generics.Collections,
Collections.Base,
Collections.MultiMaps;
type
/// <summary>The base abstract class for all <c>bidi-maps</c> in this package.</summary>
TAbstractBidiMap<TKey, TValue> = class abstract(TEnexAssociativeCollection<TKey, TValue>, IBidiMap<TKey, TValue>)
private
FByKeyMap: IDistinctMultiMap<TKey, TValue>;
FByValueMap: IDistinctMultiMap<TValue, TKey>;
{ Got from the underlying collections }
FValueCollection: IEnexCollection<TValue>;
FKeyCollection: IEnexCollection<TKey>;
protected
/// <summary>Specifies the internal map used as back-end to store key relations.</summary>
/// <returns>A map used as back-end.</summary>
property ByKeyMap: IDistinctMultiMap<TKey, TValue> read FByKeyMap;
/// <summary>Specifies the internal map used as back-end to store value relations.</summary>
/// <returns>A map used as back-end.</summary>
property ByValueMap: IDistinctMultiMap<TValue, TKey> read FByValueMap;
/// <summary>Called when the map needs to initialize its internal key map.</summary>
/// <param name="AKeyRules">The rule set describing the keys.</param>
function CreateKeyMap(const AKeyRules: TRules<TKey>;
const AValueRules: TRules<TValue>): IDistinctMultiMap<TKey, TValue>; virtual; abstract;
/// <summary>Called when the map needs to initialize its internal value map.</summary>
/// <param name="AValueRules">The rule set describing the values.</param>
function CreateValueMap(const AValueRules: TRules<TValue>;
const AKeyRules: TRules<TKey>): IDistinctMultiMap<TValue, TKey>; virtual; abstract;
/// <summary>Returns the number of pairs in the bidi-map.</summary>
/// <returns>A positive value specifying the total number of pairs in the bidi-map.</returns>
function GetCount(): NativeInt; override;
/// <summary>Returns the collection of keys associated with a value.</summary>
/// <param name="AValue">The value for which to obtain the associated keys.</param>
/// <returns>An Enex collection that contains the values associated with this key.</returns>
/// <exception cref="Collections.Base|EKeyNotFoundException">The value is not found in the bidi-map.</exception>
function GetKeyList(const AValue: TValue): IEnexCollection<TKey>;
/// <summary>Returns the collection of values associated with a key.</summary>
/// <param name="AKey">The key for which to obtain the associated values.</param>
/// <returns>An Enex collection that contains the values associated with this key.</returns>
/// <exception cref="Collections.Base|EKeyNotFoundException">The key is not found in the bidi-map.</exception>
function GetValueList(const AKey: TKey): IEnexCollection<TValue>;
public
/// <summary>Creates a new instance of this class.</summary>
/// <remarks>The default rule set is requested.</remarks>
constructor Create(); overload;
/// <summary>Creates a new instance of this class.</summary>
/// <param name="ACollection">A collection to copy pairs from.</param>
/// <exception cref="SysUtils|EArgumentNilException"><paramref name="ACollection"/> is <c>nil</c>.</exception>
/// <remarks>The default rule set is requested.</remarks>
constructor Create(const ACollection: IEnumerable<TPair<TKey,TValue>>); overload;
/// <summary>Creates a new instance of this class.</summary>
/// <param name="AArray">An array to copy pairs from.</param>
/// <remarks>The default rule set is requested.</remarks>
constructor Create(const AArray: array of TPair<TKey,TValue>); overload;
/// <summary>Creates a new instance of this class.</summary>
/// <param name="AKeyRules">A rule set describing the keys in the bidi-map.</param>
/// <param name="AValueRules">A rule set describing the values in the bidi-map.</param>
constructor Create(const AKeyRules: TRules<TKey>; const AValueRules: TRules<TValue>); overload;
/// <summary>Creates a new instance of this class.</summary>
/// <param name="AKeyRules">A rule set describing the keys in the bidi-map.</param>
/// <param name="AValueRules">A rule set describing the values in the bidi-map.</param>
/// <param name="ACollection">A collection to copy pairs from.</param>
/// <exception cref="SysUtils|EArgumentNilException"><paramref name="ACollection"/> is <c>nil</c>.</exception>
constructor Create(const AKeyRules: TRules<TKey>; const AValueRules: TRules<TValue>;
const ACollection: IEnumerable<TPair<TKey,TValue>>); overload;
/// <summary>Creates a new instance of this class.</summary>
/// <param name="AKeyRules">A rule set describing the keys in the bidi-map.</param>
/// <param name="AValueRules">A rule set describing the values in the bidi-map.</param>
/// <param name="AArray">An array to copy pairs from.</param>
constructor Create(const AKeyRules: TRules<TKey>; const AValueRules: TRules<TValue>;
const AArray: array of TPair<TKey,TValue>); overload;
/// <summary>Destroys this instance.</summary>
/// <remarks>Do not call this method directly; call <c>Free</c> instead.</remarks>
destructor Destroy(); override;
/// <summary>Clears the contents of the bidi-map.</summary>
procedure Clear();
/// <summary>Adds a key-value pair to the bidi-map.</summary>
/// <param name="APair">The key-value pair to add.</param>
/// <exception cref="Collections.Base|EDuplicateKeyException">The map already contains a pair with the given key.</exception>
procedure Add(const APair: TPair<TKey, TValue>); overload;
/// <summary>Adds a key-value pair to the bidi-map.</summary>
/// <param name="AKey">The key of the pair.</param>
/// <param name="AValue">The value associated with the key.</param>
/// <exception cref="Collections.Base|EDuplicateKeyException">The map already contains a pair with the given key.</exception>
procedure Add(const AKey: TKey; const AValue: TValue); overload;
/// <summary>Removes a key-value pair using a given key.</summary>
/// <param name="AKey">The key (and its associated values) to remove.</param>
/// <remarks>This method removes all the values that are associated with the given key. The rule set's cleanup
/// routines are used to clean up the values that are dropped from the bidi-map.</remarks>
procedure RemoveKey(const AKey: TKey);
/// <summary>Removes a key-value pair using a given key.</summary>
/// <param name="AKey">The key of the pair.</param>
/// <remarks>If the specified key was not found in the bidi-map, nothing happens.</remarks>
procedure Remove(const AKey: TKey); overload;
/// <summary>Removes a key-value pair using a given value.</summary>
/// <param name="AValue">The value (and its associated keys) to remove.</param>
/// <remarks>This method removes all the keys that are associated with the given value. The rule set's cleanup
/// routines are used to clean up the keys that are dropped from the bidi-map.</remarks>
procedure RemoveValue(const AValue: TValue);
/// <summary>Removes a specific key-value combination.</summary>
/// <param name="AKey">The key to remove.</param>
/// <param name="AValue">The value to remove.</param>
/// <remarks>This method only removes a key-value combination if that combination actually exists in the bidi-map.
/// If the key is associated with another value, nothing happens.</remarks>
procedure Remove(const AKey: TKey; const AValue: TValue); overload;
/// <summary>Removes a key-value combination.</summary>
/// <param name="APair">The pair to remove.</param>
/// <remarks>This method only removes a key-value combination if that combination actually exists in the bidi-map.
/// If the key is associated with another value, nothing happens.</remarks>
procedure Remove(const APair: TPair<TKey, TValue>); overload;
/// <summary>Checks whether the map contains a key-value pair identified by the given key.</summary>
/// <param name="AKey">The key to check for.</param>
/// <returns><c>True</c> if the map contains a pair identified by the given key; <c>False</c> otherwise.</returns>
function ContainsKey(const AKey: TKey): Boolean;
/// <summary>Checks whether the map contains a key-value pair that contains a given value.</summary>
/// <param name="AValue">The value to check for.</param>
/// <returns><c>True</c> if the map contains a pair containing the given value; <c>False</c> otherwise.</returns>
function ContainsValue(const AValue: TValue): Boolean;
/// <summary>Checks whether the map contains the given key-value combination.</summary>
/// <param name="AKey">The key associated with the value.</param>
/// <param name="AValue">The value associated with the key.</param>
/// <returns><c>True</c> if the map contains the given association; <c>False</c> otherwise.</returns>
function ContainsPair(const AKey: TKey; const AValue: TValue): Boolean; overload;
/// <summary>Checks whether the map contains a given key-value combination.</summary>
/// <param name="APair">The key-value pair combination.</param>
/// <returns><c>True</c> if the map contains the given association; <c>False</c> otherwise.</returns>
function ContainsPair(const APair: TPair<TKey, TValue>): Boolean; overload;
/// <summary>Returns the collection of values associated with a key.</summary>
/// <param name="AKey">The key for which to obtain the associated values.</param>
/// <returns>An Enex collection that contains the values associated with this key.</returns>
/// <exception cref="Collections.Base|EKeyNotFoundException">The key is not found in the bidi-map.</exception>
property ByKey[const AKey: TKey]: IEnexCollection<TValue> read GetValueList;
/// <summary>Returns the collection of keys associated with a value.</summary>
/// <param name="AValue">The value for which to obtain the associated keys.</param>
/// <returns>An Enex collection that contains the values associated with this key.</returns>
/// <exception cref="Collections.Base|EKeyNotFoundException">The value is not found in the bidi-map.</exception>
property ByValue[const AValue: TValue]: IEnexCollection<TKey> read GetKeyList;
/// <summary>Specifies the collection that contains only the keys.</summary>
/// <returns>An Enex collection that contains all the keys stored in the bidi-map.</returns>
property Keys: IEnexCollection<TKey> read FKeyCollection;
/// <summary>Specifies the collection that contains only the values.</summary>
/// <returns>An Enex collection that contains all the values stored in the bidi-map.</returns>
property Values: IEnexCollection<TValue> read FValueCollection;
/// <summary>Returns the number of pairs in the bidi-map.</summary>
/// <returns>A positive value specifying the total number of pairs in the bidi-map.</returns>
property Count: NativeInt read GetCount;
/// <summary>Returns a new enumerator object used to enumerate this bidi-map.</summary>
/// <remarks>This method is usually called by compiler-generated code. Its purpose is to create an enumerator
/// object that is used to actually traverse the bidi-map.</remarks>
/// <returns>An enumerator object.</returns>
function GetEnumerator(): IEnumerator<TPair<TKey, TValue>>; override;
/// <summary>Copies the values stored in the bidi-map to a given array.</summary>
/// <param name="AArray">An array where to copy the contents of the bidi-map.</param>
/// <param name="AStartIndex">The index into the array at which the copying begins.</param>
/// <remarks>This method assumes that <paramref name="AArray"/> has enough space to hold the contents of the bidi-map.</remarks>
/// <exception cref="SysUtils|EArgumentOutOfRangeException"><paramref name="AStartIndex"/> is out of bounds.</exception>
/// <exception cref="Collections.Base|EArgumentOutOfSpaceException">The array is not long enough.</exception>
procedure CopyTo(var AArray: array of TPair<TKey,TValue>; const AStartIndex: NativeInt); overload; override;
/// <summary>Returns the value associated with the given key.</summary>
/// <param name="AKey">The key for which to return the associated value.</param>
/// <returns>The value associated with the given key.</returns>
/// <exception cref="Collections.Base|EKeyNotFoundException">No such key in the bidi-map.</exception>
function ValueForKey(const AKey: TKey): TValue; override;
/// <summary>Checks whether the bidi-map contains a given key-value pair.</summary>
/// <param name="AKey">The key part of the pair.</param>
/// <param name="AValue">The value part of the pair.</param>
/// <returns><c>True</c> if the given key-value pair exists; <c>False</c> otherwise.</returns>
function KeyHasValue(const AKey: TKey; const AValue: TValue): Boolean; override;
/// <summary>Returns an Enex collection that contains only the keys.</summary>
/// <returns>An Enex collection that contains all the keys stored in the bidi-map.</returns>
function SelectKeys(): IEnexCollection<TKey>; override;
/// <summary>Returns an Enex collection that contains only the values.</summary>
/// <returns>An Enex collection that contains all the values stored in the bidi-map.</returns>
function SelectValues(): IEnexCollection<TValue>; override;
end;
type
/// <summary>The generic <c>bidirectional map</c> collection.</summary>
/// <remarks>This type uses <c>distinct multimaps</c> to store its keys and values.</remarks>
TBidiMap<TKey, TValue> = class(TAbstractBidiMap<TKey, TValue>)
private
FInitialCapacity: NativeInt;
protected
/// <summary>Called when the map needs to initialize the key multimap.</summary>
/// <param name="AKeyRules">The rule set describing the keys.</param>
/// <param name="AValueRules">The rule set describing the values.</param>
/// <remarks>This method creates a distinct multimap used as the underlying back-end for the map.</remarks>
function CreateKeyMap(const AKeyRules: TRules<TKey>;
const AValueRules: TRules<TValue>): IDistinctMultiMap<TKey, TValue>; override;
/// <summary>Called when the map needs to initialize the value multimap.</summary>
/// <param name="AKeyRules">The rule set describing the keys.</param>
/// <param name="AValueRules">The rule set describing the values.</param>
/// <remarks>This method creates a distinct multimap used as the underlying back-end for the map.</remarks>
function CreateValueMap(const AValueRules: TRules<TValue>;
const AKeyRules: TRules<TKey>): IDistinctMultiMap<TValue, TKey>; override;
public
/// <summary>Creates a new instance of this class.</summary>
/// <param name="AInitialCapacity">The map's initial capacity.</param>
/// <remarks>The default rule set is requested.</remarks>
constructor Create(const AInitialCapacity: NativeInt); overload;
/// <summary>Creates a new instance of this class.</summary>
/// <param name="AKeyRules">The rule set describing the keys.</param>
/// <param name="AValueRules">The rule set describing the values.</param>
/// <param name="AInitialCapacity">The map's initial capacity.</param>
constructor Create(const AKeyRules: TRules<TKey>; const AValueRules: TRules<TValue>; const AInitialCapacity: NativeInt); overload;
end;
/// <summary>The generic <c>bidirectional map</c> collection designed to store objects.</summary>
/// <remarks>This type uses <c>distinct multimaps</c> to store its keys and values.</remarks>
TObjectBidiMap<TKey, TValue> = class(TBidiMap<TKey, TValue>)
private
FOwnsKeys, FOwnsValues: Boolean;
protected
/// <summary>Frees the key (object) that was removed from the collection.</summary>
/// <param name="AKey">The key that was removed from the collection.</param>
procedure HandleKeyRemoved(const AKey: TKey); override;
/// <summary>Frees the value (object) that was removed from the collection.</summary>
/// <param name="AKey">The value that was removed from the collection.</param>
procedure HandleValueRemoved(const AValue: TValue); override;
public
/// <summary>Specifies whether this map owns the keys.</summary>
/// <returns><c>True</c> if the map owns the keys; <c>False</c> otherwise.</returns>
/// <remarks>This property specififies the way the map controls the life-time of the stored keys. The value of this property has effect only
/// if the keys are objects, otherwise it is ignored.</remarks>
property OwnsKeys: Boolean read FOwnsKeys write FOwnsKeys;
/// <summary>Specifies whether this map owns the values.</summary>
/// <returns><c>True</c> if the map owns the values; <c>False</c> otherwise.</returns>
/// <remarks>This property specififes the way the map controls the life-time of the stored values. The value of this property has effect only
/// if the values are objects, otherwise it is ignored.</remarks>
property OwnsValues: Boolean read FOwnsValues write FOwnsValues;
end;
type
/// <summary>The generic <c>bidirectional map</c> collection.</summary>
/// <remarks>This type uses <c>sorted distinct multimaps</c> to store its keys and values.</remarks>
TSortedBidiMap<TKey, TValue> = class(TAbstractBidiMap<TKey, TValue>)
private
FAscSort: Boolean;
protected
/// <summary>Called when the map needs to initialize the key multimap.</summary>
/// <param name="AKeyRules">The rule set describing the keys.</param>
/// <param name="AValueRules">The rule set describing the values.</param>
/// <remarks>This method creates a sorted distinct multimap used as the underlying back-end for the map.</remarks>
function CreateKeyMap(const AKeyRules: TRules<TKey>;
const AValueRules: TRules<TValue>): IDistinctMultiMap<TKey, TValue>; override;
/// <summary>Called when the map needs to initialize the value multimap.</summary>
/// <param name="AKeyRules">The rule set describing the keys.</param>
/// <param name="AValueRules">The rule set describing the values.</param>
/// <remarks>This method creates a sorted distinct multimap used as the underlying back-end for the map.</remarks>
function CreateValueMap(const AValueRules: TRules<TValue>;
const AKeyRules: TRules<TKey>): IDistinctMultiMap<TValue, TKey>; override;
public
/// <summary>Creates a new instance of this class.</summary>
/// <param name="AAscending">A value specifying whether the keys are sorted in ascending order. The default is <c>True</c>.</param>
/// <remarks>The default rule set is requested.</remarks>
constructor Create(const AAscending: Boolean = true); overload;
/// <summary>Creates a new instance of this class.</summary>
/// <param name="ACollection">A collection to copy the key-value pairs from.</param>
/// <param name="AAscending">A value specifying whether the keys are sorted in ascending order. The default is <c>True</c>.</param>
/// <remarks>The default rule set is requested.</remarks>
/// <exception cref="SysUtils|EArgumentNilException"><paramref name="ACollection"/> is <c>nil</c>.</exception>
constructor Create(const ACollection: IEnumerable<TPair<TKey,TValue>>; const AAscending: Boolean = true); overload;
/// <summary>Creates a new instance of this class.</summary>
/// <param name="AArray">An array to copy the key-value pairs from.</param>
/// <param name="AAscending">A value specifying whether the keys are sorted in ascending order. The default is <c>True</c>.</param>
/// <remarks>The default rule set is requested.</remarks>
constructor Create(const AArray: array of TPair<TKey,TValue>; const AAscending: Boolean = true); overload;
/// <summary>Creates a new instance of this class.</summary>
/// <param name="AKeyRules">The rule set describing the keys.</param>
/// <param name="AValueRules">The rule set describing the values.</param>
/// <param name="AAscending">A value specifying whether the keys are sorted in ascending order. The default is <c>True</c>.</param>
constructor Create(const AKeyRules: TRules<TKey>; const AValueRules: TRules<TValue>;
const AAscending: Boolean = true); overload;
/// <summary>Creates a new instance of this class.</summary>
/// <param name="AKeyRules">The rule set describing the keys.</param>
/// <param name="AValueRules">The rule set describing the values.</param>
/// <param name="ACollection">A collection to copy the key-value pairs from.</param>
/// <param name="AAscending">A value specifying whether the keys are sorted in ascending order. The default is <c>True</c>.</param>
/// <exception cref="SysUtils|EArgumentNilException"><paramref name="ACollection"/> is <c>nil</c>.</exception>
constructor Create(const AKeyRules: TRules<TKey>; const AValueRules: TRules<TValue>;
const ACollection: IEnumerable<TPair<TKey,TValue>>; const AAscending: Boolean = true); overload;
/// <summary>Creates a new instance of this class.</summary>
/// <param name="AKeyRules">The rule set describing the keys.</param>
/// <param name="AValueRules">The rule set describing the values.</param>
/// <param name="AArray">An array to copy the key-value pairs from.</param>
/// <param name="AAscending">A value specifying whether the keys are sorted in ascending order. The default is <c>True</c>.</param>
constructor Create(const AKeyRules: TRules<TKey>; const AValueRules: TRules<TValue>;
const AArray: array of TPair<TKey,TValue>; const AAscending: Boolean = true); overload;
/// <summary>Returns the biggest key.</summary>
/// <returns>The biggest key stored in the map.</returns>
/// <exception cref="Collections.Base|ECollectionEmptyException">The map is empty.</exception>
function MaxKey(): TKey; override;
/// <summary>Returns the smallest key.</summary>
/// <returns>The smallest key stored in the map.</returns>
/// <exception cref="Collections.Base|ECollectionEmptyException">The map is empty.</exception>
function MinKey(): TKey; override;
end;
/// <summary>The generic <c>bidirectional map</c> collection designed to store objects.</summary>
/// <remarks>This type uses <c>sorted distinct multimaps</c> to store its keys and values.</remarks>
TObjectSortedBidiMap<TKey, TValue> = class(TSortedBidiMap<TKey, TValue>)
private
FOwnsKeys, FOwnsValues: Boolean;
protected
/// <summary>Frees the key (object) that was removed from the collection.</summary>
/// <param name="AKey">The key that was removed from the collection.</param>
procedure HandleKeyRemoved(const AKey: TKey); override;
/// <summary>Frees the value (object) that was removed from the collection.</summary>
/// <param name="AKey">The value that was removed from the collection.</param>
procedure HandleValueRemoved(const AValue: TValue); override;
public
/// <summary>Specifies whether this map owns the keys.</summary>
/// <returns><c>True</c> if the map owns the keys; <c>False</c> otherwise.</returns>
/// <remarks>This propertyspecififes the way the map controls the life-time of the stored keys. The value of this property has effect only
/// if the keys are objects, otherwise it is ignored.</remarks>
property OwnsKeys: Boolean read FOwnsKeys write FOwnsKeys;
/// <summary>Specifies whether this map owns the values.</summary>
/// <returns><c>True</c> if the map owns the values; <c>False</c> otherwise.</returns>
/// <remarks>This property specifies the way the map controls the life-time of the stored values. The value of this property has effect only
/// if the values are objects, otherwise it is ignored.</remarks>
property OwnsValues: Boolean read FOwnsValues write FOwnsValues;
end;
type
/// <summary>The generic <c>bidirectional map</c> collection.</summary>
/// <remarks>This type uses <c>double sorted distinct multimaps</c> to store its keys and values.</remarks>
TDoubleSortedBidiMap<TKey, TValue> = class(TAbstractBidiMap<TKey, TValue>)
private
FAscKeys, FAscValues: Boolean;
protected
/// <summary>Called when the map needs to initialize the key multimap.</summary>
/// <param name="AKeyRules">The rule set describing the keys.</param>
/// <param name="AValueRules">The rule set describing the values.</param>
/// <remarks>This method creates a double sorted distinct multimap used as the underlying back-end for the map.</remarks>
function CreateKeyMap(const AKeyRules: TRules<TKey>;
const AValueRules: TRules<TValue>): IDistinctMultiMap<TKey, TValue>; override;
/// <summary>Called when the map needs to initialize the value multimap.</summary>
/// <param name="AKeyRules">The rule set describing the keys.</param>
/// <param name="AValueRules">The rule set describing the values.</param>
/// <remarks>This method creates a double sorted distinct multimap used as the underlying back-end for the map.</remarks>
function CreateValueMap(const AValueRules: TRules<TValue>;
const AKeyRules: TRules<TKey>): IDistinctMultiMap<TValue, TKey>; override;
public
/// <summary>Creates a new instance of this class.</summary>
/// <param name="AAscendingKeys">A value specifying whether the keys are sorted in ascending order. The default is <c>True</c>.</param>
/// <param name="AAscendingValues">A value specifying whether the values are sorted in ascending order. The default is <c>True</c>.</param>
/// <remarks>The default rule set is requested.</remarks>
constructor Create(const AAscendingKeys: Boolean = true; const AAscendingValues: Boolean = true); overload;
/// <summary>Creates a new instance of this class.</summary>
/// <param name="ACollection">A collection to copy the key-value pairs from.</param>
/// <param name="AAscendingKeys">A value specifying whether the keys are sorted in ascending order. The default is <c>True</c>.</param>
/// <param name="AAscendingValues">A value specifying whether the values are sorted in ascending order. The default is <c>True</c>.</param>
/// <remarks>The default rule set is requested.</remarks>
/// <exception cref="SysUtils|EArgumentNilException"><paramref name="ACollection"/> is <c>nil</c>.</exception>
constructor Create(const ACollection: IEnumerable<TPair<TKey,TValue>>;
const AAscendingKeys: Boolean = true; const AAscendingValues: Boolean = true); overload;
/// <summary>Creates a new instance of this class.</summary>
/// <param name="AArray">An array to copy the key-value pairs from.</param>
/// <param name="AAscendingKeys">A value specifying whether the keys are sorted in ascending order. The default is <c>True</c>.</param>
/// <param name="AAscendingValues">A value specifying whether the values are sorted in ascending order. The default is <c>True</c>.</param>
/// <remarks>The default rule set is requested.</remarks>
constructor Create(const AArray: array of TPair<TKey,TValue>;
const AAscendingKeys: Boolean = true; const AAscendingValues: Boolean = true); overload;
/// <summary>Creates a new instance of this class.</summary>
/// <param name="AKeyRules">The rule set describing the keys.</param>
/// <param name="AValueRules">The rule set describing the values.</param>
/// <param name="AAscendingKeys">A value specifying whether the keys are sorted in ascending order. The default is <c>True</c>.</param>
/// <param name="AAscendingValues">A value specifying whether the values are sorted in ascending order. The default is <c>True</c>.</param>
constructor Create(const AKeyRules: TRules<TKey>; const AValueRules: TRules<TValue>;
const AAscendingKeys: Boolean = true; const AAscendingValues: Boolean = true); overload;
/// <summary>Creates a new instance of this class.</summary>
/// <param name="AKeyRules">The rule set describing the keys.</param>
/// <param name="AValueRules">The rule set describing the values.</param>
/// <param name="ACollection">A collection to copy the key-value pairs from.</param>
/// <param name="AAscendingKeys">A value specifying whether the keys are sorted in ascending order. The default is <c>True</c>.</param>
/// <param name="AAscendingValues">A value specifying whether the values are sorted in ascending order. The default is <c>True</c>.</param>
/// <exception cref="SysUtils|EArgumentNilException"><paramref name="ACollection"/> is <c>nil</c>.</exception>
constructor Create(const AKeyRules: TRules<TKey>; const AValueRules: TRules<TValue>;
const ACollection: IEnumerable<TPair<TKey,TValue>>; const AAscendingKeys: Boolean = true;
const AAscendingValues: Boolean = true); overload;
/// <summary>Creates a new instance of this class.</summary>
/// <param name="AKeyRules">The rule set describing the keys.</param>
/// <param name="AValueRules">The rule set describing the values.</param>
/// <param name="AArray">An array to copy the key-value pairs from.</param>
/// <param name="AAscendingKeys">A value specifying whether the keys are sorted in ascending order. The default is <c>True</c>.</param>
/// <param name="AAscendingValues">A value specifying whether the values are sorted in ascending order. The default is <c>True</c>.</param>
constructor Create(const AKeyRules: TRules<TKey>; const AValueRules: TRules<TValue>;
const AArray: array of TPair<TKey,TValue>; const AAscendingKeys: Boolean = true;
const AAscendingValues: Boolean = true); overload;
/// <summary>Returns the biggest key.</summary>
/// <returns>The biggest key stored in the map.</returns>
/// <exception cref="Collections.Base|ECollectionEmptyException">The map is empty.</exception>
function MaxKey(): TKey; override;
/// <summary>Returns the smallest key.</summary>
/// <returns>The smallest key stored in the map.</returns>
/// <exception cref="Collections.Base|ECollectionEmptyException">The map is empty.</exception>
function MinKey(): TKey; override;
end;
/// <summary>The generic <c>bidirectional map</c> collection designed to store objects.</summary>
/// <remarks>This type uses <c>double sorted distinct multimaps</c> to store its keys and values.</remarks>
TObjectDoubleSortedBidiMap<TKey, TValue> = class(TDoubleSortedBidiMap<TKey, TValue>)
private
FOwnsKeys, FOwnsValues: Boolean;
protected
/// <summary>Frees the key (object) that was removed from the collection.</summary>
/// <param name="AKey">The key that was removed from the collection.</param>
procedure HandleKeyRemoved(const AKey: TKey); override;
/// <summary>Frees the value (object) that was removed from the collection.</summary>
/// <param name="AKey">The value that was removed from the collection.</param>
procedure HandleValueRemoved(const AValue: TValue); override;
public
/// <summary>Specifies whether this map owns the keys.</summary>
/// <returns><c>True</c> if the map owns the keys; <c>False</c> otherwise.</returns>
/// <remarks>This property specififes the way the map controls the life-time of the stored keys. The value of this property has effect only
/// if the keys are objects, otherwise it is ignored.</remarks>
property OwnsKeys: Boolean read FOwnsKeys write FOwnsKeys;
/// <summary>Specifies whether this map owns the values.</summary>
/// <returns><c>True</c> if the map owns the values; <c>False</c> otherwise.</returns>
/// <remarks>This property specifies the way the map controls the life-time of the stored values. The value of this property has effect only
/// if the values are objects, otherwise it is ignored.</remarks>
property OwnsValues: Boolean read FOwnsValues write FOwnsValues;
end;
implementation
{ TAbstractBidiMap<TKey, TValue> }
constructor TAbstractBidiMap<TKey, TValue>.Create(const AArray: array of TPair<TKey, TValue>);
begin
Create(TRules<TKey>.Default, TRules<TValue>.Default, AArray);
end;
constructor TAbstractBidiMap<TKey, TValue>.Create;
begin
Create(TRules<TKey>.Default, TRules<TValue>.Default);
end;
constructor TAbstractBidiMap<TKey, TValue>.Create(const ACollection: IEnumerable<TPair<TKey, TValue>>);
begin
Create(TRules<TKey>.Default, TRules<TValue>.Default, ACollection);
end;
procedure TAbstractBidiMap<TKey, TValue>.Add(const AKey: TKey; const AValue: TValue);
begin
{ Add the K/V pair to the maps }
FByKeyMap.Add(AKey, AValue);
FByValueMap.Add(AValue, AKey);
end;
procedure TAbstractBidiMap<TKey, TValue>.Add(const APair: TPair<TKey, TValue>);
begin
Add(APair.Key, APair.Value);
end;
procedure TAbstractBidiMap<TKey, TValue>.Clear;
begin
if Assigned(FByKeyMap) then
FByKeyMap.Clear;
if Assigned(FByValueMap) then
FByValueMap.Clear;
end;
function TAbstractBidiMap<TKey, TValue>.ContainsKey(const AKey: TKey): Boolean;
begin
Result := FByKeyMap.ContainsKey(AKey);
end;
function TAbstractBidiMap<TKey, TValue>.ContainsPair(const APair: TPair<TKey, TValue>): Boolean;
begin
{ The by-key relation since it is always correct }
Result := FByKeyMap.ContainsValue(APair.Key, APair.Value);
end;
function TAbstractBidiMap<TKey, TValue>.ContainsPair(const AKey: TKey; const AValue: TValue): Boolean;
begin
{ The by-key relation since it is always correct }
Result := FByKeyMap.ContainsValue(AKey, AValue);
end;
function TAbstractBidiMap<TKey, TValue>.ContainsValue(const AValue: TValue): Boolean;
begin
Result := FByValueMap.ContainsKey(AValue);
end;
procedure TAbstractBidiMap<TKey, TValue>.CopyTo(var AArray: array of TPair<TKey, TValue>; const AStartIndex: NativeInt);
begin
{ Check for indexes }
if (AStartIndex >= Length(AArray)) or (AStartIndex < 0) then
ExceptionHelper.Throw_ArgumentOutOfRangeError('AStartIndex');
if (Length(AArray) - AStartIndex) < Count then
ExceptionHelper.Throw_ArgumentOutOfSpaceError('AArray');
{ Call the underlying collection }
FByKeyMap.CopyTo(AArray, AStartIndex);
end;
destructor TAbstractBidiMap<TKey, TValue>.Destroy;
begin
{ Clear out the instance }
Clear();
inherited;
end;
function TAbstractBidiMap<TKey, TValue>.GetCount: NativeInt;
begin
{ The count follows the map properties }
Result := FByKeyMap.Count;
end;
function TAbstractBidiMap<TKey, TValue>.GetEnumerator: IEnumerator<TPair<TKey, TValue>>;
begin
{ Pass the enumerator from the key map }
Result := FByKeyMap.GetEnumerator();
end;
function TAbstractBidiMap<TKey, TValue>.GetKeyList(const AValue: TValue): IEnexCollection<TKey>;
begin
Result := FByValueMap[AValue];
end;
function TAbstractBidiMap<TKey, TValue>.GetValueList(const AKey: TKey): IEnexCollection<TValue>;
begin
Result := FByKeyMap[AKey];
end;
function TAbstractBidiMap<TKey, TValue>.KeyHasValue(const AKey: TKey; const AValue: TValue): Boolean;
begin
Result := ContainsPair(AKey, AValue);
end;
procedure TAbstractBidiMap<TKey, TValue>.Remove(const AKey: TKey; const AValue: TValue);
var
LValues: IEnexCollection<TValue>;
LValue: TValue;
begin
{ Check whether there is such a key }
if not FByKeyMap.ContainsValue(AKey, AValue) then
Exit;
{ Remove the stuff }
FByKeyMap.Remove(AKey, AValue);
FByValueMap.Remove(AValue, AKey);
end;
procedure TAbstractBidiMap<TKey, TValue>.Remove(const APair: TPair<TKey, TValue>);
begin
Remove(APair.Key, APair.Value);
end;
procedure TAbstractBidiMap<TKey, TValue>.Remove(const AKey: TKey);
begin
RemoveKey(AKey);
end;
procedure TAbstractBidiMap<TKey, TValue>.RemoveKey(const AKey: TKey);
var
LValues: IEnexCollection<TValue>;
LValue: TValue;
begin
{ Check whether there is such a key }
if not FByKeyMap.TryGetValues(AKey, LValues) then
Exit;
{ Exclude the key for all values too }
for LValue in LValues do
FByValueMap.Remove(LValue, AKey);
{ And finally remove the key }
FByKeyMap.Remove(AKey);
end;
procedure TAbstractBidiMap<TKey, TValue>.RemoveValue(const AValue: TValue);
var
LKeys: IEnexCollection<TKey>;
LValue: TKey;
begin
{ Check whether there is such a key }
if not FByValueMap.TryGetValues(AValue, LKeys) then
Exit;
{ Exclude the key for all values too}
for LValue in LKeys do
FByKeyMap.Remove(LValue, AValue);
{ And finally remove the key }
FByValueMap.Remove(AValue);
// { Clean up the value if necessary }
// if ValueRules.Management = tmManual then
// ValueRules.Cleanup(LValue);
end;
function TAbstractBidiMap<TKey, TValue>.SelectKeys: IEnexCollection<TKey>;
begin
{ Pass the values on }
Result := Keys;
end;
function TAbstractBidiMap<TKey, TValue>.SelectValues: IEnexCollection<TValue>;
begin
{ Pass the value on }
Result := Values;
end;
function TAbstractBidiMap<TKey, TValue>.ValueForKey(const AKey: TKey): TValue;
begin
Result := FByKeyMap[AKey].First;
end;
constructor TAbstractBidiMap<TKey, TValue>.Create(const AKeyRules: TRules<TKey>;
const AValueRules: TRules<TValue>;
const AArray: array of TPair<TKey, TValue>);
var
I: NativeInt;
begin
{ Call upper constructor }
Create(AKeyRules, AValueRules);
{ Copy all items in }
for I := 0 to Length(AArray) - 1 do
begin
Add(AArray[I]);
end;
end;
constructor TAbstractBidiMap<TKey, TValue>.Create(const AKeyRules: TRules<TKey>; const AValueRules: TRules<TValue>);
begin
{ Install the types }
inherited Create(AKeyRules, AValueRules);
{ Create the maps }
FByKeyMap := CreateKeyMap(AKeyRules, ValueRules);
FByValueMap := CreateValueMap(AValueRules, KeyRules);
{ The collections }
FValueCollection := FByValueMap.Keys;
FKeyCollection := FByKeyMap.Keys;
end;
constructor TAbstractBidiMap<TKey, TValue>.Create(const AKeyRules: TRules<TKey>;
const AValueRules: TRules<TValue>;
const ACollection: IEnumerable<TPair<TKey, TValue>>);
var
LValue: TPair<TKey, TValue>;
begin
{ Call upper constructor }
Create(AKeyRules, AValueRules);
if not Assigned(ACollection) then
ExceptionHelper.Throw_ArgumentNilError('ACollection');
{ Pump in all items }
for LValue in ACollection do
begin
{$IF CompilerVersion < 22}
Add(LValue);
{$ELSE}
Add(LValue.Key, LValue.Value);
{$IFEND}
end;
end;
{ TBidiMap<TKey, TValue> }
constructor TBidiMap<TKey, TValue>.Create(const AInitialCapacity: NativeInt);
begin
FInitialCapacity := AInitialCapacity;
inherited Create();
end;
constructor TBidiMap<TKey, TValue>.Create(const AKeyRules: TRules<TKey>; const AValueRules: TRules<TValue>;
const AInitialCapacity: NativeInt);
begin
FInitialCapacity := AInitialCapacity;
inherited Create(AKeyRules, AValueRules);
end;
function TBidiMap<TKey, TValue>.CreateKeyMap(const AKeyRules: TRules<TKey>;
const AValueRules: TRules<TValue>): IDistinctMultiMap<TKey, TValue>;
var
LNewCapacity: NativeInt;
LMap: TDistinctMultiMap<TKey, TValue>;
begin
{ Create a simple dictionary }
if FInitialCapacity <= 0 then
LNewCapacity := CDefaultSize
else
LNewCapacity := FInitialCapacity;
{ Use a simple non-sorted map }
LMap := TDistinctMultiMap<TKey, TValue>.Create(AKeyRules, AValueRules, LNewCapacity);
LMap.KeyRemoveNotification := NotifyKeyRemoved;
Result := LMap;
end;
function TBidiMap<TKey, TValue>.CreateValueMap(const AValueRules: TRules<TValue>;
const AKeyRules: TRules<TKey>): IDistinctMultiMap<TValue, TKey>;
var
LNewCapacity: NativeInt;
LMap: TDistinctMultiMap<TValue, TKey>;
begin
{ Create a simple dictionary }
if FInitialCapacity <= 0 then
LNewCapacity := CDefaultSize
else
LNewCapacity := FInitialCapacity;
{ Use a simple non-sorted map }
LMap := TDistinctMultiMap<TValue, TKey>.Create(AValueRules, AKeyRules, LNewCapacity);
LMap.KeyRemoveNotification := NotifyValueRemoved;
Result := LMap;
end;
{ TObjectBidiMap<TKey, TValue> }
procedure TObjectBidiMap<TKey, TValue>.HandleKeyRemoved(const AKey: TKey);
begin
if FOwnsKeys then
TObject(AKey).Free;
end;
procedure TObjectBidiMap<TKey, TValue>.HandleValueRemoved(const AValue: TValue);
begin
if FOwnsValues then
TObject(AValue).Free;
end;
{ TSortedBidiMap<TKey, TValue> }
constructor TSortedBidiMap<TKey, TValue>.Create(
const AArray: array of TPair<TKey, TValue>; const AAscending: Boolean);
begin
{ Do the dew and continue }
FAscSort := AAscending;
inherited Create(AArray);
end;
constructor TSortedBidiMap<TKey, TValue>.Create(const AAscending: Boolean);
begin
{ Do the dew and continue }
FAscSort := AAscending;
inherited Create();
end;
constructor TSortedBidiMap<TKey, TValue>.Create(
const ACollection: IEnumerable<TPair<TKey, TValue>>;
const AAscending: Boolean);
begin
{ Do the dew and continue }
FAscSort := AAscending;
inherited Create(ACollection);
end;
constructor TSortedBidiMap<TKey, TValue>.Create(const AKeyRules: TRules<TKey>;
const AValueRules: TRules<TValue>;
const AArray: array of TPair<TKey, TValue>; const AAscending: Boolean);
begin
{ Do the dew and continue }
FAscSort := AAscending;
inherited Create(AKeyRules, AValueRules, AArray);
end;
constructor TSortedBidiMap<TKey, TValue>.Create(const AKeyRules: TRules<TKey>;
const AValueRules: TRules<TValue>; const AAscending: Boolean);
begin
{ Do the dew and continue }
FAscSort := AAscending;
inherited Create(AKeyRules, AValueRules);
end;
constructor TSortedBidiMap<TKey, TValue>.Create(const AKeyRules: TRules<TKey>;
const AValueRules: TRules<TValue>;
const ACollection: IEnumerable<TPair<TKey, TValue>>;
const AAscending: Boolean);
begin
{ Do the dew and continue }
FAscSort := AAscending;
inherited Create(AKeyRules, AValueRules, ACollection);
end;
function TSortedBidiMap<TKey, TValue>.CreateKeyMap(const AKeyRules: TRules<TKey>;
const AValueRules: TRules<TValue>): IDistinctMultiMap<TKey, TValue>;
var
LMap: TSortedDistinctMultiMap<TKey, TValue>;
begin
{ Use a simple sorted map }
LMap := TSortedDistinctMultiMap<TKey, TValue>.Create(AKeyRules, AValueRules, FAscSort);
LMap.KeyRemoveNotification := NotifyKeyRemoved;
Result := LMap;
end;
function TSortedBidiMap<TKey, TValue>.CreateValueMap(const AValueRules: TRules<TValue>;
const AKeyRules: TRules<TKey>): IDistinctMultiMap<TValue, TKey>;
var
LMap: TSortedDistinctMultiMap<TValue, TKey>;
begin
{ Use a simple sorted map }
LMap := TSortedDistinctMultiMap<TValue, TKey>.Create(AValueRules, AKeyRules, FAscSort);
LMap.KeyRemoveNotification := NotifyValueRemoved;
Result := LMap;
end;
function TSortedBidiMap<TKey, TValue>.MaxKey: TKey;
begin
Result := ByKeyMap.MaxKey;
end;
function TSortedBidiMap<TKey, TValue>.MinKey: TKey;
begin
Result := ByKeyMap.MinKey;
end;
{ TObjectSortedBidiMap<TKey, TValue> }
procedure TObjectSortedBidiMap<TKey, TValue>.HandleKeyRemoved(const AKey: TKey);
begin
if FOwnsKeys then
TObject(AKey).Free;
end;
procedure TObjectSortedBidiMap<TKey, TValue>.HandleValueRemoved(const AValue: TValue);
begin
if FOwnsValues then
TObject(AValue).Free;
end;
{ TDoubleSortedBidiMap<TKey, TValue> }
constructor TDoubleSortedBidiMap<TKey, TValue>.Create(
const AArray: array of TPair<TKey, TValue>; const AAscendingKeys,
AAscendingValues: Boolean);
begin
{ Do the dew and continue! }
FAscKeys := AAscendingKeys;
FAscValues := AAscendingValues;
inherited Create(AArray);
end;
constructor TDoubleSortedBidiMap<TKey, TValue>.Create(const AAscendingKeys, AAscendingValues: Boolean);
begin
{ Do the dew and continue! }
FAscKeys := AAscendingKeys;
FAscValues := AAscendingValues;
inherited Create();
end;
constructor TDoubleSortedBidiMap<TKey, TValue>.Create(
const ACollection: IEnumerable<TPair<TKey, TValue>>;
const AAscendingKeys, AAscendingValues: Boolean);
begin
{ Do the dew and continue! }
FAscKeys := AAscendingKeys;
FAscValues := AAscendingValues;
inherited Create(ACollection);
end;
constructor TDoubleSortedBidiMap<TKey, TValue>.Create(
const AKeyRules: TRules<TKey>; const AValueRules: TRules<TValue>;
const AArray: array of TPair<TKey, TValue>; const AAscendingKeys,
AAscendingValues: Boolean);
begin
{ Do the dew and continue! }
FAscKeys := AAscendingKeys;
FAscValues := AAscendingValues;
inherited Create(AKeyRules, AValueRules, AArray);
end;
constructor TDoubleSortedBidiMap<TKey, TValue>.Create(
const AKeyRules: TRules<TKey>; const AValueRules: TRules<TValue>;
const AAscendingKeys, AAscendingValues: Boolean);
begin
{ Do the dew and continue! }
FAscKeys := AAscendingKeys;
FAscValues := AAscendingValues;
inherited Create(AKeyRules, AValueRules);
end;
constructor TDoubleSortedBidiMap<TKey, TValue>.Create(
const AKeyRules: TRules<TKey>; const AValueRules: TRules<TValue>;
const ACollection: IEnumerable<TPair<TKey, TValue>>;
const AAscendingKeys, AAscendingValues: Boolean);
begin
{ Do the dew and continue! }
FAscKeys := AAscendingKeys;
FAscValues := AAscendingValues;
inherited Create(AKeyRules, AValueRules, ACollection);
end;
function TDoubleSortedBidiMap<TKey, TValue>.CreateKeyMap(const AKeyRules: TRules<TKey>;
const AValueRules: TRules<TValue>): IDistinctMultiMap<TKey, TValue>;
var
LMap: TDoubleSortedDistinctMultiMap<TKey, TValue>;
begin
{ Use a double sorted map }
LMap := TDoubleSortedDistinctMultiMap<TKey, TValue>.Create(AKeyRules, AValueRules, FAscKeys, FAscValues);
LMap.KeyRemoveNotification := NotifyKeyRemoved;
Result := LMap;
end;
function TDoubleSortedBidiMap<TKey, TValue>.CreateValueMap(const AValueRules: TRules<TValue>;
const AKeyRules: TRules<TKey>): IDistinctMultiMap<TValue, TKey>;
var
LMap: TDoubleSortedDistinctMultiMap<TValue, TKey>;
begin
{ Use a double sorted map }
LMap := TDoubleSortedDistinctMultiMap<TValue, TKey>.Create(AValueRules, AKeyRules, FAscKeys, FAscValues);
LMap.KeyRemoveNotification := NotifyValueRemoved;
Result := LMap;
end;
function TDoubleSortedBidiMap<TKey, TValue>.MaxKey: TKey;
begin
Result := ByKeyMap.MaxKey;
end;
function TDoubleSortedBidiMap<TKey, TValue>.MinKey: TKey;
begin
Result := ByKeyMap.MinKey;
end;
{ TObjectDoubleSortedBidiMap<TKey, TValue> }
procedure TObjectDoubleSortedBidiMap<TKey, TValue>.HandleKeyRemoved(const AKey: TKey);
begin
if FOwnsKeys then
TObject(AKey).Free;
end;
procedure TObjectDoubleSortedBidiMap<TKey, TValue>.HandleValueRemoved(const AValue: TValue);
begin
if FOwnsValues then
TObject(AValue).Free;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.