五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

【數(shù)據(jù)結(jié)構(gòu)】多維數(shù)組如何實(shí)現(xiàn)按需分配內(nèi)存,自動擴(kuò)容?浮點(diǎn)數(shù)如何作為定點(diǎn)數(shù)壓縮,高

2022-10-04 09:38 作者:不會跑路的小向晚  | 我要投稿

std::unique_ptr<Block>[B1][B1];作為一個大體積成員變量在MSVC上編譯時會內(nèi)存溢出(gcc似乎沒有這個問題)。所以從course-master\10\03\00.cpp開始就需要將std::unique_ptr<Block> m_data[B1][B1];改為using Matrix = std::unique_ptr<Block>[B1][B1];

? ? std::unique_ptr<Matrix> m_data;

完美解決

附上修改之后的代碼:

#include<iostream>

#include"bate.h"


#define N (512*512)


struct Grid{

? ? constexpr static int Bshitft = 8; ? ? ? // 整除用的

? ? constexpr static int B = 1 << Bshitft; ?// 索引用的

? ? constexpr static int Bmask = B - 1; ? ? // 取余用的


? ? constexpr static int B1shitft = 11; ? ? ?

? ? constexpr static int B1 = 1 << B1shitft; ?

? ? constexpr static int B1mask = B1 - 1; ? ?


? ? Grid(): m_data((Matrix*)new Matrix){};


? ? struct Block{

? ? ? ? char m_block[B][B];

? ? };


? ? using Matrix = std::unique_ptr<Block>[B1][B1];

? ? std::unique_ptr<Matrix> m_data;


? ? char read(int x, int y) const{

? ? ? ? const std::unique_ptr<Block>& block = (*m_data)[(x >> Bshitft) & B1mask][(y >> Bshitft) & B1mask]; // 因?yàn)閞ead是const函數(shù),所以類屬性的引用需要用const接收

? ? ? ? if(!block) return 0; ? ?// 如果塊指針沒有激活,返回0

? ? ? ? return block->m_block[x & Bmask][y & Bmask];

? ? }


? ?void write(int x, int y, char value){

? ? ? ?std::unique_ptr<Block>& block = (*m_data)[(x >> Bshitft) & B1mask][(y >> Bshitft) & B1mask];

? ? ? ?if(!block) block = std::make_unique<Block>(); ? ?// 如果指針沒有激活,則新建一個塊

? ? ? ?block->m_block[x & Bmask][y & Bmask] = value;

? ?}


? ?template<class Func>

? ?void foreach(const Func& func){

? ? ? ?for(int x1 = 0; x1 < B1; x1++){ ?// 遍歷每個block pointer

? ? ? ? ? ?for(int y1 = 0; y1 < B1; y1++){

? ? ? ? ? ? ? ?const std::unique_ptr<Block>& block = (*m_data)[x1 & B1mask][y1 & B1mask];

? ? ? ? ? ? ? ?if(!block) continue;

? ? ? ? ? ? ? ?int xb = x1 << Bshitft, yb = y1 << Bshitft;

? ? ? ? ? ? ? ? for(int dx = 0; dx < B; dx++){

? ? ? ? ? ? ? ? ? ? for(int dy = 0; dy < B; dy++){

? ? ? ? ? ? ? ? ? ? ? ? func(xb | dx, yb | dy, block->m_block[dx][dy]);

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ?}

? ? ? ?}

? ?}

};


int main(){

? ? bate::timing("main");


? ? Grid* a = new Grid{};


? ? float px = -100.f, py = 100.f;

? ? float vx = 0.2f, vy = -0.6f;


? ? for(int step = 0; step < N; step++){

? ? ? ? px += vx;

? ? ? ? py += vy;

? ? ? ? int x = (int)std::floor(px);

? ? ? ? int y = (int)std::floor(py);

? ? ? ? a->write(x, y, 1);

? ? }


? ? int count = 0;

? ? a->foreach([&](int x, int y, char& value){

? ? ? ? if(value != 0) ++count;

? ? });


? ? bate::timing("main");

? ? printf("Count=%d", count);

? ? return 0;

}


【數(shù)據(jù)結(jié)構(gòu)】多維數(shù)組如何實(shí)現(xiàn)按需分配內(nèi)存,自動擴(kuò)容?浮點(diǎn)數(shù)如何作為定點(diǎn)數(shù)壓縮,高的評論 (共 條)

分享到微博請遵守國家法律
二连浩特市| 休宁县| 哈尔滨市| 武鸣县| 昭觉县| 缙云县| 镇平县| 宜宾市| 惠来县| 白城市| 兴城市| 金华市| 永泰县| 文山县| 田林县| 翁源县| 东平县| 南康市| 盐城市| 长治市| 泾源县| 河东区| 延川县| 株洲县| 合阳县| 延津县| 怀仁县| 遵义市| 吉安市| 吉首市| 通化市| 东乡族自治县| 大荔县| 巴彦县| 开远市| 泗洪县| 绿春县| 申扎县| 准格尔旗| 南澳县| 西乌|