Adelsbach/VSIPL
Core Programming Reference Guide
DD-00016-015
Core

4.6.3 vsip_vindexbool - Find Indices of True Elements in Boolean Vector

vsip_length vsip_vindexbool(const vsip_vview_bl *a, vsip_vview_vi *index);
Description

This function finds the indices of all true elements in a boolean vector and stores them in an integer index vector. It returns the number of true elements found.

The function scans the input boolean vector a and records the positions of all elements that are true in the output index vector. The function returns the count of true elements found.

Parameters
Return Value
Example

vsip_vview_bl *conditions; 
vsip_vview_vi *indices; 
vsip_length n = 100; 
vsip_length true_count; 
 
// Create boolean vector 
conditions = vsip_vcreate_bl(n, VSIP_MEM_NONE); 
 
// Create index vector (same length as conditions) 
indices = vsip_vcreate_vi(n, VSIP_MEM_NONE); 
 
// Set some conditions to true (for example, every 5th element) 
for (vsip_length i = 0; i < n; i++) { 
    vsip_vput_bl(conditions, i, (i % 5) == 0); 
} 
 
// Find indices of true elements 
true_count = vsip_vindexbool(conditions, indices); 
 
printf("Found %lu true elements at positions:\n", true_count); 
for (vsip_length i = 0; i < true_count; i++) { 
    printf("%ld ", vsip_vget_vi(indices, i)); 
} 
printf("\n"); 
 
// Use the indices for further processing in your algorithms 
// For example, you could use these indices to select specific elements 
// from another vector that corresponds to your conditions 
 
// Clean up 
vsip_valldestroy_bl(conditions); 
vsip_valldestroy_vi(indices);