private long findSlot()

in baremaps-data/src/main/java/org/apache/baremaps/data/collection/DirectHashDataMap.java [205:235]


  private long findSlot(long key, boolean forInsertion) {
    long index = hash(key);
    long tombstoneSlot = -1;

    // Linear probing to handle collisions
    for (long i = 0; i < capacity; i++) {
      long currentIndex = (index + i) % capacity;
      long currentKey = readKey(currentIndex);

      if (currentKey == EMPTY_KEY) {
        // Found empty slot
        return forInsertion ? (tombstoneSlot != -1 ? tombstoneSlot : currentIndex) : -1;
      } else if (currentKey == TOMBSTONE) {
        // Mark first tombstone for possible reuse
        if (tombstoneSlot == -1) {
          tombstoneSlot = currentIndex;
        }
      } else if (currentKey == key) {
        // Found the key
        return currentIndex;
      }
    }

    // If we're inserting and found a tombstone earlier, use that
    if (forInsertion && tombstoneSlot != -1) {
      return tombstoneSlot;
    }

    // Map is full or key not found
    return -1;
  }