Recent

Author Topic: Function which Return a Set of System.Int64; [?]  (Read 1322 times)

guest62577

  • Guest
Function which Return a Set of System.Int64; [?]
« on: May 03, 2019, 02:30:34 am »
Quote
Hello, Fellow Pascarians from the High Mountains of Computer Programming;

coming through the path of multiple interruptions draining attention causing perceptual failures, the initialization of a new process to start new research was disabled for a brief moment; resulting in a catastrophe to the focus of this organization;

a system of tags was pondered to make use of Sets;
where values would be specified in the Function, returning the Set with the required range;


(Wondering Code)
Code: Pascal  [Select]
  1. function ReturningOfSets(myNumber:System.Integer):Set Of System.Int64;
  2. begin
  3.  case myNumber of
  4.  1: Result := [100..200];
  5.  2: Result := [1000..2000];
  6.  end;
  7. end;
  8.  
  9. if randomNumber in [ReturningOfSets( myOtherNumber  )] then
  10. ShowMessage('The Range Was Processed, The Logic Now May Proceed;');
  11.  

the ReturningOfSets logic is a development-time organization function;
then the first structure myNumber accommodates the first layer of the index;
the Result structure displays the required process indexes;

then perhaps:
Code: Pascal  [Select]
  1. if randomNumber in [ReturningOfSets( myOtherNumber  )] then
  2. case myHardCodedIndexes of
  3. {myNumber 1}
  4. 100:;
  5. {myNumber 2}
  6. 1000:;
  7. end;

the utility of the function is in having dynamic access for the organization of these structures concentrating the indexes for the request of the call;
then using myNumber to then easily restructure the calls avoiding the propagation of the triggering of the indexes disorder;

Quote
is it possible to construct such a structure, and if so, how to proceed?

jamie

  • Hero Member
  • *****
  • Posts: 2068
Re: Function which Return a Set of System.Int64; [?]
« Reply #1 on: May 03, 2019, 02:53:57 am »
Not directly but you can write a Class with an enumerator in it.

The Class would be the return type instead of using the Set  Of Int64;

So while in the FOR Loop control you can do the For I in ReturningOFSets(A Number) do.

Lookup "GetEnumerator", it returns a item that re
Number 1 at blue screen app creations!

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: Function which Return a Set of System.Int64; [?]
« Reply #2 on: May 03, 2019, 03:46:09 am »
"A set can contain at most 256 elements."

This is not possible:
Code: Pascal  [Select]
  1.   i: set of int64;

While this is ok:
Code: Pascal  [Select]
  1.   i: set of byte;

If you need more than 256, try Classes.TBits.

Edit:
Your code shows ranges, but assuming you really meant sets, not ranges:
Code: Pascal  [Select]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes;
  7.  
  8. function CreateRange(AFrom, ATo: integer): TBits;
  9. var
  10.   i: Integer;
  11. begin
  12.   Result := TBits.Create(ATo+1);
  13.   for i := AFrom to ATo do
  14.     Result[i] := True;
  15. end;
  16.  
  17. var
  18.   Set100_200   : TBits;
  19.   Set1000_2000 : TBits;
  20.  
  21. function ReturningOfSets(myNum: integer): TBits;
  22. begin
  23.   if myNum<2 then
  24.     Result := Set100_200
  25.   else
  26.     Result := Set1000_2000;
  27. end;
  28.  
  29. var
  30.   i: integer;
  31.   Rand1, Rand2: integer;
  32.   MySet: TBits;
  33.  
  34. begin
  35.   Set100_200   := CreateRange(100,200);
  36.   Set1000_2000 := CreateRange(1000,2000);
  37.  
  38.   try
  39.     Randomize;
  40.     for i := 1 to 20 do
  41.     begin
  42.       Rand1 := Random(2)+1;
  43.       Rand2 := Random(2000)+1;
  44.       MySet := ReturningOfSets(Rand1);
  45.  
  46.       WriteLn(Rand1:3, Rand2:6, (MySet.Size>Rand2) and MySet[Rand2]:10);
  47.     end;
  48.   finally
  49.     Set100_200.Free;
  50.     Set1000_2000.Free;
  51.   end;
  52.  
  53.   ReadLn;
  54. end.
