PDA

View Full Version : LookUp table Implementation in Qt



rex
9th February 2011, 13:48
Hello friends i have a doubt.. i am working on a Qt based application where i show some temperature data. I have a look up table where i save a temperature value to the db for a decimal value.. Right now i have the decimal value data into a QString which i am comparing with the decimal value and appending the equivalent Temperature value into a QVector which i am saving into the database.


if(str=="387")
{
temperaturedata.append("0");
}else if(str=="414")
{
temperaturedata.append("10");
}else if(str=="441")
{
temperaturedata.append("20");
}else if(str=="469")
{
temperaturedata.append("30");
}else if(str=="497")
{
temperaturedata.append("40");
}else if(str=="525")
{
temperaturedata.append("50");
}
// And for all the other values also
.
.
.

I wanted to know if there was a better way of handling a look up table, i have too many if else statements .. how could i handle this better..

This is how the look up table is.


Temperature Decimal
-40 289
-39 292
-38 294
-37 296
-36 299
-35 301
-34 303
-33 306
-32 308
-31 311
-30 313
-29 315
-28 318
-27 320
-26 323
-25 325
-24 328
-23 330
-22 333
-21 335
-20 337
-19 340
-18 342
-17 345
-16 347
-15 350
-14 352
-13 355
-12 358
-11 360
-10 363
-9 365
-8 368
-7 370
-6 373
-5 375
-4 378
-3 381
-2 383
-1 386
0 388
1 391
2 394
3 396
4 399
5 401
6 404
7 407
8 409
9 412
10 415
11 417
12 420
13 423
14 425
15 428
16 431
17 433
18 436
19 439
20 441
21 444
22 447
23 449
24 452
25 455
26 458
27 460
28 463
29 466
30 469
31 471
32 474
33 477
34 480
35 483
36 485
37 488
38 491
39 494
40 497
41 499
42 502
43 505
44 508
45 511
46 514
47 516
48 519
49 522
50 525

looking for some help.

Thank you

Stef
9th February 2011, 13:58
Instead of using all the else if statements did you consider using a QMap which seems perfect for these lookups.

stampede
9th February 2011, 14:00
Take a look at QHash (http://trinity.pearsoncomputing.net/docs/qt4/qhash.html), you can create a hash table mapping Decimal <-> temperature values, and instead of "if-else" statements simply call:


// _hash contains your Decimal <-> temperature table
if( _hash.contains(str) ){
temperatureData.append( _hash.value(str) )
}


----
Edit: looks like Stef was faster :)

rex
9th February 2011, 14:35
Hello thank you for the response. I did look at QMap but could't figure out how i should be doing it.. Din't find any example. Can you pls show a small example.

i will try your method also stampede thank you so much for the example code.

regards

stampede
9th February 2011, 14:44
Have you looked at the examples in QMap documentation ?
You just have to store values from your temperature table in such container ( initialize it once somewhere in the program ).

Stef
9th February 2011, 14:47
It's not that hard really. First you populate the lookup table with all keys and values. Then if you want to use it you just ask for the key of a specific value.


QMap<QString, QString> lookupTable;

// Populate lookup table
lookupTable.insert("-40", "289");
lookupTable.insert("-39", "292");
lookupTable.insert("-38", "294");
lookupTable.insert("-37", "296");
lookupTable.insert("-36", "299");
lookupTable.insert("-35", "301");
// ... And so on

// Use it
temperaturedata.append(lookupTable.value(str));

wysota
9th February 2011, 14:48
QHash<int,int> lookUp;
lookup[-40] = 289;
lookup[-39] = 292;
// etc.

or using a static C array:


int valueFor(int v) {
static int lookup[] = { 289, 292, 294, ... };
return lookup[v+40];
}

rex
9th February 2011, 14:49
thanks a lot i will do it now.. :) You made it look so easy.