0

I was reading this stackoverflow post TMP: how to generalize a Cartesian Product of Vectors? and I am very intrigued by the simplicity of the template and that it doesn't require moves or copies. However, in my case, I have a vector of classes which have the arrays to perform the cross product of; so I cannot specify cross( a, b, c ), and loping through and doing cross( b, c) then crossing that with a provides a nested set.

How can I perform the same, but use a vector instead of knowing the parameters a priori [i.e. cross(vector) ]?

Better yet....can it be done with a stack rather than recursive calls?

Here is my class setup:

#include<vector>
#include<memory>

class Item {
public:
    std::string name;
    std::string id;
};

class ItemList {
public:
    std::vector<Item> items;
};

int main() {

    std::vector<std::shared_ptr<ItemList>> lists;

    // Load some sample data
    for( size_t list_idx = 0; list_idx < 3; list_idx++ ) {
        auto list = std::make_shared<ItemList>();
        for( size_t item_idx = 0; item_idx < 5; item_idx++ ) {
            Item item;
            item.name = "List " + std::to_string(list_idx) + " - Item " + std::to_string(item_idx);
            item.id = item.name;
            list->items.push_back( item );
        }
        lists.push_back(list);
    };

    // Now, how to perform the cross product of the items???
    // cross( a, b, c ) is not the same as cross( a, cross( b, c ))
}

EDIT: Clarification on what I have as inputs and outputs:

So if I have a vector of 3 lists where the following are stored in the name member of the Item class.

   A1, A2, A3
   B1, B2
   C1, C2, C3, C4

I am trying to return

   { A1, B1, C1 }
   { A1, B1, C2 }
   { A1, B1, C3 }
   { A1, B1, C4 }
   { A1, B2, C1 }
   { A1, B2, C2 }
   { A1, B2, C3 }
   { A1, B2, C4 }
   { A2, B1, C1 }
   { A2, B1, C2 }
   { A2, B1, C3 }
   { A2, B1, C4 }
   { A2, B2, C1 }
   { A2, B2, C2 }
   { A2, B2, C3 }
   { A2, B2, C4 }
   { A3, B1, C1 }
   { A3, B1, C2 }
   { A3, B1, C3 }
   { A3, B1, C4 }
   { A3, B2, C1 }
   { A3, B2, C2 }
   { A3, B2, C3 }
   { A3, B2, C4 }

EDIT2: The link provided by @Matt McNabb is along the right track, but I can't use boost, so I am not sure how to modify this appropriately without it. Also, since I have a vector<shared_ptr<vector<>>> rather than vector<vector<>>, it complicates this a bit more. Although, I could generate a vector<vector>.

Thoughts?

Community
  • 1
  • 1
user3072517
  • 513
  • 1
  • 7
  • 21
  • _"in my case, I have a vector of classes which have the arrays to perform the cross product of; so I cannot specify cross( a, b, c ), and loping through and doing cross( b, c) then crossing that with a provides a nested set."_ I can't parse this. I just don't get what you're asking in general, actually. Can you provide some sample inputs and outputs? And in what way are recursive calls not already implemented using a stack? – Lightness Races in Orbit Mar 01 '15 at 05:08
  • The logic in the duplicate can apply to your case, you will just need to make a slight modification to account for the fact that you have a vector of classes containing vector, instead of vector of vectors. – M.M Mar 01 '15 at 05:33
  • Thanks for the link. The other problem is that I have a vector of shared_ptr to a vector and that complicates things a bit. Let me study the code a bit more, but at least it seems to be on the right track. – user3072517 Mar 01 '15 at 05:46

0 Answers0