c++ - 优化数组元素的计算和设置

标签 c++ performance cocos2d-x

有一个实例化对象的二维数组,11 X 11 字段。

一个或两个字段可以设置为主要字段,这意味着该字段符合 A 值。主场成为相对坐标系的中心,其中其附近的邻居(X 和 Y 上的 +/-1)符合 B 值,其他四个级别的邻居符合 C 值。其余字段(此 11 行/列示例中没有字段)符合 D - 默认值。 -1 的索引实际上是索引 11,因此索引 0 是索引 12。

如果数组中有两个主要字段,则设置更高等级的值。

问题:在 C++ 中为实例化对象的值计算和赋能 setter 的最佳方式是什么?

  • @rhalbersma 提供的第一个示例,更改为适合条件

PrimaryFieldsNet.h

#ifndef PRIMARYFIELDSNET_H
#define PRIMARYFIELDSNET_H

#define ABS(X) ((X) > 0 ? (X) : (-(X)))
#include <iostream>

using namespace std;

typedef pair<int, int> Point;

class PrimaryFieldsNet
{
    private:
        int m_rows;
        int m_grid[11][11];
        PrimaryFieldsNet() { }
        int distance_x(Point const& a, Point const& b);
        int distance_y(Point const& a, Point const& b);
        int delta_x(Point const& a, Point const& b);
        int delta_y(Point const& a, Point const& b);
        int calculate(Point const& a, Point const& b);
        void update_grid(Point const& pry_p1);
        void update_grid(Point const& pry_p1, Point const& pry_p2);

    public:
        PrimaryFieldsNet(int rows);
        void setup();
        void setup(Point const& pry_p1);
        void setup(Point const& pry_p1, Point const& pry_p2);
        int* grid();
};

#endif

PrimaryFieldsNet.cpp

#include "PrimaryFieldsNet.h"

PrimaryFieldsNet::PrimaryFieldsNet(int rows)
{
    m_rows = rows;
}

int* PrimaryFieldsNet::grid()
{
    //cout << m_grid << endl;
    return *m_grid;
}

int PrimaryFieldsNet::distance_x(Point const& a, Point const& b)
{
    return a.first - b.first;
}

int PrimaryFieldsNet::distance_y(Point const& a, Point const& b)
{
    return a.second - b.second;
}

int PrimaryFieldsNet::delta_x(Point const& a, Point const& b)
{
    int d_x;

    d_x = distance_x(a, b);

    if (d_x < -(m_rows-1)/2)
        d_x = a.first + m_rows - b.first;
    else if (d_x > (m_rows-1)/2)
        d_x = ABS(a.first - m_rows - b.first);
    else
        d_x = ABS(d_x);

    return d_x;
}

int PrimaryFieldsNet::delta_y(Point const& a, Point const& b)
{
    int d_y;

    d_y = distance_y(a, b);

    if (d_y < -(m_rows-1)/2)
        d_y = a.second + m_rows - b.second;
    else if (d_y > (m_rows-1)/2)
        d_y = ABS(a.second - m_rows - b.second);
    else
        d_y = ABS(d_y);

    return d_y;
}

int PrimaryFieldsNet::calculate(Point const& a, Point const& b)
{
    int dmax, result;

    dmax = max(delta_x(a, b), delta_y(a, b));

    if (dmax < 2)
        result = dmax;
    else if (dmax < (m_rows+1)/2)
        result = 2;
    else
        result = 3;

    return result;
}

void PrimaryFieldsNet::update_grid(Point const& pry_p1)
{
    for (int x = 0; x < m_rows; ++x)
        for (int y = 0; y < m_rows; ++y)
            m_grid[x][y] = calculate(Point(x,y), pry_p1);
}

void PrimaryFieldsNet::update_grid(Point const& pry_p1, Point const& pry_p2)
{
    for (int x = 0; x < m_rows; ++x)
        for (int y = 0; y < m_rows; ++y)
            m_grid[x][y] = min(calculate(Point(x,y), pry_p1), calculate(Point(x,y), pry_p2));
}

void PrimaryFieldsNet::setup()
{
    Point pry_p1((m_rows-1)/2,(m_rows-1)/2);
    update_grid(pry_p1);
}

void PrimaryFieldsNet::setup(Point const& pry_p1)
{
    update_grid(pry_p1);
}

void PrimaryFieldsNet::setup(Point const& pry_p1, Point const& pry_p2)
{
    update_grid(pry_p1, pry_p2);
}

主要.cpp

#include <iostream>
#include "PrimaryFieldsNet.h"

using namespace std;

int main()
{
    int* grid;

    PrimaryFieldsNet pfnet(11);
    //pfnet.setup(Point(5,2));
    pfnet.setup(Point(10,6), Point(2,2));
    grid = pfnet.grid();

    for (int x = 0; x < 11; ++x) {
        for (int y = 0; y < 11; ++y)
            cout << *(grid + x*11 + y) << " ";
        cout << "\n";
        }

    return 0;
}

示例输出为:

2 2 2 2 2 1 1 1 2 2 2 
2 1 1 1 2 2 2 2 2 2 2 
2 1 0 1 2 2 2 2 2 2 2 
2 1 1 1 2 2 2 2 2 2 2 
2 2 2 2 2 2 2 2 2 2 2 
2 2 2 2 2 2 2 2 2 2 2 
2 2 2 2 2 2 2 2 2 2 2 
2 2 2 2 2 2 2 2 2 2 2 
2 2 2 2 2 2 2 2 2 2 2 
2 2 2 2 2 1 1 1 2 2 2 
2 2 2 2 2 1 0 1 2 2 2

最佳答案

您的问题本质上与 C++ 无关。看来您只需要遍历整个数组(使用双重嵌套循环,先行,然后列),并根据到 A 值字段的距离确定值。例如

SomeValueType values[4] = { A, B, C, D };

typedef std::pair<int, int> Point;
Point grid[12][12];
// allocate memory for grid  

int distance(Point const& a, Point const& b)
{
    int delta.x = a.first - b.first;
    int delta.y = a.second - b.second;
    return std::max(std::abs(delta.x), std::abs(delta.y));
}

Point center(0,0);

for (int x = 0; x < 12; ++x)
    for (int y = 0; y < 12; ++y)              
        grid[x][y] = values[dist(Point(x,y), center)];

关于c++ - 优化数组元素的计算和设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15989749/

相关文章:

c# - 根据某些符号的子字符串

r - 如何在 R 中优化 sapply 以计算数据帧上的运行总计

c++ - CMFCPropertyGridProperty 数字输入

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?

java - 对 Java8 Stream 性能感到困惑

cocos2d-x - cocos2d-x 如何在两层之间交换数据?

c++ - 关闭 UIViewController 时出现 cocos2dx OpenGL 错误 0x0506

c++ - Cocos2d-x V3.2。使用 RenderTexture 创建 Sprite

c++ - 我如何从西里尔字符串中获取一个字符 C++

c++ - 将函数和成员函数传递给模板方法