Honestly fuck if i know atp
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
#include <cstring>
|
||||
#ifndef _NB_DATA_TYPES
|
||||
#define _NB_DATA_TYPES
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <memory>
|
||||
|
||||
namespace nb {
|
||||
|
||||
template<typename T, unsigned int N>
|
||||
class NDArray {
|
||||
protected:
|
||||
std::unique_ptr<T[]> _data;
|
||||
static unsigned int calculateTotalSize(const std::array<unsigned int, N>& dims) {
|
||||
unsigned int ret = 1;
|
||||
std::for_each(dims.begin(), dims.end(), [&](unsigned int n){
|
||||
ret *= n;
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
public:
|
||||
const std::array<unsigned int, N> dimensions;
|
||||
const unsigned int totalSize;
|
||||
static constexpr unsigned int NumberDimensions = N;
|
||||
NDArray(
|
||||
const std::array<unsigned int, N>& dimensions_,
|
||||
T* data_=nullptr
|
||||
) :
|
||||
dimensions(dimensions_),
|
||||
totalSize(calculateTotalSize(dimensions_))
|
||||
{
|
||||
if (data_) { data(data_); }
|
||||
}
|
||||
NDArray(
|
||||
const std::array<unsigned int, N>& dimensions_,
|
||||
const T* data_
|
||||
) : NDArray(dimensions_) { data(data_); }
|
||||
|
||||
unsigned int coordinateToIndex(const std::array<T, N>& coord) const {
|
||||
unsigned int blockSize = 1;
|
||||
unsigned int ret = 0;
|
||||
for (auto i = 0; i < dimensions.size(); ++i) {
|
||||
ret += coord[i] * blockSize;
|
||||
blockSize *= dimensions[i];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline const T& at(const std::array<T, N>& coord) const {
|
||||
return _data[coordinateToIndex(coord)];
|
||||
}
|
||||
inline std::shared_ptr<T> data() const { return _data; }
|
||||
|
||||
inline T& at(const std::array<T, N>& coord) {
|
||||
return _data[coordinateToIndex(coord)];
|
||||
}
|
||||
inline T* data(const T* data_) {
|
||||
_data = std::make_unique<T[]>(totalSize);
|
||||
std::memcpy(_data.get(), data_, totalSize * sizeof(T));
|
||||
return _data.get();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}; // namespace nb
|
||||
|
||||
}
|
||||
#endif // _NB_DATA_TYPES
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#ifndef _NB_CORE_TYPES
|
||||
#define _NB_CORE_TYPES
|
||||
#ifndef _NB_UTILTIES
|
||||
#define _NB_UTILTIES
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
@@ -131,4 +131,4 @@ class ThreadsafeObjectLock {
|
||||
}; */
|
||||
|
||||
} // namespace nb
|
||||
#endif // _NB_CORE_TYPES
|
||||
#endif // _NB_CORE_TYPES
|
||||
|
||||
Reference in New Issue
Block a user