301 lines
4.9 KiB
C++
301 lines
4.9 KiB
C++
/**
|
||
* @file dl_vector2.h
|
||
* @brief 2维vector
|
||
*
|
||
*
|
||
* @version 1.0
|
||
* @author lveyou
|
||
* @date 23-07-09
|
||
*
|
||
* @note 提供行指针,避免乘法计算
|
||
* @note 设计为深拷贝,与std::vector一致
|
||
* @note 访问以y,x
|
||
* @note 注意auto迭代是全部,而不是按行
|
||
*/
|
||
#pragma once
|
||
|
||
#include <cstddef>
|
||
#include <vector>
|
||
#include <cassert>
|
||
#include <span>
|
||
|
||
#include "dl_type.h"
|
||
#include "base/dl_range_accumulate.h"
|
||
#include "math/dl_math.h"
|
||
|
||
namespace dl
|
||
{
|
||
//! 2维vector
|
||
template<typename T, typename Index = unsigned>
|
||
class Vector2
|
||
{
|
||
public:
|
||
using Size = Array<Index, 2>;
|
||
using PointU = Size;
|
||
/**
|
||
* @brief 空构造
|
||
*/
|
||
Vector2() :
|
||
_w{ 0 }
|
||
{}
|
||
|
||
/**
|
||
* @brief 以指定大小构造
|
||
*/
|
||
Vector2(const Size& size)
|
||
{
|
||
_init(size);
|
||
}
|
||
|
||
/**
|
||
* @brief 以指定大小构造,并传入指针赋值初始化
|
||
*/
|
||
Vector2(const Size& size, const T* p)
|
||
{
|
||
_init(size);
|
||
|
||
for (size_t i = 0; i < _data.size(); ++i)
|
||
{
|
||
_data[i] = *(p + i);
|
||
}
|
||
}
|
||
/**
|
||
* @brief 复制构造 todo
|
||
*/
|
||
Vector2(const Vector2& b)
|
||
{
|
||
*this = b;
|
||
_rebuild_ptr_line((Index)b._ptrLine.size());
|
||
}
|
||
|
||
/**
|
||
* @brief 调整为指定大小
|
||
* @note 保留原有值
|
||
*/
|
||
void Resize(const Size& size)
|
||
{
|
||
Vector2 temp{ *this };
|
||
|
||
_init(size);
|
||
Fill({});
|
||
|
||
CopyCross(temp);
|
||
}
|
||
|
||
/**
|
||
* @brief 返回第j行
|
||
*/
|
||
std::span<T> operator[](Index j)
|
||
{
|
||
assert(j < _ptrLine.size());
|
||
return { _ptrLine[j], _w };
|
||
}
|
||
/**
|
||
* @brief 返回第j行
|
||
*/
|
||
const std::span<T> operator[](Index j) const
|
||
{
|
||
assert(j < _ptrLine.size());
|
||
return { _ptrLine[j], _w };
|
||
}
|
||
|
||
/**
|
||
* @brief 返回第j行 第i个
|
||
* @note 与传统[]访问一致,先访问行
|
||
*/
|
||
/*T& operator[](std::size_t j, std::size_t i)
|
||
{
|
||
assert(j < _ptrLine.size());
|
||
assert(i < _w);
|
||
return _ptrLine[j][i];
|
||
}*/
|
||
|
||
const T& Get(Index j, Index i) const
|
||
{
|
||
assert(j < _ptrLine.size());
|
||
assert(i < _w);
|
||
return _ptrLine[j][i];
|
||
}
|
||
const T& Get(const PointU& xy) const
|
||
{
|
||
return Get(xy[1], xy[0]);
|
||
}
|
||
T& GetRef(Index j, Index i)
|
||
{
|
||
assert(j < _ptrLine.size());
|
||
assert(i < _w);
|
||
return _ptrLine[j][i];
|
||
}
|
||
T& GetRef(const PointU& xy)
|
||
{
|
||
return GetRef(xy[1], xy[0]);
|
||
}
|
||
/**
|
||
* @brief 返回元素地址,失败返回空
|
||
*/
|
||
T* GetPointer(Index j, Index i)
|
||
{
|
||
if (j >= _ptrLine.size()
|
||
|| i >= _w)
|
||
return nullptr;
|
||
return &_ptrLine[j][i];
|
||
}
|
||
T* GetPointer(const PointU& xy)
|
||
{
|
||
return GetPointer(xy[1], xy[0]);
|
||
}
|
||
T* GetPointerOffset(const PointU& xy, const Point& offset)
|
||
{
|
||
Point xy_out = toPoint(xy) + offset;
|
||
if (xy_out[0] < 0 || xy_out[1] < 0)
|
||
return nullptr;
|
||
return GetPointer((Index)xy_out[1], (Index)xy_out[0]);
|
||
}
|
||
|
||
void Set(Index j, Index i, const T& v)
|
||
{
|
||
assert(j < _ptrLine.size());
|
||
assert(i < _w);
|
||
_ptrLine[j][i] = v;
|
||
}
|
||
|
||
/**
|
||
* @brief 填充值
|
||
*/
|
||
void Fill(const T& v)
|
||
{
|
||
for (auto& iter : _data)
|
||
iter = v;
|
||
}
|
||
void Clear()
|
||
{
|
||
_data.clear();
|
||
_w = 0;
|
||
_ptrLine.clear();
|
||
}
|
||
/**
|
||
* @brief 从b拷贝重复的面积
|
||
* @note 其余地方不变
|
||
*/
|
||
void CopyCross(const Vector2& b)
|
||
{
|
||
Index i_max = (std::min)(_w, b._w);
|
||
Index j_max = (Index)(std::min)(_ptrLine.size(), b._ptrLine.size());
|
||
for(Index j = 0; j < j_max; ++j)
|
||
for (Index i = 0; i < i_max; ++i)
|
||
{
|
||
GetRef(j, i) = b.Get(j, i);
|
||
}
|
||
}
|
||
/**
|
||
* @brief 插入行
|
||
* @note 会使 y+1
|
||
*/
|
||
void PushBack(std::vector<T>& vec)
|
||
{
|
||
assert(vec.size() == _w);
|
||
_data.insert(_data.end(), vec.begin(), vec.end());
|
||
_rebuild_ptr_line((Index)_ptrLine.size() + 1);
|
||
}
|
||
|
||
|
||
bool IsEmpty() const { return _data.empty(); }
|
||
|
||
const std::byte* GetBuffer() const
|
||
{
|
||
return (const std::byte*)_data.data();
|
||
}
|
||
|
||
T* GetData()
|
||
{
|
||
return _data.data();
|
||
}
|
||
|
||
const T* GetData() const
|
||
{
|
||
return _data.data();
|
||
}
|
||
|
||
Index GetWidth() const
|
||
{
|
||
return _w;
|
||
}
|
||
|
||
Index GetHeight() const
|
||
{
|
||
return (Index)_ptrLine.size();
|
||
}
|
||
|
||
Size GetSize() const
|
||
{
|
||
return { GetWidth(), GetHeight() };
|
||
}
|
||
|
||
// 由于标准库 size与 length等价,不使用length命令
|
||
Index GetSizeMul() const
|
||
{
|
||
return GetWidth() * GetHeight();
|
||
}
|
||
|
||
Index GetSizeByte()
|
||
{
|
||
return (Index)(_data.size() * sizeof(T));
|
||
}
|
||
|
||
auto begin()
|
||
{
|
||
return _data.begin();
|
||
}
|
||
auto end()
|
||
{
|
||
return _data.end();
|
||
}
|
||
void reserve(size_t i)
|
||
{
|
||
_data.reserve(i);
|
||
}
|
||
void emplace_back(T&& t)
|
||
{
|
||
_data.emplace_back(t);
|
||
}
|
||
size_t size() const
|
||
{
|
||
return _data.size();
|
||
}
|
||
// 顺时针旋转90度
|
||
static Vector2 RotateRightAngle1(const Vector2& v)
|
||
{
|
||
Vector2 ret;
|
||
ret.Resize({ v.GetHeight(), v.GetWidth() });
|
||
|
||
for (Index j = 0; j < v.GetHeight(); ++j)
|
||
{
|
||
for (Index i = 0; i < v.GetWidth(); ++i)
|
||
{
|
||
ret[i][v.GetHeight() - j - 1] = v[j][i];
|
||
}
|
||
}
|
||
return ret;
|
||
}
|
||
private:
|
||
std::vector<T> _data;
|
||
Index _w;// 宽
|
||
std::vector<T*> _ptrLine; // 行指针
|
||
|
||
void _init(const Size& size)
|
||
{
|
||
_w = size[0];
|
||
_data.resize(Accumulate::Mul(size));
|
||
_rebuild_ptr_line(size[1]);
|
||
}
|
||
// 重建行指针
|
||
void _rebuild_ptr_line(Index h)
|
||
{
|
||
_ptrLine.resize(h);
|
||
for (Index i = 0; i < _ptrLine.size(); ++i)
|
||
_ptrLine[i] = _data.data() + i * _w;
|
||
}
|
||
};
|
||
|
||
|
||
} |