static inline H3Error validateCellSet()

in src/h3lib/lib/cellsToMultiPoly.c [14:53]


static inline H3Error validateCellSet(const H3Index *cells,
                                      const int64_t numCells) {
    if (numCells < 0) {
        return E_DOMAIN;
    }
    if (numCells == 0) {
        return E_SUCCESS;
    }

    // Check that all cells are valid and have the same resolution
    int res = H3_EXPORT(getResolution)(cells[0]);
    for (int64_t i = 0; i < numCells; i++) {
        if (!H3_EXPORT(isValidCell)(cells[i])) {
            return E_CELL_INVALID;
        }
        if (H3_EXPORT(getResolution)(cells[i]) != res) {
            return E_RES_MISMATCH;
        }
    }

    // Check for duplicate cells by sorting a copy and looking for adjacent
    // duplicates
    if (numCells >= 2) {
        H3Index *cellsCopy = H3_MEMORY(malloc)(numCells * sizeof(H3Index));
        if (!cellsCopy) {
            return E_MEMORY_ALLOC;
        }
        memcpy(cellsCopy, cells, numCells * sizeof(H3Index));
        qsort(cellsCopy, numCells, sizeof(H3Index), cmp_uint64);
        for (int64_t i = 1; i < numCells; i++) {
            if (cellsCopy[i] == cellsCopy[i - 1]) {
                H3_MEMORY(free)(cellsCopy);
                return E_DUPLICATE_INPUT;
            }
        }
        H3_MEMORY(free)(cellsCopy);
    }

    return E_SUCCESS;
}