You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.6 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
  1. /* Copyright (C) 2000 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  13. /*
  14. Code for generell handling of priority Queues.
  15. Implemention of queues from "Algoritms in C" by Robert Sedgewick.
  16. Copyright Monty Program KB.
  17. By monty.
  18. */
  19. #ifndef _queues_h
  20. #define _queues_h
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. typedef struct st_queue {
  25. byte **root;
  26. void *first_cmp_arg;
  27. uint elements;
  28. uint max_elements;
  29. uint offset_to_key; /* compare is done on element+offset */
  30. int max_at_top; /* Set if queue_top gives max */
  31. int (*compare)(void *, byte *,byte *);
  32. uint auto_extent;
  33. } QUEUE;
  34. #define queue_top(queue) ((queue)->root[1])
  35. #define queue_element(queue,index) ((queue)->root[index+1])
  36. #define queue_end(queue) ((queue)->root[(queue)->elements])
  37. #define queue_replaced(queue) _downheap(queue,1)
  38. #define queue_set_cmp_arg(queue, set_arg) (queue)->first_cmp_arg= set_arg
  39. #define queue_set_max_at_top(queue, set_arg) \
  40. (queue)->max_at_top= set_arg ? (-1 ^ 1) : 0
  41. typedef int (*queue_compare)(void *,byte *, byte *);
  42. int init_queue(QUEUE *queue,uint max_elements,uint offset_to_key,
  43. pbool max_at_top, queue_compare compare,
  44. void *first_cmp_arg);
  45. int init_queue_ex(QUEUE *queue,uint max_elements,uint offset_to_key,
  46. pbool max_at_top, queue_compare compare,
  47. void *first_cmp_arg, uint auto_extent);
  48. int reinit_queue(QUEUE *queue,uint max_elements,uint offset_to_key,
  49. pbool max_at_top, queue_compare compare,
  50. void *first_cmp_arg);
  51. int resize_queue(QUEUE *queue, uint max_elements);
  52. void delete_queue(QUEUE *queue);
  53. void queue_insert(QUEUE *queue,byte *element);
  54. int queue_insert_safe(QUEUE *queue, byte *element);
  55. byte *queue_remove(QUEUE *queue,uint idx);
  56. #define queue_remove_all(queue) { (queue)->elements= 0; }
  57. #define queue_is_full(queue) (queue->elements == queue->max_elements)
  58. void _downheap(QUEUE *queue,uint idx);
  59. void queue_fix(QUEUE *queue);
  60. #define is_queue_inited(queue) ((queue)->root != 0)
  61. #ifdef __cplusplus
  62. }
  63. #endif
  64. #endif