1*3cb98950SDavid Howells /* Generic associative array implementation. 2*3cb98950SDavid Howells * 3*3cb98950SDavid Howells * See Documentation/assoc_array.txt for information. 4*3cb98950SDavid Howells * 5*3cb98950SDavid Howells * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved. 6*3cb98950SDavid Howells * Written by David Howells ([email protected]) 7*3cb98950SDavid Howells * 8*3cb98950SDavid Howells * This program is free software; you can redistribute it and/or 9*3cb98950SDavid Howells * modify it under the terms of the GNU General Public Licence 10*3cb98950SDavid Howells * as published by the Free Software Foundation; either version 11*3cb98950SDavid Howells * 2 of the Licence, or (at your option) any later version. 12*3cb98950SDavid Howells */ 13*3cb98950SDavid Howells 14*3cb98950SDavid Howells #ifndef _LINUX_ASSOC_ARRAY_H 15*3cb98950SDavid Howells #define _LINUX_ASSOC_ARRAY_H 16*3cb98950SDavid Howells 17*3cb98950SDavid Howells #ifdef CONFIG_ASSOCIATIVE_ARRAY 18*3cb98950SDavid Howells 19*3cb98950SDavid Howells #include <linux/types.h> 20*3cb98950SDavid Howells 21*3cb98950SDavid Howells #define ASSOC_ARRAY_KEY_CHUNK_SIZE BITS_PER_LONG /* Key data retrieved in chunks of this size */ 22*3cb98950SDavid Howells 23*3cb98950SDavid Howells /* 24*3cb98950SDavid Howells * Generic associative array. 25*3cb98950SDavid Howells */ 26*3cb98950SDavid Howells struct assoc_array { 27*3cb98950SDavid Howells struct assoc_array_ptr *root; /* The node at the root of the tree */ 28*3cb98950SDavid Howells unsigned long nr_leaves_on_tree; 29*3cb98950SDavid Howells }; 30*3cb98950SDavid Howells 31*3cb98950SDavid Howells /* 32*3cb98950SDavid Howells * Operations on objects and index keys for use by array manipulation routines. 33*3cb98950SDavid Howells */ 34*3cb98950SDavid Howells struct assoc_array_ops { 35*3cb98950SDavid Howells /* Method to get a chunk of an index key from caller-supplied data */ 36*3cb98950SDavid Howells unsigned long (*get_key_chunk)(const void *index_key, int level); 37*3cb98950SDavid Howells 38*3cb98950SDavid Howells /* Method to get a piece of an object's index key */ 39*3cb98950SDavid Howells unsigned long (*get_object_key_chunk)(const void *object, int level); 40*3cb98950SDavid Howells 41*3cb98950SDavid Howells /* Is this the object we're looking for? */ 42*3cb98950SDavid Howells bool (*compare_object)(const void *object, const void *index_key); 43*3cb98950SDavid Howells 44*3cb98950SDavid Howells /* How different are two objects, to a bit position in their keys? (or 45*3cb98950SDavid Howells * -1 if they're the same) 46*3cb98950SDavid Howells */ 47*3cb98950SDavid Howells int (*diff_objects)(const void *a, const void *b); 48*3cb98950SDavid Howells 49*3cb98950SDavid Howells /* Method to free an object. */ 50*3cb98950SDavid Howells void (*free_object)(void *object); 51*3cb98950SDavid Howells }; 52*3cb98950SDavid Howells 53*3cb98950SDavid Howells /* 54*3cb98950SDavid Howells * Access and manipulation functions. 55*3cb98950SDavid Howells */ 56*3cb98950SDavid Howells struct assoc_array_edit; 57*3cb98950SDavid Howells 58*3cb98950SDavid Howells static inline void assoc_array_init(struct assoc_array *array) 59*3cb98950SDavid Howells { 60*3cb98950SDavid Howells array->root = NULL; 61*3cb98950SDavid Howells array->nr_leaves_on_tree = 0; 62*3cb98950SDavid Howells } 63*3cb98950SDavid Howells 64*3cb98950SDavid Howells extern int assoc_array_iterate(const struct assoc_array *array, 65*3cb98950SDavid Howells int (*iterator)(const void *object, 66*3cb98950SDavid Howells void *iterator_data), 67*3cb98950SDavid Howells void *iterator_data); 68*3cb98950SDavid Howells extern void *assoc_array_find(const struct assoc_array *array, 69*3cb98950SDavid Howells const struct assoc_array_ops *ops, 70*3cb98950SDavid Howells const void *index_key); 71*3cb98950SDavid Howells extern void assoc_array_destroy(struct assoc_array *array, 72*3cb98950SDavid Howells const struct assoc_array_ops *ops); 73*3cb98950SDavid Howells extern struct assoc_array_edit *assoc_array_insert(struct assoc_array *array, 74*3cb98950SDavid Howells const struct assoc_array_ops *ops, 75*3cb98950SDavid Howells const void *index_key, 76*3cb98950SDavid Howells void *object); 77*3cb98950SDavid Howells extern void assoc_array_insert_set_object(struct assoc_array_edit *edit, 78*3cb98950SDavid Howells void *object); 79*3cb98950SDavid Howells extern struct assoc_array_edit *assoc_array_delete(struct assoc_array *array, 80*3cb98950SDavid Howells const struct assoc_array_ops *ops, 81*3cb98950SDavid Howells const void *index_key); 82*3cb98950SDavid Howells extern struct assoc_array_edit *assoc_array_clear(struct assoc_array *array, 83*3cb98950SDavid Howells const struct assoc_array_ops *ops); 84*3cb98950SDavid Howells extern void assoc_array_apply_edit(struct assoc_array_edit *edit); 85*3cb98950SDavid Howells extern void assoc_array_cancel_edit(struct assoc_array_edit *edit); 86*3cb98950SDavid Howells extern int assoc_array_gc(struct assoc_array *array, 87*3cb98950SDavid Howells const struct assoc_array_ops *ops, 88*3cb98950SDavid Howells bool (*iterator)(void *object, void *iterator_data), 89*3cb98950SDavid Howells void *iterator_data); 90*3cb98950SDavid Howells 91*3cb98950SDavid Howells #endif /* CONFIG_ASSOCIATIVE_ARRAY */ 92*3cb98950SDavid Howells #endif /* _LINUX_ASSOC_ARRAY_H */ 93