Solved

Manupulating Sets in Pascal

Posted on 1998-04-08
4
219 Views
Last Modified: 2010-04-06
Is there a way to iterate through all the elements of a Set without using  a for loop that checks if every possible element is IN the Set ?
0
Comment
Question by:aloha
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
julio011597 earned 100 total points
Comment Utility
The answer, AFAIK, is: no, there's no way.

Sets operators (at least in Delphi2.0) are: "+", "-", "*", "in"... that's all.

To have more, your only chance is switching to another data type; e.g., arrays.
0
 
LVL 4

Expert Comment

by:d003303
Comment Utility
Yo,
sure, again, it is possible. Just have a look at the memory structure:
(from Delphi help)
A set is a bit array where each bit indicates whether an element is in the set or not. The maximum number of elements in a set is 256, so a set never occupies more than 32 bytes. The number of bytes occupied by a particular set is calculated as

ByteSize = (Max div 8) - (Min div 8) + 1

where Min and Max are the lower and upper bounds of the base type of that set. The byte number of a specific element E is

ByteNumber = (E div 8) - (Min div 8)

and the bit number within that byte is

BitNumber = E mod 8

where E denotes the ordinal value of the element.

So you can take use of the memory structure. Construct a power set with bitwise algorithms and compare your set to the power set (PowerSet - MySet). If the result is [], all elements are in MySet.
Code will follow tomorrow, I have to search it on my wide-spread hard disk ;-)

Slash/d003303
0
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
And what if implementation changes??

In general, i believe that, when there's so much work to do for such a very common task, the _approach_ itself is - at least - suspicious.

Indeed, you end up discovering that any interesting approach, as d003303's, needs at least as much development efforts as switching to a straight array, with all the drawbacks related to: documenting, mantaining, and porting a code out of the ordinary.

This is not to say that one must never investigate an alternative approach... just be sure it is really worth doing.
0
 
LVL 4

Expert Comment

by:d003303
Comment Utility
My example consists of one procedure, CreatePowerSet, that creates a power set for each set you supply. If the implementation changes, you just need to change this procedure.
Borland uses the memory structure of sets heavily, because it resists on bit flags, and bit flags are most common through all platforms. The easiest example on how Borland uses them is (if you have D3 Pro) in .\Source\VCL\Toolwin.pas, method TToolWindow.WMNCPaint, Line 137.

Slash/d003303
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now