VS calc( VS s ) { Set S; for ( VS::iterator it_i = s.begin(); it_i != s.end(); ++ it_i ) { string a = *it_i; string b = rotate(a); cout << a << ", " << b << endl; if ( ! is_valid(a) ) { if ( is_valid(b) ) { S.insert(b); } } else if ( ! is_valid(b) ) { if ( is_valid(a) ) { S.insert(a); } } else { if ( S.count(a) ) S.insert(b); else if ( S.count(b) ) S.insert(a); else S.insert(min(a, b)); } } if ( S.size() != s.size() ) return VS(); VS res(S.begin(), S.end()); return res; }
void UndoManager::CalculateSpaceUsage() { TIMER_START( "CalculateSpaceUsage", space_calc ); TrackListOfKindIterator iter(Track::Wave); space.Clear(); space.Add(0, stack.GetCount()); Set s1, s2; Set *prev = &s1; Set *cur = &s2; for (size_t i = 0, cnt = stack.GetCount(); i < cnt; i++) { // Swap map pointers std::swap(cur, prev); // And clean out the NEW current map cur->clear(); // Scan all tracks at current level WaveTrack *wt = (WaveTrack *) iter.First(stack[i]->tracks); while (wt) { // Scan all clips within current track WaveClipList::compatibility_iterator it = wt->GetClipIterator(); while (it) { // Scan all blockfiles within current clip BlockArray *blocks = it->GetData()->GetSequenceBlockArray(); for (size_t b = 0, cnt = blocks->size(); b < cnt; b++) { BlockFile *file = (*blocks)[b].f; // Accumulate space used by the file if the file didn't exist // in the previous level if (prev->count(file) == 0 && cur->count(file) == 0) { space[i] += file->GetSpaceUsage().GetValue(); } // Add file to current set cur->insert(file); } it = it->GetNext(); } wt = (WaveTrack *) iter.Next(); } } TIMER_STOP( space_calc ); }
std::deque<Bid> SearchEngine::processAds(std::string inStr) { // Bring input to lowercase std::string line; std::transform(inStr.begin(),inStr.end(),inStr.begin(),::tolower); // Get only the first word std::stringstream ss; ss << inStr; ss >> inStr; // And then the rest of the line getline(ss,line); // Add on blank space to allow last word to be processed if AND/OR line += "\n"; // Make set of words used Set<std::string> searchterms; if(inStr == "and" || inStr == "or") { std::string word; for(unsigned int i=0; i<line.size(); ++i) { if(alphanumeric(line[i])) word += line[i]; else { // This means we're at end of the word - check for content if(word.size() > 0) { // Put the query to lowercase and try to get; if not, make an entry if necessary std::transform(word.begin(),word.end(),word.begin(),::tolower); // Then add it to result (doesn't matter if already contained for insert function) searchterms.insert(word); // Reset word for the next word in query word = ""; } } } } else searchterms.insert(inStr); // Carter's search algorithm, reimplemented std::deque<Bid> relevantBids; Set<std::string> companies; for(std::deque<Bid>::iterator it = bids.begin(); it != bids.end(); ++it) { if(searchterms.count(it->searchTerm) == 1 && companies.count(it->company) == 0) // checks if search term included { // Should be in order relevantBids.push_back(*it); companies.insert(it->company); } } return relevantBids; }
void excludeByType( ObjectList& list, const Set& set ) { for( auto it=list.begin(); it != list.end(); ) { if( set.count( (*it)->type() ) > 0 ) { it=list.erase( it ); } else { ++it; } } }
bool overlaps(const Set& set, const Iterator& begin, const Iterator& end) { for (auto iter=begin; iter != end; iter++) { if (set.count(*iter)) { return true; } } return false; }
void TestTools::set_initializerList() { Set<int> set = {1, 1, 2, 3, 4, 5}; QCOMPARE(set.count(), 5); QVERIFY(set.contains(1)); QVERIFY(set.contains(2)); QVERIFY(set.contains(3)); QVERIFY(set.contains(4)); QVERIFY(set.contains(5)); // check _which_ of the equal elements gets inserted (in the QHash/QMap case, it's the last): const Set<IdentityTracker> set2 = {{1, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}; QCOMPARE(set2.count(), 5); const int dummy = -1; const IdentityTracker searchKey = {1, dummy}; QCOMPARE(set2.find(searchKey)->id, 0); Set<int> emptySet{}; QVERIFY(emptySet.isEmpty()); Set<int> set3{{}, {}, {}}; QVERIFY(!set3.isEmpty()); }
void EGLViewProtocol::getSetOfTouchesEndOrCancel(Set& set, int num, int ids[], float xs[], float ys[]) { for (int i = 0; i < num; ++i) { int id = ids[i]; float x = xs[i]; float y = ys[i]; Integer* pIndex = (Integer*)s_TouchesIntergerDict.objectForKey(id); if (pIndex == NULL) { CCLOG("if the index doesn't exist, it is an error"); continue; } /* Add to the set to send to the director */ Touch* pTouch = s_pTouches[pIndex->getValue()]; if (pTouch) { CCLOGINFO("Ending touches with id: %d, x=%f, y=%f", id, x, y); pTouch->setTouchInfo(pIndex->getValue(), (x - _viewPortRect.origin.x) / _scaleX, (y - _viewPortRect.origin.y) / _scaleY); set.addObject(pTouch); // release the object pTouch->release(); s_pTouches[pIndex->getValue()] = NULL; removeUsedIndexBit(pIndex->getValue()); s_TouchesIntergerDict.removeObjectForKey(id); } else { CCLOG("Ending touches with id: %d error", id); return; } } if (set.count() == 0) { CCLOG("touchesEnded or touchesCancel: count = 0"); return; } }
Int rec( const Int& k, const Int& rem, const Int& x ) { if ( N - k <= std::min(in->N, Int(5)) ) { if ( ! S.count(x) ) return 0; } if ( x < 0 || x - rem > 0 ) return 0; if ( k >= N ) { return x == 0 ? 1 : 0; } if ( memo[k].count(x) ) return memo[k][x]; Int& res = memo[k][x]; res = 0; res += rec(k + 1, rem - A[k], x); res += rec(k + 1, rem - A[k], x - A[k]); return res; }
void EGLViewProtocol::handleTouchesBegin(int num, int ids[], float xs[], float ys[]) { Set set; for (int i = 0; i < num; ++i) { int id = ids[i]; float x = xs[i]; float y = ys[i]; Integer* pIndex = (Integer*)s_TouchesIntergerDict.objectForKey(id); int nUnusedIndex = 0; // it is a new touch if (pIndex == NULL) { nUnusedIndex = getUnUsedIndex(); // The touches is more than MAX_TOUCHES ? if (nUnusedIndex == -1) { CCLOG("The touches is more than MAX_TOUCHES, nUnusedIndex = %d", nUnusedIndex); continue; } Touch* pTouch = s_pTouches[nUnusedIndex] = new Touch(); pTouch->setTouchInfo(nUnusedIndex, (x - _viewPortRect.origin.x) / _scaleX, (y - _viewPortRect.origin.y) / _scaleY); //CCLOG("x = %f y = %f", pTouch->getLocationInView().x, pTouch->getLocationInView().y); Integer* pInterObj = new Integer(nUnusedIndex); s_TouchesIntergerDict.setObject(pInterObj, id); set.addObject(pTouch); pInterObj->release(); } } if (set.count() == 0) { CCLOG("touchesBegan: count = 0"); return; } _delegate->touchesBegan(&set, NULL); }
Provenance::Set* Provenance::ancestors(const Set& roots) { Set open = roots; Set* closed = new Set(); while (!open.empty()) { const Provenance* n = *(open.begin()); std::pair<CommandChronicle::ParentVector::const_iterator, CommandChronicle::ParentVector::const_iterator> pr = n->parents(); for (CommandChronicle::ParentVector::const_iterator it = pr.first; it != pr.second; ++it) { const Provenance* prov = *it; if (closed->count(prov) == 0) open.insert(prov); } open.erase(n); closed->insert(n); } return closed; }
void EGLViewProtocol::handleTouchesMove(int num, int ids[], float xs[], float ys[]) { Set set; for (int i = 0; i < num; ++i) { int id = ids[i]; float x = xs[i]; float y = ys[i]; Integer* pIndex = (Integer*)s_TouchesIntergerDict.objectForKey(id); if (pIndex == NULL) { CCLOG("if the index doesn't exist, it is an error"); continue; } CCLOGINFO("Moving touches with id: %d, x=%f, y=%f", id, x, y); Touch* pTouch = s_pTouches[pIndex->getValue()]; if (pTouch) { pTouch->setTouchInfo(pIndex->getValue(), (x - _viewPortRect.origin.x) / _scaleX, (y - _viewPortRect.origin.y) / _scaleY); set.addObject(pTouch); } else { // It is error, should return. CCLOG("Moving touches with id: %d error", id); return; } } if (set.count() == 0) { CCLOG("touchesMoved: count = 0"); return; } _delegate->touchesMoved(&set, NULL); }
void TestTools::set_size() { Set<int> set; QVERIFY(set.size() == 0); QVERIFY(set.isEmpty()); QVERIFY(set.count() == set.size()); set.insert(1); QVERIFY(set.size() == 1); QVERIFY(!set.isEmpty()); QVERIFY(set.count() == set.size()); set.insert(1); QVERIFY(set.size() == 1); QVERIFY(!set.isEmpty()); QVERIFY(set.count() == set.size()); set.insert(2); QVERIFY(set.size() == 2); QVERIFY(!set.isEmpty()); QVERIFY(set.count() == set.size()); set.remove(1); QVERIFY(set.size() == 1); QVERIFY(!set.isEmpty()); QVERIFY(set.count() == set.size()); set.remove(1); QVERIFY(set.size() == 1); QVERIFY(!set.isEmpty()); QVERIFY(set.count() == set.size()); set.remove(2); QVERIFY(set.size() == 0); QVERIFY(set.isEmpty()); QVERIFY(set.count() == set.size()); }
main() { Set<int> s; assert(s.size() == 0); assert(s.empty()); s.insert(10); Set<int>::iterator iter = s.begin(); assert(*iter == 10); s.insert(6); s.insert(6); assert(s.count(6) == 1); assert(s.count(10) == 1); assert(s.count(12) == 0); iter = s.begin(); assert(*iter == 6); ++iter; assert(*iter == 10); ++iter; assert(iter == s.end()); s.insert(7); s.insert(9); s.insert(9); s.insert(8); s.insert(11); iter = s.begin(); assert(*iter == 6); ++iter; assert(*iter == 7); ++iter; assert(*iter == 8); ++iter; assert(*iter == 9); ++iter; assert(*iter == 10); ++iter; assert(*iter == 11); Set<int> s2; s2.insert(3); s2.insert(7); s2.insert(-1); s2.insert(16); s2.insert(11); s2.insert(4); iter = s2.find(3); assert(*iter == 3); iter = s2.find(888); assert(iter == s2.end()); s2.erase(7); iter = s2.begin(); assert(*iter == -1); ++iter; assert(*iter == 3); ++iter; assert(*iter == 4); ++iter; assert(*iter == 11); ++iter; assert(*iter == 16); ++iter; assert(iter == s2.end()); s2.erase(16); iter = s2.begin(); assert(*iter == -1); ++iter; assert(*iter == 3); ++iter; assert(*iter == 4); ++iter; assert(*iter == 11); ++iter; assert(iter == s2.end()); s2.erase(3); iter = s2.begin(); assert(*iter == -1); ++iter; assert(*iter == 4); ++iter; assert(*iter == 11); ++iter; assert(iter == s2.end()); s2.erase(11); iter = s2.begin(); assert(*iter == -1); ++iter; assert(*iter == 4); ++iter; assert(iter == s2.end()); s2.erase(-1); iter = s2.begin(); assert(*iter == 4); ++iter; assert(iter == s2.end()); s2.erase(4); iter = s2.begin(); assert(iter == s2.end()); cout << "All tests passed." << endl; }