Пример #1
0
struct segwrap *
seghash_get (struct segwrap **hash_table, size_t table_size, seq_t key)
{
    int i;
    struct segwrap *head;
    struct segwrap *cur;
    struct segwrap *selected;

    assert (hash_table != NULL);
    assert (table_size > 0);

    i = hash_fun (key, table_size);
    assert (i >= 0);
    assert (i < table_size);

    selected = NULL;
    head = getHead (hash_table[i]);
    if (head != NULL) {
        cur = head;
        do {
            if (key == seg_seq (cur->sw_seg))
                selected = cur;
            else
                cur = getNext (cur);
        } while (selected == NULL && cur != getHead (hash_table[i]));
    }

    return selected;

}
Пример #2
0
int
hash_get(struct hash *hash,
         uint32_t laddr, uint32_t raddr, uint16_t lport, uint16_t rport,
         struct timeval *result)
{
    struct session *session;
    unsigned long port;
    
    port = hash_fun(laddr, raddr, lport, rport) % hash->sz;
    for (session = hash->sessions + port; session->next; session = session->next)
        if (
            session->next->raddr == laddr &&
            session->next->laddr == raddr &&
            session->next->rport == lport &&
            session->next->lport == rport
        )
        {
            *result = session->next->tv;
            return 1;
            
        }
        
    return 0;
    
}
Пример #3
0
int
hash_get_rem(struct hash *hash,
         uint32_t laddr, uint32_t raddr, uint16_t lport, uint16_t rport,
         struct timeval *result)
{
    struct session *session, *next;
    unsigned long port;

    port = hash_fun(laddr, raddr, lport, rport) % hash->sz;
    for (session = hash->sessions + port; session->next; session = session->next)
        if (
            session->next->raddr == raddr &&
            session->next->laddr == laddr &&
            session->next->rport == rport &&
            session->next->lport == lport
        )
        {
            *result = session->next->tv;
            
            // Now remove
            next = session->next->next;
            free(session->next);
            session->next = next;
            
            hash->count --;
            
            return 1;
            
        }
        
    return 0;
    

}
Пример #4
0
double retrieve_value(PHASH* ph, TPAIR tp)
{
    int start_idx = hash_fun(tp) % (ph -> length);
    int idx = start_idx;
    while(!pair_equal((ph -> p)[idx].key, tp))
	idx = (idx + 1) % (ph -> length);
    return (ph -> p)[idx].val;
}
Пример #5
0
static struct owner* find_by_uid(int n)
{
	struct owner* p;

	p = hash_table[hash_fun(n)];
	while(p) {
		if(p->uid == n) break;
		p=p->next;
	}
	return p;
}
Пример #6
0
int hash_exist(struct hash *hash, uint32_t laddr, uint32_t raddr, uint16_t lport, uint16_t rport)
{
    struct session *session;
    unsigned long port;

    port = hash_fun(laddr, raddr, lport, rport) % hash->sz;
    for (session = hash->sessions + port; session->next; session = session->next){
		if (session->next->raddr == raddr && session->next->laddr == laddr && session->next->rport == rport && session->next->lport == lport)
			return 1; 
	}  
    return 0;
}
Пример #7
0
HASHNODE * hash_search(HASHNODE *hash_table[],char *key)
{
	unsigned int index;
	index = hash_fun(key);
	HASHNODE *p = NULL;
	p = hash_table[index];
	for(;p!=NULL;p=p->next)
	{
		if(strcmp(p->key,key)==0)
			return p;
	}
	return p;
}
Пример #8
0
struct segwrap *
seghash_remove (struct segwrap **hash_table, size_t table_size, seq_t key)
{
    struct segwrap *selected;

    selected = seghash_get (hash_table, table_size, key);
    if (selected != NULL) {
        int i;
        i = hash_fun (key, table_size);
        qremove (&hash_table[i], selected);
    }
    return selected;
}
Пример #9
0
struct hashtable_s* read_into_hash(const char* file_name, int *array, struct hashtable_s* hash_table) {
  FILE *fp;
  char *line = NULL;
  size_t len = 0;
  ssize_t read;
  long hash;
  struct entry_s *entry;
  int i = 0;

