From f954a62efa5afb43a6ccfc8633f57df2e94c763e Mon Sep 17 00:00:00 2001 From: Ivan Yarcev Date: Wed, 5 Jun 2024 09:14:05 +0600 Subject: [PATCH] Added function for loop checking in dictionary --- source/libublsettings.c | 67 ++++++++++++++++++++++++++++++++++++++++- source/libublsettings.h | 6 ++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/source/libublsettings.c b/source/libublsettings.c index f73a32a..2ee4a3d 100644 --- a/source/libublsettings.c +++ b/source/libublsettings.c @@ -332,6 +332,71 @@ dictionary *yon_dictionary_get_nth(dictionary *dict, int place) } else return NULL; } + +dictionary *yon_dictionary_sort(dictionary *dict){ + dictionary *first = dict->first; + dictionary *prev = first; + dictionary *current = first->next; + while (current){ + if (current!=prev&&strcmp(current->key,prev->key)<0){ + if (current->next) + current->next->prev=prev; + prev->next=current->next; + current->next=prev; + current->prev=prev->prev; + if (prev->prev) + prev->prev->next=current; + prev->prev=current; + if (prev==prev->first){ + yon_dictionary_make_first(current); + } + dictionary *temp = prev; + prev = current; + current=temp; + } + prev=prev->next; + current = current->next; + } + return first; +} + +int _yon_dictionary_check_loops(dictionary *_REG_DICT,dictionary *link){ + if (_REG_DICT){ + dictionary *dict; + for_dictionaries(dict,_REG_DICT){ + if (dict->data==link){ + return 1; + } + } + } + return 0; +} + +dictionary *yon_dictionary_check_loops(dictionary *target){ + dictionary *found = NULL; + dictionary *dict; + for_dictionaries(dict,target){ + if (_yon_dictionary_check_loops(found,dict)){ + return dict; + } else { + yon_dictionary_add_or_create_if_exists_with_data(found,NULL,dict); + } + } + yon_dictionary_free_all(found,NULL); + + found = NULL; + dict=yon_dictionary_get_last(target); + for(;dict;dict=dict->prev){ + if (_yon_dictionary_check_loops(found,dict)){ + return dict; + } else { + yon_dictionary_add_or_create_if_exists_with_data(found,NULL,dict); + } + } + yon_dictionary_free_all(found,NULL); + return NULL; +} + // char functions int yon_char_find_last(char *source, char find){ @@ -858,7 +923,7 @@ config_str yon_dir_get_contents(char *dir_path, int *size){ config_str yon_dir_get_by_mask(char *path, char *mask, int *size){ (*size)=0; - if (yon_char_find_count(mask,"*")<=1){ + if (yon_char_count(mask,"*")<=1){ char *lpart; char *rpart=yon_char_new(mask); lpart = yon_char_divide_search(rpart,"*",-1); diff --git a/source/libublsettings.h b/source/libublsettings.h index d31fab5..0b13309 100644 --- a/source/libublsettings.h +++ b/source/libublsettings.h @@ -290,6 +290,11 @@ void *yon_dictionary_free(dictionary *dictionary_to_free); */ void *yon_dictionary_free_all(dictionary *dictionary,void (data_manipulation)(void*)); +[[ deprecated ]] +dictionary *yon_dictionary_sort(dictionary *dict); + +dictionary *yon_dictionary_check_loops(dictionary *target); + // char functions #define yon_char_divide_search_self(str,find,delete_divider) {char *temp = str; str = yon_char_divide_search(str,find,delete_divider); free(temp);} @@ -441,6 +446,7 @@ int yon_char_parsed_check_repeats(char **parameters, int size, int *first_overla * [RU] * Считает количество символов [find] в строке [source] */ +[[ deprecated("Use yon_char_count instead") ]] int yon_char_find_count(char *source, char *find); int yon_char_count(char *source, char *find);