0

I want to design a data structure with the following properties:

  1. Accessible as a single array of some type say T. For example s[i].
  2. Ability to access named ranges in it. For example s.GoodObjects[i], s.BadObjects[j].
  3. Iterate over it as a whole (STL algorithms etc.).
  4. Converting the name-index to index in the whole array. Here the interface is a good question too.

Also the desired behaviour would be the ability to

  1. I really want ranges names to be checked at compile time, not some string-named ranges, but C++ identifier-named.
  2. Add or remove elements to subarrays. s.BadObjects.push_back(yourMom).
  3. Implement it without using macros, just good C++ and templates, but readability of usages is of course the #1 priority.
  4. Possibly range arrays could have element type of a pointer to derived class where T is a pointer to base class.

So, how would you design such a structure?

Edit

Looks like I haven't stated it too explicitly, but the 4th requirement (the first list) is actually very important one. Maybe a more general question would be as follows: how to properly design an indexed collection of objects of different types (having a common superclass) with easy indexed access to objects of particular subtype? So, having a through index is a neccesity.

One can assume that all the types are known at compile-time, and amount of every particular object in the whole array is constant (from config file).

4
  • I can imagine this being really confusing to use. Commented Jul 21, 2011 at 9:23
  • @Tomalak, I'm trying to avoid confusion, but now I have a single array consisting of 4 parts, each of which has its number of elements, so I have to perform some awkward arithmetic like s[config->numOfBadObjects + config->numOfGoodOnes + config->numOfBearableOnes + j], where j is the index of some disgusting object I want from that array. This looks error-prone and confusing. This is the reason for the question. Commented Jul 21, 2011 at 9:28
  • actually, this is not end of story, there are groups consisting of one element, so magical constants come into play to make things worse... Commented Jul 21, 2011 at 9:30
  • 2
    I think you should keep separate containers, and create a type of iterator to go over multiple containers at once if you want to join them up somehow. Commented Jul 21, 2011 at 9:35

2 Answers 2

3

If you really want to to this, you could have a class storing several std::vectors and then supply an operator[] for the class itself that redirects into these vectors, perhaps depending on their respective size.

While you're at it, the outer class could also provide begin() and end() to allow iteration over the entire dataset.

I doubt though that the good-design tag is applicable here. :-)

Sign up to request clarification or add additional context in comments.

Comments

0

It sounds to me like you have separate objects and occasionally you need to iterate over all of them. Rather than making a confusing vector with subranges monster, just use separate vectors. When you need to iterate over all of them, just iterate over each one separately. Your code will be much cleaner that way.

1 Comment

no, I have one array, and it's not a coincidence or strive for monsters :) It's because every object in this array has its number, which is logically justified. This numbering cannot go away, it's part of the subject field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.