@@ -128,4 +128,90 @@ float vuapi fast_sqrtf(const float number)
128
128
return 1 .F / q_rsqrt (number);
129
129
}
130
130
131
+ /* *
132
+ * Divides a set of items into a specified number of pieces.
133
+ * @param[in] num_items The number of items.
134
+ * @param[in] num_pieces The number of pieces.
135
+ * @param[in] fn The function that apply to each piece.
136
+ * idx The piece index.
137
+ * beg The index of the begin item in piece.
138
+ * end The index of the end item in piece.
139
+ * num The number of items in piece.
140
+ * Eg. In these cases, the number of items of each piece as the following:
141
+ * (4, 1) => (4)
142
+ * (4, 2) => (2, 2)
143
+ * (4, 3) => (1, 1, 2)
144
+ * (4, 4) => (1, 1, 1, 1)
145
+ * (4, 5) => (1, 1, 1, 1, 0)
146
+ */
147
+ void divide_items_into_pieces (const size_t num_items, const size_t num_pieces,
148
+ std::function<void (size_t idx, size_t f, size_t end, size_t num)> fn)
149
+ {
150
+ if (fn == nullptr )
151
+ {
152
+ return ;
153
+ }
154
+
155
+ size_t num_items_per_piece = std::max (1 , int (num_items / float (num_pieces) + 0 .5F ));
156
+
157
+ for (size_t idx = 0 ; idx < num_pieces; ++idx)
158
+ {
159
+ size_t beg = idx * num_items_per_piece;
160
+ size_t end = idx == num_pieces - 1 ? num_items : (idx + 1 ) * num_items_per_piece;
161
+ size_t num = end - beg;
162
+
163
+ beg = idx < num_items ? beg : -1 ;
164
+ end = idx < num_items ? end - 1 : -1 ;
165
+ num = idx < num_items ? num : 0 ;
166
+
167
+ fn (idx, beg, end, num);
168
+ }
169
+ }
170
+
171
+ /* *
172
+ * Divides a set of items into a specified number of items per piece.
173
+ * @param[in] num_items The number of items.
174
+ * @param[in] num_pieces The number of item per piece.
175
+ * @param[in] fn The function that apply to each piece.
176
+ * idx The piece index.
177
+ * beg The index of the begin item in piece.
178
+ * end The index of the end item in piece.
179
+ * num The number of items in piece.
180
+ * @return The number of pieces.
181
+ * Eg. The number of items of each piece as the following.
182
+ * (4, 1) => (1, 1, 1, 1)
183
+ * (4, 2) => (2, 2)
184
+ * (4, 3) => (3, 1, 1)
185
+ * (4, 4) => (4)
186
+ * (4, 5) => (4)
187
+ */
188
+ size_t divide_items_into_num_items_per_piece (const size_t num_items, const size_t num_items_per_piece,
189
+ std::function<void (size_t idx, size_t f, size_t end, size_t num)> fn)
190
+ {
191
+ if (fn == nullptr )
192
+ {
193
+ return 0 ;
194
+ }
195
+
196
+ size_t result = 0 ;
197
+
198
+ std::vector<size_t > piece;
199
+
200
+ for (size_t j = 1 ; j <= num_items; j++)
201
+ {
202
+ size_t i = j - 1 ;
203
+
204
+ piece.push_back (i);
205
+
206
+ if (j % num_items_per_piece == 0 || i == num_items - 1 )
207
+ {
208
+ fn (result, piece.front (), piece.back (), piece.size ());
209
+ piece.clear ();
210
+ result += 1 ;
211
+ }
212
+ }
213
+
214
+ return result;
215
+ }
216
+
131
217
} // namespace vu
0 commit comments