Exemple #1
0
void SCHashTable<K,V>::resizeTable() {
    typename list< pair<K,V> >::iterator it;
    /**
     * @todo Implement this function.
     *
     * Please read the note in the spec about list iterators!
     * The size of the table should be the closest prime to size * 2.
     *
     * @hint Use findPrime()!
     */
    size_t doubleSize = findPrime(size*2);

    list<pair<K, V> > * temp = new list< pair<K,V> >[doubleSize];
    
    for (size_t i = 0; i < size; i++)
    {   
        for (it = table[i].begin(); it != table[i].end(); it++)
        {
           int id = hash(it->first, doubleSize);
           pair<K,V> pear(it->first, it->second);
           
           temp[id].push_back(pear);
        }
    }
    
    delete [] table;
    table = temp;
    size = doubleSize;


}
void LPHashTable<K,V>::resizeTable() {
    size_t newSize = findPrime( size * 2 );
    pair<K,V> ** temp = new pair<K,V>*[newSize];
    delete [] should_probe;
    should_probe = new bool[newSize];
    for( int i = 0; i < newSize; i++ ) {
        temp[i] = NULL;
        should_probe[i] = false;
    }

    for( int i = 0; i < size; i++ ) {
        if( table[i] != NULL ) {
            int idx = hash( table[i]->first, newSize );
            while( temp[idx] != NULL )
                idx = ( idx + 1 ) % newSize;
            temp[idx] = table[i];
            should_probe[idx] = true;
        }
    }

    delete [] table;
    // don't delete elements since we just moved their pointers around
    table = temp;
    size = newSize;
}
void SCHashTable<K,V>::resizeTable() {
    typename list< pair<K,V> >::iterator it;
    /**
     * @todo Implement this function.
     *
     * Please read the note in the spec about list iterators!
     * The size of the table should be the closest prime to size * 2.
     *
     * @hint Use findPrime()!
     */
    
    size_t new_size = findPrime( 2*size );     
	std::list< std::pair<K, V> > * my_table = new list< pair<K,V> >[new_size];
		
	for( int i = 0; i < size; i++)
	{
		for (it = table[i].begin(); it != table[i].end(); it++ ) 
		{	
			int index = hash(it->first, new_size);
    		pair<K,V> p(it->first, it->second);
    		my_table[index].push_back(p);
		}
	}

    delete [] table;
    table = my_table;
    size = new_size;

}
Exemple #4
0
void LPHashTable<K,V>::insert( K const & key, V const & value ) {
    /**
     * @todo Implement this function.
     *
     * @note Remember to resize the table when necessary (load factor >=
     *  0.7). **Do this check *after* increasing elems!!** Also, don't
     *  forget to mark the cell for probing with should_probe!
     */
		
	elems++;
	
	if((double)elems/size>=0.7)
	resizeTable();	//
		
	int idx = hash(key, size);
        while(should_probe[idx])
	{        
		idx = (idx + 1) % size;
        }
        
	if (table[idx] == NULL)
        {
                table[idx] = new pair<K,V>(key, value);
                should_probe[idx] = true;
         }	
	
	
	
	

}
void LPHashTable<K,V>::insert( K const & key, V const & value ) {
    /**
     * @todo Implement this function.
     *
     * @note Remember to resize the table when necessary (load factor >=
     *  0.7). **Do this check *after* increasing elems!!** Also, don't
     *  forget to mark the cell for probing with should_probe!
     */
	++elems;
	if(shouldResize())
		resizeTable();
        
    if(findIndex(key) == -1){
    		int temp = hash(key,size);
    		if(table[temp] == NULL){
    			table[temp] = new std::pair<K,V>(key,value);
    			should_probe[temp] = true;
    		
    		}else{
    		int i = temp + 1;
    		for( ; table[i%size] != table[temp]; i++){
    			if(table[i%size] == NULL){
    				table[i%size] = new std::pair<K,V>(key,value);
    				should_probe[i%size] = true;
    				return;
    			
    			}
    		
    		
    		}
    	
    	
 		  }   	
    	}
}
void SCHashTable<K,V>::resizeTable() {
    typename list< pair<K,V> >::iterator it;
    /**
     * @todo Implement this function.
     *
     * Please read the note in the spec about list iterators!
     * The size of the table should be the closest prime to size * 2.
     *
     * @hint Use findPrime()!
     */
     int ret_value=findPrime(2*size);
     std::list< std::pair<K, V> > * new_table = new list< pair<K,V> >[ret_value];
     for(int i=0;i<size;i++)
     {
 	for(it=table[i].begin();it!=table[i].end();it++)
 	{
 		  int idx = hash( it->first, ret_value );
    
 		new_table[idx].push_front(*it); 	
 		
 	}    	
     }
 
   delete [] table;
   table=new_table;
    size=ret_value; 
}
Exemple #7
0
V & SCHashTable<K,V>::operator[]( K const & key ) {
    size_t idx = hash( key, size );
    typename list< pair<K,V> >::iterator it;
    for( it = table[idx].begin(); it != table[idx].end(); it++ ) {
        if( it->first == key )
            return it->second;
    }

    ++elems;
    if( shouldResize() )
        resizeTable();
 
    idx = hash( key, size );
    pair<K,V> p( key, V() );
    table[idx].push_front( p );
    return table[idx].front().second;
}
Exemple #8
0
void SCHashTable<K,V>::insert( K const & key, V const & value ) {
    ++elems;
    if( shouldResize() )
        resizeTable();
    pair<K,V> p( key, value );
    size_t idx = hash( key, size );
    table[idx].push_front( p );
}
Exemple #9
0
V SCHashTable<K,V>::find( K const & key ) const {
    size_t idx = hash( key, size );
    typename list< pair<K,V> >::iterator it;
    for( it = table[idx].begin(); it != table[idx].end(); it++ ) {
        if( it->first == key )
            return it->second;
    }
    return V();
}
Exemple #10
0
bool SCHashTable<K,V>::keyExists( K const & key ) const {
    size_t idx = hash( key, size );
    typename list< pair<K,V> >::iterator it;
    for( it = table[idx].begin(); it != table[idx].end(); it++ ) {
        if( it->first == key )
            return true;
    }
    return false;
}
void SCHashTable<K,V>::resizeTable() {
    
    /**
     * @todo Implement this function.
     *
     * Please read the note in the spec about list iterators!
     * The size of the table should be the closest prime to size * 2.
     *
     * @hint Use findPrime()!
     */
	
	list< pair<K, V> > * new_table = new list< pair<K, V> >[size*2];
	for(int i=0;i<size;i++)
	{
		if(!table[i].empty())
		{	
			typename list< pair<K,V> >::iterator it;
			for(it = table[i].begin();it!=table[i].end();it++)
			{
				int idx = hash(it->first,size*2);
				new_table[idx].push_back(*it);
			}		
		}
			
	}
	delete []table;
	table = new_table;
	size = size*2;
	elems = elems;
/*
	typename list< pair<K,V> >::iterator it;
 
 
        int newSize = findPrime( 2*size );
        list<pair<K,V> > * newTable = new list<pair<K,V> > [newSize];
       
        for (int idx=0;idx<size;idx++)
        {
                if (!table[idx].empty())
                {
                         for( it = table[idx].begin(); it != table[idx].end(); it++ )
                        {
                                pair<K,V> p( it->first, it->second );
                                int i = hash( it->first, it->second );
                                newTable[i].push_front( p );
                        }
                }
        }
 
        delete [] table;
        table = newTable;
        size = newSize;
*/	
}
int LPHashTable<K,V>::findIndex( const K & key ) const {
    int idx = hash( key, size );
    int start = idx;
    while( should_probe[idx] ) {
        if( table[idx] != NULL && table[idx]->first == key )
            return idx;
        idx = ( idx + 1 ) % size;
        // if we've looped all the way around, the key has not been found
        if( idx == start )
            break;
    }
    return -1;
}
Exemple #13
0
void SCHashTable<K,V>::remove( K const & key ) {
    typename list< pair<K,V> >::iterator it;
    /**
     * @todo Implement this function.
     *
     * Please read the note in the lab spec about list iterators and the
     * erase() function on std::list!
     */
    int temp=hash(key, size);
    for(it=table[temp].begin(); it!=table[temp].end(); it++){
        if(it->first==key){
            table[temp].erase(it);
            elems--;
        }
    }


    //(void) key; // prevent warnings... When you implement this function, remove this line.
}
void SCHashTable<K,V>::remove( K const & key ) {
    typename list< pair<K,V> >::iterator it;
    /**
     * @todo Implement this function.
     *
     * Please read the note in the lab spec about list iterators and the
     * erase() function on std::list!
     */
    int idx = hash( key, size );
    for(it=table[idx].begin();it!=table[idx].end();it++)
    {
    	if(it->first==key)
    	{
    		table[idx].erase( it );
		break;
    	}
    }

}
void LPHashTable<K,V>::insert( K const & key, V const & value ) {
    /**
     * @todo Implement this function.
     *
     * @note Remember to resize the table when necessary (load factor >=
     *  0.7). **Do this check *after* increasing elems!!** Also, don't
     *  forget to mark the cell for probing with should_probe!
     */
   
     ++elems;
       if( shouldResize() )
        resizeTable();
          int idx = hash( key, size );
     while (table[idx]!=NULL)
     {
     idx++;
     }
     pair<K,V> *temp = new pair<K,V> (key, value);
     table[idx] = temp;
     should_probe[idx] = true;
}
Exemple #16
0
void SCHashTable<K, V>::resizeTable()
{
    typename list<pair<K, V>>::iterator it;
    /**
     * @todo Implement this function.
     *
     * Please read the note in the spec about list iterators!
     * The size of the table should be the closest prime to size * 2.
     *
     * @hint Use findPrime()!
     */
    size_t size2 = findPrime(size*2);
    std::list<std::pair<K, V>>* table2 = new list<pair<K, V>>[size2];
    for (size_t i = 0; i < size; i++){
	for (it = table[i].begin(); it != table[i].end(); it++){
	    size_t idx = hash(it -> first, size2);
	    table2[idx].push_back(*it);
	}
    }
    delete [] table;
    table = table2;
    size = size2;
}