Solved

Representation for Set

Posted on 2000-04-09
7
189 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

705 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

15 Experts available now in Live!

Get 1:1 Help Now