Esempio n. 1
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++;
}
// ------------------------------
// 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++;
}
Esempio n. 3
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++;
}