What is the best way to create a sparse array in C++?

后端 未结 11 2180
心在旅途
心在旅途 2020-11-29 01:45

I am working on a project that requires the manipulation of enormous matrices, specifically pyramidal summation for a copula calculation.

In short, I need to keep

11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 01:52

    I would suggest doing something like:

    typedef std::tuple coord_t;
    typedef boost::hash coord_hash_t;
    typedef std::unordered_map sparse_array_t;
    
    sparse_array_t the_data;
    the_data[ { x, y, z } ] = 1; /* list-initialization is cool */
    
    for( const auto& element : the_data ) {
        int xx, yy, zz, val;
        std::tie( std::tie( xx, yy, zz ), val ) = element;
        /* ... */
    }
    

    To help keep your data sparse, you might want to write a subclass of unorderd_map, whose iterators automatically skip over (and erase) any items with a value of 0.

提交回复
热议问题