void Solver::InsertSort(Bucket& list)
{
    //对单个桶中的元素进行插入排序
    for(int i=0;i<list.length();++i){
        int trans=list.at(i);
        for(int j=i-1;j>=0;--j){
            if(list.at(j)>list.at(i))
                list.replace(j+1,list.at(j));
            else{
                list.replace(j+1,trans);
                break;
            }
        }
    }
}
void HashExtensible::save()
{
     std::ofstream *file= new ofstream(this->filename.c_str(),ios::trunc);
     
     pair<int,int> auxPair;
     Bucket auxBucket;
     
     pair<Key_Node,Refs> auxKey;
     
     *file<< "Bucket_Capacity"<<endl;
     *file<< this->bucketCapacity <<endl;
     
     for(int i=0; i<directory.size();i++)
     {           
          auxPair=directory.at(i);
          auxBucket=buckets.getBucket(auxPair.second);
          
          for(int j=0; j<auxBucket.size();j++)
          {
                auxKey=auxBucket.at(j);   
                auxKey.first.Serialize(file);
                *file<< "Value"<<endl;
                *file<< auxKey.second.Serialize()<<endl;
	//	*file<< auxKey.second.posReg<<endl;
                *file<< "Value_End"<<endl;              
          }
          
          
     }
   file->close();
   delete(file); 
}
Exemple #3
0
unsigned SortedDeque<T, TSize>::size_total()
{
	int count = 0;
	for (unsigned i = 0; i < deque.size(); i++)
	{
		Bucket bucket = deque.at(i);
		for (unsigned j = 0; j < bucket.size(); j++)
		{
			count += bucket.at(j).second;
		}
	}
	return count;
}
void HashExtensible::add(Key_Node &key, Refs value)
{ 
    //load();
	//cout << "HashExtensible::add: " << key.toInt() << endl;
	if (!contains(key))
	{ 
		int Dir = fHash(key);
		
		//Obtengo el bucket al que apunta la direcci�n que devolvi� la funci�n de Hash.
		int	nBucket = directory.getNBlockBucket(Dir);
		 
		if (nBucket == -1)
		{
			//No existe la direccion -> creo un Bucket, creo una nueva direcci�n que lo apunte e inserto la clave	
			int nBlockBucket = buckets.createBucket();
			directory.insertDir(Dir,nBlockBucket);	
			buckets.addRef(nBlockBucket);
			buckets.insertKey(nBlockBucket, key, value);
        }			
		else
		{
			while (buckets.isFull(nBucket))
			{
				//Si no se puede dividir -> duplico el directorio
				if (buckets.getBucket(nBucket).getRef() == 1)
				{
					buckets.duplicateRef(directory.getDirectory());
					directory.duplicateDirectory();
					this->actualbits++;
					directory.updateBits(actualbits);
				}
	
				//Creo un nuevo Bucket y redistribuyo
				int newBlockBucket = buckets.createBucket();
				//Redirecciono al nuevo Bucket y actualizo las referencias
				directory.redirect(nBucket, newBlockBucket, buckets.getBucket(nBucket).getRef());	

				int nRef = buckets.getBucket(nBucket).getRef()/2;
				buckets.updateRef(nBucket,nRef);
				buckets.updateRef(newBlockBucket, nRef);

				//Inserto las claves que colisionaron y redistribuyo.
				Bucket TempBuck = buckets.getBucket(nBucket);
				buckets.clearBucket(nBucket);
				for (int n= 0; n < TempBuck.size(); n++)
				{
					Key_Node k=Key_Node(TempBuck.at(n).first);
					Dir = fHash(k);
					nBucket = directory.getNBlockBucket(Dir);
					buckets.insertKey(nBucket, k, TempBuck.at(n).second );
					//this->add(k,TempBuck.at(n).second);		
				}
				
				//Obtengo la nueva direccion
				Dir = fHash(key);
				//Obtengo el bucket al que apunta la direcci�n que devolvi� la funci�n de Hash.
				nBucket = directory.getNBlockBucket(Dir);
			}
			
			buckets.insertKey(nBucket, key, value);
		}
	
	}
	else
    {
     cout<<"Error la clave ya existe"<<endl;;    
    }
	
}