dict: Add lif_dict_find_all().
As a dict can have multiple values for any given key we add a function to query all occurences of a given key and return them as a (new) list. Signed-off-by: Maximilian Wilhelm <max@sdn.clinic>
This commit is contained in:
		
							parent
							
								
									cb9d347c8a
								
							
						
					
					
						commit
						b1dfa609f9
					
				
					 2 changed files with 28 additions and 0 deletions
				
			
		|  | @ -3,6 +3,7 @@ | |||
|  * Purpose: wrapping linked lists to provide a naive dictionary | ||||
|  * | ||||
|  * Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org> | ||||
|  * Copyright (c) 2020 Maximilian Wilhelm <max@sdn.clinic> | ||||
|  * | ||||
|  * Permission to use, copy, modify, and/or distribute this software for any | ||||
|  * purpose with or without fee is hereby granted, provided that the above | ||||
|  | @ -65,6 +66,31 @@ lif_dict_find(struct lif_dict *dict, const char *key) | |||
| 	return NULL; | ||||
| } | ||||
| 
 | ||||
| struct lif_list * | ||||
| lif_dict_find_all(struct lif_dict *dict, const char *key) | ||||
| { | ||||
| 	struct lif_list *entries = calloc(1, sizeof(struct lif_list)); | ||||
| 	struct lif_node *iter; | ||||
| 
 | ||||
| 	LIF_LIST_FOREACH(iter, dict->list.head) | ||||
| 	{ | ||||
| 		struct lif_dict_entry *entry = iter->data; | ||||
| 		if (!strcmp(entry->key, key)) | ||||
| 		{ | ||||
| 			struct lif_node *new = calloc(1, sizeof(struct lif_node)); | ||||
| 			lif_node_insert_tail(new, entry->data, entries); | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	if (entries->length == 0) | ||||
| 	{ | ||||
| 		free(entries); | ||||
| 		return NULL; | ||||
| 	} | ||||
| 
 | ||||
| 	return entries; | ||||
| } | ||||
| 
 | ||||
| void | ||||
| lif_dict_delete(struct lif_dict *dict, const char *key) | ||||
| { | ||||
|  |  | |||
|  | @ -3,6 +3,7 @@ | |||
|  * Purpose: wrapping linked lists to provide a naive dictionary | ||||
|  * | ||||
|  * Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org> | ||||
|  * Copyright (c) 2020 Maximilian Wilhelm <max@sdn.clinic> | ||||
|  * | ||||
|  * Permission to use, copy, modify, and/or distribute this software for any | ||||
|  * purpose with or without fee is hereby granted, provided that the above | ||||
|  | @ -38,6 +39,7 @@ extern void lif_dict_init(struct lif_dict *dict); | |||
| extern void lif_dict_fini(struct lif_dict *dict); | ||||
| extern struct lif_dict_entry *lif_dict_add(struct lif_dict *dict, const char *key, void *data); | ||||
| extern struct lif_dict_entry *lif_dict_find(struct lif_dict *dict, const char *key); | ||||
| extern struct lif_list *lif_dict_find_all(struct lif_dict *dict, const char *key); | ||||
| extern void lif_dict_delete(struct lif_dict *dict, const char *key); | ||||
| extern void lif_dict_delete_entry(struct lif_dict *dict, struct lif_dict_entry *entry); | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue