默认情况下,纯静态/动态数组(或它们的包装器)是最可取的:它们对程序员(随机访问 API 等)和处理器(内存局部性等)来说都是最舒服的。
Block
数组/向量中最容易实现的布局是[first row Blocks..., second row Blocks..., etc]
- 充当二维数组的一维数组。它可以像 一样被索引crossword[x + y * crossword.width()]
,这并不漂亮,因此您可能希望使用带有 API 的库/自行编写的包装器,crossword(x, y)
它可以在后台执行该xy
-to- i
-index 转换。
也许是这样的:
class Crossword {
std::vector<Block> raw;
size_t length{}; // can't name it "width" because there's a "width()" member function below
public:
Crossword() {}
Crossword(size_t x, size_t y): raw(x * y), length{x} {}
Crossword(Crossword&& other) noexcept { *this = std::move(other); }
Crossword& operator=(Crossword&& other) noexcept {
std::swap(raw, other.raw);
std::swap(length, other.length);
return *this;
}
auto size() const { return raw.size(); }
auto width() const { return length; }
auto height() const { return size() / length; }
auto& operator()(size_t x, size_t y) const { return raw[x + y * length]; }
auto& operator()(size_t x, size_t y) { return raw[x + y * length]; }
};