快速填充整個數組這樣做fill_n
¹:
std::fill_n(a.data(), a.num_elements(), 0);
有了提升multi_array
您可以使用以您自己的內存緩衝區,以獲得相同的性能(std::uninitialized_copy
是你的朋友)。 (實際上,您甚至可以在現有內存上映射數組視圖,並且希望保留現有值)。
我寫了一個比較演示這個位置:pointers to a class in dynamically allocated boost multi_array, not compiling
Live On Coliru
#include <boost/multi_array.hpp>
#include <type_traits>
#include <memory>
struct octreenode { int a; int b; };
class world {
public:
world(double x, double y, double z, int widtheast, int widthnorth, int height)
:
originx(x), originy(y), originz(z),
chunkseast(widtheast), chunksnorth(widthnorth), chunksup(height)
{
#define OPTION 4
#if OPTION == 1
static_assert(std::is_trivially_destructible<octreenode>::value, "assumption made");
//std::uninitialized_fill_n(chunk.data(), chunk.num_elements(), octreenode {1, 72});
std::fill_n(chunk.data(), chunk.num_elements(), octreenode {1, 72});
#elif OPTION == 2
for(auto a:chunk) for(auto b:a) for(auto&c:b) c = octreenode{1, 72};
#elif OPTION == 3
for (index cz = 0; cz < chunksnorth; ++cz) {
for (index cx = 0; cx < chunkseast; ++cx) {
for (index cy = 0; cy < chunksup; ++cy) {
chunk[cz][cx][cy] = octreenode{1, 72};
}
}
}
#elif OPTION == 4
static_assert(std::is_trivially_destructible<octreenode>::value, "assumption made");
for (index cz = 0; cz < chunksnorth; ++cz) {
for (index cx = 0; cx < chunkseast; ++cx) {
for (index cy = 0; cy < chunksup; ++cy) {
new (&chunk[cz][cx][cy]) octreenode{1, 72};
}
}
}
#endif
(void) originx, (void) originy, (void) originz, (void) chunksup, (void) chunkseast, (void) chunksnorth;
}
private:
double originx, originy, originz;
int chunkseast, chunksnorth, chunksup;
#if 1
typedef boost::multi_array<octreenode, 3> planetchunkarray; // a boost_multi for chunks
typedef planetchunkarray::index index;
planetchunkarray chunk{boost::extents[chunksnorth][chunkseast][chunksup]};
#else
static_assert(boost::is_trivially_destructible<octreenode>::value, "assumption made");
std::unique_ptr<octreenode[]> raw { new octreenode[chunksnorth*chunkseast*chunksup] };
typedef boost::multi_array_ref<octreenode, 3> planetchunkarray;
typedef planetchunkarray::index index;
planetchunkarray chunk{raw.get(), boost::extents[chunksnorth][chunkseast][chunksup]};
#endif
};
int main() {
world w(1,2,3,4,5,6);
}
使用multi_array_ref
的變體是如何避免拷貝構造元素的例子(這是類似於std::vector
使用未初始化的內存用於保留但未使用的元素的優化)。
¹課程的唯一值,使用std::iota
或std::generate
請注意'std :: vector'的行爲取決於您使用的C++版本!這是C++ 98和C++ 11標準之間的變化之一。 (在C++ 98中,第一個元素是構建的,其餘的都是複製的,在C++ 11中都是構建的)。 'multi_array'的行爲與舊的C++ 98'std :: vector'一致,並且在過去一致。可能需要更新(一致)版本的'multi_array'。 – alfC 2015-07-02 18:40:30