dl
dl_vector2.h
浏览该文件的文档.
1
15#pragma once
16
17#include <cstddef>
18#include <vector>
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
63 void Resize(const Size& size)
64 {
65 assert(_w == 0);
66 _init(size);
67 }
68
72 T* operator[](std::size_t j)
73 {
74 assert(j < _ptrLine.size());
75 return _ptrLine[j];
76 }
80 const T* operator[](std::size_t j) const
81 {
82 assert(j < _ptrLine.size());
83 return _ptrLine[j];
84 }
85
90 /*T& operator[](std::size_t j, std::size_t i)
91 {
92 assert(j < _ptrLine.size());
93 assert(i < _w);
94 return _ptrLine[j][i];
95 }*/
96
97 const T& Get(size_t j, size_t i) const
98 {
99 assert(j < _ptrLine.size());
100 assert(i < _w);
101 return _ptrLine[j][i];
102 }
103
104 void Set(size_t j, size_t i, const T& v)
105 {
106 assert(j < _ptrLine.size());
107 assert(i < _w);
108 _ptrLine[j][i] = v;
109 }
110
114 void Fill(const T& v)
115 {
116 for (auto& iter : _data)
117 iter = v;
118 }
119
124 void PushBack(std::vector<T>& vec)
125 {
126 assert(vec.size() == _w);
127 _data.insert(_data.end(), vec.begin(), vec.end());
128 _rebuild_ptr_line(_ptrLine.size() + 1);
129 }
130
131
132 bool IsEmpty() const { return _data.empty(); }
133
134 const std::byte* GetBuffer() const
135 {
136 return (const std::byte*)_data.data();
137 }
138
140 {
141 return _data.data();
142 }
143
144 const T* GetData() const
145 {
146 return _data.data();
147 }
148
149 unsigned GetWidth() const
150 {
151 return _w;
152 }
153
154 unsigned GetHeight() const
155 {
156 return (unsigned)_ptrLine.size();
157 }
158
159 Size GetSize() const
160 {
161 return { GetWidth(), GetHeight() };
162 }
163
164 // 由于标准库 size与 length等价,不使用length命令
165 unsigned GetSizeMul() const
166 {
167 return GetWidth() * GetHeight();
168 }
169
170 size_t GetSizeByte()
171 {
172 return _data.size() * sizeof(T);
173 }
174
175 auto begin()
176 {
177 return _data.begin();
178 }
179 auto end()
180 {
181 return _data.end();
182 }
183private:
184 std::vector<T> _data;
185 unsigned _w;// 宽
186 std::vector<T*> _ptrLine; // 行指针
187
188 void _init(const Size& size)
189 {
190 _w = size[0];
191 _data.resize(Accumulate::Mul(size));
192 _rebuild_ptr_line(size[1]);
193 }
194 // 重建行指针
195 void _rebuild_ptr_line(std::size_t h)
196 {
197 _ptrLine.resize(h);
198 for (std::size_t i = 0; i < _ptrLine.size(); ++i)
199 _ptrLine[i] = _data.data() + i * _w;
200 }
201};
202
203
204}
unsigned GetWidth() const
Definition: dl_vector2.h:149
void Resize(const Size &size)
调整为指定大小
Definition: dl_vector2.h:63
void Set(size_t j, size_t i, const T &v)
Definition: dl_vector2.h:104
bool IsEmpty() const
Definition: dl_vector2.h:132
unsigned GetSizeMul() const
Definition: dl_vector2.h:165
const T & Get(size_t j, size_t i) const
返回第j行 第i个
Definition: dl_vector2.h:97
const T * GetData() const
Definition: dl_vector2.h:144
unsigned GetHeight() const
Definition: dl_vector2.h:154
Vector2(const Size &size, const T *p)
以指定大小构造,并传入指针赋值初始化
Definition: dl_vector2.h:49
T * GetData()
Definition: dl_vector2.h:139
Size GetSize() const
Definition: dl_vector2.h:159
size_t GetSizeByte()
Definition: dl_vector2.h:170
T * operator[](std::size_t j)
返回第j行
Definition: dl_vector2.h:72
Vector2()
空构造
Definition: dl_vector2.h:34
void PushBack(std::vector< T > &vec)
插入行
Definition: dl_vector2.h:124
auto end()
Definition: dl_vector2.h:179
const T * operator[](std::size_t j) const
返回第j行
Definition: dl_vector2.h:80
auto begin()
Definition: dl_vector2.h:175
const std::byte * GetBuffer() const
Definition: dl_vector2.h:134
Vector2(const Size &size)
以指定大小构造
Definition: dl_vector2.h:41
void Fill(const T &v)
填充值
Definition: dl_vector2.h:114
通用类型
constexpr auto Mul(const C &container)
求积product
Definition: dl_array2.h:18
PointU Size
Definition: dl_type_def.h:34