~drizzle-trunk/drizzle/development

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