コード例 #1
0
ファイル: hashset.cpp プロジェクト: rusingineer/quazaa
/**
 * @brief HashVector::insert Allows to insert the hashes managed by an other HashVector into this
 * one. If there is a conflict, neither HashVector is modified.
 * @param other The other HashVector.
 * @return true if the insertion was successful (e.g. no conflicts); false otherwise.
 */
bool HashSet::insert( const HashSet& other )
{
	std::vector<Hash*> vNewHashes;
	vNewHashes.reserve( other.hashCount() );

	for ( quint8 i = 0; i < Hash::NO_OF_TYPES; ++i )
	{
		// if there is a hash at position i
		if ( other.m_pHashes[i] )
		{
			if ( !conflicts( *other.m_pHashes[i] ) )
			{
				// if it doesn't clash with an existing hash
				// and we don't have a hash for that type yet
				if ( !m_pHashes[i] )
				{
					// remember the hash for later insertion
					vNewHashes.push_back( new Hash( *other.m_pHashes[i] ) );
				}
			}
			else
			{
				// conflict detected; clear remembered hashes
				for ( size_t i = 0, nMax = vNewHashes.size(); i < nMax; ++i )
				{
					delete vNewHashes[i];
				}
				return false;
			}
		}
	}

	// no conflicts detected; insert the hashes
	for ( size_t i = 0, nMax = vNewHashes.size(); i < nMax; ++i )
	{
		const quint8 type = vNewHashes[i]->type();
		m_pHashes[type] = vNewHashes[i];
	}

	return true;
}