~ken-vandine/libqofono/0.90-suggests

« back to all changes in this revision

Viewing changes to src/qofono.cpp

  • Committer: Slava Monich
  • Date: 2015-03-02 10:39:19 UTC
  • mto: This revision was merged to the branch mainline in revision 186.
  • Revision ID: git-v1:8d481a5d79211db0f8d0a87ef1c78e4565f0fbd1
[libqofono] Updated country code search algorithm

This one provides better performance in most important cases.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1928
1928
 
1929
1929
QString QOfono::mobileCountryCodeToAlpha2CountryCode(int mcc)
1930
1930
{
 
1931
    const int n = sizeof(qofonoMccList)/sizeof(qofonoMccList[0]);
1931
1932
    int low = 0;
1932
 
    int high = (sizeof(qofonoMccList)/sizeof(qofonoMccList[0]))-1;
 
1933
    int high = n;
1933
1934
 
1934
 
    while (low <= high) {
1935
 
        int mid = (low + high)/2;
1936
 
        int val = qofonoMccList[mid].mcc;
1937
 
        if (val < mcc) {
 
1935
    while (low < high) {
 
1936
        const int mid = (low + high)/2;
 
1937
        if (qofonoMccList[mid].mcc >= mcc) {
 
1938
            high = mid;
 
1939
        } else {
1938
1940
            low = mid + 1;
1939
 
        } else if (val > mcc) {
1940
 
            high = mid - 1;
1941
 
        } else {
1942
 
            /* MCC is found. Back up a litte to the first entry */
1943
 
            while (mid > 0 && qofonoMccList[mid-1].mcc == val) mid--;
1944
 
            return QString(qofonoMccList[mid].cc);
1945
1941
        }
1946
1942
    }
1947
1943
 
1948
 
    /* MCC not found. */
1949
 
    qWarning() << "Unknown Mobile Country Code:" << mcc;
1950
 
    return QString();
 
1944
    if (high < n && qofonoMccList[high].mcc == mcc) {
 
1945
        return QString(qofonoMccList[high].cc);
 
1946
    } else {
 
1947
        qWarning() << "Unknown Mobile Country Code:" << mcc;
 
1948
        return QString();
 
1949
    }
1951
1950
}