Recent

Author Topic: what is the equal to a C++ STD::SET<Type> enumerator  (Read 982 times)

jamie

  • Hero Member
  • *****
  • Posts: 6529
what is the equal to a C++ STD::SET<Type> enumerator
« on: July 10, 2024, 12:24:57 am »

as it states in the title. In this case its only one TYPE and I assume that it must not allow duplicates of values.

Also this SET has an enumerator, So I for now am using a TFPGList and the enumerator Generic, but something tells me that is wrong, but it gets me out of the hole until I can get the converted app to actually do something.

The only true wisdom is knowing you know nothing

ASerge

  • Hero Member
  • *****
  • Posts: 2317
Re: what is the equal to a C++ STD::SET<Type> enumerator
« Reply #1 on: July 10, 2024, 01:34:26 am »

as it states in the title. In this case its only one TYPE and I assume that it must not allow duplicates of values.

Also this SET has an enumerator, So I for now am using a TFPGList and the enumerator Generic, but something tells me that is wrong, but it gets me out of the hole until I can get the converted app to actually do something.
And why are the usual sets not suitable?

jamie

  • Hero Member
  • *****
  • Posts: 6529
Re: what is the equal to a C++ STD::SET<Type> enumerator
« Reply #2 on: July 10, 2024, 01:52:55 am »
https://en.cppreference.com/w/cpp/container/set

This is dynamic so one can add,Delete unique values.


The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 15553
  • Censorship about opinions does not belong here.
Re: what is the equal to a C++ STD::SET<Type> enumerator
« Reply #3 on: July 10, 2024, 06:23:16 am »
rtl-generics: TCustomSet/tHashset in generics.collections.
fcl-stl: gHashSet
fcl-stl is inspired by C++ functionality of stl::<class> so may come closest.
rtl-generics sets are just as good. fgl does not have sets afaik.
avk's lgenerics has a variety of sets (install via OPM or https://github.com/avk959/LGenerics ) firmly founded in theory and with a nice comprehensive wiki entry.
The c++ set you mention is in fact a hash set.
For generics.collections I have put a series of pascal set operator overloads somewhere, but you seem to want something close to C++ instead of pascal set notation, so probably not relevant to you.
In general, but given its limitations, normal pascal sets should be used. I do not quite understand why you can't use Pascal sets? But there are plenty of good implementations.
« Last Edit: July 10, 2024, 06:44:29 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

jamie

  • Hero Member
  • *****
  • Posts: 6529
Re: what is the equal to a C++ STD::SET<Type> enumerator
« Reply #4 on: July 11, 2024, 03:41:36 am »
Something like this code.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,fgl;
  9.  
  10. type
  11.  generic TSet<T> = Class(Specialize TFPGList<T>)
  12.    Private
  13.     Type
  14.      fArrayType = Array of T;
  15.   Public
  16.    constructor Create(A:fArrayType);
  17.   End;
  18.  
  19.   { TForm1 }
  20.  
  21.   TForm1 = class(TForm)
  22.     Button1: TButton;
  23.     procedure Button1Click(Sender: TObject);
  24.   private
  25.  
  26.   public
  27.  
  28.   end;
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34.  
  35. {$R *.lfm}
  36. constructor TSet.Create(A:fArrayType);
  37. Var
  38.   AA:T;
  39. Begin
  40.   Inherited Create;
  41.   For AA in A do
  42.   if Self.IndexOf(AA)<0 Then
  43.    Add(AA); {Else Optional fault trigger}
  44. end;
  45.  
  46. { TForm1 }
  47.  
  48. procedure TForm1.Button1Click(Sender: TObject);
  49. Type
  50.   TS = Specialize TSet<Byte>;
  51. Var
  52.   S:TS;
  53. begin
  54.   S:= TS.Create([1,2,4,5]); //Should not duplicate
  55.   Caption := S.Count.Tostring;
  56.   S.Free;
  57. end;
  58.  
  59. end.
  60.  
  61.  
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 15553
  • Censorship about opinions does not belong here.
Re: what is the equal to a C++ STD::SET<Type> enumerator
« Reply #5 on: July 11, 2024, 06:44:08 am »
That is exactly what all the sets I mentioned do: of course they do not duplicate, because they are sets. Sets only contain unique values by definition. They also need to implement the set operators/functions except maybe symmetric difference.(><)
The latter is not implemented in C++ std library nor in many other sets in different languages. In your case I would use the fcl-stl set or implement one for fgl. That is not difficult, rather basic stuff. implement include, exclude and contains and it is a set. A set needs not be ordered, although Pascal sets are ordered by nature. In C++ the std library is a hash set, because that allows for fast membership determination, given the unordered nature of a mathematical set. All other operants can be done by testing for membership.
Hence you only need the three basics: add/include, remove/exclude, contains. So if you want to stick to fgl, you know what you need to do  ;D
Tip: the underlying structure of a set is not an array but a list, you link and unlink elements of a list.
[edit]
In fgl, TFpsMap with duplicates is dupError or dupIgnore is already a set.....
I wanted to implement it for you, but the code is already there.
« Last Edit: July 11, 2024, 09:59:01 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018