Results 1 to 6 of 6

Thread: Best way to provide lookup values (over 1000)

  1. #1
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Best way to provide lookup values (over 1000)

    I have a custom network server Daemon that returns error codes to the client as numbers like "10000", "10003" etc.

    These numbers correspond to text descriptions like this:

    10000 = Success
    10003 = Failed
    etc.

    There are over 1000 of these codes, what is the best way to store them in the client app so i can search through them later?

    Thanks for the help

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Best way to provide lookup values (over 1000)

    QHash or some other (custom) kind of tree.

  3. #3
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Best way to provide lookup values (over 1000)

    or why not just a switch/case statement:
    Qt Code:
    1. QString errorString(int code)
    2. {
    3. switch(code)
    4. {
    5. case 1000 : return "Success";
    6. case 1003 : return "Error";
    7. ...
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Best way to provide lookup values (over 1000)

    Hmm... that would mean 1000 conditional branch operations in the worst case and only 32 in case of using a binary tree.

  5. #5
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Re: Best way to provide lookup values (over 1000)

    Here's what I did . . .

    I defined the structure like this:

    Qt Code:
    1. QMap<int, QString> map;
    2. map[100] = "event 1";
    3. map[101] = "event 2";
    4. map[102] = "event 3";
    To copy to clipboard, switch view to plain text mode 

    Then to lookup the human text where the incoming network data is "num"

    Qt Code:
    1. int num = statusData[1].toInt();
    2.  
    3. QMap<int, QString>::const_iterator i = map.find(num);
    4. while (i != map.end() && i.key() == num) {
    5. qDebug() << "DEBUG: Status Definition = " << i.value();
    6. machineStatusLabel->setText(tr("%1").arg(i.value()));
    7. ++i;
    8. }
    To copy to clipboard, switch view to plain text mode 

    Works great...

    Thanks.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Best way to provide lookup values (over 1000)

    I really suggest to use QHash instead of QMap. It will work much faster.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.