queues.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* Copyright (C) 2010 Monty Program Ab
  2. All Rights reserved
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright
  6. notice, this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the following disclaimer
  8. in the documentation and/or other materials provided with the
  9. distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  11. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  12. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  13. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  14. <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  15. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  16. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  17. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  18. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  19. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  20. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  21. SUCH DAMAGE.
  22. */
  23. /*
  24. Code for generell handling of priority Queues.
  25. Implemention of queues from "Algoritms in C" by Robert Sedgewick.
  26. */
  27. #ifndef _queues_h
  28. #define _queues_h
  29. #include "my_global.h" /* uchar */
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. typedef struct st_queue {
  34. uchar **root;
  35. void *first_cmp_arg;
  36. uint elements;
  37. uint max_elements;
  38. uint offset_to_key; /* compare is done on element+offset */
  39. uint offset_to_queue_pos; /* If we want to store position in element */
  40. uint auto_extent;
  41. int max_at_top; /* Normally 1, set to -1 if queue_top gives max */
  42. int (*compare)(void *, uchar *,uchar *);
  43. } QUEUE;
  44. #define queue_first_element(queue) 1
  45. #define queue_last_element(queue) (queue)->elements
  46. #define queue_empty(queue) ((queue)->elements == 0)
  47. #define queue_top(queue) ((queue)->root[1])
  48. #define queue_element(queue,index) ((queue)->root[index])
  49. #define queue_end(queue) ((queue)->root[(queue)->elements])
  50. #define queue_replace_top(queue) _downheap(queue, 1, (queue)->root[1])
  51. #define queue_set_cmp_arg(queue, set_arg) (queue)->first_cmp_arg= set_arg
  52. #define queue_set_max_at_top(queue, set_arg) \
  53. (queue)->max_at_top= set_arg ? -1 : 1
  54. #define queue_remove_top(queue_arg) queue_remove((queue_arg), queue_first_element(queue_arg))
  55. typedef int (*queue_compare)(void *,uchar *, uchar *);
  56. int init_queue(QUEUE *queue,uint max_elements,uint offset_to_key,
  57. pbool max_at_top, queue_compare compare,
  58. void *first_cmp_arg, uint offset_to_queue_pos,
  59. uint auto_extent);
  60. int reinit_queue(QUEUE *queue,uint max_elements,uint offset_to_key,
  61. pbool max_at_top, queue_compare compare,
  62. void *first_cmp_arg, uint offset_to_queue_pos,
  63. uint auto_extent);
  64. int resize_queue(QUEUE *queue, uint max_elements);
  65. void delete_queue(QUEUE *queue);
  66. void queue_insert(QUEUE *queue,uchar *element);
  67. int queue_insert_safe(QUEUE *queue, uchar *element);
  68. uchar *queue_remove(QUEUE *queue,uint idx);
  69. void queue_replace(QUEUE *queue,uint idx);
  70. #define queue_remove_all(queue) { (queue)->elements= 0; }
  71. #define queue_is_full(queue) (queue->elements == queue->max_elements)
  72. void _downheap(QUEUE *queue, uint idx, uchar *element);
  73. void queue_fix(QUEUE *queue);
  74. #define is_queue_inited(queue) ((queue)->root != 0)
  75. #ifdef __cplusplus
  76. }
  77. #endif
  78. #endif