Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Manupulating Sets in Pascal

Posted on 1998-04-08
4
Medium Priority
?
228 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 200 total points
ID: 1361349
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
ID: 1361350
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
ID: 1361351
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
ID: 1361352
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

877 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