Reorder vector using a vector of indices

前端 未结 14 1685
耶瑟儿~
耶瑟儿~ 2020-11-28 06:15

I\'d like to reorder the items in a vector, using another vector to specify the order:

char   A[]     = { \'a\', \'b\', \'c\' };
size_t ORDER[] = { 1, 0, 2 }         


        
相关标签:
14条回答
  • 2020-11-28 06:33

    If it is ok to modify the ORDER array then an implementation that sorts the ORDER vector and at each sorting operation also swaps the corresponding values vector elements could do the trick, I think.

    0 讨论(0)
  • 2020-11-28 06:33

    It is not clear by the title and the question if the vector should be ordered with the same steps it takes to order vOrder or if vOrder already contains the indexes of the desired order. The first interpretation has already a satisfying answer (see chmike and Potatoswatter), I add some thoughts about the latter. If the creation and/or copy cost of object T is relevant

    template <typename T>
    void reorder( std::vector<T> & data, std::vector<std::size_t> & order )
    {
     std::size_t i,j,k;
      for(i = 0; i < order.size() - 1; ++i) {
        j = order[i];
        if(j != i) {
          for(k = i + 1; order[k] != i; ++k);
          std::swap(order[i],order[k]);
          std::swap(data[i],data[j]);
        }
      }
    }
    

    If the creation cost of your object is small and memory is not a concern (see dribeas):

    template <typename T>
    void reorder( std::vector<T> & data, std::vector<std::size_t> const & order )
    {
     std::vector<T> tmp;         // create an empty vector
     tmp.reserve( data.size() ); // ensure memory and avoid moves in the vector
     for ( std::size_t i = 0; i < order.size(); ++i ) {
      tmp.push_back( data[order[i]] );
     }
     data.swap( tmp );          // swap vector contents
    }
    

    Note that the two pieces of code in dribeas answer do different things.

    0 讨论(0)
  • 2020-11-28 06:34

    Your code is broken. You cannot assign to vA and you need to use template parameters.

    vector<char> REORDER(const vector<char>& vA, const vector<size_t>& vOrder)  
    {   
        assert(vA.size() == vOrder.size());  
        vector<char> vCopy(vA.size()); 
        for(int i = 0; i < vOrder.size(); ++i)  
            vCopy[i] = vA[ vOrder[i] ];  
        return vA;
    } 
    

    The above is slightly more efficient.

    0 讨论(0)
  • 2020-11-28 06:34

    I was trying to use @Potatoswatter's solution to sort multiple vectors by a third one and got really confused by output from using the above functions on a vector of indices output from Armadillo's sort_index. To switch from a vector output from sort_index (the arma_inds vector below) to one that can be used with @Potatoswatter's solution (new_inds below), you can do the following:

    vector<int> new_inds(arma_inds.size());
    for (int i = 0; i < new_inds.size(); i++) new_inds[arma_inds[i]] = i;
    
    0 讨论(0)
  • 2020-11-28 06:34

    It's an interesting intellectual exercise to do the reorder with O(1) space requirement but in 99.9% of the cases the simpler answer will perform to your needs:

    void permute(vector<T>& values, const vector<size_t>& indices)  
    {   
        vector<T> out;
        out.reserve(indices.size());
        for(size_t index: indices)
        {
            assert(0 <= index && index < values.size());
            out.push_back(values[index]);
        }
        values = std::move(out);
    }
    

    Beyond memory requirements, the only way I can think of this being slower would be due to the memory of out being in a different cache page than that of values and indices.

    0 讨论(0)
  • 2020-11-28 06:36

    This algorithm is based on chmike's, but the vector of reorder indices is const. This function agrees with his for all 11! permutations of [0..10]. The complexity is O(N^2), taking N as the size of the input, or more precisely, the size of the largest orbit.

    See below for an optimized O(N) solution which modifies the input.

    template< class T >
    void reorder(vector<T> &v, vector<size_t> const &order )  {   
        for ( int s = 1, d; s < order.size(); ++ s ) {
            for ( d = order[s]; d < s; d = order[d] ) ;
            if ( d == s ) while ( d = order[d], d != s ) swap( v[s], v[d] );
        }
    }
    

    Here's an STL style version which I put a bit more effort into. It's about 47% faster (that is, almost twice as fast over [0..10]!) because it does all the swaps as early as possible and then returns. The reorder vector consists of a number of orbits, and each orbit is reordered upon reaching its first member. It's faster when the last few elements do not contain an orbit.

    template< typename order_iterator, typename value_iterator >
    void reorder( order_iterator order_begin, order_iterator order_end, value_iterator v )  {   
        typedef typename std::iterator_traits< value_iterator >::value_type value_t;
        typedef typename std::iterator_traits< order_iterator >::value_type index_t;
        typedef typename std::iterator_traits< order_iterator >::difference_type diff_t;
        
        diff_t remaining = order_end - 1 - order_begin;
        for ( index_t s = index_t(), d; remaining > 0; ++ s ) {
            for ( d = order_begin[s]; d > s; d = order_begin[d] ) ;
            if ( d == s ) {
                -- remaining;
                value_t temp = v[s];
                while ( d = order_begin[d], d != s ) {
                    swap( temp, v[d] );
                    -- remaining;
                }
                v[s] = temp;
            }
        }
    }
    

    And finally, just to answer the question once and for all, a variant which does destroy the reorder vector (filling it with -1's). For permutations of [0..10], It's about 16% faster than the preceding version. Because overwriting the input enables dynamic programming, it is O(N), asymptotically faster for some cases with longer sequences.

    template< typename order_iterator, typename value_iterator >
    void reorder_destructive( order_iterator order_begin, order_iterator order_end, value_iterator v )  {
        typedef typename std::iterator_traits< value_iterator >::value_type value_t;
        typedef typename std::iterator_traits< order_iterator >::value_type index_t;
        typedef typename std::iterator_traits< order_iterator >::difference_type diff_t;
        
        diff_t remaining = order_end - 1 - order_begin;
        for ( index_t s = index_t(); remaining > 0; ++ s ) {
            index_t d = order_begin[s];
            if ( d == (diff_t) -1 ) continue;
            -- remaining;
            value_t temp = v[s];
            for ( index_t d2; d != s; d = d2 ) {
                swap( temp, v[d] );
                swap( order_begin[d], d2 = (diff_t) -1 );
                -- remaining;
            }
            v[s] = temp;
        }
    }
    
    0 讨论(0)
提交回复
热议问题