static void calculate_max_displacement()

in src/hashwriter.c [235:284]


static void calculate_max_displacement(sparkey_hashheader *hash_header, uint8_t *hashtable) {
  uint64_t capacity = hash_header->hash_capacity;
  int hash_size = hash_header->hash_size;
  int slot_size = hash_header->address_size + hash_size;

  uint64_t max_displacement = 0;
  uint64_t num_hash_collisions = 0;
  uint64_t total_displacement = 0;

  int has_first = 0;
  uint64_t first_hash = 0;

  int has_last = 0;
  uint64_t last_hash = 0;

  int has_prev = 0;
  uint64_t prev_hash = -1;
  for (uint64_t slot = 0; slot < capacity; slot++) {
    uint64_t hash = hash_header->hash_algorithm.read_hash(hashtable, slot * slot_size);
    if (has_prev && prev_hash == hash) {
      num_hash_collisions++;
    }
    uint64_t position = read_addr(hashtable, slot * slot_size + hash_size, hash_header->address_size);
    if (position != 0) {
      prev_hash = hash;
      has_prev = 1;
      uint64_t displacement = get_displacement(capacity, slot, hash);
      total_displacement += displacement;
      if (displacement > max_displacement) {
        max_displacement = displacement;
      }
      if (slot == 0) {
        first_hash = hash;
        has_first = 1;
      }
      if (slot == capacity - 1) {
        last_hash = hash;
        has_last = 1;
      }
    } else {
      has_prev = 0;
    }
  }
  if (has_first && has_last && first_hash == last_hash) {
    num_hash_collisions++;
  }
  hash_header->total_displacement = total_displacement;
  hash_header->max_displacement = max_displacement;
  hash_header->hash_collisions = num_hash_collisions;
}