示例#1
0
文件: GC_net.c 项目: Gaspi/pvs2C
/* Insert a key-value pair into a hash table. */
void ht_set( hashtable_t *hashtable, void* key, int value ) {
  entry_t *newpair = NULL;
  entry_t *next = NULL;
  entry_t *last = NULL;
  int bin = ht_hash( hashtable, key );
  
  next = hashtable->table[ bin ];
  
  while( next != NULL && next->key != NULL && key != next->key ) {
    last = next;
    next = next->next;
  }
  
  /* There's already a pair. Let's replace that string. */
  if( next != NULL && next->key != NULL && key == next->key ) {
    next->value = value;
    
    /* Nope, could't find it. Time to grow a pair. */
  } else {
    newpair = ht_newpair( key, value );
    
    /* We're at the start of the linked list in this bin. */
    if( next == hashtable->table[ bin ] ) {
      newpair->next = next;
      hashtable->table[ bin ] = newpair;
      /* We're at the end of the linked list in this bin. */
    } else if ( next == NULL ) {
      last->next = newpair;
      /* We're in the middle of the list. */
    } else {
      newpair->next = next;
      last->next = newpair;
    }
  }
}
示例#2
0
void ht_append(hashtable *hashtable , char *key){
        int bin = NULL;
        entry *lastPair = NULL;
        entry *next = NULL;
        entry *newpair=NULL;

        bin = ht_hash(hashtable , key);
        
        next = hashtable->table[ bin ];

        while (next != NULL && next->lastName != NULL && strcmp(next->lastName , key) >0){
            lastPair = next ;
            next = next->pNext;
        }

            newpair = ht_newpair(key);
        if( next == hashtable->table[ bin ]){
            newpair-> pNext = next;      
            hashtable->table[ bin ] = newpair;
        }
        else if (next == NULL){
            lastPair->pNext = newpair;
        }
        else{
            newpair->pNext = next ;
            lastPair->pNext = newpair;
        }
}
示例#3
0
文件: print.c 项目: byeah/cs294_hw
void ht_put(Hashtable* hashtable, char* key, void* value) {
    entry_t* newpair = NULL;
    entry_t* current = NULL;
    entry_t* prev = NULL;

    int bin = ht_hash(hashtable, key);
    current = hashtable->table[bin];

    while (current != NULL && strcmp(key, current->key) != 0) {
        prev = current;
        current = current->next;
    }

    /* There's already a pair.  Let's replace that string. */
    if (current != NULL) {
        current->value = value;
    }
    /* Nope, could't find it.  Time to grow a pair. */
    else {
        newpair = ht_newpair(key, value);

        /* We're at the start of the linked list in this bin. */
        if (prev == NULL) {
            hashtable->table[bin] = newpair;
        }
        /* We're at the end of the linked list in this bin. */
        else {
            prev->next = newpair;
        }
    }
}
示例#4
0
文件: htable2.c 项目: Elytum/Lemins
void					ht_set(t_hashtable *hashtable, char *key, void *value)
{
	const int			bin = ht_hash(hashtable, key);
	t_entry				*newpair;
	t_entry				*next;
	t_entry				*last;

	ht_fln(hashtable->table[bin], &next, &last, key);
	if (next != NULL && next->key != NULL && ft_strcmp(key, next->key) == 0)
	{
		free(next->value);
		next->value = value;
	}
	else
	{
		newpair = ht_newpair(key, value);
		if (next == hashtable->table[bin])
		{
			newpair->next = next;
			hashtable->table[bin] = newpair;
			return ;
		}
		last->next = newpair;
		if (next)
			newpair->next = next;
	}
}
示例#5
0
/* Insert a key-value pair into a hash table. */
void ht_set( hashtable_t *hashtable, char *key, char *value ) {
	int bin = ht_hash( hashtable, key );
	entry_t *next = hashtable->table[bin];
	entry_t *last, *newpair;
	
	while(next && next->key && strcmp(key, next->key) > 0) {
		last = next;
		next = next->next;
	}
	
	if (!hashtable->table[bin])
		hashtable->table[bin] = ht_newpair(key, value, NULL);
	else if (!next || !next->key || strcmp(key, next->key)==0)
		last->next = ht_newpair(key, value, next);
	else {
		free (next->value);
		next->value = strdup(value);
	}
}
示例#6
0
/**
 * @brief Sets the Key and Value into the hashtable.
 *
 * @param hashtable A pointer to the hash table.
 * @param key The string that stores the key.
 * @param value The string that stores the value.
 * @return No return value.
 */
int ht_set( HashTable *hashtable, char *key, char *value ) {
	int bin = 0;
	Entry *newpair = NULL;
	Entry *next = NULL;
	Entry *last = NULL;
 
	bin = ht_hash( hashtable, key );

	next = hashtable->table[ bin ];
	while( next != NULL && next->key != NULL && strcmp( key, next->key ) > 0 ) {
		last = next;
		next = next->next;
	}
 
	/* There's already a pair.  Let's replace that string. */
	if( next != NULL && next->key != NULL && strcmp( key, next->key ) == 0 ) {
 	


		free( next->value );
		next->value = myStrDup( value );
		next->metadata += 1;
		return HASH_SET_UPDATE;
	/* Nope, could't find it.  Time to grow a pair. */
	} else {

		newpair = ht_newpair( key, value );

		/* Ensure we have not run out of memory */
		if (newpair == NULL)
			return HASH_SET_FAIL;

		/* We're at the start of the linked list in this bin. */
		if( next == hashtable->table[ bin ] ) {
			newpair->next = next;
			hashtable->table[ bin ] = newpair;
		/* We're at the end of the linked list in this bin. */
		} else if ( next == NULL ) {
			last->next = newpair;
	
		/* We're in the middle of the list. */
		} else  {
			newpair->next = next;
			last->next = newpair;
		}

		/* We have successfully inserted */
		return HASH_SET_INSERT;
	}
	return 0; // success
}
示例#7
0
文件: hashtable.c 项目: Thundzz/TORCC
/* Insert a key-value pair into a hash table. */
void ht_set( hashtable_t *hashtable, char *key, struct symbol_s *value ) {
	int bin = 0;
	entry_t *newpair = NULL;
	entry_t *next = NULL;
	entry_t *last = NULL;

	bin = ht_hash( hashtable, key );

	next = hashtable->table[ bin ];

	while( next != NULL && next->key != NULL && strcmp( key, next->key ) > 0 ) {
		last = next;
		next = next->next;
	}

/* There's already a pair. Let's replace that string. */
	if( next != NULL && next->key != NULL && strcmp( key, next->key ) == 0 ) {
		//TODO yyerror ("redefinition");
		free( next->value );
		next->value = value;

/* Nope, could't find it. Time to grow a pair. */
	} else {
		newpair = ht_newpair( key, value );

/* We're at the start of the linked list in this bin. */
		if( next == hashtable->table[ bin ] ) {
			newpair->next = next;
			hashtable->table[ bin ] = newpair;
/* We're at the end of the linked list in this bin. */
		} else if ( next == NULL ) {
			last->next = newpair;
/* We're in the middle of the list. */
		} else {
			newpair->next = next;
			last->next = newpair;
		}
	}
}