|
1 | 1 | #include "data.table.h"
|
2 | 2 | #include <Rdefines.h>
|
3 | 3 |
|
4 |
| - |
5 | 4 | // #include <signal.h> // the debugging machinery + breakpoint aidee
|
6 | 5 | // raise(SIGINT);
|
7 | 6 |
|
@@ -178,72 +177,58 @@ bool is_default_measure(SEXP vec) {
|
178 | 177 |
|
179 | 178 | // maybe unlist, then unique, then set_diff.
|
180 | 179 | SEXP uniq_diff(SEXP int_or_list, int ncol, bool is_measure) {
|
181 |
| - // Protect input list/vector, unlisting if necessary |
182 | 180 | SEXP int_vec = PROTECT(isNewList(int_or_list) ? unlist_(int_or_list) : int_or_list);
|
183 | 181 |
|
184 |
| - // Check for duplicated elements in the input vector |
185 | 182 | SEXP is_duplicated = PROTECT(duplicated(int_vec, FALSE));
|
186 | 183 |
|
187 | 184 | int n_unique_cols = 0;
|
188 |
| - |
189 |
| - // Allocate a vector to store invalid column indices (initially max size is length of int_vec) |
| 185 | + |
190 | 186 | SEXP invalid_columns = PROTECT(allocVector(INTSXP, length(int_vec)));
|
191 | 187 | int* invalid_col_ptr = INTEGER(invalid_columns);
|
192 | 188 | int invalid_count = 0;
|
193 | 189 |
|
194 |
| - // Iterate through the column numbers to identify invalid and unique columns |
195 | 190 | for (int i = 0; i < length(int_vec); ++i) {
|
196 | 191 | int col_number = INTEGER(int_vec)[i];
|
197 | 192 |
|
198 |
| - // Check if the column number is within valid range |
199 | 193 | bool good_number = 0 < col_number && col_number <= ncol;
|
200 | 194 |
|
201 |
| - // Special check for 'measure' case (NA_INTEGER handling) |
202 | 195 | if (is_measure) good_number |= (col_number == NA_INTEGER);
|
203 | 196 |
|
204 |
| - // Collect invalid columns if not valid or out of range |
205 | 197 | if (!good_number || col_number == 0) {
|
206 | 198 | invalid_col_ptr[invalid_count++] = col_number;
|
207 | 199 | } else if (!LOGICAL(is_duplicated)[i]) {
|
208 | 200 | n_unique_cols++;
|
209 | 201 | }
|
210 | 202 | }
|
211 | 203 |
|
212 |
| - // If invalid columns are found, construct the error message |
213 | 204 | if (invalid_count > 0) {
|
214 |
| - // Buffer for concatenated invalid column messages |
215 |
| - char buffer[4096] = ""; // Large enough to store the concatenated string |
| 205 | + char buffer[4096] = ""; |
216 | 206 | for (int i = 0; i < invalid_count; ++i) {
|
217 | 207 | char temp[32];
|
218 |
| - snprintf(temp, 32, "[%d]", invalid_col_ptr[i]); // Format the column number |
| 208 | + snprintf(temp, 32, "[%d]", invalid_col_ptr[i]); |
219 | 209 |
|
220 | 210 | if (i > 0) {
|
221 |
| - strncat(buffer, ", ", sizeof(buffer) - strlen(buffer) - 1); // Add separator |
| 211 | + strncat(buffer, ", ", sizeof(buffer) - strlen(buffer) - 1); |
222 | 212 | }
|
223 |
| - strncat(buffer, temp, sizeof(buffer) - strlen(buffer) - 1); // Append to the buffer |
| 213 | + strncat(buffer, temp, sizeof(buffer) - strlen(buffer) - 1); |
224 | 214 | }
|
225 | 215 |
|
226 |
| - // Throw the error with the concatenated message |
227 | 216 | error(_("One or more values in '%s' are invalid; please fix by removing: %s"),
|
228 | 217 | is_measure ? "measure.vars" : "id.vars", buffer);
|
229 | 218 | }
|
230 |
| - |
231 |
| - // Proceed with collecting unique columns |
| 219 | + |
232 | 220 | SEXP unique_col_numbers = PROTECT(allocVector(INTSXP, n_unique_cols));
|
233 | 221 | int unique_i = 0;
|
234 | 222 |
|
235 |
| - // Populate the unique column numbers into the new vector |
236 | 223 | for (int i = 0; i < length(is_duplicated); ++i) {
|
237 | 224 | if (!LOGICAL(is_duplicated)[i]) {
|
238 | 225 | INTEGER(unique_col_numbers)[unique_i++] = INTEGER(int_vec)[i];
|
239 | 226 | }
|
240 | 227 | }
|
241 | 228 |
|
242 |
| - // Apply set difference to get final unique column indices |
243 | 229 | SEXP out = set_diff(unique_col_numbers, ncol);
|
244 | 230 |
|
245 |
| - // Unprotect all allocated objects |
246 |
| - UNPROTECT(4); // Unprotect input, duplication check, invalid columns, and unique columns |
| 231 | + UNPROTECT(4); |
247 | 232 |
|
248 | 233 | return out;
|
249 | 234 | }
|
|
0 commit comments