assoc_array.h 3.1 KB

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