Exemple #1
0
// OK
void* hashmap_get(t_hashmap* map, char* path) {
  int slot = hashmap_hashcode(path, map->slots);
  t_hashmap_entry* entries = map->entries[slot];

  while (strcmp(entries->key,path) != 0 && entries != NULL)  {
    entries = entries->next;
  }
  return strcmp(entries->key,path) != 0 ? NULL : entries->value;
}
// ------------------------------
// Function:    hashmap_put
// Description: add entry to hashmap
// Return:      null
// ------------------------------
void hashmap_put(t_hashmap* map, char* key, void* value)
{
	if (map->size >= (map->load_factor * map->slots))
	{
		t_hashmap_entry** new_entries = (t_hashmap_entry**)calloc(map->slots * map->grow_factor, sizeof(t_hashmap_entry*));

		int i;
		for (i = 0; i < map->slots; i++)
		{
			t_hashmap_entry* entry = map->entries[i];

			while (entry)
			{
				t_hashmap_entry* next = entry->next;
				unsigned int code = hashmap_hashcode(entry->key, map->slots * map->grow_factor);
				entry->next = new_entries[code];
				new_entries[code] = entry;
				entry = next;
			}
		}

		free(map->entries);
		map->entries = new_entries;

		printf("new entries created\n");
	}

	unsigned int hashcode = hashmap_hashcode(key, map->slots);

	t_hashmap_entry** current = &map->entries[hashcode];
	while (*current)
	{
		if (!strcmp((*current)->key, key))
		{
			(*current)->value = value;
			return;
		}
		*current = (*current)->next;
	}
	*current = hashmap_entry_create(key, value);
	map->size++;
}
Exemple #3
0
void hashmap_put(t_hashmap* map, char* key, void* value) {
    int slot = hashmap_hashcode(key, map->slots);
    t_hashmap_entry** entries = &(map->entries[slot]);
    while (*entries != NULL) {
        if (strcmp(((*entries)->key), key) == 0) {
            (*entries)->value = value;
            return;
        }
        entries = &((*entries)->next);
    }
    (*entries) = hashmap_entry_create(key, value);
    map->size++;
}
Exemple #4
0
void* hashmap_get(t_hashmap* map, char* key) {
    int slot = hashmap_hashcode(key, map->slots);
    if (map->entries[slot] == NULL)
        return NULL;

    t_hashmap_entry* entries = map->entries[slot];
    while (entries != NULL) {
        if (strcmp(entries->key,key) == 0) {
            return entries->value;
        }
        entries = entries->next;
    }
    return NULL;
}
// ------------------------------
// Function:    hashmap_get
// Description: get the value from the key
// Return:      value
// ------------------------------
void* hashmap_get(t_hashmap* map, char* key)
{
	unsigned int hashcode = hashmap_hashcode(key, map->slots);

	t_hashmap_entry* current = map->entries[hashcode];
	while (current)
	{
		if (!strcmp(current->key, key))
		{
			return current->value;
		}
		current = current->next;
	}
	return NULL;
}
Exemple #6
0
// OK
void* hashmap_delete(t_hashmap* map, char* key){
  int slot = hashmap_hashcode(key, map->slots);
  void* value = NULL;
  t_hashmap_entry **entry = &(map->entries[slot]), *toDelete;
  while(*entry){
    if(strcmp((*entry)->key, key) == 0){
      value = (*entry)->value;
      toDelete = *entry;
      *entry = (*entry)->next;
      free(toDelete);
      return value;
    }
    *entry = (*entry)->next;
  }
  return value;
}
Exemple #7
0
void* hashmap_delete(t_hashmap* map, char* key) {
    int slot = hashmap_hashcode(key, map->slots);
    if (map->entries[slot] == NULL)
        return NULL;

    t_hashmap_entry** entries = &(map->entries[slot]);
    while ((*entries) != NULL) {
        if (strcmp((*entries)->key, key) == 0) {
            t_hashmap_entry* tmp = *entries;
            void* value = tmp->value;
            *entries = (*entries)->next;
            free(tmp);
            return value;
        }
        entries = &((*entries)->next);
    }
    return NULL;
}
// ------------------------------
// Function:    hashmap_remove
// Description:
// Return:      None
// ------------------------------
void hashmap_remove(t_hashmap * map, char* key)
{
	unsigned int hash = hashmap_hashcode(key, map->slots);
	t_hashmap_entry* tmp;
	t_hashmap_entry** entry = &map->entries[hash];

	while (*entry)
	{
		if (!strcmp(key, (*entry)->key))
		{
			tmp = *entry;
			*entry = (*entry)->next;
			free(tmp);
			map->size--;
			break;
		}
		entry = &(*entry)->next;
	}
}
Exemple #9
0
// OK
// Ex :  hashmap_put(map, 'student.rate', 56);
//       hashmap_traverse(map, 'student.rate')   56
void hashmap_put(t_hashmap* map, char* path, void* value, Type type) {

  printf("\n---------------MAP_PUT-------------------");
  printf("\nmap->size: %d\tmap->slots*load_factor: %1.1f\tpath: %s\tvalue: %s\ttype: %s", map->size, map->slots * map->load_factor, path, (char*)value,printType(type));
  if(map->size >= (map->slots * map->load_factor)){
    hashmap_resize(map);
    printf("\nDEBUG: after resize map->slots: %d\tmap->size: %d\n", map->slots, map->size);
  }
  // printf("DEBUG: not resizing\n");
  int slot = hashmap_hashcode(path, map->slots);
  printf("\thashcode: %d\n", slot);
  t_hashmap_entry** entries = &(map->entries[slot]);
  // printf("DEBUG: before while\nvalue=%s", );
  while ((*entries) != NULL) {
    if (strcmp((*entries)->key, path) == 0) {
      (*entries)->value = value;
      (*entries)->type = type;
      return;
    }
    entries = &((*entries)->next);
  }
  (*entries) = hashmap_entry_create(path, value,type);
  map->size++;
}
Exemple #10
0
// OK
void* hashmap_remove(t_hashmap* map, char* key){

  int mKey = hashmap_hashcode(key, map->slots);
  t_hashmap_entry* current_entry = map->entries[mKey];

  t_hashmap_entry* previous_entry = NULL;

  while(strcmp(current_entry->key, key) != 0 && current_entry->next != NULL){
    // printf("\nwhile");
    previous_entry = current_entry;
    current_entry = current_entry->next;
  }

  if(strcmp(current_entry->key, key) != 0)
    return NULL;

  char* value = current_entry->value;

  //le maillon est le premier de la chaîne
  if(!previous_entry){
    //printf("\nle maillon est le premier de la chaine");
    map->entries[mKey % (map->slots)] = current_entry->next;
  }

  //le maillon est au milieu de la chaine
  if(previous_entry && current_entry->next){
    // printf("\nle maillon est au milieu de la chaine");
    previous_entry->next = current_entry->next;
  }

  //sinon: le maillon est en fin de chaine
  free(current_entry);
  map->size--;

  return value;
}