PeriDyno 1.0.0
Loading...
Searching...
No Matches
Array.h
Go to the documentation of this file.
1
16#pragma once
17#include "Platform.h"
18#include <cassert>
19#include <vector>
20#include <iostream>
21#include <cstring>
22#include <memory>
23#include <cmath>
24
25namespace dyno {
26
27 template<typename T, DeviceType deviceType> class Array;
28
29 template<typename T>
30 class Array<T, DeviceType::CPU>
31 {
32 public:
33 Array()
34 {
35 };
36
37 Array(uint num)
38 {
39 mData.resize((size_t)num);
40 }
41
42 ~Array() {};
43
44 void resize(uint n);
45
49 void reset();
50
51 void clear();
52
53 inline const T* begin() const { return mData.size() == 0 ? nullptr : &mData[0]; }
54 inline T* begin() { return mData.size() == 0 ? nullptr : &mData[0]; }
55
56 inline const std::vector<T>* handle() const { return &mData; }
57 inline std::vector<T>* handle() { return &mData; }
58
59 DeviceType deviceType() { return DeviceType::CPU; }
60
61 inline T& operator [] (unsigned int id)
62 {
63 return mData[id];
64 }
65
66 inline const T& operator [] (unsigned int id) const
67 {
68 return mData[id];
69 }
70
71 inline uint size() const { return (uint)mData.size(); }
72 inline bool isCPU() const { return true; }
73 inline bool isGPU() const { return false; }
74 inline bool isEmpty() const { return mData.empty(); }
75
76 inline void pushBack(T ele) { mData.push_back(ele); }
77
78 void assign(const T& val);
79 void assign(uint num, const T& val);
80
81 #ifndef NO_BACKEND
82 void assign(const Array<T, DeviceType::GPU>& src);
83 #endif
84
85 void assign(const Array<T, DeviceType::CPU>& src);
86 void assign(const std::vector<T>& src);
87
88 friend std::ostream& operator<<(std::ostream &out, const Array<T, DeviceType::CPU>& cArray)
89 {
90 for (uint i = 0; i < cArray.size(); i++)
91 {
92 out << i << ": " << cArray[i] << std::endl;
93 }
94
95 return out;
96 }
97
98 private:
99 std::vector<T> mData;
100 };
101
102 template<typename T>
104 {
105 mData.resize(n);
106 }
107
108 template<typename T>
110 {
111 mData.clear();
112 }
113
114 template<typename T>
116 {
117 memset((void*)mData.data(), 0, mData.size()*sizeof(T));
118 }
119
120 template<typename T>
122 {
123 if (mData.size() != src.size())
124 this->resize(src.size());
125
126 memcpy(this->begin(), src.begin(), src.size() * sizeof(T));
127 }
128
129 template<typename T>
130 void Array<T, DeviceType::CPU>::assign(const std::vector<T>& src)
131 {
132 if (mData.size() != src.size())
133 this->resize(src.size());
134
135 mData.assign(src.begin(), src.end());
136 }
137
138 template<typename T>
139 void Array<T, DeviceType::CPU>::assign(const T& val)
140 {
141 mData.assign(mData.size(), val);
142 }
143
144 template<typename T>
145 void Array<T, DeviceType::CPU>::assign(uint num, const T& val)
146 {
147 mData.assign(num, val);
148 }
149
150 template<typename T>
152}
153
154#ifdef CUDA_BACKEND
156#endif
157
158#ifdef VK_BACKEND
160#endif
This class is designed to be elegant, so it can be directly passed to GPU as parameters.
Definition Array.h:27
#define T(t)
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Array< T, DeviceType::CPU > CArray
Definition Array.h:151
unsigned int uint
Definition VkProgram.h:14