Jump to content
Sign in to follow this  
Tommi Prami

Anyone have good code to get difference of two TList<T>

Recommended Posts

Have not done much Generics stuff, but the trivial stuff.

 

I think this must be class of some sort, right? To get the/return T

Something like :  (This could be actual helper also, maybe??)

 

TGenericListHelper<T> = class(TObject)

public

  function Diffference(const AList1, AList2: TLIst<T>): TList<T>;

end;

 

Something like that. And then add rest.  I'll try to implement that on my own but if someone has good code/ideas for that, let me know.

Share this post


Link to post

Got it. Not super good but seems to work,

 

  TGenericListHelper = class(TObject)
  public
    class function Difference<T>(const AList1, AList2: TList<T>): TList<T>;
  end;
 

class function TGenericListHelper.Difference<T>(const AList1, AList2: TList<T>): TList<T>;
var
  I: Integer;
begin
  Result := TList<T>.Create;

  for I := 0 to AList1.Count - 1 do
    if AList2.IndexOf(AList1) = -1 then
      Result.Add(AList1);

  for I := 0 to AList2.Count - 1 do
    if AList1.IndexOf(AList2) = -1 then
      Result.Add(AList2);
end;
 

Surely with sorting etc can make this faster and most likely there is better algorithm for this. If you know how to make this better, I am all ears.

 

-Tee-

 

Share this post


Link to post

The solution heavily depends on what you call different on these lists. Your solution simply makes the lists containing the same elements, which is quite off checking for equality. Others might see two lists equal if the elements and their order match. Also equality of two elements can mean different things depending on the actual type T. At least TList<T> offers to provide a comparer for the latter.

Share this post


Link to post
1 hour ago, Rollo62 said:

Have you checked Spring4D ?

I is too large of an library to add into our SVN for such a little use cases, at least so far, Could check for the algorithm tough.

Share this post


Link to post
33 minutes ago, Uwe Raabe said:

The solution heavily depends on what you call different on these lists. Your solution simply makes the lists containing the same elements, which is quite off checking for equality. Others might see two lists equal if the elements and their order match. Also equality of two elements can mean different things depending on the actual type T. At least TList<T> offers to provide a comparer for the latter.

True dat. I am interested in the values, not the order.

Share this post


Link to post
Guest

At http://www.angusj.com/delphi/ there a TDiff for textfiles. However, the textfiles are CRC:ed int two integer lists that are compared. It uses "An O(NP) Sequence Comparison Algorithm".

The thing is re-implemented in mORMot.

I have a suspicion that it could be interesting to look at either implementation.

 

/D

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×