void HashTable::putQuadratic(Item* item){
	int counter=0;
	int key = item->getKey();
	int cell = hashfunction(key+counter*counter);

	while(table[cell] != NULL){
		counter++;
		cell = hashfunction(key+counter*counter);
	}

	table[cell] = item;
}
Item* HashTable::getQuadratic(int key_){
	int cell = hashfunction(key_);

			if(table[cell]->getKey()== key_){
				return table[cell];
			}else{
				int counter=1;
				while(table[cell]->getKey()!= key_){
					cell = hashfunction(key_+counter*counter);
					counter++;
				}
				return table[cell];
			}
}
Exemple #3
0
/* Create a new cache struct and use hash function to 
 * put it to the appropriate linked list */
int Create_cache( char *url, char *data, int size){

    while (CachedSize + size > MAX_CACHE_SIZE){
        DeleteLRUCache();
    }



    c_data* cache = (c_data*)malloc(sizeof(c_data));
    int n = hashfunction(url);
    cache->url = url;
    cache->data = data;
    cache->size = size;
    cache->counter = globalcounter++;
    cache->prev_cache = NULL;
    cache->next_cache = NULL;
    if(CacheHead[n] == NULL){
        CacheHead[n] = cache;
    }
    else{
        CacheHead[n]->prev_cache = cache;
        cache->next_cache = CacheHead[n];
        CacheHead[n] = cache;
    }
    CachedSize += size;
    return 1;
}
void append(char lastName[], entry table[])
{
    /* allocate memory for the new entry and put lastName */
    int hash = hashfunction(lastName);
    //printf("%d\n",hash);
    entry *n_entry = (entry *) malloc(sizeof(entry));
    n_entry->pNext = table[hash].pNext;
    table[hash].pNext = n_entry;
    strcpy(n_entry->lastName, lastName);

}
Exemple #5
0
/* Examine whether the url is in the cached list 
 * If true, write the content to the browser
 * Otherwise, return NULL */
