| 173 | |
| 174 | |
| 175 | static void |
| 176 | rebalance_centroids(centroid_t *centroids, int ncentroids, |
| 177 | int64 weight_before, int64 weight_after) |
| 178 | { |
| 179 | double ratio = weight_before / (double) weight_after; |
| 180 | int64 count_before = 0; |
| 181 | int64 count_after = 0; |
| 182 | int start = 0; |
| 183 | int end = (ncentroids - 1); |
| 184 | int i; |
| 185 | |
| 186 | centroid_t *scratch = palloc(sizeof(centroid_t) * ncentroids); |
| 187 | |
| 188 | i = 0; |
| 189 | while (i < ncentroids) |
| 190 | { |
| 191 | while (i < ncentroids) |
| 192 | { |
| 193 | scratch[start] = centroids[i]; |
| 194 | count_before += centroids[i].count; |
| 195 | i++; |
| 196 | start++; |
| 197 | |
| 198 | if (count_before > count_after * ratio) |
| 199 | { |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | while (i < ncentroids) |
| 205 | { |
| 206 | scratch[end] = centroids[i]; |
| 207 | count_after += centroids[i].count; |
| 208 | i++; |
| 209 | end--; |
| 210 | |
| 211 | if (count_before < count_after * ratio) |
| 212 | { |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | memcpy(centroids, scratch, sizeof(centroid_t) * ncentroids); |
| 219 | pfree(scratch); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /* |