@@ -5,10 +5,10 @@ using namespace Rcpp;
55using namespace std ;
66
77template <int RTYPE> Vector<RTYPE> cpp_stack_impl (List array_list, int along, Vector<RTYPE> fill, bool ovr) {
8- auto dimnames = vector<CharacterVector >(along); // dim: names along
8+ auto dimnames = vector<vector<String> >(along); // dim: names along
99 auto axmap = vector<unordered_map<string, int >>(along); // dim: element name->index
1010 auto ax_unnamed = vector<int >(along); // counter for unnamed dimension elements
11- auto a2r = vector<vector<vector<int >>>(array_list.size ()); // array > dim > element
11+ auto a2r = vector<vector<vector<int >>>(array_list.size ()); // index array> dim> element
1212
1313 // create lookup tables for all present dimension names
1414 for (int ai=0 ; ai<Rf_xlength (array_list); ai++) { // array index
@@ -46,12 +46,16 @@ template<int RTYPE> Vector<RTYPE> cpp_stack_impl(List array_list, int along, Vec
4646 } else {
4747 auto dni = as<vector<string>>(dn[d]);
4848 for (int e=0 ; e<da[d]; e++) { // element in dimension
49- if (axmap[d].count (dni[e]) == 0 ) {
50- axmap[d].emplace (dni[e], axmap[d].size () + ax_unnamed[d]);
49+ auto it = axmap[d].find (dni[e]);
50+ if (it == axmap[d].end ()) {
51+ int val = axmap[d].size () + ax_unnamed[d];
52+ axmap[d].emplace (dni[e], val);
5153 dimnames[d].push_back (dni[e]);
54+ a2r[ai][d].push_back (val);
55+ } else {
56+ a2r[ai][d].push_back (it->second );
5257 }
5358// Rprintf("array %i dim %i: %s -> %i\n", ai, d, dni[e].c_str(), axmap[d][dni[e]]);
54- a2r[ai][d].push_back (axmap[d][dni[e]]);
5559 }
5660 }
5761 }
@@ -73,10 +77,13 @@ template<int RTYPE> Vector<RTYPE> cpp_stack_impl(List array_list, int along, Vec
7377 auto rdnames = List (dimnames.size ());
7478 for (int i=0 ; i<dimnames.size (); i++) {
7579 rdim[i] = dimnames[i].size ();
76- if (all (is_na (dimnames[i])))
80+ auto rdni = CharacterVector (dimnames[i].size ());
81+ for (int j=0 ; j<rdni.size (); j++)
82+ rdni[j] = dimnames[i][j];
83+ if (all (is_na (rdni)))
7784 rdnames[i] = R_NilValue;
7885 else
79- rdnames[i] = dimnames[i] ;
86+ rdnames[i] = rdni ;
8087 }
8188 auto n = accumulate (rdim.begin (), rdim.end (), 1 , multiplies<int >());
8289 auto result = Vector<RTYPE>(n, fill[0 ]);
0 commit comments