int Get_cachedata(char *url, int browser_fd){
    int n = hashfunction(url);
    c_data* curr = CacheHead[n];
    while(curr!=NULL){
        if(!strcmp(url,curr->url)){
            rio_writen(browser_fd,curr->data,curr->size);
            return 1;
        }
        curr = curr->next_cache;
    }
    return 0;
}
entry *findName(char lastname[], entry *pHead, entry table[])
{
    int hash = hashfunction(lastname);
    pHead = &table[hash];

    while (pHead != NULL) {
        if (strcasecmp(lastname, pHead->lastName) == 0)
            return pHead;
        pHead = pHead->pNext;
    }
    return NULL;
}
Exemple #7
0
void insert(SymbolTableEntry *entry){
	int position;
	if((entry->type==USERFUNC)||(entry->type==LIBFUNC)){
		Function *func;
		func=entry->value.funcVal;
		position=hashfunction(func->name);
	}
	else{
		Variable *var;
		var=entry->value.varVal;
		position=hashfunction(var->name);
	}
	if(entries[position].next==NULL){
		entries[position].next=entry;
	}
	else{
		SymbolTableEntry *temp;
		temp=get_last(entries[position].next);
		temp->next=entry;
	}
//	update_heads(entry);
//	insert_in_heads(entry);
}
Exemple #8
0
int Lookup(char *name,int token_type,int var_type){//epistrefei 1 ean mporei na mpei,0 an den mporei,2 ean prepei na dhmiourgithei kataxwrish kai sto scope_heads
	//printf("Scope =%d\n",get_current_scope());
	int position,state;
	char *prev_name;
	position=hashfunction(name);
	SymbolTableEntry *libfunc;
		SymbolTableEntry *temp,*scopes_temp;
		if(token_type==VAR){//An exoume metavlhth
			if(var_type==SYM_LOCAL){
				state=isLibfunc(name);
                        	if(state==0){
                                	printf("Lib!\n");
                                	return 0;
		                 }

				state=search_scope_heads(name,current_scope,0);
				if((state==1)||(state==4)) {
					//vrethike,anaferomaste ekei
					//printf("Vrethike\n");
					return 5;
				}
				else if(state==0){
					//printf("Den vrethike pouthena");
					 return 2;//mpainei kai sto hash kai sth lista twn scopes
				}
				else if(state==2){
					//printf("Den vrethike");
					return 1;//mpainei sto hash mono
				}
			}
			else if(var_type==GLOBAL){
				state=search_scope_heads(name,0,0);
				if((state==1)||(state==4)){
					//printf("Global found\n");
					//Found it
					return 5;
				}
				else{
					printf("ERROR global not found\n");
					return 0;
				}
			}
			else if(var_type==UNDEF){
				int i,check,update_heads;;
				//printf("Mpainw\n");
				SymbolTableEntry *temp,*scope_temp;
				if(get_current_scope()>0){
					i=get_current_scope();
					while(i>=0){
						//printf("Psaxnw to scope %d gia to symvolo %s\n",i,name);
						state=search_scope_heads(name,i,1);
						//printf("Epestrafh state %d\n",state);
						if(state==2) {update_heads=0;}//elegxoume an yparxei proigoumenh kataxwrhsh sto current scope
                                                else if(state==0) update_heads=2;
						if(state==0){
							check=check_function_starts(i);
							if(check==1) {
								state=3;
								}
						}
						if(state==1){
							//printf("Vrethike\n");
							return 5;
						}
						else if(state==4) {

						return 4;}//vrhke onoma synarthshs
						else if(state==3){
							i--;
							while(i>=0){
								state=search_scope_heads(name,i,0);
								if(state==1){
									if(i!=0){
										//error
										printf("Error cannot access variable %s\n",name);
										return 0;
									}
									else{
										//printf("Found it,anaferomaste ekei\n");
										//th vrhkame,anaferomaste ekei
										return 5;
									}
								}
								else if(state==4){
									//vrethike synarthsh me to idio onoma,ara mporei na proseggistei
									//printf("Vrethike synarthsh me idio onoma,mporoume na anaferthoume\n");
									return 4;
								}
								i--;
							}
							if(update_heads==2){
							//printf("Den vrethike pouthena,mporei na mpei %d\n",get_current_scope());
							return 2;}//den vrethike pouthena,ara mporei na eisaxthei}
							else {
								//printf("Den vrethike,ginetai insert_in_heads\n");
								return 1;}
						}
						i--;
					}
					//printf("Den vrethike apolytws pouthena\n");
					return 2;//den vrethike pouthena
				}
				else{//global scope
					state=search_scope_heads(name,0,0);
					if(state==1){
						//printf("Vrhkame global,anaferomaste ekei\n");
						//vrethike anaferomaste ekei
						return 5;
					}
					else if(state==4){
						//vrethike onoma synarthshs
						//printf("vrethike onoma synarthshs!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
						return 4;
					}
					else{
						//printf("H global mporei na dhmiourgithei kai na eisaxthei\n");
						return 1;//mporei na dhmiourgithei kai na eisaxthei
					}
				}
			}
			else if(var_type==FORMAL){
				state=isLibfunc(name);
                                if(state==0){
                                        printf("Lib!\n");
                                        return 0;
                                 }
				int search_scope=get_current_scope()+1;
				state=search_scope_heads(name,search_scope,0);
				if((state==1)||(state==4)){
					printf("ERROR: Multiple declaration as formals %d\n",state);
					return 0;
				}
				if(state==0){
					return 2;
				}
				else if(state==2){
					return 1;
				}
				else if(state==5){
					return 1;
				}
				/*state=search_scope_heads(name,get_current_scope(),0);
				if((state==1)&&(get_current_scope()!=0)) {
					printf("Error,formal exists in scope\n");
					return 0;}//error,already exists
				if(state==0) {
					printf("Formal Mpainei pantou\n");
					return 2;}//prepei na mpei kai sta scope_heads me scope au3hmeno kata 1
				if(state==2) {
					printf("Formal Apla mpainei %d\n",get_current_scope());
					return 1;}//apla eisagwgh me scope au3hmeno kata 1*/
				return 0;
				}
		}
		else{//an exoume synarthsh
			if(isLibfunc(name)==0) {
				printf("Shadowing lib func\n");
				return 0;}//error
			state=search_scope_heads(name,get_current_scope(),0);
			if ((state==1)||(state==4)) {
				printf("Error :Vrethike idio symvolo\n");
				return 0;}//error
			if(state==0) {
				//printf("Func,mpainei pantou\n");
				return 2;}//prepei na mpei kai sta scope_heads
			if(state==2){
				//printf("Func mpainei\n");
				return 1;}//apla eisagwgh

		}
}
int getMapVal(char key){
//mengembalikan value untuk map. bila tidak ada, kembalikan mapvalundef
	return getTabVal(hashfunction(key));
}
Exemple #10
0
void print(char *key, void* obj)
{
  printf ("table value: %s ", key);
  printf("%d\n", hashfunction(key));
}
Exemple #11
0
static boolean validate (hdlhashtable htable, boolean flalert) {
	
	/*
	a bullshit detector for hash tables.  written on 3/21/90 by DW.
	
	we call it invalid if:
	
	1. the table handle is nil -- there's nothing we can do with such a table!
	
	2. one of the hashbuckets has an item chained into it that shouldn't be in 
	its list.  this is a very strong test -- it seems improbable if the chain
	is somehow invalid that all the handles would work out to point to a key
	string that just happens to hash to the same bucket number.
	
	3. the sorted list is out of order -- this seems like a little icing on the
	cake -- how could it pass test 2 and not pass this test?
	
	11/13/90 DW: relaxed the rule about the sorted list being in order, since
	we're allowing it.
	
	11/14/90 DW: added flonlyinmemory -- if false, not being in memory is not an 
	obstacle.  side-effect of doing a full traversal -- all the tables get loaded
	into memory.
	*/
	
	register hdlhashtable ht = htable;
	register short i;
	register hdlhashnode x;
	hdltablevariable hvariable;
	short errcode;
	
	if (ht == nil) {
		
		if (flalert)
			shellinternalerror (idnilhashtable, BIGSTRING ("\x17" "hashtable handle is nil"));
		
		return (false);
		}
	
	if (!(**ht).fllocaltable) { /*8/15/92 dmb*/
		
		for (i = 0; i < (**ht).cttmpstack; i++) {
			
			if ((**ht).tmpstack [i].data.binaryvalue != nil) {
				
				if (flalert)
					shellinternalerror (idnilhashtable, BIGSTRING ("\x14" "non-empty temp stack"));
				
				return (false);
				}
			}
		}
	
	for (i = 0; i < ctbuckets; i++) {
		
		x = (**ht).hashbucket [i];
		
		while (x != nil) { /*chain through the hash list*/
			
			bigstring bs;
			
			if (!flonlyinmemory)
				rollbeachball ();
			
			gethashkey (x, bs);
			
			if (gethandlesize ((Handle) x) != (long) sizeof (tyhashnode) + stringsize (bs)) {
				
				if (flalert)
					shellinternalerror (idbadbucketliststring, BIGSTRING ("\x17" "bad string in hash node"));
				
				return (false);
				}
			
			if (hashfunction (bs) != i) {
				
				if (flalert)
					shellinternalerror (idbadbucketliststring, BIGSTRING ("\x1b" "bad string in a bucket list"));
				
				return (false);
				}
			
			if (!gettablevariable ((**x).val, &hvariable, &errcode))
				goto nextx;
			
			if (!(**hvariable).flinmemory) {
				
				if (flonlyinmemory)
					goto nextx;
					
				if (!tableverbinmemory ((hdlexternalvariable) hvariable, x)) {
					
					if (flalert)
						shellinternalerror (iderrorloadingtable, BIGSTRING ("\x13" "error loading table"));
					
					return (false);
					}
				}
			
			assert (tablesetdebugglobals (ht, x));
			
			if (!validate ((hdlhashtable) (**hvariable).variabledata, flalert)) /*recurse*/
				return (false);
			
			nextx:
			
			x = (**x).hashlink; /*advance to next node in chain*/
			} /*while*/
		} /*for*/
	
	/*
	x = (**ht).hfirstsort;
	
	setstringlength (bslast, 0);
	
	while (x != nil) {
		
		bigstring bs;
		
		gethashkey (x, bs);
		
		if (!stringlessthan (bslast, bs)) {
		
			if (flalert)
				shellinternalerror (idunsortedhashlist, "\psorted list out of order");
			
			return (false);
			}
		
		x = (**x).sortedlink;
		
		copystring (bs, bslast);
		} /%while%/
	*/
	
	return (true);
	} /*validate*/