label: label name, start: begin of each class, count: #data of classes, perm: indices to the original data perm, length l, must be allocated before calling this subroutine
| 2327 | // label: label name, start: begin of each class, count: #data of classes, perm: indices |
| 2328 | // to the original data perm, length l, must be allocated before calling this subroutine |
| 2329 | static void |
| 2330 | svm_group_classes(const svm_problem* prob, |
| 2331 | int* nr_class_ret, |
| 2332 | int** label_ret, |
| 2333 | int** start_ret, |
| 2334 | int** count_ret, |
| 2335 | int* perm) |
| 2336 | { |
| 2337 | int l = prob->l; |
| 2338 | int max_nr_class = 16; |
| 2339 | int nr_class = 0; |
| 2340 | int* label = Malloc(int, max_nr_class); |
| 2341 | int* count = Malloc(int, max_nr_class); |
| 2342 | int* data_label = Malloc(int, l); |
| 2343 | |
| 2344 | for (int i = 0; i < l; i++) { |
| 2345 | int this_label = static_cast<int>(prob->y[i]); |
| 2346 | int j; |
| 2347 | |
| 2348 | for (j = 0; j < nr_class; j++) { |
| 2349 | if (this_label == label[j]) { |
| 2350 | ++count[j]; |
| 2351 | break; |
| 2352 | } |
| 2353 | } |
| 2354 | |
| 2355 | data_label[i] = j; |
| 2356 | |
| 2357 | if (j == nr_class) { |
| 2358 | if (nr_class == max_nr_class) { |
| 2359 | max_nr_class *= 2; |
| 2360 | label = static_cast<int*>(realloc(label, max_nr_class * sizeof(int))); |
| 2361 | count = static_cast<int*>(realloc(count, max_nr_class * sizeof(int))); |
| 2362 | } |
| 2363 | |
| 2364 | label[nr_class] = this_label; |
| 2365 | |
| 2366 | count[nr_class] = 1; |
| 2367 | ++nr_class; |
| 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | int* start = Malloc(int, nr_class); |
| 2372 | |
| 2373 | start[0] = 0; |
| 2374 | |
| 2375 | for (int i = 1; i < nr_class; i++) |
| 2376 | start[i] = start[i - 1] + count[i - 1]; |
| 2377 | |
| 2378 | for (int i = 0; i < l; i++) { |
| 2379 | perm[start[data_label[i]]] = i; |
| 2380 | ++start[data_label[i]]; |
| 2381 | } |
| 2382 | |
| 2383 | start[0] = 0; |
| 2384 | |
| 2385 | for (int i = 1; i < nr_class; i++) |
| 2386 | start[i] = start[i - 1] + count[i - 1]; |
no outgoing calls
no test coverage detected