Solved

Representation for Set

Posted on 2000-04-09
7
197 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
[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
  • 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
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!

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

636 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