« Last Edit: May 03, 2019, 04:15:46 am by engkin »

PascalDragon

  • Hero Member
  • *****
  • Posts: 667
  • Compiler Developer
Re: Function which Return a Set of System.Int64; [?]
« Reply #3 on: May 03, 2019, 09:21:29 am »
a system of tags was pondered to make use of Sets;
where values would be specified in the Function, returning the Set with the required range;
Sets are restricted to 256 elements.
If you don't need to handle ranges you can use generic classes in Generics.Collections: THashSet<>, TSortedSet<>, TSortedHashSet. Also there is TSet<,> of unit gset. For efficient use of ranges you might want to implement your own.
You could even throw in an operator overload of in so that it would behave a bit more like a set.

Code: Pascal  [Select]
  1. function ReturningOfSets(myNumber:System.Int64):Set Of System.Int64;
  2. begin
  3.  case myNumber of
  4.  1: Result := [100..200];
  5.  2: Result := [1000..2000];
  6.  end;
  7. end;
  8.  
  9. if randomNumber in [ReturningOfSets( myOtherNumber  )] then
  10. ShowMessage('The Range Was Processed, The Logic Now May Proceed;');
  11.  
Please note that even if such sets would be supported this code would be wrong as the square brackets around the call to ReturningOfSets would turn this into a set of sets so to speak (which is not allowed).

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7494
Re: Function which Return a Set of System.Int64; [?]
« Reply #4 on: May 03, 2019, 10:58:35 am »
Note that a set of int64 would be  2305843009213693952 bytes large, in the magnitude of 2 billion gigabyte.

The average machine nowadays has slightly less than 2000000000 GB memory, so the compiler refusing it is probably a good thing.
« Last Edit: May 03, 2019, 03:57:18 pm by marcov »

Thaddy

  • Hero Member
  • *****
  • Posts: 9151
Re: Function which Return a Set of System.Int64; [?]
« Reply #5 on: May 03, 2019, 03:30:32 pm »
here is such a set  8-) Ok string example, but feel free to dis-abstract to int64.....or anything :P :P :P :P
Code: Pascal  [Select]
  1. {$mode objfpc}{$H+}
  2. uses
  3.   types,
  4.   generics.collections;
  5.  
  6. type
  7.   PStringset = ^TStringset;
  8.   TStringSet = specialize THashSet<string>;
  9.  
  10. // symetric difference
  11. operator ><(const a,b:TStringSet):TStringSet;
  12. begin
  13.   Result := a;
  14.   Result.SymmetricExceptWith(b);
  15. end;
  16.  
  17. // difference
  18. operator -(const a,b:TStringSet):TStringSet;
  19. begin
  20.   Result := a;
  21.   a.ExceptWith(b);
  22. end;
  23.  
  24. // Union
  25. operator +(const a,b:TStringSet):TStringSet;
  26. begin
  27.   a.UnionWith(b);
  28.   Result := a;
  29. end;
  30.  
  31. // Intersection
  32. operator * (const a,b:TStringSet):TStringSet;
  33. begin
  34.   a.IntersectWith(b);
  35.   Result := a;
  36. end;
  37.  
  38. // in
  39. operator in(const a:string;const b:TStringSet):Boolean;
  40. begin
  41.   result := b.Contains(a);
  42. end;
  43.  
  44. // operator contains
  45. operator <=(constref a, b:TStringSet):Boolean;
  46. var
  47.   s:string;
  48. begin
  49.   Result := False;
  50.   for s in b do
  51.   begin
  52.     result := a.Contains(s);
  53.     if Result = False then break;
  54.   end;
  55. end;
  56.  
  57.  
  58. procedure include(const a,b:TStringSet);
  59. begin
  60.   a.AddRange(b);
  61. end;
  62.  
  63. procedure Exclude(const a:TStringSet;const b:string);
  64. begin
  65.   a.Remove(b);
  66. end;
  67.  
  68. procedure Include(const a:TStringSet;const b:string);
  69. begin
  70.   a.Add(b);
  71. end;
  72. Type
  73.   testset = (test, me,too); // for comparison with standard pascal set.
  74. var
  75.   a,b:TStringset;
  76.   sa:array of string = ('test','me','too');
  77.   sb:array of string = ('test');
  78.   s:string;
  79.   sas:set of testset = [test,me,too];
  80.   sbs:set of testset =[test];
  81.   scs:testset;
  82. begin
  83.   writeln(HInstance);
  84.   a := TStringset.create;
  85.   a.addrange(sa);
  86.   b := TStringset.Create;
  87.   b.addrange(sb);
  88.   a:= a >< b;
  89.   for s in a do writeln(s);writeln('------');
  90.   b.free;
  91.   a.free;
  92.   for scs in (sas><sbs) do writeln(scs);
  93.   readln;
  94. end.

