Generating the union of two (or more) arrays at compile time

Started by
7 comments, last by hughiecoles 11 years, 10 months ago
I've got a few master arrays of items, and as well as arrays containing items that hold certain properties.

I want to create arrays containing the union at compile time to make the generation of these arrays more flexible. I'll give you an example.



const Players blueTeam[] =
{ a,b,c,d,e,f};

const Players redTeam[] =
{a,g,b,x,c};

const Players goodPlayers[] =
{ a,c,d,r,t};

const Players badPlayers[] =
{ c,d,f,w,q};


const Players goodRedPlayers[] = UNION(goodPlayers, redTeam);


I'm thinking either macros or templates could solve this problem, but I'm having trouble getting started.


Thanks.
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
Advertisement
It might be possible using various combinations of metaprogramming and preprocessor tricks. The question is: why bother with the headaches that will cause you? Doing this at compile-time is to me the very opposite of flexible. If you really want to do this, then take a look at Boost.MPL library and Boost.Preprocessor library. Also this stackoverflow question might be useful.

But if you do this at run-time instead, it is as simple as using std::set_union or std::set_intersection to generate an array of "good red players".

By the way, in your example given, "goodRedPlayers" is really the intersection of "goodPlayers" and "redTeam". The union would be all players that are either good OR red.
While this isn't an answer to your question, I think a better question is why are you doing this? Why do they need to be compile time constant arrays? Developing your game will be a nightmare if everything is hard coded (even with macro/template magic). Why not just have a file that describes the players, and be able to load this file into some [font=courier new,courier,monospace]std::vector<Players>[/font]s or something?
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Thanks for the replys. It's not a game, it's actually for work. I've got N credit cards, grouped into categories such as rewards cards, business cards, etc.... On top of this, I need them to be members of sub-categories such as low interest rewards cards, income range, etc.... This leaves me with 80+ lists of cards, which is very difficult to maintain. If I can maintain just the high-level lists and generate the more specific lists from those, a change in one card wouldn't require me to change ~20 other lists.

I'm aware that the code could get a bit dicey, but I just see it as more maintainable, since the changing of that many lists would lead to many errors, as opposed to one chunk of code.

And yeah I know that was the intersection, and all through the post I had used intresection, but I realized I put union in the title erroneously, and couldn't figure out how to change the post title, so I went through and changed it all to union, and forgot the change the example :S It happens.

The StackOverflow post is great, but I'm stuck using MSVC2008 for this specific project, so C++11 is a no go.
I'll consider using non-const arrays and creating sublists at runtime, since the solution would be easier,
thanks for the help.
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
I'm still not seeing why this needs to be done at compile time...

To be honest, a little database sounds nicer, where you store the cards, then have tables that reference the cards that belong to that table (i.e. tables like low interest, low income, rewards, etc.). Then it's just a matter of performing the necessary SQL query. Or maybe just a text file with JSON/some other format where you can just describe the categories each card belongs to... Anything but compile time.

And to be honest, the only way I know of to do this at compile time (that is more maintainable than just hard coding the values) is writing a little script (maybe a Python script) that generates the arrays/categories/header/source file for you.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Granted, it is ~2:45 here and i really should be sleepoing, but ...


I've got N credit cards, grouped into categories such as rewards cards, business cards, etc.... On top of this, I need them to be members of sub-categories such as low interest rewards cards, income range, etc.... This leaves me with 80+ lists of cards, which is very difficult to maintain. If I can maintain just the high-level lists and generate the more specific lists from those, a change in one card wouldn't require me to change ~20 other lists.

I'm aware that the code could get a bit dicey, but I just see it as more maintainable, since the changing of that many lists would lead to many errors, as opposed to one chunk of code.


Why on earth would anyone ever hard-code+compile-time any of it? This is insane ... well, it sure as hell looks like that ... can you elaborate?

Granted, it is ~2:45 here and i really should be sleepoing, but ...

[quote name='godsenddeath' timestamp='1338760382' post='4945922']
I've got N credit cards, grouped into categories such as rewards cards, business cards, etc.... On top of this, I need them to be members of sub-categories such as low interest rewards cards, income range, etc.... This leaves me with 80+ lists of cards, which is very difficult to maintain. If I can maintain just the high-level lists and generate the more specific lists from those, a change in one card wouldn't require me to change ~20 other lists.

I'm aware that the code could get a bit dicey, but I just see it as more maintainable, since the changing of that many lists would lead to many errors, as opposed to one chunk of code.


Why on earth would anyone ever hard-code+compile-time any of it? This is insane ... well, it sure as hell looks like that ... can you elaborate?
[/quote]


Pre-existing codebase that is a mess to say the least, and a tight deadline, I'm trying to incrementally improve it while still getting work done and hitting deadlines. Doing that much work isn't possible at the moment.
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried

Pre-existing codebase that is a mess to say the least, and a tight deadline, I'm trying to incrementally improve it while still getting work done and hitting deadlines. Doing that much work isn't possible at the moment.

To be honest though, even in the short run, building a small SQLite database populated with all the necessary information and tables could be the best solution and could be done very quickly. You could then populate the arrays (or [font=courier new,courier,monospace]std::vector[/font]s) you need at the static initialization stage when your program is launched (or by using an [font=courier new,courier,monospace]init()[/font] function at the beginning of [font=courier new,courier,monospace]main()[/font]). This shouldn't take more than a day, and should be more easily debuggable and extendable for the future.

Like I said, the easiest way to hard-code these things is using a python (or some other language) script to generate the code for you. Hacking with macros and templates to try and bend them to your will in this matter will probably take far more time and produce far less desirable results than either of these other two solutions.

I don't even know if macros and templates can properly do what you want to. They weren't made for that purpose.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Thanks for the help. I decided to hard code it to make sure it was done today, but also informed my boss that I'd be pulling that code back out to improve the design. Not the ideal solution, but we don't always get an ideal situation.
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried

This topic is closed to new replies.

Advertisement