优化的阵列元素计算和设置

时间:2013-04-13 15:58:55

标签: c++ performance cocos2d-x

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

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

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

问题:在c ++中计算实例化对象的值并设置器的最佳方法是什么?

  • 由@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);
}

的main.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

1 个答案:

答案 0 :(得分:1)

关于你的问题没有任何内在的C ++。您似乎只需要遍历整个数组(使用双嵌套循环,第一行,然后是列),并根据到A值字段的距离确定值。 E.g。

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)];