  fp = fopen(file_name, "r");
  if (fp == NULL)
    exit(EXIT_FAILURE);

  //read from file
  while ((read = getline(&line, &len, fp)) != -1) {
    long x;
    sscanf(line, "%ld\n", &x);
    //printf("Original x: %lld\n", x);
    array[i++] = x;
    struct entry_s* data = (struct entry_s*)malloc(sizeof(struct entry_s));
    data->key = x;
    //printf("%lld\n", data->key);
    data->value = 1;
    data->next = NULL;

    if (hash_table->number_of_elements >= hash_table->max) {
      printf("FULL\n");
      break;
    }
    hash = hash_fun(data->key, hash_table->max);
    //printf("Hash value: %lld\n", hash);
    if (hash_table->elements[hash] == NULL)
      hash_table->elements[hash] = data;
    else {
      entry = hash_table->elements[hash];
      while (entry->next != NULL && entry->key != data->key)
	entry = entry->next;
      if (entry->key == data->key)
	entry->value++;
      else
	entry->next = data;
    }
    hash_table->number_of_elements++;
  }
  fclose(fp);
  if (line)
    free(line);

  return hash_table;
}
Пример #10
0
void add_entry(PHASH* ph, HELEM elem)
{
    if(ph -> num_used == ph -> length)
    {
	printf("The hash table is already full.\n");
	exit(1);
    }
    int start_idx = hash_fun(elem.key) % (ph -> length);
    int idx = start_idx; 
    while(!is_null_entry((ph -> p)[idx].key))
	idx = (idx + 1) % (ph -> length);
    (ph -> p)[idx] = elem;
    (ph -> num_used) += 1;

}
Пример #11
0
/* Check whether data in hashtable */
int hash_count(long key, struct hashtable_s* hash_table) {
  long hash;
  int count = 0;
  struct entry_s* entry;
  hash = hash_fun(key, hash_table->max);
  entry = hash_table->elements[hash];
  while (entry != NULL) {
    if (entry->key == key) {
      count = count + entry->value;
      break;
    }
    entry = entry->next;
  }
  return count;
}
Пример #12
0
void
seghash_add
(struct segwrap **hash_table, size_t table_size, struct segwrap *sw)
{
    int i;

    assert (hash_table != NULL);
    assert (table_size > 0);
    assert (sw != NULL);

    i = hash_fun (seg_seq (sw->sw_seg), table_size);
    assert (i >= 0);
    assert (i < table_size);

    qenqueue (&hash_table[i], sw);
}
Пример #13
0
static struct owner* new_owner(int n)
{
	struct owner* p;
	struct passwd *u;
	p = (struct owner*) malloc(sizeof *p);
	if (!p) allocate_error();
	memset(p, 0, sizeof *p);
	u = getpwuid(n);
	if (!u) sprintf(p->name, "%d", n);
	else {
		strncpy(p->name, u->pw_name, NAME_SIZE);
		p->name[NAME_SIZE] = 0;
	}
	p->uid = n;
	list_add(hash_table[hash_fun(n)], p);
	return p;
}
Пример #14
0
//way==1用于ip连接数的删除
int hash_del(HASHNODE *hash_table[],char *key,int way)
{
	int tempi=0;
	char tempstr[20];
	//
	HASHNODE *p = hash_search(hash_table,key);
	if(p == NULL)
		return -1;
	if(way==1)//如果ip连接数大于1,只是把数量减1,不删除节点
	{
		tempi=atoi(p->val);
		tempi-=1;
		free(p->val);
		sprintf(tempstr,"%d",tempi);
		p->val=(char *)strdup(tempstr);

	}
	else
	{
		unsigned int index = hash_fun(key);
		if(p==hash_table[index]  )//如果要删除的p结点是该
		{
			if(p->next==NULL)//第一个并且只有一个节点
				hash_table[index]=NULL;
			else  //是第一个结点但其后面还有节点
			{
				//p->next->prev=p->prev;
				hash_table[index]=p->next;

			}
		}
		else //要删除的不是第一个结点
		{
			p->prev->next=p->next;//不是最后一个结点
			if(p->next==NULL)//删除的是最后一个结点
				p->prev->next=NULL;
		}
		free(p);
		p=NULL;
	}
	return 0;
}
Пример #15
0
//Return the idx of the element, -1 if not exist
int exist_in_hash(PHASH* ph, TPAIR tp)
{
    int start_idx = hash_fun(tp) % (ph -> length);
    int idx = start_idx;
    TPAIR temp_pair;
    while(1)
    {
	temp_pair = (ph -> p)[idx].key;
	if(is_null_entry(temp_pair))
	    return -1;
	else if(pair_equal(temp_pair, tp))
	    return idx;
	else
	{
	    idx = (idx + 1) % (ph -> length);
	    if(idx == start_idx)
		return -1;
	}
    }

}
Пример #16
0
static int
hash_set_internal(struct session *sessions, unsigned long sz,
         uint32_t laddr, uint32_t raddr, uint16_t lport, uint16_t rport,
         struct timeval value)
{
    struct session *session;
    unsigned long port;
    
