I need a dictionary that can be sorted. I think Spring4D TOrderedDictionary is a class implementing that but I cannot make it work: sorting doesn't work.
I build a small test program showing the issue I have:
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.
The code above display the values in the add order, not the order that - I think - AComparer should create.
I tried to create the OrderedDictionary using an equality comparer or not: with no change.
Any help appreciated.
First you have to know that usually the order of items in a dictionary is undefined as it depends on the underlying data structure - usually it's a hashtable so the order of items while deterministic appears "random" or in no particular order.
Now there are certain dictionaries in spring4d that make a guarantee on item order.
In 1.2.2 (the currently latest released version) there are generally three ways to create dictionaries:
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
In the upcoming 2.0 (currently develop branch) there are two ways:
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>
Second you have to know that the Ordered
method comes from IEnumerable<T>
and does not modify the collection it is being called on but returns a new IEnumerable<T>
which represents the items in the order determined by the comparer you passed to that function.
So you have two options:
IComparer<TKey>
- keep in mind that due to the way a red/black tree works the order might break if you change the value that determines the order afterwards.Ordered
result and iterate that