private static void calculateMaxDisplacement()

in src/main/java/com/spotify/sparkey/IndexHash.java [184:234]


  private static void calculateMaxDisplacement(IndexHeader header, RandomAccessData indexData) throws IOException {
    HashType hashData = header.getHashType();
    AddressSize addressData = header.getAddressData();

    long pos = 0;
    indexData.seek(pos);
    long capacity = header.getHashCapacity();

    long maxDisplacement = 0;
    long numHashCollisions = 0;
    long totalDisplacement = 0;

    boolean hasFirst = false;
    long firstHash = 0;

    boolean hasLast = false;
    long lastHash = 0;

    boolean hasPrev = false;
    long prevHash = -1;
    for (long slot = 0; slot < capacity; slot++) {
      long hash = hashData.readHash(indexData);
      if (hasPrev && prevHash == hash) {
        numHashCollisions++;
      }
      long position = addressData.readAddress(indexData);
      if (position != 0) {
        prevHash = hash;
        hasPrev = true;
        long displacement = getDisplacement(capacity, slot, hash);
        totalDisplacement += displacement;
        maxDisplacement = Math.max(maxDisplacement, displacement);
        if (slot == 0) {
          firstHash = hash;
          hasFirst = true;
        }
        if (slot == capacity - 1) {
          lastHash = hash;
          hasLast = true;
        }
      } else {
        hasPrev = false;
      }
    }
    if (hasFirst && hasLast && firstHash == lastHash) {
      numHashCollisions++;
    }
    header.setTotalDisplacement(totalDisplacement);
    header.setMaxDisplacement(maxDisplacement);
    header.setHashCollisions(numHashCollisions);
  }