1 /* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #ifndef QUEUES_INCLUDED
24 #define QUEUES_INCLUDED
25 
26 /*
27   Code for handling of priority Queues.
28   Implemention of queues from "Algoritms in C" by Robert Sedgewick.
29   By monty.
30 */
31 
32 #include "my_global.h"                          /* uchar */
33 
34 #ifdef	__cplusplus
35 extern "C" {
36 #endif
37 
38 typedef struct st_queue {
39   uchar **root;
40   void *first_cmp_arg;
41   uint elements;
42   uint max_elements;
43   uint offset_to_key;	/* compare is done on element+offset */
44   int max_at_top;	/* Normally 1, set to -1 if queue_top gives max */
45   int  (*compare)(void *, uchar *,uchar *);
46   uint auto_extent;
47 } QUEUE;
48 
49 void _downheap(QUEUE *queue,uint idx);
50 void queue_fix(QUEUE *queue);
51 
52 #define queue_top(queue) ((queue)->root[1])
53 #define queue_element(queue,index) ((queue)->root[index+1])
54 #define queue_end(queue) ((queue)->root[(queue)->elements])
55 
queue_replaced(QUEUE * queue)56 static inline void queue_replaced(QUEUE *queue)
57 {
58   _downheap(queue, 1);
59 }
60 
queue_set_max_at_top(QUEUE * queue,int set_arg)61 static inline void queue_set_max_at_top(QUEUE *queue, int set_arg)
62 {
63   queue->max_at_top= set_arg ? -1 : 1;
64 }
65 
66 typedef int (*queue_compare)(void *,uchar *, uchar *);
67 
68 int init_queue(QUEUE *queue,uint max_elements,uint offset_to_key,
69 	       pbool max_at_top, queue_compare compare,
70 	       void *first_cmp_arg);
71 int init_queue_ex(QUEUE *queue,uint max_elements,uint offset_to_key,
72 	       pbool max_at_top, queue_compare compare,
73 	       void *first_cmp_arg, uint auto_extent);
74 int reinit_queue(QUEUE *queue,uint max_elements,uint offset_to_key,
75                  pbool max_at_top, queue_compare compare,
76                  void *first_cmp_arg);
77 void delete_queue(QUEUE *queue);
78 void queue_insert(QUEUE *queue,uchar *element);
79 uchar *queue_remove(QUEUE *queue,uint idx);
80 
queue_remove_all(QUEUE * queue)81 static inline void queue_remove_all(QUEUE *queue)
82 {
83   queue->elements= 0;
84 }
85 
queue_is_full(QUEUE * queue)86 static inline my_bool queue_is_full(QUEUE *queue)
87 {
88   return queue->elements == queue->max_elements;
89 }
90 
is_queue_inited(QUEUE * queue)91 static inline my_bool is_queue_inited(QUEUE *queue)
92 {
93   return queue->root != NULL;
94 }
95 
96 #ifdef	__cplusplus
97 }
98 #endif
99 
100 #endif  // QUEUES_INCLUDED
101