Please follow my official account [Jizhi Vision] for more notes to share
O_o >_< O_o O_o ~_~ O_o
This article introduces the implementation of the linked list lookup option_find_xx interface in Darknet.
Linked list operations will be used extensively in Darknet. For example, in the analysis of Darknet read_data_CFG interface written last time, there is an example of storing the data before and after the “=” in each row of VOC. data into linked list VAL in the form of key-value pairs. For some list and node construction, insert list, release list implementation can see my article “[Programming art] Anatomy darknet C list implementation”, here mainly talk about the linked list lookup related interface.
1. Linked list lookup interface definition
List lookups are defined in the option_list.h header:
/// option_list.h #ifndef OPTION_LIST_H #define OPTION_LIST_H #include "darknet.h" #include "list.h" /// You can think of it as a dictionary in Python, the key-value pair typedef struct{char *key; char *val; int used; } kvp; #ifdef __cplusplus extern "C" { #endif list *read_data_cfg(char *filename); int read_option(char *s, list *options); void option_insert(list *l, char *key, char *val); / / / that is on the list to find relevant interface declares the / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / char * option_find (list * l, char *key); // char *option_find_str(list *l, char *key, char *def); // char *option_find_str_quiet(list *l, char *key, char *def); // int option_find_int(list *l, char *key, int def); // int option_find_int_quiet(list *l, char *key, int def); // float option_find_float(list *l, char *key, float def); // float option_find_float_quiet(list *l, char *key, float def); // ////////////////////////////////////////////////////////////////// void option_unused(list *l); //typedef struct { // int classes; // char **names; //} metadata; //LIB_API metadata get_metadata(char *file); #ifdef __cplusplus } #endif #endifCopy the code
Let’s take a look at each one.
2, option_find
Take a look at the implementation of option_find:
NULL char *option_find(list *l, char *key) {node *n = l->front; While (n){// when the node is not NULL KVP *p = (KVP *)n->val; If (STRCMP (p->key, key) == 0){// STRCMP compare p->used = 1; Return p->val; } n = n->next; } return 0; }Copy the code
STRCMP is a string comparison function in C, declared in string.h:
/// string.h
_Check_return_
int __cdecl strcmp(
_In_z_ char const* _Str1,
_In_z_ char const* _Str2
);
Copy the code
Option_find_str & option_find_str_quiet
It will look similar to option_find. Let’s look at the implementation of option_find_str:
// option_list.c // select val for key. Def char *option_find_str(list *l, char *key, char *def) {char *v = option_find(l, key); // call option_find if(v) return v; If (def) fprintf(stderr, "%s: Using default '%s'\n", key, def); if(def) fprintf(stderr, "%s: Using default '%s'\n", key, def); // if not found, use default pass parameter def return def; }Copy the code
Option_find_str_quiet is similar to option_find_str:
// option_list.c // select val for key. Def char *option_find_str_quiet(list *l, char *key, char *def) {char *v = option_find(l, key); if (v) return v; return def; }Copy the code
Option_find_int & option_find_int_quiet
Look at option_find_int:
// option_list.c // select val from the specified key. Def int option_find_int(list *l, char *key, int def) {char *v = option_find(l, key); if(v) return atoi(v); // atoi converts string to int fprintf(stderr, "%s: Using default '%d'\n", key, def); Def return def; }Copy the code
The main difference is an atoi, which converts the string to an integer, and so on.
Option_find_int_quiet: option_find_int_quiet: option_find_int_quiet: option_find_int_quiet: option_find_int_quiet: option_find_int_quiet:
/// option_list.c
int option_find_int_quiet(list *l, char *key, int def)
{
char *v = option_find(l, key);
if(v) return atoi(v);
return def;
}
Copy the code
Option_find_float & option_finD_FLOAT_quiet
Finally, option_find_float and option_find_FLOAT_quiet.
Option_find_float is implemented as follows:
// option_list.c // select val from the specified key. Def float option_find_float(list *l, char *key, float def) {char *v = option_find(l, key); if(v) return atof(v); fprintf(stderr, "%s: Using default '%lf'\n", key, def); return def; }Copy the code
Option_find_int option_find_float_quiet
float option_find_float_quiet(list *l, char *key, float def)
{
char *v = option_find(l, key);
if(v) return atof(v);
return def;
}
Copy the code
The above analysis of linked list search related interfaces in Darknet is relatively simple, but it is necessary to lay a solid foundation and learn from the past.
Analysis of Darknet linked list to find option_find_XX interface