MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cluster_build_one

Function cluster_build_one

src/store/store.c:5230–5304  ·  view source on GitHub ↗

Build the cluster_info for one community c into *ci. */

Source from the content-addressed store, hash-verified

5228
5229/* Build the cluster_info for one community c into *ci. */
5230static void cluster_build_one(cbm_cluster_info_t *ci, int c, int n, const int *comm,
5231 const int *degree, const char **names, const char **qns, int members,
5232 double cohesion) {
5233 memset(ci, 0, sizeof(*ci));
5234 ci->id = c;
5235 ci->members = members;
5236 ci->cohesion = cohesion;
5237
5238 /* Top nodes by degree. */
5239 int top_idx[CBM_CLUSTER_MAX_TOPNODES];
5240 int top_deg[CBM_CLUSTER_MAX_TOPNODES];
5241 int tn = 0;
5242 for (int i = 0; i < n; i++) {
5243 if (comm[i] != c) {
5244 continue;
5245 }
5246 int d = degree[i];
5247 int pos = tn;
5248 while (pos > 0 && top_deg[pos - 1] < d) {
5249 pos--;
5250 }
5251 if (pos < CBM_CLUSTER_MAX_TOPNODES) {
5252 int last = (tn < CBM_CLUSTER_MAX_TOPNODES) ? tn : CBM_CLUSTER_MAX_TOPNODES - 1;
5253 for (int k = last; k > pos; k--) {
5254 top_idx[k] = top_idx[k - 1];
5255 top_deg[k] = top_deg[k - 1];
5256 }
5257 top_idx[pos] = i;
5258 top_deg[pos] = d;
5259 if (tn < CBM_CLUSTER_MAX_TOPNODES) {
5260 tn++;
5261 }
5262 }
5263 }
5264 if (tn > 0) {
5265 ci->top_nodes = malloc((size_t)tn * sizeof(char *));
5266 for (int i = 0; i < tn; i++) {
5267 ci->top_nodes[i] = heap_strdup(names[top_idx[i]]);
5268 }
5269 ci->top_node_count = tn;
5270 }
5271
5272 /* Distinct packages (+ dominant one as the label). */
5273 const char *pkgs[CBM_CLUSTER_MAX_PKGS];
5274 int pkg_counts[CBM_CLUSTER_MAX_PKGS];
5275 int pc = 0;
5276 for (int i = 0; i < n; i++) {
5277 if (comm[i] == c) {
5278 cluster_add_pkg(pkgs, pkg_counts, &pc, CBM_CLUSTER_MAX_PKGS,
5279 cbm_qn_to_top_package(qns[i]));
5280 }
5281 }
5282 if (pc > 0) {
5283 ci->packages = malloc((size_t)pc * sizeof(char *));
5284 int best = 0;
5285 for (int i = 0; i < pc; i++) {
5286 ci->packages[i] = heap_strdup(pkgs[i]);
5287 if (pkg_counts[i] > pkg_counts[best]) {

Callers 1

arch_clustersFunction · 0.85

Calls 4

cluster_add_pkgFunction · 0.85
cbm_qn_to_top_packageFunction · 0.85
safe_str_freeFunction · 0.85
heap_strdupFunction · 0.70

Tested by

no test coverage detected