« Last Edit: May 03, 2019, 03:34:35 pm by Thaddy »
also related to equus asinus.

440bx

  • Hero Member
  • *****
  • Posts: 1190
Re: Function which Return a Set of System.Int64; [?]
« Reply #6 on: May 03, 2019, 04:04:50 pm »
The average machine nowadays has slightly less than 2000000000 GB memory, so that is probably a good thing.
Now, that is funny.

2,000,000,000 GB of memory !? how many memory slots does your motherboard have ?

Even at 1,000 GB per memory module, that would take 2 million memory slots.

It is a fact, the average machine has "slightly less than" that.




using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7494
Re: Function which Return a Set of System.Int64; [?]
« Reply #7 on: May 03, 2019, 04:24:03 pm »
The average machine nowadays has slightly less than 2000000000 GB memory, so that is probably a good thing.
Now, that is funny.

2,000,000,000 GB of memory !? how many memory slots does your motherboard have ?

Well, it is 2^64/8 so 2^61 bits. Afaik modern processors can only address 48-bits. So yes, this is way, way out of reach of the real world.

For a mere 32-bit set it is still half a gig. Not something you routinely operate on, since the zeroing alone is painful.

guest62577

  • Guest
Re: Function which Return a Set of System.Int64; [?]
« Reply #8 on: May 03, 2019, 06:52:27 pm »
the ReturningOfSets logic is a development-time organization function;
then the first structure myNumber accommodates the first layer of the index;
the Result structure displays the required process indexes;

then perhaps:
Code: Pascal  [Select]
  1. if randomNumber in [ReturningOfSets( myOtherNumber  )] then
  2. case myHardCodedIndexes of
  3. {myNumber 1}
  4. 100:;
  5. {myNumber 2}
  6. 1000:;
  7. end;

the utility of the function is in having dynamic access for the organization of these structures concentrating the indexes for the request of the call;
then using myNumber to then easily restructure the calls avoiding the propagation of the triggering of the indexes disorder;

cause a Set works perfect on being updated on its context like [100..200] -> [100..200,230,350..500]
Quote
there is no compatible usability of Sets ?

Thaddy

  • Hero Member
  • *****
  • Posts: 9151
Re: Function which Return a Set of System.Int64; [?]
« Reply #9 on: May 04, 2019, 08:43:19 am »
If you don't need to handle ranges you can use generic classes in Generics.Collections: THashSet<>, TSortedSet<>, TSortedHashSet. Also there is TSet<,> of unit gset. For efficient use of ranges you might want to implement your own.
I have missed your answer, Sven, but wrote an example - also above - along those same lines.
I would like to add that set operations in itself do not need ranges as such, because of in and <= etc. operators, which basically does the job.
Alas, set operations do not come natural to many programmers...They should! To me it is one of the defining features of Pascal.
« Last Edit: May 04, 2019, 08:46:36 am by Thaddy »
also related to equus asinus.

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: Function which Return a Set of System.Int64; [?]
« Reply #10 on: May 04, 2019, 12:49:05 pm »
cause a Set works perfect on being updated on its context like [100..200] -> [100..200,230,350..500]
Quote
there is no compatible usability of Sets ?

1st answer:

try Classes.TBits.

