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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

Representation for Set

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
srineet
Asked:
srineet
  • 4
  • 2
1 Solution
 
KangaRooCommented:
0
 
srineetAuthor Commented:
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
 
VEngineerCommented:
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
Independent Software Vendors: 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!

 
srineetAuthor Commented:
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
 
VEngineerCommented:
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
 
srineetAuthor Commented:
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
 
srineetAuthor Commented:
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now