Solved

Manupulating Sets in Pascal

Posted on 1998-04-08
4
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
julio011597 earned 100 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month10 days, 19 hours left to enroll

628 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