Recent

Author Topic: The size of a set  (Read 1212 times)

jamie

  • Hero Member
  • *****
  • Posts: 3275
Re: The size of a set
« Reply #15 on: April 11, 2020, 03:57:12 pm »
No it does not work like that but it could be what the poster actually needs and a field is being indexed in that manner..
 
 I don't do a lot of GENERIC coding due to the fact that if not careful you can generate a lot of bloat but there are some simple uses of GENERICS that seems to be just fine in saving you time if you setup for a couple of types here and there.

 The TArrary<?> seems to work out nicely for a resulting type on functions  :)
The only true wisdom is knowing you know nothing

Mogens Lundholm

  • New Member
  • *
  • Posts: 23
Re: The size of a set
« Reply #16 on: April 11, 2020, 05:28:30 pm »
Why don't you simply write a container class with operator overloads to give you that bloated field?

That is what I did. But have to made a lot of changed because "[]" cannot be programmed as operator.

 Have you looked at the "TBITS" class yet ?

Yes, but I will take another look. I might have been worried for performance of classes and variable arrays. Thought that 4 "and- operations" of 64 bits takes no time (set addition). Freeing and Reserving memory takes time. Variable arrays may also take
time (reserving memory).   

 That allows you to address bits up to any size until you run out of memory and it has a [] bracket default for array indexing of bits..

This sounds interesting - very important for my program is the "for i in a_set do". I have not looked at the FPC implementation, but the machine instruction for finding first bit (element) can make this very fast.

Mogens Lundholm

  • New Member
  • *
  • Posts: 23
Re: The size of a set
« Reply #17 on: April 11, 2020, 05:31:58 pm »
From the beginning of Pascal the size of a set is limited to 256 elements. The size has not been increased in 50 years.

I need bigger sets.
I was proposed to change the compiler but - I don't know where to begin. In the Facebook group (Free Pascal and Lazarus Foundation) I was told that the file '..../rtl/inc/compproc.inc' has a definition of length (Part of PFC).
The size may be "fpc_small_set" or "fpc_normal_set".

What would the effort be to increase this limit, remove the limit or make fpc_huge_set?

Alternative: Allow "[]" as an operator. Then all operators could be programmed.

The larger sets get, the larger the waste of memory is. A set of 256 elements already takes 32 Byte or 8 32-bit values. Especially if you're working with sparse sets that is a huge waste of space. Thus it would be better to use a sparse set mechanism. The functionality provided by FPC 3.2 (operator overloads, generic and the new dynamic array constructors) allow for such implementation. There is no need to extend the compiler for this and it can be done in a more efficient way.

For my program: Speed is important. It is OK to waste a lot of memory.

Mogens Lundholm

  • New Member
  • *
  • Posts: 23
Re: The size of a set
« Reply #18 on: April 11, 2020, 08:58:49 pm »
Why don't you simply write a container class with operator overloads to give you that bloated field?

That is what I did. But have to made a lot of changed because "[]" cannot be programmed as operator.

 Have you looked at the "TBITS" class yet ?

 That allows you to address bits up to any size until you run out of memory and it has a [] bracket default for array indexing of bits..

Why don't you simply write a container class with operator overloads to give you that bloated field?

That is what I did. But have to made a lot of changed because "[]" cannot be programmed as operator.

 Have you looked at the "TBITS" class yet ?

 That allows you to address bits up to any size until you run out of memory and it has a [] bracket default for array indexing of bits..

Trying TBITS:
I am surprised - seems like TBITS takes the same time as standard 256-set. Compared UNION and OR. Made a loop of 10 million operation.  Standard set - 5 seconds. TBITS 6 seconds. But - I am novice in Class-programming. I must try to find an example of programming operators for classes (How to free the TBITS - can the class be created on the stack, so it will be freed at return?).


Mogens Lundholm

  • New Member
  • *
  • Posts: 23
Re: The size of a set
« Reply #19 on: April 11, 2020, 09:13:18 pm »
Why don't you simply write a container class with operator overloads to give you that bloated field?

That is what I did. But have to made a lot of changed because "[]" cannot be programmed as operator.

 Have you looked at the "TBITS" class yet ?

Wrote an answer - but cannot see it. It seems like TBITS runs almost as fast as the standard set. I am surprised. But I am a novice in class-programming. Trying to find an example of programming the operators (problem is freeing. - can the class be placed on stack so it can be freed at return?)

 That allows you to address bits up to any size until you run out of memory and it has a [] bracket default for array indexing of bits..

Why don't you simply write a container class with operator overloads to give you that bloated field?

