void BufferIODevice::addData( int block, const QByteArray& ba ) { Q_D( BufferIODevice ); { QMutexLocker lock( &d->mut ); while ( d->buffer.count() <= block ) d->buffer << QByteArray(); d->buffer.replace( block, ba ); } // If this was the last block of the transfer, check if we need to fill up gaps if ( block + 1 == maxBlocks() ) { if ( nextEmptyBlock() >= 0 ) { emit blockRequest( nextEmptyBlock() ); } } d->received += ba.count(); emit bytesWritten( ba.count() ); emit readyRead(); }
bool CSolverDoc::init_with_nums(const std::vector< std::vector<size_t> >& nums) { int all = std::for_each(nums.begin(), nums.end(), sum()); if (all <= 0) return false; int half = all / 2; if (half * 2 != all) return false; int s = 0; int i; for (i = 0; s < half; ++i) s += std::for_each(nums[i].begin(), nums[i].end(), sum()); if (s != half) return false; newField(i, nums.size() - i); m_cols_blocks.assign(nums.begin(), nums.begin() + i); m_rows_blocks.assign(nums.begin() + i, nums.end()); for (size_t i = 0; i < m_cols_blocks.size(); ++i) m_cols_blocks_flags[i].resize(m_cols_blocks[i].size(), false); for (size_t i = 0; i < m_rows_blocks.size(); ++i) m_rows_blocks_flags[i].resize(m_rows_blocks[i].size(), false); m_max_col_blocks = maxBlocks(m_cols_blocks); m_max_row_blocks = maxBlocks(m_rows_blocks); return true; }
void CSolverDoc::newField(int rows, int cols) { // clear, because SDI will reuse same document m_rows_blocks.clear(); m_cols_blocks.clear(); m_rows_blocks_flags.clear(); m_cols_blocks_flags.clear(); m_field = m_gess = std::string(rows * cols, unknown_cell); m_rows = rows; m_cols = cols; m_rows_blocks.resize(rows); m_cols_blocks.resize(cols); m_rows_blocks_flags.resize(rows); m_cols_blocks_flags.resize(cols); m_max_col_blocks = maxBlocks(m_cols_blocks); m_max_row_blocks = maxBlocks(m_rows_blocks); }
int BufferIODevice::nextEmptyBlock() const { Q_D( const BufferIODevice ); int i = 0; foreach( const QByteArray& ba, d->buffer ) { if ( ba.isEmpty() ) return i; i++; } if ( i == maxBlocks() ) return -1; return i; }
QByteArray BufferIODevice::getData( qint64 pos, qint64 size ) { Q_D( BufferIODevice ); // qDebug() << Q_FUNC_INFO << pos << size << 1; QByteArray ba; int block = blockForPos( pos ); int offset = offsetForPos( pos ); QMutexLocker lock( &d->mut ); while( ba.count() < size ) { if ( block > maxBlocks() ) break; if ( isBlockEmpty( block ) ) break; ba.append( d->buffer.at( block++ ).mid( offset ) ); } // qDebug() << Q_FUNC_INFO << pos << size << 2; return ba.left( size ); }