uint64_t PrimeSieve::nthPrime(int64_t n, uint64_t start) { setStart(start); double t1 = getWallTime(); if (n == 0) n = 1; // Like Mathematica else if (n > 0) start = add_overflow_safe(start, 1); else if (n < 0) start = sub_underflow_safe(start, 1); uint64_t stop = start; uint64_t dist = nthPrimeDist(n, 0, start); uint64_t nthPrimeGuess = add_overflow_safe(start, dist); int64_t count = 0; int64_t tinyN = 10000; tinyN = max(tinyN, pix(isqrt(nthPrimeGuess))); while ((n - count) > tinyN || sieveBackwards(n, count, stop)) { if (count < n) { checkLimit(start); dist = nthPrimeDist(n, count, start); stop = add_overflow_safe(start, dist); count += countPrimes(start, stop); start = add_overflow_safe(stop, 1); } if (sieveBackwards(n, count, stop)) { checkLowerLimit(stop); dist = nthPrimeDist(n, count, stop); start = sub_underflow_safe(start, dist); count -= countPrimes(start, stop); stop = sub_underflow_safe(start, 1); } } if (n < 0) count -= 1; checkLimit(start); uint64_t overValue = 3; dist = nthPrimeDist(n, count, start) * overValue; stop = add_overflow_safe(start, dist); NthPrime np; np.findNthPrime(n - count, start, stop); seconds_ = getWallTime() - t1; return np.getNthPrime(); }
/** * \brief Call this when you need to update vCard in cache. */ void VCardFactory::setVCard(const Jid &j, const VCard &v) { VCard *vcard = new VCard; *vcard = v; checkLimit(j.userHost(), vcard); emit vcardChanged(j); }
void Joystick::update(float dt) { // 조이스틱 - 포지션 값을 계속 업데이트 해줌 joystick_control->setPosition(controlerPos); // 터치 도중일때만 움직임 if (_isTouch) { // 움직일 거리 구하기 float moveX = controlerPos.x - centerPos.x; float moveY = controlerPos.y - centerPos.y; Vec2 charPos = mainChar->getPosition(); if (checkLimit()) { if (0 < charPos.x + moveX * _speed && charPos.x + moveX * _speed < _winSize.width) { charPos.x += moveX * _speed; } if (0 < charPos.y + moveY * _speed && charPos.y + moveY * _speed < _winSize.height) { charPos.y += moveY * _speed; } mainChar->setPosition(charPos); } else { charPos.x += moveX * _speed; charPos.y += moveY * _speed; mainChar->setPosition(charPos); } } }
/* Check if the entry is open and on the lru list--- if not re-open it @throws SystemException if the lru limit is reached and the whole lru list is pinned. */ void FileManager::checkActive(File& file) { ScopedMutexLock scm(_fileLock); assert(*(file._listPos) == &file); /* Check if the entry is already open, if so, update the lru and mark it pinned */ if (file._fd >= 0) { _lru.erase(file._listPos); _lru.push_front(&file); file._listPos = _lru.begin(); file._pin++; return; } /* Make sure there is room in the lru */ checkLimit(); /* Try to open the file using the saved flags */ file._fd = File::openFile(file._path, file._flags); /* Remove from the closed list and add to the lru */ file._pin++; _closed.erase(file._listPos); _lru.push_front(&file); file._listPos = _lru.begin(); }
void DateTime::normalize() { checkLimit(_microsecond, _millisecond, 999); checkLimit(_millisecond, _second, 999); checkLimit(_second, _minute, 59); checkLimit(_minute, _hour, 59); checkLimit(_hour, _day, 23); if (_day > daysOfMonth(_year, _month)) { _day -= daysOfMonth(_year, _month); if (++_month > 12) { ++_year; _month -= 12; } } }
void DateTime::normalize() { checkLimit(m_microsecond, m_millisecond, 1000); checkLimit(m_millisecond, m_second, 1000); checkLimit(m_second, m_minute, 60); checkLimit(m_minute, m_hour, 60); checkLimit(m_hour, m_day, 24); if (m_day > daysOfMonth(m_year, m_month)) { m_day -= daysOfMonth(m_year, m_month); if (++m_month > 12) { ++m_year; m_month -= 12; } } }
/* Add a new open entry to the lru */ void FileManager::addFd(File& file) { ScopedMutexLock scm(_fileLock); checkInit(); checkLimit(); /* Add to the LRU */ _lru.push_front(&file); file._listPos = _lru.begin(); }
/** * \brief Call this, when you need a cached vCard. */ const VCard* VCardFactory::vcard(const Jid &j) { // first, try to get vCard from runtime cache if (vcardDict_.contains(j.userHost())) { return vcardDict_[j.userHost()]; } // then try to load from cache on disk QFile file ( vCardsDir_ + "/" + JIDUtil::encode(j.userHost()).lower() + ".xml" ); file.open (QIODevice::ReadOnly); QDomDocument doc; VCard *vcard = new VCard; if ( doc.setContent(&file, false) ) { vcard->fromXml( doc.documentElement() ); checkLimit(j.userHost(), vcard); return vcard; } delete vcard; return 0; }
void VCardFactory::taskFinished() { JT_VCard *task = (JT_VCard *)sender(); if ( task->success() ) { Jid j = task->jid(); VCard *vcard = new VCard; *vcard = task->vcard(); checkLimit(j.userHost(), vcard); // save vCard to disk QFile file ( vCardsDir_ + "/" + JIDUtil::encode(j.userHost()).lower() + ".xml" ); file.open ( QIODevice::WriteOnly ); QTextStream out ( &file ); out.setEncoding ( QTextStream::UnicodeUTF8 ); QDomDocument doc; doc.appendChild( vcard->toXml ( &doc ) ); out << doc.toString(4); emit vcardChanged(j); } }
void NameProfile::Set_name2_num(int num) { checkLimit(num); this->name2_num = num; isSet = true; }
void NameProfile::Set_lastname_num(int num) { checkLimit(num); this->lastname_num = num; }
void Enemy::move(Screen& screen, Player &player) { int nextTileC1; int nextTileC2; double tempX; double tempY; int nextTile; double tempC1; double tempC2; direction = calculateDirection(player); switch (direction) { case U: //Animation section animationRow = 1; frameCounter++; if (frameCounter >= frameDelay) { frameCounter = 0; if (animationOrder == 1) currentFrame++; else if (animationOrder == -1) currentFrame--; if (currentFrame >= 2) animationOrder = -1; else if (currentFrame <= 0) animationOrder = 1; } //Next-tile checking section tempY = y - boundY; nextTile = curTile; tempY -= velocity; nextTile = (x / (TILESIZE)) + (((int)(tempY / (TILESIZE))) * NBTILESWIDTH); //nextTileC1 and nextTileC2 are used to check if the enemy collide with any of the next tiles on its way nextTileC1 = ((x - boundX) / (TILESIZE)) + (((int)((tempY) / (TILESIZE))) * NBTILESWIDTH); nextTileC2 = ((x + boundX) / (TILESIZE)) + (((int)((tempY) / (TILESIZE))) * NBTILESWIDTH); if (screen.tiles[curTile]->getIsLimit() == true) checkLimit(U, screen, nextTileC1, nextTileC2); if (curTile != nextTileC1 || curTile != nextTileC2) { if (screen.tiles[nextTileC1]->getIsObstacle() == false && screen.tiles[nextTileC2]->getIsObstacle() == false) { y -= velocity; curTile = nextTile; } else direction = D; } else y -= velocity; break; case D: //Animation section animationRow = 0; frameCounter++; if (frameCounter >= frameDelay) { frameCounter = 0; if (animationOrder == 1) currentFrame++; else if (animationOrder == -1) currentFrame--; if (currentFrame >= 2) animationOrder = -1; else if (currentFrame <= 0) animationOrder = 1; } //Next-tile checking section tempY = y + boundY; nextTile = curTile; tempY += velocity; nextTile = (x / (TILESIZE)) + (((int)(tempY / (TILESIZE))) * NBTILESWIDTH); //nextTileC1 and nextTileC2 are used to check if the enemy collide with any of the next tiles on its way nextTileC1 = ((x - boundX) / (TILESIZE)) + (((int)((tempY) / (TILESIZE))) * NBTILESWIDTH); nextTileC2 = ((x + boundX) / (TILESIZE)) + (((int)((tempY) / (TILESIZE))) * NBTILESWIDTH); if (screen.tiles[curTile]->getIsLimit() == true) checkLimit(D, screen, nextTileC1, nextTileC2); if (curTile != nextTileC1 || curTile != nextTileC2) { if (screen.tiles[nextTileC1]->getIsObstacle() == false && screen.tiles[nextTileC2]->getIsObstacle() == false) { y += velocity; curTile = nextTile; } else direction = U; } else y += velocity; break; case L: //Animation section animationRow = 3; frameCounter++; if (frameCounter >= frameDelay) { frameCounter = 0; if (animationOrder == 1) currentFrame++; else if (animationOrder == -1) currentFrame--; if (currentFrame >= 2) animationOrder = -1; else if (currentFrame <= 0) animationOrder = 1; } //Next-tile checking section tempX = x - boundX; tempC1 = y - boundY; tempC2 = y + boundY; nextTile = curTile; tempX -= velocity; //nextTileC1 and nextTileC2 are used to check if the enemy collide with any of the next tiles on its way nextTileC1 = (tempX / (TILESIZE)) + (((int)((y - boundY) / (TILESIZE))) * NBTILESWIDTH); nextTileC2 = (tempX / (TILESIZE)) + (((int)((y + boundY) / (TILESIZE))) * NBTILESWIDTH); nextTile = (tempX / (TILESIZE)) + (((int)(y / (TILESIZE))) * NBTILESWIDTH); //nextTile à partir du centre if (screen.tiles[curTile]->getIsLimit() == true) checkLimit(L, screen, nextTileC1, nextTileC2); if (curTile != nextTileC1 || curTile != nextTileC2) { if (screen.tiles[nextTileC1]->getIsObstacle() == false && screen.tiles[nextTileC2]->getIsObstacle() == false) { x -= velocity; curTile = nextTile; } else direction = R; } else x -= velocity; break; case R: //Animation section animationRow = 2; frameCounter++; if (frameCounter >= frameDelay) { frameCounter = 0; if (animationOrder == 1) currentFrame++; else if (animationOrder == -1) currentFrame--; if (currentFrame >= 2) animationOrder = -1; else if (currentFrame <= 0) animationOrder = 1; } //Next-tile checking section tempX = x + boundX; nextTile = curTile; tempX += velocity; nextTile = (tempX / (TILESIZE)) + (((int)(y / (TILESIZE))) * NBTILESWIDTH); //nextTileC1 and nextTileC2 are used to check if the enemy collide with any of the next tiles on its way nextTileC1 = (tempX / (TILESIZE)) + (((int)((y - boundY) / (TILESIZE))) * NBTILESWIDTH); nextTileC2 = (tempX / (TILESIZE)) + (((int)((y + boundY) / (TILESIZE))) * NBTILESWIDTH); if (screen.tiles[curTile]->getIsLimit() == true) checkLimit(R, screen, nextTileC1, nextTileC2); if (curTile != nextTileC1 || curTile != nextTileC2) { if (screen.tiles[nextTileC1]->getIsObstacle() == false && screen.tiles[nextTileC2]->getIsObstacle() == false) { x += velocity; curTile = nextTile; } else direction = L; } else x += velocity; break; } }
void Enemy::move(Screen& screen) { int nextTileC1; int nextTileC2; double tempX; double tempY; int nextTile; double tempC1; double tempC2; switch (direction) { case U: tempY = y - boundY; nextTile = curTile; tempY -= velocity; nextTile = (x / (TILESIZE)) + (((int)(tempY / (TILESIZE))) * NBTILESWIDTH); nextTileC1 = ((x - boundX) / (TILESIZE)) + (((int)((tempY) / (TILESIZE))) * NBTILESWIDTH); nextTileC2 = ((x + boundX) / (TILESIZE)) + (((int)((tempY) / (TILESIZE))) * NBTILESWIDTH); if (screen.tiles[curTile]->getIsLimit() == true) checkLimit(U, screen, nextTileC1, nextTileC2); if (curTile != nextTileC1 || curTile != nextTileC2) { if (screen.tiles[nextTileC1]->getIsObstacle() == false && screen.tiles[nextTileC2]->getIsObstacle() == false) { y -= velocity; curTile = nextTile; } else direction = D; } else y -= velocity; break; case D: tempY = y + boundY; nextTile = curTile; tempY += velocity; nextTile = (x / (TILESIZE)) + (((int)(tempY / (TILESIZE))) * NBTILESWIDTH); nextTileC1 = ((x - boundX) / (TILESIZE)) + (((int)((tempY) / (TILESIZE))) * NBTILESWIDTH); nextTileC2 = ((x + boundX) / (TILESIZE)) + (((int)((tempY) / (TILESIZE))) * NBTILESWIDTH); if (screen.tiles[curTile]->getIsLimit() == true) checkLimit(D, screen, nextTileC1, nextTileC2); if (curTile != nextTileC1 || curTile != nextTileC2) { if (screen.tiles[nextTileC1]->getIsObstacle() == false && screen.tiles[nextTileC2]->getIsObstacle() == false) { y += velocity; curTile = nextTile; } else direction = U; } else y += velocity; break; case L: tempX = x - boundX; tempC1 = y - boundY; tempC2 = y + boundY; nextTile = curTile; tempX -= velocity; nextTileC1 = (tempX / (TILESIZE)) + (((int)((y - boundY) / (TILESIZE))) * NBTILESWIDTH); nextTileC2 = (tempX / (TILESIZE)) + (((int)((y + boundY) / (TILESIZE))) * NBTILESWIDTH); nextTile = (tempX / (TILESIZE)) + (((int)(y / (TILESIZE))) * NBTILESWIDTH); if (screen.tiles[curTile]->getIsLimit() == true) checkLimit(L, screen, nextTileC1, nextTileC2); if (curTile != nextTileC1 || curTile != nextTileC2) { if (screen.tiles[nextTileC1]->getIsObstacle() == false && screen.tiles[nextTileC2]->getIsObstacle() == false) { x -= velocity; curTile = nextTile; } else direction = R; } else x -= velocity; break; case R: tempX = x + boundX; nextTile = curTile; tempX += velocity; nextTile = (tempX / (TILESIZE)) + (((int)(y / (TILESIZE))) * NBTILESWIDTH); nextTileC1 = (tempX / (TILESIZE)) + (((int)((y - boundY) / (TILESIZE))) * NBTILESWIDTH); nextTileC2 = (tempX / (TILESIZE)) + (((int)((y + boundY) / (TILESIZE))) * NBTILESWIDTH); if (screen.tiles[curTile]->getIsLimit() == true) checkLimit(R, screen, nextTileC1, nextTileC2); if (curTile != nextTileC1 || curTile != nextTileC2) { if (screen.tiles[nextTileC1]->getIsObstacle() == false && screen.tiles[nextTileC2]->getIsObstacle() == false) { x += velocity; curTile = nextTile; } else direction = L; } else x += velocity; break; } }