void usage_advanced() { char *aName[3] = {"Alice", "Bob", "Wesker"}; OrderedMap *pMap; int32_t iRtnCode = OdrMapInit(&pMap); if (iRtnCode != SUCC) return; /*--------------------------------------------------* * Set the comparison and resource clean methods. * *--------------------------------------------------*/ pMap->set_compare(pMap, advanced_compare); pMap->set_destroy(pMap, advanced_destroy); /*--------------------------------------------------* * Insert key value pairs into the map. * *--------------------------------------------------*/ Employ *pEmploy = (Employ*)malloc(sizeof(Employ)); pEmploy->iId = 1; pEmploy->cYear = 25; pEmploy->cLevel = 100; Pair *pPair = (Pair*)malloc(sizeof(Pair)); pPair->key = (Key)aName[0]; pPair->value = (Value)pEmploy; pMap->put(pMap, (Entry)pPair, true); pEmploy = (Employ*)malloc(sizeof(Employ)); pEmploy->iId = 2; pEmploy->cYear = 25; pEmploy->cLevel = 90; pPair = (Pair*)malloc(sizeof(Pair)); pPair->key = (Key)aName[1]; pPair->value = (Value)pEmploy; pMap->put(pMap, (Entry)pPair, true); pEmploy = (Employ*)malloc(sizeof(Employ)); pEmploy->iId = 3; pEmploy->cYear = 25; pEmploy->cLevel = 80; pPair = (Pair*)malloc(sizeof(Pair)); pPair->key = (Key)aName[2]; pPair->value = (Value)pEmploy; pMap->put(pMap, (Entry)pPair, true); /*--------------------------------------------------* * Retrive the value with the specified key. * *--------------------------------------------------*/ Value value; pMap->get(pMap, (Key)aName[0], &value); assert(((Employ*)value)->iId == 1); assert(((Employ*)value)->cYear == 25); assert(((Employ*)value)->cLevel == 100); /*--------------------------------------------------* * Remove the key value pair with the specified key.* *--------------------------------------------------*/ pMap->remove(pMap, (Key)aName[1], true); int32_t iSize = pMap->size(pMap); assert(iSize == 2); OdrMapDeinit(&pMap, true); return; }
void usage_simple() { OrderedMap *pMap; int32_t iRtnCode = OdrMapInit(&pMap); if (iRtnCode != SUCC) return; /*--------------------------------------------------* * Set the comparison and resource clean methods. * *--------------------------------------------------*/ pMap->set_compare(pMap, simple_compare); pMap->set_destroy(pMap, simple_destroy); /*--------------------------------------------------* * Insert key value pairs into the map. * *--------------------------------------------------*/ Pair *pPair; pPair = (Pair*)malloc(sizeof(Pair)); #if __x86_64__ pPair->key = (Key)(int64_t)1; pPair->value = (Key)(int64_t)-1; #else pPair->key = (Key)1; pPair->value = (Key)-1; #endif pMap->put(pMap, (Entry)pPair, true); pPair = (Pair*)malloc(sizeof(Pair)); #if __x86_64__ pPair->key = (Key)(int64_t)3; pPair->value = (Key)(int64_t)-3; #else pPair->key = (Key)3; pPair->value = (Key)-3; #endif pMap->put(pMap, (Entry)pPair, true); pPair = (Pair*)malloc(sizeof(Pair)); #if __x86_64__ pPair->key = (Key)(int64_t)2; pPair->value = (Key)(int64_t)-2; #else pPair->key = (Key)2; pPair->value = (Key)-2; #endif pMap->put(pMap, (Entry)pPair, true); /*--------------------------------------------------* * Retrive the value by with the specified key. * *--------------------------------------------------*/ Value value; #if __x86_64__ pMap->get(pMap, (Key)(int64_t)1, &value); assert((int64_t)value == -1); #else pMap->get(pMap, (Key)1, &value); assert(value == -1); #endif /*--------------------------------------------------* * Remove the key value pair with the specified key.* *--------------------------------------------------*/ #if __x86_64__ pMap->remove(pMap, (Key)(int64_t)2, true); #else pMap->remove(pMap, (Key)2, true); #endif int32_t iSize = pMap->size(pMap); assert(iSize == 2); OdrMapDeinit(&pMap, true); return; }
int main(int argc, char **argv) { int itemCount = 0; bool ok = false; if (argc > 1) { itemCount = QString(argv[1]).toInt(&ok); if (!ok) { qWarning() << "\nError! Enter a valid number."; qDebug() << "\nUsage:\n\t" << *argv << "<number of items for testing>\n"; return 1; } } else { qDebug() << "\nUsage:\n\t" << *argv << "<number of items for testing>\n"; return 1; } QMap<int, QString> map; QHash<int, QString> hash; QLinkedList<QString> linkList; OrderedMap<int, QString> om; QTime timer; qDebug() << "Timing insertion of" << itemCount << "items...\n"; timer.start(); for (int i=0; i<itemCount; i++) { map.insert(i, QString::number(i)); } qDebug() << "Map :" << timer.elapsed() << "msecs"; timer.start(); for (int i=0; i<itemCount; i++) { hash.insert(i, QString::number(i)); } qDebug() << "Hash :" << timer.elapsed() << "msecs"; timer.start(); for (int i=0; i<itemCount; i++) { linkList.append(QString::number(i)); } qDebug() << "Link list :" << timer.elapsed() << "msecs"; timer.start(); for (int i=0; i<itemCount; i++) { om.insert(i, QString::number(i)); } qDebug() << "Ordered ma :" << timer.elapsed() << "msecs"; qDebug() << "\n"; qDebug() << "Timing iteration over" << itemCount << "items...\n"; int dummy = 0; timer.start(); foreach (const QString& val, map.values()) { dummy += val.size(); } qDebug() << "Map :" << timer.elapsed() << "msecs"; dummy = 0; timer.start(); foreach (const QString& val, hash.values()) { dummy += val.size(); } qDebug() << "Hash :" << timer.elapsed() << "msecs"; dummy = 0; timer.start(); foreach (const QString& val, linkList) { dummy += val.size(); } qDebug() << "Linked list :" << timer.elapsed() << "msecs"; dummy = 0; timer.start(); foreach (const QString& val, om.values()) { dummy += val.size(); } qDebug() << "Ordered map :" << timer.elapsed() << "msecs"; qDebug() << "\n"; qDebug() << "Timing removal of random item from" << itemCount << "items...\n"; int rand = qrand() % itemCount; timer.start(); map.remove(rand); qDebug() << "Map :" << timer.elapsed() << "msecs"; rand = qrand() % itemCount; timer.start(); hash.remove(rand); qDebug() << "Hash :" << timer.elapsed() << "msecs"; rand = qrand() % itemCount; timer.start(); linkList.removeOne(QString::number(rand)); qDebug() << "Link list :" << timer.elapsed() << "msecs"; rand = qrand() % itemCount; timer.start(); om.remove(rand); qDebug() << "Ordered map :" << timer.elapsed() << "msecs"; qDebug() << "\n"; return 0; }