12 #ifndef SMALLMATRIX_TYPE_HPP 13 #define SMALLMATRIX_TYPE_HPP 49 SmallMatrix() : data(NULL), width(0), height(0), capacity(0) {}
80 if (&other ==
this)
return;
82 this->height = other.Height();
83 this->width = other.Width();
84 uint num_items = this->width * this->
height;
85 if (num_items > this->capacity) {
86 this->capacity = num_items;
88 this->data = MallocT<T>(num_items);
89 MemCpyT(this->data, other[0], num_items);
90 }
else if (num_items > 0) {
91 MemCpyT(this->data, other[0], num_items);
123 uint capacity = this->height * this->
width;
124 if (capacity >= this->capacity)
return;
126 this->data =
ReallocT(this->data, this->capacity);
135 if (x < --this->width) {
136 MemCpyT<T>(this->data + x * this->
height,
137 this->data + this->width * this->
height,
149 if (count == 0)
return;
150 assert(x < this->width);
151 assert(x + count <= this->width);
152 this->width -= count;
153 uint to_move = (this->width - x) * this->height;
155 MemMoveT(this->data + x * this->height,
156 this->data + (x + count) * this->height, to_move);
166 if (y < this->height - 1) {
167 for (uint x = 0; x < this->
width; ++x) {
168 this->data[x * this->height + y] =
169 this->data[(x + 1) * this->height - 1];
172 this->
Resize(this->width, this->height - 1);
182 if (this->height > count + y) {
183 for (uint x = 0; x < this->
width; ++x) {
184 MemMoveT(this->data + x * this->height + y,
185 this->data + x * this->height + y + count,
186 this->height - count - y);
189 this->
Resize(this->width, this->height - count);
198 this->
Resize(this->width, to_add + this->height);
207 this->
Resize(to_add + this->width, this->height);
216 inline void Resize(uint new_width, uint new_height)
218 uint new_capacity = new_width * new_height;
220 void (*copy)(T *dest,
const T *src,
size_t count) = NULL;
221 if (new_capacity > this->capacity) {
223 new_data = MallocT<T>(new_capacity);
227 new_data = this->
data;
230 if (this->height != new_height || new_data != this->data) {
231 if (this->height > 0) {
232 if (new_height > this->height) {
235 for (uint x = this->width; x > 0; --x) {
236 if (x * new_height > new_capacity)
continue;
237 (*copy)(new_data + (x - 1) * new_height,
238 this->data + (x - 1) * this->
height,
239 min(this->height, new_height));
243 for (uint x = 0; x < this->
width; ++x) {
244 if ((x + 1) * new_height > new_capacity)
break;
245 (*copy)(new_data + x * new_height,
246 this->data + x * this->
height,
247 min(this->height, new_height));
251 this->height = new_height;
252 if (new_data != this->data) {
254 this->data = new_data;
255 this->capacity = new_capacity;
258 this->width = new_width;
261 inline uint Height()
const 266 inline uint Width()
const 278 inline const T &
Get(uint x, uint y)
const 280 assert(x < this->width && y < this->height);
281 return this->data[x * this->height + y];
291 inline T &
Get(uint x, uint y)
293 assert(x < this->width && y < this->height);
294 return this->data[x * this->height + y];
305 assert(x < this->width);
306 return this->data + x * this->
height;
317 assert(x < this->width);
318 return this->data + x * this->
height;
SmallMatrix(const SmallMatrix &other)
Copy constructor.
uint height
Number of items over second axis.
const T & Get(uint x, uint y) const
Get item x/y (const).
void EraseColumnPreservingOrder(uint x, uint count=1)
Remove columns from the matrix while preserving the order of other columns.
SmallMatrix & operator=(const SmallMatrix &other)
Assignment.
static void MemMoveT(T *destination, const T *source, size_t num=1)
Type-safe version of memmove().
void Resize(uint new_width, uint new_height)
Set the size to a specific width and height, preserving item positions as far as possible in the proc...
Functions related to the allocation of memory.
void EraseRowPreservingOrder(uint y, uint count=1)
Remove columns from the matrix while preserving the order of other columns.
static T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type...
const T * operator[](uint x) const
Get column "number" (const)
void Compact()
Compact the matrix down to the smallest possible size.
static T min(const T a, const T b)
Returns the minimum of two values.
uint width
Number of items over first axis.
static void MemCpyT(T *destination, const T *source, size_t num=1)
Type-safe version of memcpy().
uint capacity
The available space for storing items.
void EraseColumn(uint x)
Erase a column, replacing it with the last one.
T * data
The pointer to the first item.
void Clear()
Remove all rows from the matrix.
void EraseRow(uint y)
Erase a row, replacing it with the last one.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
void Assign(const SmallMatrix &other)
Assign items from other vector.
Simple matrix template class.
void AppendColumn(uint to_add=1)
Append rows.
void AppendRow(uint to_add=1)
Append rows.
Functions related to memory operations.
void Reset()
Remove all items from the list and free allocated memory.