That is what I did. But have to made a lot of changed because "[]" cannot be programmed as operator.

 Have you looked at the "TBITS" class yet ?

 That allows you to address bits up to any size until you run out of memory and it has a [] bracket default for array indexing of bits..

avra

  • Hero Member
  • *****
  • Posts: 1943
    • Additional info
Re: The size of a set
« Reply #20 on: April 11, 2020, 09:52:24 pm »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

PascalDragon

  • Hero Member
  • *****
  • Posts: 1950
  • Compiler Developer
Re: The size of a set
« Reply #21 on: April 11, 2020, 09:55:26 pm »
Trying TBITS:
I am surprised - seems like TBITS takes the same time as standard 256-set. Compared UNION and OR. Made a loop of 10 million operation.  Standard set - 5 seconds. TBITS 6 seconds. But - I am novice in Class-programming. I must try to find an example of programming operators for classes (How to free the TBITS - can the class be created on the stack, so it will be freed at return?).

TBits is a class, classes can only exist on the heap. You create it with yourbitsvar := TBits.Create(<YourSize>) and destroy it with yourbitsvar.Free.

For operators you need to use global operator overloads, because classes can't have member operator overloads (only records can).

Mogens Lundholm

  • New Member
  • *
  • Posts: 23
Re: The size of a set
« Reply #22 on: April 18, 2020, 10:41:28 am »
You can take a look if this solution suits your needs:
https://forum.lazarus.freepascal.org/index.php/topic,46699.0.html
Will not compile:
unithugeset.pas(23,3) Fatal: Cannot find generics.collections used by UnitHugeSet of the Project Inspector.

PascalDragon

  • Hero Member
  • *****
  • Posts: 1950
  • Compiler Developer
Re: The size of a set
« Reply #23 on: April 18, 2020, 10:53:09 am »
You either need to use FPC 3.2 or newer as Generics.Collections ships with that by default or if you want to use 3.0.4 you can download or clone this repo of the original author (hnb on this forum).

Mogens Lundholm

  • New Member
  • *
  • Posts: 23
Re: The size of a set
« Reply #24 on: April 18, 2020, 12:27:04 pm »
You either need to use FPC 3.2 or newer

Upgraded Lazarus - but same 3.0.4. Have searched for FPC 3.2 download, but cannot find it.

jamie

  • Hero Member
  • *****
  • Posts: 3275
Re: The size of a set
« Reply #25 on: April 18, 2020, 01:50:36 pm »
What he suggested was to use a collection of generics published by a third party etc..

If you look at his message again you'll see he high lighted it.

https://github.com/maciej-izak/generics.collections
The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 1950
  • Compiler Developer
Re: The size of a set
« Reply #26 on: April 18, 2020, 01:52:40 pm »
You either need to use FPC 3.2 or newer

Upgraded Lazarus - but same 3.0.4. Have searched for FPC 3.2 download, but cannot find it.

We are currently in the Release Candidate phase. You can find it here. Snapshots of Lazarus bundled with that RC can be found here.

Mogens Lundholm

  • New Member
  • *
  • Posts: 23
Re: The size of a set
« Reply #27 on: April 18, 2020, 04:06:52 pm »
You either need to use FPC 3.2 or newer
We are currently in the Release Candidate phase. You can find it here. Snapshots of Lazarus bundled with that RC can be found here.

Thanks. It compiles now. I run this codesnip:
Code: Pascal  [Select][+][-]
  1.   // Set 1 = [10,11], Set 2 = [20,1000020],
  2.   write('Set 1:   '); writeset(s1);
  3.   write('Set 2:   '); writeset(s2);
  4.   write('Set 1+2: '); writeset(s1+s2);
  5.  
  6.   write('Set 1:   '); writeset(s1);
  7.   write('Set 2:   '); writeset(s2);
  8.   write('Set 1+2: '); writeset(s1+s2);        
  9.  

The result output is:
Code: Pascal  [Select][+][-]
  1. Set 1:   [10,11]
  2. Set 2:   [20,1000020]
  3. Set 1+2: [10,11,20,1000020]
  4. Set 1:   [10,11]
  5. Set 2:   [10,11,20,1000020]
  6. Set 1+2: [10,11,20,1000020]
  7.  

Seems to work good and fast but the problem is that "operator+"  distroys the s2-variable.

PascalDragon

  • Hero Member
  • *****
  • Posts: 1950
  • Compiler Developer
Re: The size of a set
« Reply #28 on: April 18, 2020, 05:58:03 pm »
Seems to work good and fast but the problem is that "operator+"  distroys the s2-variable.

Best to report this in the other thread, so that Thaddy knows that he might want to fix this.

 

TinyPortal © 2005-2018