Better answer:
generic classes in Generics.Collections: THashSet<>, TSortedSet<>, TSortedHashSet. Also there is TSet<,> of unit gset.

Thaddy provided a full example using strings.

I still see ranges:
[100..200,230,350..500] is [100..200,230..230,350..500]

You can deal with three ranges way faster than a set with 101+1+151=252 elements.

Edit:
Probably the most important point:
Note that a set of int64 would be  2305843009213693952 bytes large.

Unless you put some realistic limits.
« Last Edit: May 04, 2019, 12:59:59 pm by engkin »

jamie

  • Hero Member
  • *****
  • Posts: 2068
Re: Function which Return a Set of System.Int64; [?]
« Reply #11 on: May 04, 2019, 03:18:12 pm »
I interpret the question differently..

 if  you look at the example provided by the poster there would be only two numbers to deal with, the low and high..

 Now, one could pass back a Class/Record with a GetEnumerator to be used for the "IN" statement of these two numbers
or, one could just simply use a MinMax function to test the range of a given Random number generated.

In his example it is a RANGE Set not an array of numbers in that range with how the question is asked.

If this being the case then there are several ways to solved this issue without complex code.
Number 1 at blue screen app creations!

Thaddy

  • Hero Member
  • *****
  • Posts: 9151
Re: Function which Return a Set of System.Int64; [?]
« Reply #12 on: May 04, 2019, 06:57:20 pm »
Unless you put some realistic limits.
Yeah right...
For some context and the famous fun...: https://www.wired.com/1997/01/did-gates-really-say-640k-is-enough-for-anyone/

Know your limits... there aren't any....
Do not ever define or express anything with the word realistic....  History proves you wrong all the time. (That's a free lesson.. <grumpy allowed by myself... >:D >:D >:D> )

My math teacher showed me there is always infinity + 1.... O:-)
« Last Edit: May 04, 2019, 07:06:25 pm by Thaddy »
also related to equus asinus.

guest62577

  • Guest
Function which Return Requirement of System.Int64; [?]
« Reply #13 on: May 25, 2019, 08:46:52 pm »
Quote
Classes.TBits
Looks Cool, but due to incessant attacks of sanitary magnitude of the social perceptions towards the reality of the planet, the censorship resulting from these acts were to drain the attention to the computer projects not being dictated of the contracted ones of the observer of the social assimilations; in order to perform better tests on the projects that where corrupted;

a simpler logic not directly applying the values were placed in a CASE preceding two INTEGER variables to verify the requirements;

Code: Pascal  [Select]
  1. Result := false;
  2. case myStructure of                //[?]RequirementsDatabase
  3.  
  4. 1: case myNumber of                //[?]RequiredNumber;
  5.    100..200: Result := true;
  6.    end;
  7. 2: case myNumber of                //[?]RequiredNumber;
  8.    1000..2000: Result := true;
  9.    end;
  10.  
  11. end;

in this example, when updating project tags; the code are filtered FALSE if it goes OutOfRange;

Code: Pascal  [Select]
  1. if test(1,100) then
  2. //code perform
  3.  
  4. if test(1,500) then
  5. //code error

the resulting is to have better access on coding the required issue, preventing all executions from the previous code;

jamie

  • Hero Member
  • *****
  • Posts: 2068
Re: Function which Return a Set of System.Int64; [?]
« Reply #14 on: May 25, 2019, 09:49:32 pm »
look at this idea.

Code: Pascal  [Select]
  1. Function ReturnMyRange:TMyRange;
  2. Begin
  3.  // Do some code to set the ranges.
  4.  //Fake it out for now;
  5.  Result.L := 100;
  6.  Result.H := 200;
  7. end;
  8.  
  9. { TForm1 }
  10.  
  11. procedure TForm1.Button1Click(Sender: TObject);
  12. var
  13.   R:TmyRange;
  14. begin
  15.   R := ReturnMyRange;
  16.   If 150 in [R.L..R.H] Then beep;
  17. end;                  
  18.  

For some reason, at least in 3.0.2, you can not specify variables in the ranges but  you can do it here..
Number 1 at blue screen app creations!