    port = hash_fun(laddr, raddr, lport, rport) % sz;

    for (session = sessions + port; session->next; session = session->next)
        if (
            session->next->raddr == raddr &&
            session->next->laddr == laddr &&
            session->next->rport == rport &&
            session->next->lport == lport
        )
        {
            session->next->tv = value;
            
            return 0;
            
        }
    
    session->next = malloc(sizeof(struct session));
    if (!session->next)
        abort();
    
    session->next->raddr = raddr;
    session->next->laddr = laddr;
    session->next->rport = rport;
    session->next->lport = lport;
    
    session->next->tv = value;
    
    session->next->next = NULL;
    
    return 1;
    
}
Пример #17
0
int hash_insert(HASHNODE *hash_table[],char *key, char *val,int way,HASH_FUN fun1)
{
	HASHNODE * p=hash_search(hash_table,key);
	int index,tempi=0;
	char tempstr[10];
	if(p==NULL)//hash表中没有该节点
	{
		p=(HASHNODE *)malloc(sizeof(HASHNODE));
		if(p==NULL)
		{
			perror("malloc");
			return -1 ;
		}
		memset(p,0,sizeof(HASHNODE));
		p->key = (char *)strdup(key);
		if(p->key==NULL)
		{
			free(p);
			return -1;
		}
		if(way==0)//hash_conf,hash_pid_ip
		{
			p->val =(char *) strdup(val);//(char *)
			if(p->val==NULL)
			{
				free(p->key);
				free(p);
				return -1;
			}
		}
		else if(way==1)//hash_ip_count
		{
			char str[2];
			sprintf(str,"%d",1);
			p->val=(char *)strdup(str);
			if(p->val==NULL)
			{
				free(p->key);
				free(p);
				return -1;
			}
		}
		else if(way==2)//hash_fun
		{
			p->fun=fun1;//(HASH_FUN)val
		}

		p->next = NULL;
		p->prev = NULL;

		index=hash_fun(key);
		if(hash_table[index]==NULL)//头节点
			hash_table[index]=p;
		else
		{
			p->next = hash_table[index];
			//hash_table[index]->prev = p;
			hash_table[index] = p;
		}
	}
	else //p不为空,插入hash_ip_count时 同一IP的数量加1
	{
		tempi=atoi(p->val);
		tempi+=1;
		free(p->val);
		sprintf(tempstr,"%d",tempi);
		p->val=(char *)strdup(tempstr);
	}
	return 0;
}
Пример #18
0
unsigned int testHashFunction(const void *key) 
{
    Key_t *k1 = (Key_t*)key;
    return  hash_fun(k1->laddr, k1->raddr, k1->lport, k1->rport);
};