dl
dl_vector2.h
浏览该文件的文档.
1
14#pragma once
15
16#include <cstddef>
17#include <vector>
18#include <span>
19#include <cassert>
20
21#include "dl_type.h"
23
24namespace dl
25{
26
27template<typename T>
29{
30public:
35 _w{ 0 }
36 {}
37
41 Vector2(const Size& size)
42 {
43 _init(size);
44 }
45
49 Vector2(const Size& size, const T* p)
50 {
51 _init(size);
52
53 for (size_t i = 0; i < _data.size(); ++i)
54 {
55 _data[i] = *(p + i);
56 }
57 }
58
62 std::span<T> operator[](std::size_t j)
63 {
64 assert(j < _ptrLine.size());
65 return { _ptrLine[j], _w };
66 }
67
72 /*T& operator[](std::size_t j, std::size_t i)
73 {
74 assert(j < _ptrLine.size());
75 assert(i < _w);
76 return _ptrLine[j][i];
77 }*/
78
79 const T& Get(std::size_t j, std::size_t i) const
80 {
81 assert(j < _ptrLine.size());
82 assert(i < _w);
83 return _ptrLine[j][i];
84 }
85
89 void Fill(const T& v)
90 {
91 for (auto& iter : _data)
92 iter = v;
93 }
94
98 void PushBack(std::vector<T>& vec)
99 {
100 assert(vec.size() == _w);
101 _data.insert(_data.end(), vec.begin(), vec.end());
102 _rebuild_ptr_line(_ptrLine.size() + 1);
103 }
104
105
106 bool IsEmpty() const { return _data.empty(); }
107
108 const std::byte* GetBuffer() const
109 {
110 return (const std::byte*)_data.data();
111 }
112
114 {
115 return _data.data();
116 }
117
118 const T* GetData() const
119 {
120 return _data.data();
121 }
122
123 unsigned GetWidth() const
124 {
125 return _w;
126 }
127
128 unsigned GetHeight() const
129 {
130 return (unsigned)_ptrLine.size();
131 }
132
133 Size GetSize() const
134 {
135 return { GetWidth(), GetHeight() };
136 }
137
138 // 由于标准库 size与 length等价,不使用length命令
139 unsigned GetSizeMul() const
140 {
141 return GetWidth() * GetHeight();
142 }
143
144 size_t GetSizeByte()
145 {
146 return _data.size() * sizeof(T);
147 }
148
149 auto begin()
150 {
151 return _data.begin();
152 }
153 auto end()
154 {
155 return _data.end();
156 }
157private:
158 std::vector<T> _data;
159 unsigned _w;// 宽
160 std::vector<T*> _ptrLine; // 行指针
161
162 void _init(const Size& size)
163 {
164 _w = size[0];
165 _data.resize(Accumulate::Mul(size));
166 _rebuild_ptr_line(size[1]);
167 }
168 // 重建行指针
169 void _rebuild_ptr_line(std::size_t h)
170 {
171 _ptrLine.resize(h);
172 for (size_t i = 0; i < _ptrLine.size(); ++i)
173 _ptrLine[i] = _data.data() + i;
174 }
175};
176
177
178}
unsigned GetWidth() const
Definition: dl_vector2.h:123
bool IsEmpty() const
Definition: dl_vector2.h:106
unsigned GetSizeMul() const
Definition: dl_vector2.h:139
const T * GetData() const
Definition: dl_vector2.h:118
unsigned GetHeight() const
Definition: dl_vector2.h:128
Vector2(const Size &size, const T *p)
以指定大小构造,并传入指针赋值初始化
Definition: dl_vector2.h:49
T * GetData()
Definition: dl_vector2.h:113
const T & Get(std::size_t j, std::size_t i) const
返回第j行 第i个
Definition: dl_vector2.h:79
Size GetSize() const
Definition: dl_vector2.h:133
size_t GetSizeByte()
Definition: dl_vector2.h:144
std::span< T > operator[](std::size_t j)
返回第j行
Definition: dl_vector2.h:62
Vector2()
空构造
Definition: dl_vector2.h:34
void PushBack(std::vector< T > &vec)
插入行
Definition: dl_vector2.h:98
auto end()
Definition: dl_vector2.h:153
auto begin()
Definition: dl_vector2.h:149
const std::byte * GetBuffer() const
Definition: dl_vector2.h:108
Vector2(const Size &size)
以指定大小构造
Definition: dl_vector2.h:41
void Fill(const T &v)
填充值
Definition: dl_vector2.h:89
constexpr auto Mul(const C &container)
求积product
Definition: dl_array2.h:17
PointU Size
Definition: dl_type_def.h:34