static void destroy_ship(struct GameState *game_state, struct Ship *ship) { struct UIntHashPair *pair = find_pair(&game_state->ship_id_map, ship->id); ASSERT_NOT_NULL(pair); uint32 array_index = pair->value; ASSERT(array_index < game_state->ship_count); remove_pair(&game_state->ship_id_map, ship->id); // Ship is already at the end of the array. if (array_index == game_state->ship_count - 1) { --game_state->ship_count; return; } // Swap the destroyed ship with the last active ship in the array. struct Ship last = game_state->ships[game_state->ship_count - 1]; game_state->ships[array_index] = last; --game_state->ship_count; // Update the ship ID map with the swapped ship's new array index. struct UIntHashPair *last_pair = find_pair(&game_state->ship_id_map, last.id); ASSERT_NOT_NULL(last_pair); last_pair->value = array_index; }
int map_remove(Map *map, const char *key) { unsigned int index; Bucket *bucket; if (map == NULL) { return 0; } if (key == NULL) { return 0; } index = hash(key) % map->count; bucket = &(map->buckets[index]); return remove_pair(map, bucket, key); }