bool buildWord(string str, bool original, Hash strHash){ if(strHash.find((char*)&str[0])&&!original) return true; for(int i=1;i<str.length();i++){ string left=str.substr(0,i); string right=str.substr(i,str.length()-i); if(strHash.find((char*)&left[0])&&buildWord(right, false, strHash)) return true; } return false; }
int main(){ Hash<int>* tab = new Hash<int>(100); tab->insert("chickin" , 5); tab->insert("car" , 1); tab->insert("asdf", 3); cout << tab->find("chickin") << endl; cout << tab->find("asdf") << endl; tab->remove("car"); cout << tab->find("car"); delete tab; return 0; }
int main() { Hash<int,string> h; if(h.find(111)!=0) cout<<*(h.find(111))<<endl; else cout<<111<<" Not exist"<<endl; h.insert(111,"111"); h.insert(222,"222"); h.insert(333,"333"); if(h.find(111)!=0) cout<<*(h.find(111))<<endl; else cout<<111<<"Not exist"<<endl; if(h.find(222)!=0) cout<<*(h.find(222))<<endl; else cout<<222<<"Not exist"<<endl; h.remove(111); cout<<"Remove 111 ..."<<endl; if(h.find(111)!=0) cout<<*(h.find(111))<<endl; else cout<<111<<" Not exist"<<endl; }
void t2(){ // string key checks printf("test 1, strings----\n"); Hash h; Value v,w; char keys[HCOUNT][32]; char vals[HCOUNT][32]; for(int i=0;i<HCOUNT;i++){ sprintf(keys[i],"foo%x",i*31); Types::tString->set(&v,keys[i]); sprintf(vals[i],"bar%d",rand()%100000); Types::tString->set(&w,vals[i]); // printf("%s %s\n",keys[i],vals[i]); h.set(&v,&w); } for(int i=0;i<HCOUNT;i++){ Types::tString->set(&v,keys[i]); if(h.find(&v)){ w.copy(h.getval()); if(w.t != Types::tString) die("value not an int"); const StringBuffer& buf = w.toString(); if(strcmp(buf.get(),vals[i])){ printf("%s != %s\n",buf.get(),vals[i]); die("value mismatch"); } } else die("key not found"); } }
void t1(){ printf("test 1, integers----\n"); int keys[HCOUNT]; int vals[HCOUNT]; Hash h; Value v,w; for(int i=0;i<HCOUNT;i++){ keys[i]=i*31; Types::tInteger->set(&v,keys[i]); vals[i]=rand()%100000; Types::tInteger->set(&w,vals[i]); h.set(&v,&w); } for(int i=0;i<HCOUNT;i++){ Types::tInteger->set(&v,keys[i]); if(h.find(&v)){ w.copy(h.getval()); if(w.t != Types::tInteger) die("value not an int"); else if(w.toInt() != vals[i]){ printf("%d != %d\n",w.toInt(),vals[i]); die("value mismatch"); } } else die("key not found"); } }
/* Brief: TODO description * Parameter: index, TODO description * Parameter: role, TODO description * * Note: This function is called */ QVariant CDailyWeatherModel::data( const QModelIndex & index, int role ) const { int column = index.column(); int row = index.row(); //qDebug("from inside Data(), row:%i, col:%i, role=%i, counter=%i", row, column, role, counter); if (role == Qt::DisplayRole || role == Qt::EditRole) { if (mStation == NULL) return QVariant("n/a"); if (column == CDailyWeatherModel::YEAR) return QVariant(mYear); else if (column == CDailyWeatherModel::DOY) return QVariant(row+1); else { // Note: Be sure to use pointer here! Else, the map will be returned by value. This means // that a copy of this huge map will be made whenever this function is called (whenever // the model needs to be updated or the table needs to be redraen - potentially many times. // This will lag the interface. Hash* hash = mStation->getWeather(); QString yr = QString::number(mYear); QString doy = QString::number(row+1); Pair pair(yr, doy); // Find item in weather map with key Hash::const_iterator iter = hash->find(pair); // (Year, Day of Year) not found in weather data map for the current station if(iter == hash->end()) return QVariant("n/a"); return QVariant(iter.value()->at(column-2)); } } else if (role == Qt::TextAlignmentRole) return QVariant(Qt::AlignCenter); else return QVariant(); }
IntPair mode(const int *values, unsigned int size) { Hash::iterator it; Hash hash; for (int i = 0; i < size; i++) { it = hash.find(values[i]); if(it == hash.end()) { hash.insert( std::make_pair(values[i], 1)); } else { it->second += 1; } } unsigned int counts = 0; Hash::iterator found; for (it = hash.begin(); it != hash.end(); ++it) { if(it->second > counts) { counts = it->second; found = it; } } return *found; }
void findSmallestSubString (string word, unsigned int indexInString) { Hash::iterator nptr; bool checkForNewSmallest = false; /* its not their in words to check */ if( (nptr=words.find(word)) == words.end() ) return ; if( nptr->second->index == -1 ) noOfWords--; nptr->second->index = indexInString; if( start == NULL ) start = nptr->second; else if( start == nptr->second) { start = start->next; checkForNewSmallest = true; } if( end == NULL ) end = nptr->second; else if( end != nptr->second) { Node *n = nptr->second; if( n->prev ) n->prev->next = n->next; if( n->next ) n->next->prev = n->prev; n->prev = end; end->next = n; end = n; } if( noOfWords == 0 && checkForNewSmallest ) { if( smallestCount > end->index - start->index + 1 ) { smallestIndex = start->index; smallestCount = end->index - start->index + 1; } } }
/* Brief: TODO description * Parameter: index, TODO description * Parameter: value, TODO description * Parameter: role, TODO description */ bool CDailyWeatherModel::setData( const QModelIndex & index, const QVariant & value, int role ) { int column = index.column(); int row = index.row(); Hash* hash = mStation->getWeather(); QString yr = QString::number(mYear); QString doy = QString::number(row+1); Pair pair(yr, doy); // Find item in weather map with key Hash::iterator iter = hash->find(pair); QVector<QString>* values = iter.value(); if (index.isValid() && role == Qt::EditRole) { (*values)[column-2] = value.toString(); emit dataChanged(index, index); return true; } return false; }
int main() { Value a; assert(!a.isInteger()); assert(!a.isString()); assert(!a.isList()); assert(!a.isHash()); assert(a.type() == Value::UNDEFINED); assert(!a); a = Value(11); assert(a.isInteger()); assert(!a.isString()); assert(!a.isList()); assert(!a.isHash()); assert(a); assert(a.asInteger() == 11); a = Value("123"); assert(!a.isInteger()); assert(a.isString()); assert(!a.isList()); assert(!a.isHash()); assert(a); assert(a.asString() == "123"); List b; b.push_back(Value(11)); b.push_back(Value("111")); a = Value(b); b.push_back(Value(34)); assert(b.size() == 3); assert(a.isList()); assert(a.asList().size() == 2); Hash m; m["aa"] = Value(12); m["bb"] = Value(11); a = Value(m); m.erase("bb"); assert(m.size() == 1); assert(m["aa"].asInteger() == 12); assert(m.find("bb") == m.end()); assert(a.isHash()); assert(a.asHash().size() == 2); a = Value(Vector(1,-1,2)); assert(a.type() == Value::VECTOR); assert(a.isVector()); assert(a.asVector().y == -1); assert(a); a = Value(Position(1,-1,2)); assert(a.type() == Value::POSITION); assert(a.isPosition()); assert(a.asPosition().x == 1); assert(a); std::cout << "Success!" << std::endl; return 0; }
void tst_QHash::insert1() { const char *hello = "hello"; const char *world = "world"; const char *allo = "allo"; const char *monde = "monde"; { typedef QHash<QString, QString> Hash; Hash hash; QString key; for (int i = 0; i < 10; ++i) { key[0] = i + '0'; for (int j = 0; j < 10; ++j) { key[1] = j + '0'; hash.insert(key, "V" + key); } } for (int i = 0; i < 10; ++i) { key[0] = i + '0'; for (int j = 0; j < 10; ++j) { key[1] = j + '0'; hash.remove(key); } } } { typedef QHash<int, const char *> Hash; Hash hash; hash.insert(1, hello); hash.insert(2, world); QVERIFY(hash.size() == 2); QVERIFY(!hash.isEmpty()); { Hash hash2 = hash; hash2 = hash; hash = hash2; hash2 = hash2; hash = hash; hash2.clear(); hash2 = hash2; QVERIFY(hash2.size() == 0); QVERIFY(hash2.isEmpty()); } QVERIFY(hash.size() == 2); { Hash hash2 = hash; hash2[1] = allo; hash2[2] = monde; QVERIFY(hash2[1] == allo); QVERIFY(hash2[2] == monde); QVERIFY(hash[1] == hello); QVERIFY(hash[2] == world); hash2[1] = hash[1]; hash2[2] = hash[2]; QVERIFY(hash2[1] == hello); QVERIFY(hash2[2] == world); hash[1] = hash[1]; QVERIFY(hash[1] == hello); } { Hash hash2 = hash; hash2.detach(); hash2.remove(1); QVERIFY(hash2.size() == 1); hash2.remove(1); QVERIFY(hash2.size() == 1); hash2.remove(0); QVERIFY(hash2.size() == 1); hash2.remove(2); QVERIFY(hash2.size() == 0); QVERIFY(hash.size() == 2); } hash.detach(); { Hash::iterator it1 = hash.find(1); QVERIFY(it1 != hash.end()); Hash::iterator it2 = hash.find(0); QVERIFY(it2 != hash.begin()); QVERIFY(it2 == hash.end()); *it1 = monde; QVERIFY(*it1 == monde); QVERIFY(hash[1] == monde); *it1 = hello; QVERIFY(*it1 == hello); QVERIFY(hash[1] == hello); hash[1] = monde; QVERIFY(it1.key() == 1); QVERIFY(it1.value() == monde); QVERIFY(*it1 == monde); QVERIFY(hash[1] == monde); hash[1] = hello; QVERIFY(*it1 == hello); QVERIFY(hash[1] == hello); } { const Hash hash2 = hash; Hash::const_iterator it1 = hash2.find(1); QVERIFY(it1 != hash2.end()); QVERIFY(it1.key() == 1); QVERIFY(it1.value() == hello); QVERIFY(*it1 == hello); Hash::const_iterator it2 = hash2.find(2); QVERIFY(it1 != it2); QVERIFY(it1 != hash2.end()); QVERIFY(it2 != hash2.end()); int count = 0; it1 = hash2.begin(); while (it1 != hash2.end()) { count++; ++it1; } QVERIFY(count == 2); count = 0; it1 = hash.begin(); while (it1 != hash.end()) { count++; ++it1; } QVERIFY(count == 2); } { QVERIFY(hash.contains(1)); QVERIFY(hash.contains(2)); QVERIFY(!hash.contains(0)); QVERIFY(!hash.contains(3)); } { QVERIFY(hash.value(1) == hello); QVERIFY(hash.value(2) == world); QVERIFY(hash.value(3) == 0); QVERIFY(hash.value(1, allo) == hello); QVERIFY(hash.value(2, allo) == world); QVERIFY(hash.value(3, allo) == allo); QVERIFY(hash.value(0, monde) == monde); } { QHash<int,Foo> hash; for (int i = 0; i < 10; i++) hash.insert(i, Foo()); QVERIFY(Foo::count == 10); hash.remove(7); QVERIFY(Foo::count == 9); } QVERIFY(Foo::count == 0); { QHash<int, int*> hash; QVERIFY(((const QHash<int,int*>*) &hash)->operator[](7) == 0); } } }
void TextNavigation::onUpdate( float t ) { WindowText::onUpdate( t ); if ( visible() && activeTime() > UPDATE_NAV_TEXT ) { setActiveTime( 0 ); GameDocument * pDoc = (GameDocument *)document(); ASSERT( pDoc ); CharString sText; if ( pDoc->isTeamValid() ) { GameContext * pContext = pDoc->context(); ASSERT( pContext ); Hash< dword, Array<NounPlanet *> > factionPlanets; Hash< dword, Array<NounShip *> > factionShips; int nStars = 0; int nAsteroids = 0; int nGates = 0; // get the players teamId int nFactionId = pDoc->factionId(); // push all detected objects for(int z=0;z<pContext->zoneCount();z++) { NodeZone * pZone = pContext->zone( z ); for(int i=0;i<pZone->childCount();i++) { NounGame * pNoun = WidgetCast<NounGame>( pZone->child(i) ); if (! pNoun ) continue; if ( pNoun->isDetected( nFactionId ) ) { if ( WidgetCast<NounShip>( pNoun ) ) factionShips[ pNoun->factionId() ].push( (NounShip *)pNoun ); else if ( WidgetCast<NounPlanet>( pNoun ) ) factionPlanets[ pNoun->factionId() ].push( (NounPlanet *)pNoun ); else if ( WidgetCast<NounStar>( pNoun ) ) nStars++; else if ( WidgetCast<NounAsteroid>( pNoun ) ) nAsteroids++; else if ( WidgetCast<NounJumpGate>( pNoun ) ) nGates++; } } } // update navigation text information const GameContext::Team & fleet = pContext->team( pDoc->teamId() ); sText += CharString().format( "<b;l>%s</b;/l>:\n", fleet.name ); Array< NounShip * > & ships = factionShips[ fleet.factionId ]; sText += CharString().format("<x;10>Ships:<x;150>%d\n", ships.size() ); for(int k=0;k<NounShip::TYPE_COUNT;k++) { // how many ships of this type int count = 0; for(int j=0;j<ships.size();j++) if ( ships[j]->type() == (NounShip::Type)k ) count++; if ( count > 0 ) sText += CharString().format( "<X;20>%s:<X;150>%d\n", NounShip::typeText( (NounShip::Type)k ), count ); } if ( factionPlanets.find( fleet.factionId ).valid() ) { Array< NounPlanet *> & planets = factionPlanets[ fleet.factionId ]; sText += CharString().format("<X;10>Planets:<X;150>%d\n", planets.size() ); int population = 0; int ports = 0; int depots = 0; int yards = 0; int units = 0; for(int j=0;j<planets.size();j++) { NounPlanet * pPlanet = planets[ j ]; population += pPlanet->population(); units += pPlanet->friendlyUnitCount(); if ( pPlanet->flags() & NounPlanet::FLAG_HAS_DEPOT ) depots++; if ( pPlanet->flags() & NounPlanet::FLAG_HAS_PORT ) ports++; if ( pPlanet->flags() & NounPlanet::FLAG_HAS_SHIPYARD ) yards++; } if ( population > 0 ) sText += CharString().format( "<X;20>Population:<X;150>%d\n", population ); if ( units > 0 ) sText += CharString().format( "<X;20>Units:<X;150>%d\n", units ); if ( ports > 0 ) sText += CharString().format( "<X;20>Ports:<X;150>%d\n", ports ); if ( depots > 0 ) sText += CharString().format( "<X;20>Depots:<X;150>%d\n", depots ); if ( yards > 0 ) sText += CharString().format( "<X;20>Ship Yards:<X;150>%d\n", yards ); } } setText( sText ); } }