diff options
author | admin <admin@optics-design.com> | 2025-08-05 10:11:14 +0200 |
---|---|---|
committer | admin <admin@optics-design.com> | 2025-08-05 10:11:14 +0200 |
commit | 9496ae0a50e6848121c7e913ca2dc55c8e6c84c1 (patch) | |
tree | 0a8705a31233bc07710950e653d1f55e7155f78a /src/glautils/fgla.c | |
parent | b59995f9732720e8c82e22f44c0c8cb3efe2c708 (diff) |
rewrote clustering
Diffstat (limited to 'src/glautils/fgla.c')
-rw-r--r-- | src/glautils/fgla.c | 72 |
1 files changed, 45 insertions, 27 deletions
diff --git a/src/glautils/fgla.c b/src/glautils/fgla.c index f2ed8eb..baf2fef 100644 --- a/src/glautils/fgla.c +++ b/src/glautils/fgla.c @@ -442,54 +442,66 @@ int main(int argc, char* argv[]) { // Load all glasses first, then filter by catalog during display
loaded = load_glasses_from_json((const byte*)successful_path, NULL);
- u32 glass_count = get_glass_count();
+ // Search across all catalogs
+ u32 total_catalog_count = get_catalog_count();
u32 found_count = 0;
// Determine search type
int is_glass_code_search = is_glass_code_pattern(search_term);
- // Pre-allocate matches array for performance (avoids double iteration)
- u32* matching_indices = malloc(glass_count * sizeof(u32));
- if (!matching_indices) {
+ // Pre-allocate matches array for performance - estimate max size across all catalogs
+ u32* matching_indices = malloc(10000 * sizeof(u32)); // Generous allocation for all glasses
+ u32* matching_catalogs = malloc(10000 * sizeof(u32)); // Track which catalog each match is from
+ if (!matching_indices || !matching_catalogs) {
fprintf(stderr, "Error: Memory allocation failed\n");
+ free(matching_indices);
+ free(matching_catalogs);
cleanup_glass_data();
return FGLA_ERROR_MEMORY;
}
- // Single pass: find and store matches
+ // Search through all catalogs
found_count = 0;
- for (u32 i = 0; i < glass_count; i++) {
- const Glass* glass = get_glass(i);
- if (!glass) continue;
-
- const char* glass_name = (const char*)get_glass_name(i);
- if (!glass_name) continue;
-
- int matches = 0;
+ for (u32 catalog_idx = 0; catalog_idx < total_catalog_count; catalog_idx++) {
+ set_current_catalog(catalog_idx);
+ u32 glass_count = get_glass_count();
- if (is_glass_code_search) {
- // Glass code pattern search
- const char* glass_code = (const char*)glass->glass_code;
- if (glass_code && matches_glass_code_pattern_safe(glass_code, search_term)) {
- matches = 1;
+ for (u32 i = 0; i < glass_count; i++) {
+ const Glass* glass = get_glass(i);
+ if (!glass) continue;
+
+ const char* glass_name = (const char*)get_glass_name(i);
+ if (!glass_name) continue;
+
+ int matches = 0;
+
+ if (is_glass_code_search) {
+ // Glass code pattern search
+ const char* glass_code = (const char*)glass->glass_code;
+ if (glass_code && matches_glass_code_pattern_safe(glass_code, search_term)) {
+ matches = 1;
+ }
+ } else {
+ // Name search
+ if (contains_substring_safe(glass_name, search_term)) {
+ matches = 1;
+ }
}
- } else {
- // Name search
- if (contains_substring_safe(glass_name, search_term)) {
- matches = 1;
+
+ // Check if this glass matches the search term and catalog filter
+ if (matches && matches_catalog((const char*)glass->manufacturer, catalog_list, catalog_count)) {
+ matching_indices[found_count] = i;
+ matching_catalogs[found_count] = catalog_idx;
+ found_count++;
}
}
-
- // Check if this glass matches the search term and catalog filter
- if (matches && matches_catalog((const char*)glass->manufacturer, catalog_list, catalog_count)) {
- matching_indices[found_count++] = i;
- }
}
// Output results
if (found_count == 0) {
print_error_with_suggestion(FGLA_ERROR_NO_MATCHES, search_term);
free(matching_indices);
+ free(matching_catalogs);
cleanup_glass_data();
return FGLA_ERROR_NO_MATCHES;
}
@@ -500,6 +512,11 @@ int main(int argc, char* argv[]) { // Display matches from stored indices
for (u32 j = 0; j < found_count; j++) {
u32 i = matching_indices[j];
+ u32 catalog_idx = matching_catalogs[j];
+
+ // Set the correct catalog for this glass
+ set_current_catalog(catalog_idx);
+
const Glass* glass = get_glass(i);
const char* glass_name = (const char*)get_glass_name(i);
@@ -512,6 +529,7 @@ int main(int argc, char* argv[]) { print_footer(output_format);
free(matching_indices);
+ free(matching_catalogs);
// Cleanup
cleanup_glass_data();
|