thashset_intersectwith.lpr 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Generic types for NewPascal.org and FPC!
  2. // by Maciej Izak (hnb), 2018
  3. // sponsored by Sphere 10 Software (http://sphere10.com)
  4. program thashset_intersectwith;
  5. {$MODE DELPHI}
  6. {$APPTYPE CONSOLE}
  7. uses
  8. SysUtils, Generics.Collections;
  9. function SetToStr(ASet: THashSet<string>): string;
  10. var
  11. i: string;
  12. begin
  13. Result := '(';
  14. for i in ASet do
  15. Result := Result + ' ' + i;
  16. Result := Result + ' )';
  17. end;
  18. procedure WriteLnHashSet(const AName: string; AHashSet: THashSet<string>);
  19. begin
  20. WriteLn(Format('%0:s.Count = %1:d %0:s = %2:s', [AName, AHashSet.Count, SetToStr(AHashSet)]));
  21. end;
  22. var
  23. Group1: THashSet<string>;
  24. Group2: THashSet<string>;
  25. Group3: THashSet<string>;
  26. begin
  27. Group1 := THashSet<string>.Create;
  28. Group2 := THashSet<string>.Create;
  29. Group1.Add('User1');
  30. Group1.Add('User2');
  31. Group1.Add('User3');
  32. Group2.Add('User3');
  33. Group2.Add('User4');
  34. Group2.Add('User5');
  35. WriteLnHashSet('Group1', Group1);
  36. WriteLnHashSet('Group2', Group2);
  37. WriteLn('< Group3 IntersectWith Group2 >');
  38. Group3 := THashSet<string>.Create(Group1);
  39. Group3.IntersectWith(Group2);
  40. WriteLnHashSet('Group3', Group3);
  41. Group3.Free;
  42. Group2.Free;
  43. Group1.Free;
  44. ReadLn;
  45. end.