Solved

Representation for Set

Posted on 2000-04-09
7
192 Views
Last Modified: 2010-04-02
Hi,
I'm translating a language to C++.The language has direct support for sets of any type, where a set is a collection with no duplicates, and has union and intersection done on it. I need to write a template for it. The language provides defining equality between two objects of same type. There need not exist any comparison (except for equality) between elements of a set.
    I can't use hashing, since I can't assume that obects which give "true" on equality tests will hash to the same value. I am looking for an efficient representation, especially for union and intersection.
   Any help will be greatly appreciated. Thank you very much.
- Srineet.
 
0
Comment
Question by:srineet
  • 4
  • 2
7 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2699431
0
 

Author Comment

by:srineet
ID: 2701149
I can't use the STL template because it expects what is called is a strict weak ordering between its elements and I can't always supply that because I am just translating between a language to C++ and the first language doesn't provide that. That's why I need help, to be able to efficiently represent sets and perform their operations.
   Thanks anyway.

- Srineet.
0
 
LVL 2

Expert Comment

by:VEngineer
ID: 2702446
You can just use the STL list then.  list does not expect any ordering.  From what I understand, you can use the set_union and set_difference functions from STL on the list container.

The only problem with using a list with set_union and set_difference is it can be as costly as O(n^2) - not terrible, but not so great either.

Let me know if this is feasible or not.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:srineet
ID: 2702979
Hi,
    Thanks for the suggestion. Well, thats what we've done right now, we've used list, but honestly speaking its not really feasible.
    I know one thing that union and intersection is especially easy on bitsets but can't figure out how to use this in my case. Should I have two representations, one huge collection of all the values of the given type which have been added so far in some set variable or other of the same type, and have individual set variables as bitset representations? Well, I could do this but what about set variables that have gone out of scope or values which no longer exist in any individual set variable but continue to exist in the huge collection. Again, the huge collection also be a set (no duplicates), so we're back to square one.
   Efficiency is of the essence, because the language I'm translating, makes extensive use of sets and I must provide reasonable performance. Can I use a ghost or dummy ordering that I impose myself in the translation? I do not know how to go about it.
   Thanks for any kind of help. Waiting in eager anticipation.

- Srineet.
0
 
LVL 2

Accepted Solution

by:
VEngineer earned 100 total points
ID: 2729249
What you could try to do is make a set of pointers to the objects.  Keep all the objects in a master pool of data and only insert pointers to them in the sets.  That way your large objects can be members of multiple sets with only the cost of adding pointers, not objects.

Union/intersect/difference can be O(n) operations if you keep the pointers in address order.  I have code that can do the O(n) union/intersection/difference.

Let me know if this sounds like a more feasible option..
0
 

Author Comment

by:srineet
ID: 2729540
Hey thanks VEngineer, this is a good idea. But however, the master pool must have no duplicate items. This will involve a complete loop over the master pool for every "add" to the set. But yes, I guess union and intersection etc. will be faster because pointers are in address order. Actually even bit vectors will do, won't they? Only argument against this scheme would be space usage, but it takes a much lower priority than time.
    Thanks, I'll accept this comment as the answer. If however, I encounter any more problem or even find another way, I'll let you know by posting here.
   Thank you.
   Cheerio.
- Srineet.
0
 

Author Comment

by:srineet
ID: 2729551
Boy this is such a relief. I was really upset about the inefficient implementation (though it was not the implementation but the language that we designed that was of importance.) Who'll stand such a compiler?
  BTW, I'll just let you know, that I am a student and my project was to design and implement an optimizer generator for compiler that must also support specification of data flow analysis problems. We take specifications in a language we designed
 and generate C++ code.
    Thank you.
- Srineet.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

815 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

8 Experts available now in Live!

Get 1:1 Help Now