4

我需要一个可以排序的字典。我认为 Spring4D TOrderedDictionary 是一个实现它的类,但我不能让它工作:排序不起作用。

我构建了一个小测试程序来显示我遇到的问题:

program Spring4DOrderedDictionaryTest;
{$APPTYPE CONSOLE}
{$R *.res}
uses
    System.SysUtils,
    System.Generics.Collections,
    System.Generics.Defaults,
    Spring.Collections;

type
    TKey   = TPair<Double, Integer>;
    TValue = Integer;

    TEqComparer = class(TEqualityComparer<TKey>, IEqualityComparer<TKey>)
        function Equals(const Left, Right: TKey): Boolean; reintroduce;
        function GetHashCode(const Value: TKey): Integer; reintroduce;
    end;

    TAComparer = class(TComparer<TPair<TPair<Double, Integer>, Integer>>, IComparer<TPair<TKey, TValue>>)
        function Compare(const Left, Right: TPair<TKey, Integer>): Integer; override;
    end;

function TEqComparer.Equals(const Left, Right: TKey): Boolean;
begin
    Result := Abs(Left.Key - Right.Key) < 1E-9;
    if Result then
        Result := Left.Value = Right.Value;
end;


function TEqComparer.GetHashCode(const Value: TKey): Integer;
begin
    Result := Round(Value.Key * 1E6) + Value.Value;
end;

function TAComparer.Compare(const Left, Right: TPair<TKey, Integer>): Integer;
begin
    if Abs(Left.Key.Key - Right.Key.Key) < 1E-9 then begin
        if Left.Key.Value < Right.Key.Value then
            Result := -1
        else if Left.Key.Value > Right.Key.Value then
            Result := 1
        else
            Result := 0;
    end
    else begin
        if Left.Key.Key < Right.Key.Key then
            Result := -1
        else if Left.Key.Key > Right.Key.Key then
            Result := 1
        else
            Result := 0;
    end;
end;

const
    SEG_START = 0;
    SEG_END   = 1;
var
    Events      : IOrderedDictionary<TKey, TValue>;
    Event       : TPair<TKey, TValue>;
    EqComparer  : IEqualityComparer<TKey>;
    AComparer   : IComparer<TPair<TKey, TValue>>;
begin
    EqComparer := TEqComparer.Create;
    AComparer  := TAComparer.Create;
    Events     := TCollections.CreateOrderedDictionary<TKey, TValue>;//(EqComparer);

    // Put some data into the dictionary
    Events.Add(TKey.Create(-7.41, SEG_START), 0);
    Events.Add(TKey.Create(-1.3,  SEG_END  ), 0);
    Events.Add(TKey.Create(-4.0,  SEG_START), 1);
    Events.Add(TKey.Create(-4.21, SEG_END  ), 1);
    Events.Add(TKey.Create(-4.92, SEG_START), 2);
    Events.Add(TKey.Create(-4.26, SEG_END  ), 2);
    Events.Add(TKey.Create(-4.55, SEG_START), 3);
    Events.Add(TKey.Create(-2.54, SEG_END  ), 3);
    Events.Add(TKey.Create(-3.70, SEG_START), 4);
    Events.Add(TKey.Create(-3.70, SEG_END  ), 4);

    // Sort the dictionary
    Events.Ordered(AComparer);

    // Show the values in dictionary (Should be sorted -7.41 first and -1.3 last)    
    for Event in Events do
        WriteLn(Format('  %5.2f %d %d', [Event.Key.Key, Event.Key.Value, Event.Value]));

    ReadLn;
end.

上面的代码以添加顺序显示值,而不是 - 我认为 - AComparer 应该创建的顺序。

我尝试使用相等比较器创建 OrderedDictionary:没有变化。

任何帮助表示赞赏。

4

1 回答 1

4

首先,您必须知道,字典中项目的顺序通常是未定义的,因为它取决于底层数据结构 - 通常它是一个哈希表,因此项目的顺序在确定性时显得“随机”或没有特定顺序。

现在spring4d中有一些字典可以保证物品的顺序。

在 1.2.2(当前最新发布的版本)中,创建字典的方式一般有以下三种:

TCollections.CreateDictionary // internally is just a wrapper around the TDictionary from System.Generics.Collections
TCollections.CreateSortedDictionary // internally uses a red/black tree which makes items ordered - when not explicitly passed it uses the default comparer for the key type
TCollections.CreateOrderedDictionary // internally uses an additional list to preserve items in order they were added to the dictionary

在即将到来的 2.0(目前为开发分支)中,有两种方式:

TCollections.CreateDictionary // internally uses a new implementation based on a hashtable which also preserves the order of addition
TCollections.CreateSortedDictionary // same as in 1.2.2 this uses a red/black tree to store items based on the IComparer<TKey>

其次,您必须知道该Ordered方法来自IEnumerable<T>并且不会修改正在调用它的集合,而是返回一个新IEnumerable<T>的,它表示按照您传递给该函数的比较器确定的顺序的项目。

所以你有两个选择:

  • 使用 TCollections.CreateSortedDictionary 传递IComparer<TKey>- 请记住,由于红/黑树的工作方式,如果您更改之后确定顺序的值,则顺序可能会中断。
  • 利用Ordered结果并迭代
于 2021-02-10T13:40:04.700 回答