PeriDyno 1.0.0
Loading...
Searching...
No Matches
ArrayList.h
Go to the documentation of this file.
1
16#pragma once
17#include <vector>
18#include <iostream>
19
20#include "Platform.h"
21
22#include "STL/List.h"
23#include "Array/Array.h"
24
25namespace dyno {
26 template<class ElementType, DeviceType deviceType> class ArrayList;
27
28 template<class ElementType>
29 class ArrayList<ElementType, DeviceType::CPU>
30 {
31 public:
34
35 bool resize(uint num);
36
44 bool resize(const CArray<uint>& counts);
45 bool resize(const uint arraySize, const uint eleSize);
46
47 inline uint size() const { return mLists.size(); }
48 uint elementSize();
49
51 {
52 return id == mIndex.size() - 1 ? mElements.size() - mIndex[id] : mIndex[id + 1] - mIndex[id];
53 }
54
55 inline List<ElementType>& operator [] (unsigned int id)
56 {
57 return mLists[id];
58 }
59
60 inline const List<ElementType>& operator [] (unsigned int id) const
61 {
62 return mLists[id];
63 }
64
65 inline bool isCPU() const { return true; }
66 inline bool isGPU() const { return false; }
67 inline bool isEmpty() const { return mLists.empty(); }
68
69 void clear();
70
71 void assign(const ArrayList<ElementType, DeviceType::CPU>& src);
72
73 #ifndef NO_BACKEND
74 void assign(const ArrayList<ElementType, DeviceType::GPU>& src);
75 #endif
76
77 friend std::ostream& operator<<(std::ostream &out, const ArrayList<ElementType, DeviceType::CPU>& aList)
78 {
79 out << std::endl;
80 for (uint i = 0; i < aList.size(); i++)
81 {
82 List<ElementType> lst = aList[i];
83 out << "List " << i << " (" << lst.size() << "):";
84 for (auto it = lst.begin(); it != lst.end(); it++)
85 {
86 std::cout << " " << *it;
87 }
88 out << std::endl;
89 }
90 return out;
91 }
92
93 const CArray<uint>& index() const { return mIndex; }
94 const CArray<ElementType>& elements() const { return mElements; }
95 const CArray<List<ElementType>>& lists() const { return mLists; }
96
101
102 private:
105
107 };
108
109 template<class ElementType>
111 {
112 for (int i = 0; i < mLists.size(); i++)
113 {
114 mLists[i].clear();
115 }
116
117 mLists.clear();
118 mIndex.clear();
119 mElements.clear();
120 }
121
122 template<class ElementType>
127
128 template<class ElementType>
130 {
131 assert(counts.size() > 0);
132
133 if (mIndex.size() != counts.size())
134 {
135 mIndex.resize(counts.size());
136 mLists.resize(counts.size());
137 }
138
139 mIndex.assign(counts);
140
141 uint total_num = 0;
142 for (uint i = 0; i < mIndex.size(); i++)
143 {
144 uint num = mIndex[i];
145
146 //exclusive scan
147 mIndex[i] = total_num;
148
149 //summation
150 total_num += num;
151 }
152
153 mElements.resize(total_num);
154
155 //initialize all lists.
156 for (uint i = 0; i < mLists.size(); i++)
157 {
158 mLists[i].reserve(mElements.begin() + mIndex[i], counts[i]);
159 }
160
161 return true;
162 }
163
164 template<class ElementType>
165 bool ArrayList<ElementType, DeviceType::CPU>::resize(const uint arraySize, const uint eleSize)
166 {
167 assert(arraySize > 0);
168
169 if (mIndex.size() != arraySize)
170 {
171 mIndex.resize(arraySize);
172 mLists.resize(arraySize);
173 mElements.resize(arraySize * eleSize);
174 }
175
176 uint total_num = 0;
177 for (uint i = 0; i < mIndex.size(); i++)
178 {
179 mIndex[i] = i * eleSize;
180 }
181
182 //initialize all lists.
183 for (uint i = 0; i < mLists.size(); i++)
184 {
185 mLists[i].reserve(mElements.begin() + mIndex[i], eleSize);
186 }
187
188 return true;
189 }
190
191 template<class ElementType>
193 {
194 mIndex.assign(src.index());
195 mElements.assign(src.elements());
196
197 mLists.assign(src.lists());
198
199 //redirect the element address
200 for (uint i = 0; i < src.size(); i++)
201 {
202 mLists[i].reserve(mElements.begin() + mIndex[i], mLists[i].size());
203 }
204 }
205
206 template<typename T>
208}
209
210#ifdef CUDA_BACKEND
212#endif
213
214#ifdef VK_BACKEND
216#endif
assert(queueCount >=1)
ArrayList< ElementType, DeviceType::CPU > & operator=(const ArrayList< ElementType, DeviceType::CPU > &)=delete
To avoid erroneous shallow copy.
friend std::ostream & operator<<(std::ostream &out, const ArrayList< ElementType, DeviceType::CPU > &aList)
Definition ArrayList.h:77
CArray< List< ElementType > > mLists
Definition ArrayList.h:106
const CArray< ElementType > & elements() const
Definition ArrayList.h:94
const CArray< uint > & index() const
Definition ArrayList.h:93
const CArray< List< ElementType > > & lists() const
Definition ArrayList.h:95
Be aware do not use this structure on GPU if the data size is large.
Definition List.h:21
DYN_FUNC iterator end()
Definition List.h:33
DYN_FUNC iterator begin()
Definition List.h:29
DYN_FUNC uint size()
Definition List.inl:69
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
ArrayList< T, DeviceType::CPU > CArrayList
Definition ArrayList.h:207
Array< T, DeviceType::CPU > CArray
Definition Array.h:151
unsigned int uint
Definition VkProgram.h:14