File size: 2,349 Bytes
efadae0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "algorithm_registry.h"
#include <stdlib.h>
#include <string.h>

#define MAX_ALGORITHMS 100

static uci_algorithm_impl_t *algorithm_table[MAX_ALGORITHMS];
static size_t algorithm_count = 0;

int registry_init(void) {
    memset(algorithm_table, 0, sizeof(algorithm_table));
    algorithm_count = 0;
    return UCI_SUCCESS;
}

int registry_cleanup(void) {
    for (size_t i = 0; i < algorithm_count; i++) {
        if (algorithm_table[i]) {
            free(algorithm_table[i]);
            algorithm_table[i] = NULL;
        }
    }
    algorithm_count = 0;
    return UCI_SUCCESS;
}

int registry_register_algorithm(const uci_algorithm_impl_t *impl) {
    if (!impl) {
        return UCI_ERROR_INVALID_PARAM;
    }
    
    if (algorithm_count >= MAX_ALGORITHMS) {
        return UCI_ERROR_INTERNAL;
    }
    
    for (size_t i = 0; i < algorithm_count; i++) {
        if (algorithm_table[i] && algorithm_table[i]->info.id == impl->info.id) {
            return UCI_ERROR_INTERNAL;
        }
    }
    
    uci_algorithm_impl_t *new_impl = (uci_algorithm_impl_t *)malloc(sizeof(uci_algorithm_impl_t));
    if (!new_impl) {
        return UCI_ERROR_INTERNAL;
    }
    
    memcpy(new_impl, impl, sizeof(uci_algorithm_impl_t));
    algorithm_table[algorithm_count++] = new_impl;
    
    return UCI_SUCCESS;
}

const uci_algorithm_impl_t *registry_get_algorithm(uci_algorithm_id_t algorithm) {
    for (size_t i = 0; i < algorithm_count; i++) {
        if (algorithm_table[i] && algorithm_table[i]->info.id == algorithm) {
            return algorithm_table[i];
        }
    }
    return NULL;
}

int registry_list_algorithms(uci_algorithm_type_t type, uci_algorithm_id_t *algorithms, size_t *count) {
    if (!count) {
        return UCI_ERROR_INVALID_PARAM;
    }
    
    size_t matched = 0;
    
    for (size_t i = 0; i < algorithm_count; i++) {
        if (!algorithm_table[i]) {
            continue;
        }
        
        if (type == algorithm_table[i]->info.type || type == -1) {
            if (algorithms && matched < *count) {
                algorithms[matched] = algorithm_table[i]->info.id;
            }
            matched++;
        }
    }
    
    if (algorithms && matched > *count) {
        *count = matched;
        return UCI_ERROR_BUFFER_TOO_SMALL;
    }
    
    *count = matched;
    return UCI_SUCCESS;
}