vectorbase::Find

Contents

Description

Find indices of specified values in vector.


It finds all the values that match the supplied condition and returns their indices. If the type of the vectorbase is double and dTestVal is missing value(NANUM), then only equality test(MATREPL_TEST_EQUAL) should be allowed.

Syntax

int Find( vector<uint> & vecIndex, double dLower, double dUpper = _ONAN, int nPrecision = 8, int iBegin = 0, int iEnd = -1, DWORD dwCntrl = 0 )
int Find(uint wBitwiseOption, double dTestVal, vector<uint>& vn, int iBegin = 0, int iEnd = -1, double dTol = 0, DWORD dwCntrl = 0 )

Parameters

vecIndex
[modify] All indices of vecData where specified value was found.
dLower
[input] Lower bound for the value to be found. Set this to exact value when you want to search for a single value.
dUpper
[input] Upper bound for the value to be found. Leave it as default (_ONAN) if searching for only a single value.
nPrecision
[input] Level of precision to be used in the search.
This parameter will be ignored if dUpper is not the default value.
This value should bigger than zero, else it's invalid case.
iBegin
[input] Index of vecData from which the search should begin (0-based offset)
iEnd
[input] Index of vecData up to which the search should be done (0-based offset; -1 will search to end of vecData)
dwCntrl
[input] Available values :
enum
{
        VECTORBASEFIND_IF_ONE_MATCH_THEN_RETURN = 0x00000001,
};

wBitwiseOption
[input] Testing options:
MATREPL_TEST_LESSTHAN, use less than in the testing condition
MATREPL_TEST_EQUAL, use equal in the testing condition
MATREPL_TEST_GREATER, use greater Than in the testing condition
MATREPL_USE_ABSOLUTE_VALUE_IN_TEST, use absolute values in the testing condition
dTestVal
[input] The value to test against. If it is missing value (=NANUM), only MATREPL_TEST_EQUAL is permitted.
vn
[output] It receives the indices of all the elements that match the condition.
dTol
[input] Tolerance for equal testing

Return

returns -1 if invalid case, such as vecIndex is null; return 0 if not found, otherwise return "n" where n is the number of values found.


The total number of matched values (the size of vn), or a negative number for error.

Examples

EX1

void vectorbase_Find_ex1()
{
    // Fill a vector with some values and output the index and values
    vector vecData = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9};
    int ii;
    printf("\nElements of the vector:\n");
    for( ii = 0; ii < vecData.GetSize(); ii++ )
        printf(" Index= %d, Value= %5.2f\n", ii, vecData[ii] );
 
    vector<uint> vecIndex;
    int nFound;
 
    // Search for value = 0.6
    printf("\nSearching for value = 0.6:\n");
    nFound = vecData.Find(vecIndex, 0.6);
    if( -1 != nFound )
    {
        printf("Found %d element(s)\n", nFound);
        for(ii = 0; ii < nFound; ii++)
            printf("Index= %d, Value= %5.2f\n", vecIndex[ii], vecData[vecIndex[ii]]);
    }
    else
        printf("Found no element(s)\n");
 
 
    // Search for values between 0.25 and 0.6
    printf("\nSearching for values between 0.25 and 0.6:\n");
    nFound = vecData.Find(vecIndex, 0.25, 0.6);
    if( -1 != nFound )
    {
        printf("Found %d element(s)\n", nFound);
        for(ii = 0; ii < nFound; ii++)
            printf("Index= %d, Value= %5.2f\n", vecIndex[ii], vecData[vecIndex[ii]]);
    }
    else
        printf("Found no element(s)\n");
 
    // Search for value = 0.65 with precision set low
    printf("\nSearching for value = 0.65 with precision set to 1:\n");
    nFound = vecData.Find(vecIndex, 0.65, _ONAN, 1);
    if( -1 != nFound )
    {
        printf("Found %d element(s)\n", nFound);
        for(ii = 0; ii < nFound; ii++)
            printf("Index= %d, Value= %5.2f\n", vecIndex[ii], vecData[vecIndex[ii]]);
    }
    else
        printf("Found no element(s)\n");
 
    // Search for value = 0.65 with precision set higher
    printf("\nSearching for value = 0.65 with precision set to 2:\n");
    nFound = vecData.Find(vecIndex, 0.65, _ONAN, 2);
    if( -1 != nFound )
    {
        printf("Found %d element(s)\n", nFound);
        for(ii = 0; ii < nFound; ii++)
            printf("Index= %d, Value= %5.2f\n", vecIndex[ii], vecData[vecIndex[ii]]);
    }
    else
        printf("Found no element(s)\n");
}

EX2

void vectorbase_Find_ex2()
{
        vector vn = {1,2,3,4,5,6,7,8,9,10,0,5};
        vector<uint> vIndex;
        int wBitwiseOption = MATREPL_TEST_EQUAL;
        int nRet = vn.Find(wBitwiseOption, 5.0, vIndex, 0, -1);
        printf("Found %d matches.",nRet); // nRet should be 2 here
}

EX3

void vectorbase_Find_ex3()
{
        vector vn = {2.5, 0, 6, 8.9, 4.56, 0, 1.49};
        
        double dValToFind = 0;
        vector<uint> vIndex;    
        
        DWORD dwCtrl = VECTORBASEFIND_IF_ONE_MATCH_THEN_RETURN; // directly return if once find one match.
        bool bIsExistedZero = ( 1 == vn.Find(MATREPL_TEST_EQUAL, dValToFind, vIndex, 0, -1, 0, dwCtrl) );
        
        if( bIsExistedZero )
                out_str("At least existed one zero.");
}

Remark

Find indices of specified values in a vectorbase derived object.

See Also

vectorbase::FindDuplicates

header to Include

origin.h