Repository: valkey-io/valkey
File: src/geo.c (C)

Code before:
/* ====================================================================
 * This file implements the following commands:
 *
 *   - geoadd - add coordinates for value to geoset
 *   - georadius - search radius by coordinates in geoset
 *   - georadiusbymember - search radius based on geoset member position
 * ==================================================================== */

/* ====================================================================
 * geoArray implementation
 * ==================================================================== */

/* Init a new array of geoPoints. */
void geoArrayInit(geoArray *ga) {
    ga->buckets = MAX_GEO_ARRAY_BUFFER;
    ga->used = 0;
    ga->array = ga->arraybuf;
}

/* Add and populate with data a new entry to the geoArray. */
geoPoint *geoArrayAppend(geoArray *ga, double *xy, double dist, double score, char *member) {
    if (ga->used == ga->buckets) {
        ga->buckets = ga->buckets * 2;
        if (ga->array == ga->arraybuf) {
            ga->array = zmalloc(sizeof(geoPoint) * ga->buckets);
            memcpy(ga->array, ga->arraybuf, sizeof(geoPoint) * MAX_GEO_ARRAY_BUFFER);
        } else {
            ga->array = zrealloc(ga->array, sizeof(geoPoint) * ga->buckets);
        }
    }
    geoPoint *gp = ga->array + ga->used;
    gp->longitude = xy[0];
    gp->latitude = xy[1];
    gp->dist = dist;
    gp->member = member;
    gp->score = score;
    ga->used++;
    return gp;
}

/* Destroy a geoArray created with geoArrayCreate(). */
void geoArrayFree(geoArray *ga) {
    size_t i;
    for (i = 0; i < ga->used; i++) sdsfree(ga->array[i].member);
    if (ga->array != ga->arraybuf) {
        zfree(ga->array);
    }
}

/* ====================================================================
 * Helpers

Review comment (suggestion):
```suggestion
            memcpy(ga->array, ga->arraybuf, sizeof(*ga->arraybuf);
```

This is equivalent, if i'm not mistaken.

Code after:
/* ====================================================================
 * This file implements the following commands:
 *
 *   - geoadd - add coordinates for value to geoset
 *   - georadius - search radius by coordinates in geoset
 *   - georadiusbymember - search radius based on geoset member position
 * ==================================================================== */

/* ====================================================================
 * geoArray implementation
 * ==================================================================== */

/* Init a new array of geoPoints. */
void geoArrayInit(geoArray *ga) {
    ga->buckets = MAX_GEO_ARRAY_BUFFER;
    ga->used = 0;
    ga->array = ga->arraybuf;
}

/* Add and populate with data a new entry to the geoArray. */
geoPoint *geoArrayAppend(geoArray *ga, double *xy, double dist, double score, char *member) {
    if (ga->used == ga->buckets) {
        ga->buckets = ga->buckets * 2;
        if (ga->array == ga->arraybuf) {
            ga->array = zmalloc(sizeof(geoPoint) * ga->buckets);
            memcpy(ga->array, ga->arraybuf, sizeof(ga->arraybuf));
        } else {
            ga->array = zrealloc(ga->array, sizeof(geoPoint) * ga->buckets);
        }
    }
    geoPoint *gp = ga->array + ga->used;
    gp->longitude = xy[0];
    gp->latitude = xy[1];
    gp->dist = dist;
    gp->member = member;
    gp->score = score;
    ga->used++;
    return gp;
}

/* Clean a geoArray inited with geoArrayInit(). */
void geoArrayCleanup(geoArray *ga) {
    size_t i;
    for (i = 0; i < ga->used; i++) sdsfree(ga->array[i].member);
    if (ga->array != ga->arraybuf) {
        zfree(ga->array);
    }
}

/* ====================================================================
 * Helpers